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 | |
| 18 | static void set_expand_context(expand_T *xp); |
| 19 | static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int); |
| 20 | static int expand_showtail(expand_T *xp); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 21 | static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg); |
| 22 | static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]); |
| 23 | static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file); |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 24 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 25 | static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file); |
| 26 | static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 27 | #endif |
| 28 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 29 | static int |
| 30 | sort_func_compare(const void *s1, const void *s2) |
| 31 | { |
| 32 | char_u *p1 = *(char_u **)s1; |
| 33 | char_u *p2 = *(char_u **)s2; |
| 34 | |
| 35 | if (*p1 != '<' && *p2 == '<') return -1; |
| 36 | if (*p1 == '<' && *p2 != '<') return 1; |
| 37 | return STRCMP(p1, p2); |
| 38 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 39 | |
| 40 | static void |
| 41 | ExpandEscape( |
| 42 | expand_T *xp, |
| 43 | char_u *str, |
| 44 | int numfiles, |
| 45 | char_u **files, |
| 46 | int options) |
| 47 | { |
| 48 | int i; |
| 49 | char_u *p; |
| 50 | |
| 51 | // May change home directory back to "~" |
| 52 | if (options & WILD_HOME_REPLACE) |
| 53 | tilde_replace(str, numfiles, files); |
| 54 | |
| 55 | if (options & WILD_ESCAPE) |
| 56 | { |
| 57 | if (xp->xp_context == EXPAND_FILES |
| 58 | || xp->xp_context == EXPAND_FILES_IN_PATH |
| 59 | || xp->xp_context == EXPAND_SHELLCMD |
| 60 | || xp->xp_context == EXPAND_BUFFERS |
| 61 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 62 | { |
| 63 | // Insert a backslash into a file name before a space, \, %, # |
| 64 | // and wildmatch characters, except '~'. |
| 65 | for (i = 0; i < numfiles; ++i) |
| 66 | { |
| 67 | // for ":set path=" we need to escape spaces twice |
| 68 | if (xp->xp_backslash == XP_BS_THREE) |
| 69 | { |
| 70 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 71 | if (p != NULL) |
| 72 | { |
| 73 | vim_free(files[i]); |
| 74 | files[i] = p; |
| 75 | #if defined(BACKSLASH_IN_FILENAME) |
| 76 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 77 | if (p != NULL) |
| 78 | { |
| 79 | vim_free(files[i]); |
| 80 | files[i] = p; |
| 81 | } |
| 82 | #endif |
| 83 | } |
| 84 | } |
| 85 | #ifdef BACKSLASH_IN_FILENAME |
| 86 | p = vim_strsave_fnameescape(files[i], FALSE); |
| 87 | #else |
| 88 | p = vim_strsave_fnameescape(files[i], xp->xp_shell); |
| 89 | #endif |
| 90 | if (p != NULL) |
| 91 | { |
| 92 | vim_free(files[i]); |
| 93 | files[i] = p; |
| 94 | } |
| 95 | |
| 96 | // If 'str' starts with "\~", replace "~" at start of |
| 97 | // files[i] with "\~". |
| 98 | if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') |
| 99 | escape_fname(&files[i]); |
| 100 | } |
| 101 | xp->xp_backslash = XP_BS_NONE; |
| 102 | |
| 103 | // If the first file starts with a '+' escape it. Otherwise it |
| 104 | // could be seen as "+cmd". |
| 105 | if (*files[0] == '+') |
| 106 | escape_fname(&files[0]); |
| 107 | } |
| 108 | else if (xp->xp_context == EXPAND_TAGS) |
| 109 | { |
| 110 | // Insert a backslash before characters in a tag name that |
| 111 | // would terminate the ":tag" command. |
| 112 | for (i = 0; i < numfiles; ++i) |
| 113 | { |
| 114 | p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); |
| 115 | if (p != NULL) |
| 116 | { |
| 117 | vim_free(files[i]); |
| 118 | files[i] = p; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Return FAIL if this is not an appropriate context in which to do |
| 127 | * completion of anything, return OK if it is (even if there are no matches). |
| 128 | * For the caller, this means that the character is just passed through like a |
| 129 | * normal character (instead of being expanded). This allows :s/^I^D etc. |
| 130 | */ |
| 131 | int |
| 132 | nextwild( |
| 133 | expand_T *xp, |
| 134 | int type, |
| 135 | int options, // extra options for ExpandOne() |
| 136 | int escape) // if TRUE, escape the returned matches |
| 137 | { |
| 138 | cmdline_info_T *ccline = get_cmdline_info(); |
| 139 | int i, j; |
| 140 | char_u *p1; |
| 141 | char_u *p2; |
| 142 | int difflen; |
| 143 | int v; |
| 144 | |
| 145 | if (xp->xp_numfiles == -1) |
| 146 | { |
| 147 | set_expand_context(xp); |
| 148 | cmd_showtail = expand_showtail(xp); |
| 149 | } |
| 150 | |
| 151 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 152 | { |
| 153 | beep_flush(); |
| 154 | return OK; // Something illegal on command line |
| 155 | } |
| 156 | if (xp->xp_context == EXPAND_NOTHING) |
| 157 | { |
| 158 | // Caller can use the character as a normal char instead |
| 159 | return FAIL; |
| 160 | } |
| 161 | |
| 162 | msg_puts("..."); // show that we are busy |
| 163 | out_flush(); |
| 164 | |
| 165 | i = (int)(xp->xp_pattern - ccline->cmdbuff); |
| 166 | xp->xp_pattern_len = ccline->cmdpos - i; |
| 167 | |
| 168 | if (type == WILD_NEXT || type == WILD_PREV) |
| 169 | { |
| 170 | // Get next/previous match for a previous expanded pattern. |
| 171 | p2 = ExpandOne(xp, NULL, NULL, 0, type); |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | // Translate string into pattern and expand it. |
| 176 | if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, |
| 177 | xp->xp_context)) == NULL) |
| 178 | p2 = NULL; |
| 179 | else |
| 180 | { |
| 181 | int use_options = options | |
| 182 | WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT; |
| 183 | if (escape) |
| 184 | use_options |= WILD_ESCAPE; |
| 185 | |
| 186 | if (p_wic) |
| 187 | use_options += WILD_ICASE; |
| 188 | p2 = ExpandOne(xp, p1, |
| 189 | vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len), |
| 190 | use_options, type); |
| 191 | vim_free(p1); |
| 192 | // longest match: make sure it is not shorter, happens with :help |
| 193 | if (p2 != NULL && type == WILD_LONGEST) |
| 194 | { |
| 195 | for (j = 0; j < xp->xp_pattern_len; ++j) |
| 196 | if (ccline->cmdbuff[i + j] == '*' |
| 197 | || ccline->cmdbuff[i + j] == '?') |
| 198 | break; |
| 199 | if ((int)STRLEN(p2) < j) |
| 200 | VIM_CLEAR(p2); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if (p2 != NULL && !got_int) |
| 206 | { |
| 207 | difflen = (int)STRLEN(p2) - xp->xp_pattern_len; |
| 208 | if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen) |
| 209 | { |
| 210 | v = realloc_cmdbuff(ccline->cmdlen + difflen + 4); |
| 211 | xp->xp_pattern = ccline->cmdbuff + i; |
| 212 | } |
| 213 | else |
| 214 | v = OK; |
| 215 | if (v == OK) |
| 216 | { |
| 217 | mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen], |
| 218 | &ccline->cmdbuff[ccline->cmdpos], |
| 219 | (size_t)(ccline->cmdlen - ccline->cmdpos + 1)); |
| 220 | mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2)); |
| 221 | ccline->cmdlen += difflen; |
| 222 | ccline->cmdpos += difflen; |
| 223 | } |
| 224 | } |
| 225 | vim_free(p2); |
| 226 | |
| 227 | redrawcmd(); |
| 228 | cursorcmd(); |
| 229 | |
| 230 | // When expanding a ":map" command and no matches are found, assume that |
| 231 | // the key is supposed to be inserted literally |
| 232 | if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) |
| 233 | return FAIL; |
| 234 | |
| 235 | if (xp->xp_numfiles <= 0 && p2 == NULL) |
| 236 | beep_flush(); |
| 237 | else if (xp->xp_numfiles == 1) |
| 238 | // free expanded pattern |
| 239 | (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); |
| 240 | |
| 241 | return OK; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Do wildcard expansion on the string 'str'. |
| 246 | * Chars that should not be expanded must be preceded with a backslash. |
| 247 | * Return a pointer to allocated memory containing the new string. |
| 248 | * Return NULL for failure. |
| 249 | * |
| 250 | * "orig" is the originally expanded string, copied to allocated memory. It |
| 251 | * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or |
| 252 | * WILD_PREV "orig" should be NULL. |
| 253 | * |
| 254 | * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
| 255 | * is WILD_EXPAND_FREE or WILD_ALL. |
| 256 | * |
| 257 | * mode = WILD_FREE: just free previously expanded matches |
| 258 | * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches |
| 259 | * mode = WILD_EXPAND_KEEP: normal expansion, keep matches |
| 260 | * mode = WILD_NEXT: use next match in multiple match, wrap to first |
| 261 | * mode = WILD_PREV: use previous match in multiple match, wrap to first |
| 262 | * mode = WILD_ALL: return all matches concatenated |
| 263 | * mode = WILD_LONGEST: return longest matched part |
| 264 | * mode = WILD_ALL_KEEP: get all matches, keep matches |
| 265 | * |
| 266 | * options = WILD_LIST_NOTFOUND: list entries without a match |
| 267 | * options = WILD_HOME_REPLACE: do home_replace() for buffer names |
| 268 | * options = WILD_USE_NL: Use '\n' for WILD_ALL |
| 269 | * options = WILD_NO_BEEP: Don't beep for multiple matches |
| 270 | * options = WILD_ADD_SLASH: add a slash after directory names |
| 271 | * options = WILD_KEEP_ALL: don't remove 'wildignore' entries |
| 272 | * options = WILD_SILENT: don't print warning messages |
| 273 | * options = WILD_ESCAPE: put backslash before special chars |
| 274 | * options = WILD_ICASE: ignore case for files |
| 275 | * |
| 276 | * The variables xp->xp_context and xp->xp_backslash must have been set! |
| 277 | */ |
| 278 | char_u * |
| 279 | ExpandOne( |
| 280 | expand_T *xp, |
| 281 | char_u *str, |
| 282 | char_u *orig, // allocated copy of original of expanded string |
| 283 | int options, |
| 284 | int mode) |
| 285 | { |
| 286 | char_u *ss = NULL; |
| 287 | static int findex; |
| 288 | static char_u *orig_save = NULL; // kept value of orig |
| 289 | int orig_saved = FALSE; |
| 290 | int i; |
| 291 | long_u len; |
| 292 | int non_suf_match; // number without matching suffix |
| 293 | |
| 294 | // first handle the case of using an old match |
| 295 | if (mode == WILD_NEXT || mode == WILD_PREV) |
| 296 | { |
| 297 | if (xp->xp_numfiles > 0) |
| 298 | { |
| 299 | if (mode == WILD_PREV) |
| 300 | { |
| 301 | if (findex == -1) |
| 302 | findex = xp->xp_numfiles; |
| 303 | --findex; |
| 304 | } |
| 305 | else // mode == WILD_NEXT |
| 306 | ++findex; |
| 307 | |
| 308 | // When wrapping around, return the original string, set findex to |
| 309 | // -1. |
| 310 | if (findex < 0) |
| 311 | { |
| 312 | if (orig_save == NULL) |
| 313 | findex = xp->xp_numfiles - 1; |
| 314 | else |
| 315 | findex = -1; |
| 316 | } |
| 317 | if (findex >= xp->xp_numfiles) |
| 318 | { |
| 319 | if (orig_save == NULL) |
| 320 | findex = 0; |
| 321 | else |
| 322 | findex = -1; |
| 323 | } |
| 324 | #ifdef FEAT_WILDMENU |
| 325 | if (p_wmnu) |
| 326 | win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, |
| 327 | findex, cmd_showtail); |
| 328 | #endif |
| 329 | if (findex == -1) |
| 330 | return vim_strsave(orig_save); |
| 331 | return vim_strsave(xp->xp_files[findex]); |
| 332 | } |
| 333 | else |
| 334 | return NULL; |
| 335 | } |
| 336 | |
| 337 | // free old names |
| 338 | if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
| 339 | { |
| 340 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 341 | xp->xp_numfiles = -1; |
| 342 | VIM_CLEAR(orig_save); |
| 343 | } |
| 344 | findex = 0; |
| 345 | |
| 346 | if (mode == WILD_FREE) // only release file name |
| 347 | return NULL; |
| 348 | |
| 349 | if (xp->xp_numfiles == -1) |
| 350 | { |
| 351 | vim_free(orig_save); |
| 352 | orig_save = orig; |
| 353 | orig_saved = TRUE; |
| 354 | |
| 355 | // Do the expansion. |
| 356 | if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files, |
| 357 | options) == FAIL) |
| 358 | { |
| 359 | #ifdef FNAME_ILLEGAL |
| 360 | // Illegal file name has been silently skipped. But when there |
| 361 | // are wildcards, the real problem is that there was no match, |
| 362 | // causing the pattern to be added, which has illegal characters. |
| 363 | if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) |
| 364 | semsg(_(e_nomatch2), str); |
| 365 | #endif |
| 366 | } |
| 367 | else if (xp->xp_numfiles == 0) |
| 368 | { |
| 369 | if (!(options & WILD_SILENT)) |
| 370 | semsg(_(e_nomatch2), str); |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | // Escape the matches for use on the command line. |
| 375 | ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); |
| 376 | |
| 377 | // Check for matching suffixes in file names. |
| 378 | if (mode != WILD_ALL && mode != WILD_ALL_KEEP |
| 379 | && mode != WILD_LONGEST) |
| 380 | { |
| 381 | if (xp->xp_numfiles) |
| 382 | non_suf_match = xp->xp_numfiles; |
| 383 | else |
| 384 | non_suf_match = 1; |
| 385 | if ((xp->xp_context == EXPAND_FILES |
| 386 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 387 | && xp->xp_numfiles > 1) |
| 388 | { |
| 389 | // More than one match; check suffix. |
| 390 | // The files will have been sorted on matching suffix in |
| 391 | // expand_wildcards, only need to check the first two. |
| 392 | non_suf_match = 0; |
| 393 | for (i = 0; i < 2; ++i) |
| 394 | if (match_suffix(xp->xp_files[i])) |
| 395 | ++non_suf_match; |
| 396 | } |
| 397 | if (non_suf_match != 1) |
| 398 | { |
| 399 | // Can we ever get here unless it's while expanding |
| 400 | // interactively? If not, we can get rid of this all |
| 401 | // together. Don't really want to wait for this message |
| 402 | // (and possibly have to hit return to continue!). |
| 403 | if (!(options & WILD_SILENT)) |
| 404 | emsg(_(e_toomany)); |
| 405 | else if (!(options & WILD_NO_BEEP)) |
| 406 | beep_flush(); |
| 407 | } |
| 408 | if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) |
| 409 | ss = vim_strsave(xp->xp_files[0]); |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // Find longest common part |
| 415 | if (mode == WILD_LONGEST && xp->xp_numfiles > 0) |
| 416 | { |
| 417 | int mb_len = 1; |
| 418 | int c0, ci; |
| 419 | |
| 420 | for (len = 0; xp->xp_files[0][len]; len += mb_len) |
| 421 | { |
| 422 | if (has_mbyte) |
| 423 | { |
| 424 | mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]); |
| 425 | c0 =(* mb_ptr2char)(&xp->xp_files[0][len]); |
| 426 | } |
| 427 | else |
| 428 | c0 = xp->xp_files[0][len]; |
| 429 | for (i = 1; i < xp->xp_numfiles; ++i) |
| 430 | { |
| 431 | if (has_mbyte) |
| 432 | ci =(* mb_ptr2char)(&xp->xp_files[i][len]); |
| 433 | else |
| 434 | ci = xp->xp_files[i][len]; |
| 435 | if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES |
| 436 | || xp->xp_context == EXPAND_FILES |
| 437 | || xp->xp_context == EXPAND_SHELLCMD |
| 438 | || xp->xp_context == EXPAND_BUFFERS)) |
| 439 | { |
| 440 | if (MB_TOLOWER(c0) != MB_TOLOWER(ci)) |
| 441 | break; |
| 442 | } |
| 443 | else if (c0 != ci) |
| 444 | break; |
| 445 | } |
| 446 | if (i < xp->xp_numfiles) |
| 447 | { |
| 448 | if (!(options & WILD_NO_BEEP)) |
| 449 | vim_beep(BO_WILD); |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | ss = alloc(len + 1); |
| 455 | if (ss) |
| 456 | vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
| 457 | findex = -1; // next p_wc gets first one |
| 458 | } |
| 459 | |
| 460 | // Concatenate all matching names |
| 461 | if (mode == WILD_ALL && xp->xp_numfiles > 0) |
| 462 | { |
| 463 | len = 0; |
| 464 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 465 | len += (long_u)STRLEN(xp->xp_files[i]) + 1; |
| 466 | ss = alloc(len); |
| 467 | if (ss != NULL) |
| 468 | { |
| 469 | *ss = NUL; |
| 470 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 471 | { |
| 472 | STRCAT(ss, xp->xp_files[i]); |
| 473 | if (i != xp->xp_numfiles - 1) |
| 474 | STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) |
| 480 | ExpandCleanup(xp); |
| 481 | |
| 482 | // Free "orig" if it wasn't stored in "orig_save". |
| 483 | if (!orig_saved) |
| 484 | vim_free(orig); |
| 485 | |
| 486 | return ss; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Prepare an expand structure for use. |
| 491 | */ |
| 492 | void |
| 493 | ExpandInit(expand_T *xp) |
| 494 | { |
| 495 | xp->xp_pattern = NULL; |
| 496 | xp->xp_pattern_len = 0; |
| 497 | xp->xp_backslash = XP_BS_NONE; |
| 498 | #ifndef BACKSLASH_IN_FILENAME |
| 499 | xp->xp_shell = FALSE; |
| 500 | #endif |
| 501 | xp->xp_numfiles = -1; |
| 502 | xp->xp_files = NULL; |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 503 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 504 | xp->xp_arg = NULL; |
| 505 | #endif |
| 506 | xp->xp_line = NULL; |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Cleanup an expand structure after use. |
| 511 | */ |
| 512 | void |
| 513 | ExpandCleanup(expand_T *xp) |
| 514 | { |
| 515 | if (xp->xp_numfiles >= 0) |
| 516 | { |
| 517 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 518 | xp->xp_numfiles = -1; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Show all matches for completion on the command line. |
| 524 | * Returns EXPAND_NOTHING when the character that triggered expansion should |
| 525 | * be inserted like a normal character. |
| 526 | */ |
| 527 | int |
| 528 | showmatches(expand_T *xp, int wildmenu UNUSED) |
| 529 | { |
| 530 | cmdline_info_T *ccline = get_cmdline_info(); |
| 531 | #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m]) |
| 532 | int num_files; |
| 533 | char_u **files_found; |
| 534 | int i, j, k; |
| 535 | int maxlen; |
| 536 | int lines; |
| 537 | int columns; |
| 538 | char_u *p; |
| 539 | int lastlen; |
| 540 | int attr; |
| 541 | int showtail; |
| 542 | |
| 543 | if (xp->xp_numfiles == -1) |
| 544 | { |
| 545 | set_expand_context(xp); |
| 546 | i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos, |
| 547 | &num_files, &files_found); |
| 548 | showtail = expand_showtail(xp); |
| 549 | if (i != EXPAND_OK) |
| 550 | return i; |
| 551 | |
| 552 | } |
| 553 | else |
| 554 | { |
| 555 | num_files = xp->xp_numfiles; |
| 556 | files_found = xp->xp_files; |
| 557 | showtail = cmd_showtail; |
| 558 | } |
| 559 | |
| 560 | #ifdef FEAT_WILDMENU |
| 561 | if (!wildmenu) |
| 562 | { |
| 563 | #endif |
| 564 | msg_didany = FALSE; // lines_left will be set |
| 565 | msg_start(); // prepare for paging |
| 566 | msg_putchar('\n'); |
| 567 | out_flush(); |
| 568 | cmdline_row = msg_row; |
| 569 | msg_didany = FALSE; // lines_left will be set again |
| 570 | msg_start(); // prepare for paging |
| 571 | #ifdef FEAT_WILDMENU |
| 572 | } |
| 573 | #endif |
| 574 | |
| 575 | if (got_int) |
| 576 | got_int = FALSE; // only int. the completion, not the cmd line |
| 577 | #ifdef FEAT_WILDMENU |
| 578 | else if (wildmenu) |
| 579 | win_redr_status_matches(xp, num_files, files_found, -1, showtail); |
| 580 | #endif |
| 581 | else |
| 582 | { |
| 583 | // find the length of the longest file name |
| 584 | maxlen = 0; |
| 585 | for (i = 0; i < num_files; ++i) |
| 586 | { |
| 587 | if (!showtail && (xp->xp_context == EXPAND_FILES |
| 588 | || xp->xp_context == EXPAND_SHELLCMD |
| 589 | || xp->xp_context == EXPAND_BUFFERS)) |
| 590 | { |
| 591 | home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE); |
| 592 | j = vim_strsize(NameBuff); |
| 593 | } |
| 594 | else |
| 595 | j = vim_strsize(L_SHOWFILE(i)); |
| 596 | if (j > maxlen) |
| 597 | maxlen = j; |
| 598 | } |
| 599 | |
| 600 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 601 | lines = num_files; |
| 602 | else |
| 603 | { |
| 604 | // compute the number of columns and lines for the listing |
| 605 | maxlen += 2; // two spaces between file names |
| 606 | columns = ((int)Columns + 2) / maxlen; |
| 607 | if (columns < 1) |
| 608 | columns = 1; |
| 609 | lines = (num_files + columns - 1) / columns; |
| 610 | } |
| 611 | |
| 612 | attr = HL_ATTR(HLF_D); // find out highlighting for directories |
| 613 | |
| 614 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 615 | { |
| 616 | msg_puts_attr(_("tagname"), HL_ATTR(HLF_T)); |
| 617 | msg_clr_eos(); |
| 618 | msg_advance(maxlen - 3); |
| 619 | msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T)); |
| 620 | } |
| 621 | |
| 622 | // list the files line by line |
| 623 | for (i = 0; i < lines; ++i) |
| 624 | { |
| 625 | lastlen = 999; |
| 626 | for (k = i; k < num_files; k += lines) |
| 627 | { |
| 628 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 629 | { |
| 630 | msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D)); |
| 631 | p = files_found[k] + STRLEN(files_found[k]) + 1; |
| 632 | msg_advance(maxlen + 1); |
| 633 | msg_puts((char *)p); |
| 634 | msg_advance(maxlen + 3); |
| 635 | msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D)); |
| 636 | break; |
| 637 | } |
| 638 | for (j = maxlen - lastlen; --j >= 0; ) |
| 639 | msg_putchar(' '); |
| 640 | if (xp->xp_context == EXPAND_FILES |
| 641 | || xp->xp_context == EXPAND_SHELLCMD |
| 642 | || xp->xp_context == EXPAND_BUFFERS) |
| 643 | { |
| 644 | // highlight directories |
| 645 | if (xp->xp_numfiles != -1) |
| 646 | { |
| 647 | char_u *halved_slash; |
| 648 | char_u *exp_path; |
Bram Moolenaar | f1552d0 | 2019-08-21 12:54:18 +0200 | [diff] [blame] | 649 | char_u *path; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 650 | |
| 651 | // Expansion was done before and special characters |
| 652 | // were escaped, need to halve backslashes. Also |
| 653 | // $HOME has been replaced with ~/. |
| 654 | exp_path = expand_env_save_opt(files_found[k], TRUE); |
Bram Moolenaar | f1552d0 | 2019-08-21 12:54:18 +0200 | [diff] [blame] | 655 | path = exp_path != NULL ? exp_path : files_found[k]; |
| 656 | halved_slash = backslash_halve_save(path); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 657 | j = mch_isdir(halved_slash != NULL ? halved_slash |
| 658 | : files_found[k]); |
| 659 | vim_free(exp_path); |
Bram Moolenaar | f1552d0 | 2019-08-21 12:54:18 +0200 | [diff] [blame] | 660 | if (halved_slash != path) |
| 661 | vim_free(halved_slash); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 662 | } |
| 663 | else |
| 664 | // Expansion was done here, file names are literal. |
| 665 | j = mch_isdir(files_found[k]); |
| 666 | if (showtail) |
| 667 | p = L_SHOWFILE(k); |
| 668 | else |
| 669 | { |
| 670 | home_replace(NULL, files_found[k], NameBuff, MAXPATHL, |
| 671 | TRUE); |
| 672 | p = NameBuff; |
| 673 | } |
| 674 | } |
| 675 | else |
| 676 | { |
| 677 | j = FALSE; |
| 678 | p = L_SHOWFILE(k); |
| 679 | } |
| 680 | lastlen = msg_outtrans_attr(p, j ? attr : 0); |
| 681 | } |
| 682 | if (msg_col > 0) // when not wrapped around |
| 683 | { |
| 684 | msg_clr_eos(); |
| 685 | msg_putchar('\n'); |
| 686 | } |
| 687 | out_flush(); // show one line at a time |
| 688 | if (got_int) |
| 689 | { |
| 690 | got_int = FALSE; |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | // we redraw the command below the lines that we have just listed |
| 696 | // This is a bit tricky, but it saves a lot of screen updating. |
| 697 | cmdline_row = msg_row; // will put it back later |
| 698 | } |
| 699 | |
| 700 | if (xp->xp_numfiles == -1) |
| 701 | FreeWild(num_files, files_found); |
| 702 | |
| 703 | return EXPAND_OK; |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | * Private gettail for showmatches() (and win_redr_status_matches()): |
| 708 | * Find tail of file name path, but ignore trailing "/". |
| 709 | */ |
| 710 | char_u * |
| 711 | sm_gettail(char_u *s) |
| 712 | { |
| 713 | char_u *p; |
| 714 | char_u *t = s; |
| 715 | int had_sep = FALSE; |
| 716 | |
| 717 | for (p = s; *p != NUL; ) |
| 718 | { |
| 719 | if (vim_ispathsep(*p) |
| 720 | #ifdef BACKSLASH_IN_FILENAME |
| 721 | && !rem_backslash(p) |
| 722 | #endif |
| 723 | ) |
| 724 | had_sep = TRUE; |
| 725 | else if (had_sep) |
| 726 | { |
| 727 | t = p; |
| 728 | had_sep = FALSE; |
| 729 | } |
| 730 | MB_PTR_ADV(p); |
| 731 | } |
| 732 | return t; |
| 733 | } |
| 734 | |
| 735 | /* |
| 736 | * Return TRUE if we only need to show the tail of completion matches. |
| 737 | * When not completing file names or there is a wildcard in the path FALSE is |
| 738 | * returned. |
| 739 | */ |
| 740 | static int |
| 741 | expand_showtail(expand_T *xp) |
| 742 | { |
| 743 | char_u *s; |
| 744 | char_u *end; |
| 745 | |
| 746 | // When not completing file names a "/" may mean something different. |
| 747 | if (xp->xp_context != EXPAND_FILES |
| 748 | && xp->xp_context != EXPAND_SHELLCMD |
| 749 | && xp->xp_context != EXPAND_DIRECTORIES) |
| 750 | return FALSE; |
| 751 | |
| 752 | end = gettail(xp->xp_pattern); |
| 753 | if (end == xp->xp_pattern) // there is no path separator |
| 754 | return FALSE; |
| 755 | |
| 756 | for (s = xp->xp_pattern; s < end; s++) |
| 757 | { |
| 758 | // Skip escaped wildcards. Only when the backslash is not a path |
| 759 | // separator, on DOS the '*' "path\*\file" must not be skipped. |
| 760 | if (rem_backslash(s)) |
| 761 | ++s; |
| 762 | else if (vim_strchr((char_u *)"*?[", *s) != NULL) |
| 763 | return FALSE; |
| 764 | } |
| 765 | return TRUE; |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * Prepare a string for expansion. |
| 770 | * When expanding file names: The string will be used with expand_wildcards(). |
| 771 | * Copy "fname[len]" into allocated memory and add a '*' at the end. |
| 772 | * When expanding other names: The string will be used with regcomp(). Copy |
| 773 | * the name into allocated memory and prepend "^". |
| 774 | */ |
| 775 | char_u * |
| 776 | addstar( |
| 777 | char_u *fname, |
| 778 | int len, |
| 779 | int context) // EXPAND_FILES etc. |
| 780 | { |
| 781 | char_u *retval; |
| 782 | int i, j; |
| 783 | int new_len; |
| 784 | char_u *tail; |
| 785 | int ends_in_star; |
| 786 | |
| 787 | if (context != EXPAND_FILES |
| 788 | && context != EXPAND_FILES_IN_PATH |
| 789 | && context != EXPAND_SHELLCMD |
| 790 | && context != EXPAND_DIRECTORIES) |
| 791 | { |
| 792 | // Matching will be done internally (on something other than files). |
| 793 | // So we convert the file-matching-type wildcards into our kind for |
| 794 | // use with vim_regcomp(). First work out how long it will be: |
| 795 | |
| 796 | // For help tags the translation is done in find_help_tags(). |
| 797 | // For a tag pattern starting with "/" no translation is needed. |
| 798 | if (context == EXPAND_HELP |
| 799 | || context == EXPAND_COLORS |
| 800 | || context == EXPAND_COMPILER |
| 801 | || context == EXPAND_OWNSYNTAX |
| 802 | || context == EXPAND_FILETYPE |
| 803 | || context == EXPAND_PACKADD |
| 804 | || ((context == EXPAND_TAGS_LISTFILES |
| 805 | || context == EXPAND_TAGS) |
| 806 | && fname[0] == '/')) |
| 807 | retval = vim_strnsave(fname, len); |
| 808 | else |
| 809 | { |
| 810 | new_len = len + 2; // +2 for '^' at start, NUL at end |
| 811 | for (i = 0; i < len; i++) |
| 812 | { |
| 813 | if (fname[i] == '*' || fname[i] == '~') |
| 814 | new_len++; // '*' needs to be replaced by ".*" |
| 815 | // '~' needs to be replaced by "\~" |
| 816 | |
| 817 | // Buffer names are like file names. "." should be literal |
| 818 | if (context == EXPAND_BUFFERS && fname[i] == '.') |
| 819 | new_len++; // "." becomes "\." |
| 820 | |
| 821 | // Custom expansion takes care of special things, match |
| 822 | // backslashes literally (perhaps also for other types?) |
| 823 | if ((context == EXPAND_USER_DEFINED |
| 824 | || context == EXPAND_USER_LIST) && fname[i] == '\\') |
| 825 | new_len++; // '\' becomes "\\" |
| 826 | } |
| 827 | retval = alloc(new_len); |
| 828 | if (retval != NULL) |
| 829 | { |
| 830 | retval[0] = '^'; |
| 831 | j = 1; |
| 832 | for (i = 0; i < len; i++, j++) |
| 833 | { |
| 834 | // Skip backslash. But why? At least keep it for custom |
| 835 | // expansion. |
| 836 | if (context != EXPAND_USER_DEFINED |
| 837 | && context != EXPAND_USER_LIST |
| 838 | && fname[i] == '\\' |
| 839 | && ++i == len) |
| 840 | break; |
| 841 | |
| 842 | switch (fname[i]) |
| 843 | { |
| 844 | case '*': retval[j++] = '.'; |
| 845 | break; |
| 846 | case '~': retval[j++] = '\\'; |
| 847 | break; |
| 848 | case '?': retval[j] = '.'; |
| 849 | continue; |
| 850 | case '.': if (context == EXPAND_BUFFERS) |
| 851 | retval[j++] = '\\'; |
| 852 | break; |
| 853 | case '\\': if (context == EXPAND_USER_DEFINED |
| 854 | || context == EXPAND_USER_LIST) |
| 855 | retval[j++] = '\\'; |
| 856 | break; |
| 857 | } |
| 858 | retval[j] = fname[i]; |
| 859 | } |
| 860 | retval[j] = NUL; |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | else |
| 865 | { |
| 866 | retval = alloc(len + 4); |
| 867 | if (retval != NULL) |
| 868 | { |
| 869 | vim_strncpy(retval, fname, len); |
| 870 | |
| 871 | // Don't add a star to *, ~, ~user, $var or `cmd`. |
| 872 | // * would become **, which walks the whole tree. |
| 873 | // ~ would be at the start of the file name, but not the tail. |
| 874 | // $ could be anywhere in the tail. |
| 875 | // ` could be anywhere in the file name. |
| 876 | // When the name ends in '$' don't add a star, remove the '$'. |
| 877 | tail = gettail(retval); |
| 878 | ends_in_star = (len > 0 && retval[len - 1] == '*'); |
| 879 | #ifndef BACKSLASH_IN_FILENAME |
| 880 | for (i = len - 2; i >= 0; --i) |
| 881 | { |
| 882 | if (retval[i] != '\\') |
| 883 | break; |
| 884 | ends_in_star = !ends_in_star; |
| 885 | } |
| 886 | #endif |
| 887 | if ((*retval != '~' || tail != retval) |
| 888 | && !ends_in_star |
| 889 | && vim_strchr(tail, '$') == NULL |
| 890 | && vim_strchr(retval, '`') == NULL) |
| 891 | retval[len++] = '*'; |
| 892 | else if (len > 0 && retval[len - 1] == '$') |
| 893 | --len; |
| 894 | retval[len] = NUL; |
| 895 | } |
| 896 | } |
| 897 | return retval; |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * Must parse the command line so far to work out what context we are in. |
| 902 | * Completion can then be done based on that context. |
| 903 | * This routine sets the variables: |
| 904 | * xp->xp_pattern The start of the pattern to be expanded within |
| 905 | * the command line (ends at the cursor). |
| 906 | * xp->xp_context The type of thing to expand. Will be one of: |
| 907 | * |
| 908 | * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on |
| 909 | * the command line, like an unknown command. Caller |
| 910 | * should beep. |
| 911 | * EXPAND_NOTHING Unrecognised context for completion, use char like |
| 912 | * a normal char, rather than for completion. eg |
| 913 | * :s/^I/ |
| 914 | * EXPAND_COMMANDS Cursor is still touching the command, so complete |
| 915 | * it. |
| 916 | * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. |
| 917 | * EXPAND_FILES After command with EX_XFILE set, or after setting |
| 918 | * with P_EXPAND set. eg :e ^I, :w>>^I |
| 919 | * EXPAND_DIRECTORIES In some cases this is used instead of the latter |
| 920 | * when we know only directories are of interest. eg |
| 921 | * :set dir=^I |
| 922 | * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
| 923 | * EXPAND_SETTINGS Complete variable names. eg :set d^I |
| 924 | * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I |
| 925 | * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I |
| 926 | * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect |
| 927 | * EXPAND_HELP Complete tags from the file 'helpfile'/tags |
| 928 | * EXPAND_EVENTS Complete event names |
| 929 | * EXPAND_SYNTAX Complete :syntax command arguments |
| 930 | * EXPAND_HIGHLIGHT Complete highlight (syntax) group names |
| 931 | * EXPAND_AUGROUP Complete autocommand group names |
| 932 | * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I |
| 933 | * EXPAND_MAPPINGS Complete mapping and abbreviation names, |
| 934 | * eg :unmap a^I , :cunab x^I |
| 935 | * EXPAND_FUNCTIONS Complete internal or user defined function names, |
| 936 | * eg :call sub^I |
| 937 | * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I |
| 938 | * EXPAND_EXPRESSION Complete internal or user defined function/variable |
| 939 | * names in expressions, eg :while s^I |
| 940 | * EXPAND_ENV_VARS Complete environment variable names |
| 941 | * EXPAND_USER Complete user names |
| 942 | */ |
| 943 | static void |
| 944 | set_expand_context(expand_T *xp) |
| 945 | { |
| 946 | cmdline_info_T *ccline = get_cmdline_info(); |
| 947 | |
| 948 | // only expansion for ':', '>' and '=' command-lines |
| 949 | if (ccline->cmdfirstc != ':' |
| 950 | #ifdef FEAT_EVAL |
| 951 | && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '=' |
| 952 | && !ccline->input_fn |
| 953 | #endif |
| 954 | ) |
| 955 | { |
| 956 | xp->xp_context = EXPAND_NOTHING; |
| 957 | return; |
| 958 | } |
| 959 | set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE); |
| 960 | } |
| 961 | |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame^] | 962 | /* |
| 963 | * This is all pretty much copied from do_one_cmd(), with all the extra stuff |
| 964 | * we don't need/want deleted. Maybe this could be done better if we didn't |
| 965 | * repeat all this stuff. The only problem is that they may not stay |
| 966 | * perfectly compatible with each other, but then the command line syntax |
| 967 | * probably won't change that much -- webb. |
| 968 | */ |
| 969 | static char_u * |
| 970 | set_one_cmd_context( |
| 971 | expand_T *xp, |
| 972 | char_u *buff) // buffer for command string |
| 973 | { |
| 974 | char_u *p; |
| 975 | char_u *cmd, *arg; |
| 976 | int len = 0; |
| 977 | exarg_T ea; |
| 978 | int compl = EXPAND_NOTHING; |
| 979 | int delim; |
| 980 | int forceit = FALSE; |
| 981 | int usefilter = FALSE; // filter instead of file name |
| 982 | |
| 983 | ExpandInit(xp); |
| 984 | xp->xp_pattern = buff; |
| 985 | xp->xp_context = EXPAND_COMMANDS; // Default until we get past command |
| 986 | ea.argt = 0; |
| 987 | |
| 988 | // 1. skip comment lines and leading space, colons or bars |
| 989 | for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++) |
| 990 | ; |
| 991 | xp->xp_pattern = cmd; |
| 992 | |
| 993 | if (*cmd == NUL) |
| 994 | return NULL; |
| 995 | if (*cmd == '"') // ignore comment lines |
| 996 | { |
| 997 | xp->xp_context = EXPAND_NOTHING; |
| 998 | return NULL; |
| 999 | } |
| 1000 | |
| 1001 | // 3. Skip over the range to find the command. |
| 1002 | cmd = skip_range(cmd, &xp->xp_context); |
| 1003 | xp->xp_pattern = cmd; |
| 1004 | if (*cmd == NUL) |
| 1005 | return NULL; |
| 1006 | if (*cmd == '"') |
| 1007 | { |
| 1008 | xp->xp_context = EXPAND_NOTHING; |
| 1009 | return NULL; |
| 1010 | } |
| 1011 | |
| 1012 | if (*cmd == '|' || *cmd == '\n') |
| 1013 | return cmd + 1; // There's another command |
| 1014 | |
| 1015 | // Isolate the command and search for it in the command table. |
| 1016 | // Exceptions: |
| 1017 | // - the 'k' command can directly be followed by any character, but |
| 1018 | // do accept "keepmarks", "keepalt" and "keepjumps". |
| 1019 | // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r' |
| 1020 | if (*cmd == 'k' && cmd[1] != 'e') |
| 1021 | { |
| 1022 | ea.cmdidx = CMD_k; |
| 1023 | p = cmd + 1; |
| 1024 | } |
| 1025 | else |
| 1026 | { |
| 1027 | p = cmd; |
| 1028 | while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card |
| 1029 | ++p; |
| 1030 | // a user command may contain digits |
| 1031 | if (ASCII_ISUPPER(cmd[0])) |
| 1032 | while (ASCII_ISALNUM(*p) || *p == '*') |
| 1033 | ++p; |
| 1034 | // for python 3.x: ":py3*" commands completion |
| 1035 | if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3') |
| 1036 | { |
| 1037 | ++p; |
| 1038 | while (ASCII_ISALPHA(*p) || *p == '*') |
| 1039 | ++p; |
| 1040 | } |
| 1041 | // check for non-alpha command |
| 1042 | if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL) |
| 1043 | ++p; |
| 1044 | len = (int)(p - cmd); |
| 1045 | |
| 1046 | if (len == 0) |
| 1047 | { |
| 1048 | xp->xp_context = EXPAND_UNSUCCESSFUL; |
| 1049 | return NULL; |
| 1050 | } |
| 1051 | |
| 1052 | ea.cmdidx = excmd_get_cmdidx(cmd, len); |
| 1053 | |
| 1054 | if (cmd[0] >= 'A' && cmd[0] <= 'Z') |
| 1055 | while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card |
| 1056 | ++p; |
| 1057 | } |
| 1058 | |
| 1059 | // If the cursor is touching the command, and it ends in an alpha-numeric |
| 1060 | // character, complete the command name. |
| 1061 | if (*p == NUL && ASCII_ISALNUM(p[-1])) |
| 1062 | return NULL; |
| 1063 | |
| 1064 | if (ea.cmdidx == CMD_SIZE) |
| 1065 | { |
| 1066 | if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL) |
| 1067 | { |
| 1068 | ea.cmdidx = CMD_substitute; |
| 1069 | p = cmd + 1; |
| 1070 | } |
| 1071 | else if (cmd[0] >= 'A' && cmd[0] <= 'Z') |
| 1072 | { |
| 1073 | ea.cmd = cmd; |
| 1074 | p = find_ucmd(&ea, p, NULL, xp, &compl); |
| 1075 | if (p == NULL) |
| 1076 | ea.cmdidx = CMD_SIZE; // ambiguous user command |
| 1077 | } |
| 1078 | } |
| 1079 | if (ea.cmdidx == CMD_SIZE) |
| 1080 | { |
| 1081 | // Not still touching the command and it was an illegal one |
| 1082 | xp->xp_context = EXPAND_UNSUCCESSFUL; |
| 1083 | return NULL; |
| 1084 | } |
| 1085 | |
| 1086 | xp->xp_context = EXPAND_NOTHING; // Default now that we're past command |
| 1087 | |
| 1088 | if (*p == '!') // forced commands |
| 1089 | { |
| 1090 | forceit = TRUE; |
| 1091 | ++p; |
| 1092 | } |
| 1093 | |
| 1094 | // 6. parse arguments |
| 1095 | if (!IS_USER_CMDIDX(ea.cmdidx)) |
| 1096 | ea.argt = excmd_get_argt(ea.cmdidx); |
| 1097 | |
| 1098 | arg = skipwhite(p); |
| 1099 | |
| 1100 | if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update) |
| 1101 | { |
| 1102 | if (*arg == '>') // append |
| 1103 | { |
| 1104 | if (*++arg == '>') |
| 1105 | ++arg; |
| 1106 | arg = skipwhite(arg); |
| 1107 | } |
| 1108 | else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter |
| 1109 | { |
| 1110 | ++arg; |
| 1111 | usefilter = TRUE; |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | if (ea.cmdidx == CMD_read) |
| 1116 | { |
| 1117 | usefilter = forceit; // :r! filter if forced |
| 1118 | if (*arg == '!') // :r !filter |
| 1119 | { |
| 1120 | ++arg; |
| 1121 | usefilter = TRUE; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift) |
| 1126 | { |
| 1127 | while (*arg == *cmd) // allow any number of '>' or '<' |
| 1128 | ++arg; |
| 1129 | arg = skipwhite(arg); |
| 1130 | } |
| 1131 | |
| 1132 | // Does command allow "+command"? |
| 1133 | if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+') |
| 1134 | { |
| 1135 | // Check if we're in the +command |
| 1136 | p = arg + 1; |
| 1137 | arg = skip_cmd_arg(arg, FALSE); |
| 1138 | |
| 1139 | // Still touching the command after '+'? |
| 1140 | if (*arg == NUL) |
| 1141 | return p; |
| 1142 | |
| 1143 | // Skip space(s) after +command to get to the real argument |
| 1144 | arg = skipwhite(arg); |
| 1145 | } |
| 1146 | |
| 1147 | // Check for '|' to separate commands and '"' to start comments. |
| 1148 | // Don't do this for ":read !cmd" and ":write !cmd". |
| 1149 | if ((ea.argt & EX_TRLBAR) && !usefilter) |
| 1150 | { |
| 1151 | p = arg; |
| 1152 | // ":redir @" is not the start of a comment |
| 1153 | if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"') |
| 1154 | p += 2; |
| 1155 | while (*p) |
| 1156 | { |
| 1157 | if (*p == Ctrl_V) |
| 1158 | { |
| 1159 | if (p[1] != NUL) |
| 1160 | ++p; |
| 1161 | } |
| 1162 | else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM)) |
| 1163 | || *p == '|' || *p == '\n') |
| 1164 | { |
| 1165 | if (*(p - 1) != '\\') |
| 1166 | { |
| 1167 | if (*p == '|' || *p == '\n') |
| 1168 | return p + 1; |
| 1169 | return NULL; // It's a comment |
| 1170 | } |
| 1171 | } |
| 1172 | MB_PTR_ADV(p); |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | if (!(ea.argt & EX_EXTRA) && *arg != NUL |
| 1177 | && vim_strchr((char_u *)"|\"", *arg) == NULL) |
| 1178 | // no arguments allowed but there is something |
| 1179 | return NULL; |
| 1180 | |
| 1181 | // Find start of last argument (argument just before cursor): |
| 1182 | p = buff; |
| 1183 | xp->xp_pattern = p; |
| 1184 | len = (int)STRLEN(buff); |
| 1185 | while (*p && p < buff + len) |
| 1186 | { |
| 1187 | if (*p == ' ' || *p == TAB) |
| 1188 | { |
| 1189 | // argument starts after a space |
| 1190 | xp->xp_pattern = ++p; |
| 1191 | } |
| 1192 | else |
| 1193 | { |
| 1194 | if (*p == '\\' && *(p + 1) != NUL) |
| 1195 | ++p; // skip over escaped character |
| 1196 | MB_PTR_ADV(p); |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | if (ea.argt & EX_XFILE) |
| 1201 | { |
| 1202 | int c; |
| 1203 | int in_quote = FALSE; |
| 1204 | char_u *bow = NULL; // Beginning of word |
| 1205 | |
| 1206 | // Allow spaces within back-quotes to count as part of the argument |
| 1207 | // being expanded. |
| 1208 | xp->xp_pattern = skipwhite(arg); |
| 1209 | p = xp->xp_pattern; |
| 1210 | while (*p != NUL) |
| 1211 | { |
| 1212 | if (has_mbyte) |
| 1213 | c = mb_ptr2char(p); |
| 1214 | else |
| 1215 | c = *p; |
| 1216 | if (c == '\\' && p[1] != NUL) |
| 1217 | ++p; |
| 1218 | else if (c == '`') |
| 1219 | { |
| 1220 | if (!in_quote) |
| 1221 | { |
| 1222 | xp->xp_pattern = p; |
| 1223 | bow = p + 1; |
| 1224 | } |
| 1225 | in_quote = !in_quote; |
| 1226 | } |
| 1227 | // An argument can contain just about everything, except |
| 1228 | // characters that end the command and white space. |
| 1229 | else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c) |
| 1230 | #ifdef SPACE_IN_FILENAME |
| 1231 | && (!(ea.argt & EX_NOSPC) || usefilter) |
| 1232 | #endif |
| 1233 | )) |
| 1234 | { |
| 1235 | len = 0; // avoid getting stuck when space is in 'isfname' |
| 1236 | while (*p != NUL) |
| 1237 | { |
| 1238 | if (has_mbyte) |
| 1239 | c = mb_ptr2char(p); |
| 1240 | else |
| 1241 | c = *p; |
| 1242 | if (c == '`' || vim_isfilec_or_wc(c)) |
| 1243 | break; |
| 1244 | if (has_mbyte) |
| 1245 | len = (*mb_ptr2len)(p); |
| 1246 | else |
| 1247 | len = 1; |
| 1248 | MB_PTR_ADV(p); |
| 1249 | } |
| 1250 | if (in_quote) |
| 1251 | bow = p; |
| 1252 | else |
| 1253 | xp->xp_pattern = p; |
| 1254 | p -= len; |
| 1255 | } |
| 1256 | MB_PTR_ADV(p); |
| 1257 | } |
| 1258 | |
| 1259 | // If we are still inside the quotes, and we passed a space, just |
| 1260 | // expand from there. |
| 1261 | if (bow != NULL && in_quote) |
| 1262 | xp->xp_pattern = bow; |
| 1263 | xp->xp_context = EXPAND_FILES; |
| 1264 | |
| 1265 | // For a shell command more chars need to be escaped. |
| 1266 | if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal) |
| 1267 | { |
| 1268 | #ifndef BACKSLASH_IN_FILENAME |
| 1269 | xp->xp_shell = TRUE; |
| 1270 | #endif |
| 1271 | // When still after the command name expand executables. |
| 1272 | if (xp->xp_pattern == skipwhite(arg)) |
| 1273 | xp->xp_context = EXPAND_SHELLCMD; |
| 1274 | } |
| 1275 | |
| 1276 | // Check for environment variable |
| 1277 | if (*xp->xp_pattern == '$' |
| 1278 | #if defined(MSWIN) |
| 1279 | || *xp->xp_pattern == '%' |
| 1280 | #endif |
| 1281 | ) |
| 1282 | { |
| 1283 | for (p = xp->xp_pattern + 1; *p != NUL; ++p) |
| 1284 | if (!vim_isIDc(*p)) |
| 1285 | break; |
| 1286 | if (*p == NUL) |
| 1287 | { |
| 1288 | xp->xp_context = EXPAND_ENV_VARS; |
| 1289 | ++xp->xp_pattern; |
| 1290 | // Avoid that the assignment uses EXPAND_FILES again. |
| 1291 | if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST) |
| 1292 | compl = EXPAND_ENV_VARS; |
| 1293 | } |
| 1294 | } |
| 1295 | // Check for user names |
| 1296 | if (*xp->xp_pattern == '~') |
| 1297 | { |
| 1298 | for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p) |
| 1299 | ; |
| 1300 | // Complete ~user only if it partially matches a user name. |
| 1301 | // A full match ~user<Tab> will be replaced by user's home |
| 1302 | // directory i.e. something like ~user<Tab> -> /home/user/ |
| 1303 | if (*p == NUL && p > xp->xp_pattern + 1 |
| 1304 | && match_user(xp->xp_pattern + 1) >= 1) |
| 1305 | { |
| 1306 | xp->xp_context = EXPAND_USER; |
| 1307 | ++xp->xp_pattern; |
| 1308 | } |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | // 6. Switch on command name. |
| 1313 | switch (ea.cmdidx) |
| 1314 | { |
| 1315 | case CMD_find: |
| 1316 | case CMD_sfind: |
| 1317 | case CMD_tabfind: |
| 1318 | if (xp->xp_context == EXPAND_FILES) |
| 1319 | xp->xp_context = EXPAND_FILES_IN_PATH; |
| 1320 | break; |
| 1321 | case CMD_cd: |
| 1322 | case CMD_chdir: |
| 1323 | case CMD_tcd: |
| 1324 | case CMD_tchdir: |
| 1325 | case CMD_lcd: |
| 1326 | case CMD_lchdir: |
| 1327 | if (xp->xp_context == EXPAND_FILES) |
| 1328 | xp->xp_context = EXPAND_DIRECTORIES; |
| 1329 | break; |
| 1330 | case CMD_help: |
| 1331 | xp->xp_context = EXPAND_HELP; |
| 1332 | xp->xp_pattern = arg; |
| 1333 | break; |
| 1334 | |
| 1335 | // Command modifiers: return the argument. |
| 1336 | // Also for commands with an argument that is a command. |
| 1337 | case CMD_aboveleft: |
| 1338 | case CMD_argdo: |
| 1339 | case CMD_belowright: |
| 1340 | case CMD_botright: |
| 1341 | case CMD_browse: |
| 1342 | case CMD_bufdo: |
| 1343 | case CMD_cdo: |
| 1344 | case CMD_cfdo: |
| 1345 | case CMD_confirm: |
| 1346 | case CMD_debug: |
| 1347 | case CMD_folddoclosed: |
| 1348 | case CMD_folddoopen: |
| 1349 | case CMD_hide: |
| 1350 | case CMD_keepalt: |
| 1351 | case CMD_keepjumps: |
| 1352 | case CMD_keepmarks: |
| 1353 | case CMD_keeppatterns: |
| 1354 | case CMD_ldo: |
| 1355 | case CMD_leftabove: |
| 1356 | case CMD_lfdo: |
| 1357 | case CMD_lockmarks: |
| 1358 | case CMD_noautocmd: |
| 1359 | case CMD_noswapfile: |
| 1360 | case CMD_rightbelow: |
| 1361 | case CMD_sandbox: |
| 1362 | case CMD_silent: |
| 1363 | case CMD_tab: |
| 1364 | case CMD_tabdo: |
| 1365 | case CMD_topleft: |
| 1366 | case CMD_verbose: |
| 1367 | case CMD_vertical: |
| 1368 | case CMD_windo: |
| 1369 | return arg; |
| 1370 | |
| 1371 | case CMD_filter: |
| 1372 | if (*arg != NUL) |
| 1373 | arg = skip_vimgrep_pat(arg, NULL, NULL); |
| 1374 | if (arg == NULL || *arg == NUL) |
| 1375 | { |
| 1376 | xp->xp_context = EXPAND_NOTHING; |
| 1377 | return NULL; |
| 1378 | } |
| 1379 | return skipwhite(arg); |
| 1380 | |
| 1381 | #ifdef FEAT_SEARCH_EXTRA |
| 1382 | case CMD_match: |
| 1383 | if (*arg == NUL || !ends_excmd(*arg)) |
| 1384 | { |
| 1385 | // also complete "None" |
| 1386 | set_context_in_echohl_cmd(xp, arg); |
| 1387 | arg = skipwhite(skiptowhite(arg)); |
| 1388 | if (*arg != NUL) |
| 1389 | { |
| 1390 | xp->xp_context = EXPAND_NOTHING; |
| 1391 | arg = skip_regexp(arg + 1, *arg, p_magic, NULL); |
| 1392 | } |
| 1393 | } |
| 1394 | return find_nextcmd(arg); |
| 1395 | #endif |
| 1396 | |
| 1397 | // All completion for the +cmdline_compl feature goes here. |
| 1398 | |
| 1399 | case CMD_command: |
| 1400 | return set_context_in_user_cmd(xp, arg); |
| 1401 | |
| 1402 | case CMD_delcommand: |
| 1403 | xp->xp_context = EXPAND_USER_COMMANDS; |
| 1404 | xp->xp_pattern = arg; |
| 1405 | break; |
| 1406 | |
| 1407 | case CMD_global: |
| 1408 | case CMD_vglobal: |
| 1409 | delim = *arg; // get the delimiter |
| 1410 | if (delim) |
| 1411 | ++arg; // skip delimiter if there is one |
| 1412 | |
| 1413 | while (arg[0] != NUL && arg[0] != delim) |
| 1414 | { |
| 1415 | if (arg[0] == '\\' && arg[1] != NUL) |
| 1416 | ++arg; |
| 1417 | ++arg; |
| 1418 | } |
| 1419 | if (arg[0] != NUL) |
| 1420 | return arg + 1; |
| 1421 | break; |
| 1422 | case CMD_and: |
| 1423 | case CMD_substitute: |
| 1424 | delim = *arg; |
| 1425 | if (delim) |
| 1426 | { |
| 1427 | // skip "from" part |
| 1428 | ++arg; |
| 1429 | arg = skip_regexp(arg, delim, p_magic, NULL); |
| 1430 | } |
| 1431 | // skip "to" part |
| 1432 | while (arg[0] != NUL && arg[0] != delim) |
| 1433 | { |
| 1434 | if (arg[0] == '\\' && arg[1] != NUL) |
| 1435 | ++arg; |
| 1436 | ++arg; |
| 1437 | } |
| 1438 | if (arg[0] != NUL) // skip delimiter |
| 1439 | ++arg; |
| 1440 | while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL) |
| 1441 | ++arg; |
| 1442 | if (arg[0] != NUL) |
| 1443 | return arg; |
| 1444 | break; |
| 1445 | case CMD_isearch: |
| 1446 | case CMD_dsearch: |
| 1447 | case CMD_ilist: |
| 1448 | case CMD_dlist: |
| 1449 | case CMD_ijump: |
| 1450 | case CMD_psearch: |
| 1451 | case CMD_djump: |
| 1452 | case CMD_isplit: |
| 1453 | case CMD_dsplit: |
| 1454 | arg = skipwhite(skipdigits(arg)); // skip count |
| 1455 | if (*arg == '/') // Match regexp, not just whole words |
| 1456 | { |
| 1457 | for (++arg; *arg && *arg != '/'; arg++) |
| 1458 | if (*arg == '\\' && arg[1] != NUL) |
| 1459 | arg++; |
| 1460 | if (*arg) |
| 1461 | { |
| 1462 | arg = skipwhite(arg + 1); |
| 1463 | |
| 1464 | // Check for trailing illegal characters |
| 1465 | if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL) |
| 1466 | xp->xp_context = EXPAND_NOTHING; |
| 1467 | else |
| 1468 | return arg; |
| 1469 | } |
| 1470 | } |
| 1471 | break; |
| 1472 | |
| 1473 | case CMD_autocmd: |
| 1474 | return set_context_in_autocmd(xp, arg, FALSE); |
| 1475 | case CMD_doautocmd: |
| 1476 | case CMD_doautoall: |
| 1477 | return set_context_in_autocmd(xp, arg, TRUE); |
| 1478 | case CMD_set: |
| 1479 | set_context_in_set_cmd(xp, arg, 0); |
| 1480 | break; |
| 1481 | case CMD_setglobal: |
| 1482 | set_context_in_set_cmd(xp, arg, OPT_GLOBAL); |
| 1483 | break; |
| 1484 | case CMD_setlocal: |
| 1485 | set_context_in_set_cmd(xp, arg, OPT_LOCAL); |
| 1486 | break; |
| 1487 | case CMD_tag: |
| 1488 | case CMD_stag: |
| 1489 | case CMD_ptag: |
| 1490 | case CMD_ltag: |
| 1491 | case CMD_tselect: |
| 1492 | case CMD_stselect: |
| 1493 | case CMD_ptselect: |
| 1494 | case CMD_tjump: |
| 1495 | case CMD_stjump: |
| 1496 | case CMD_ptjump: |
| 1497 | if (*p_wop != NUL) |
| 1498 | xp->xp_context = EXPAND_TAGS_LISTFILES; |
| 1499 | else |
| 1500 | xp->xp_context = EXPAND_TAGS; |
| 1501 | xp->xp_pattern = arg; |
| 1502 | break; |
| 1503 | case CMD_augroup: |
| 1504 | xp->xp_context = EXPAND_AUGROUP; |
| 1505 | xp->xp_pattern = arg; |
| 1506 | break; |
| 1507 | #ifdef FEAT_SYN_HL |
| 1508 | case CMD_syntax: |
| 1509 | set_context_in_syntax_cmd(xp, arg); |
| 1510 | break; |
| 1511 | #endif |
| 1512 | #ifdef FEAT_EVAL |
| 1513 | case CMD_let: |
| 1514 | case CMD_if: |
| 1515 | case CMD_elseif: |
| 1516 | case CMD_while: |
| 1517 | case CMD_for: |
| 1518 | case CMD_echo: |
| 1519 | case CMD_echon: |
| 1520 | case CMD_execute: |
| 1521 | case CMD_echomsg: |
| 1522 | case CMD_echoerr: |
| 1523 | case CMD_call: |
| 1524 | case CMD_return: |
| 1525 | case CMD_cexpr: |
| 1526 | case CMD_caddexpr: |
| 1527 | case CMD_cgetexpr: |
| 1528 | case CMD_lexpr: |
| 1529 | case CMD_laddexpr: |
| 1530 | case CMD_lgetexpr: |
| 1531 | set_context_for_expression(xp, arg, ea.cmdidx); |
| 1532 | break; |
| 1533 | |
| 1534 | case CMD_unlet: |
| 1535 | while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) |
| 1536 | arg = xp->xp_pattern + 1; |
| 1537 | |
| 1538 | xp->xp_context = EXPAND_USER_VARS; |
| 1539 | xp->xp_pattern = arg; |
| 1540 | |
| 1541 | if (*xp->xp_pattern == '$') |
| 1542 | { |
| 1543 | xp->xp_context = EXPAND_ENV_VARS; |
| 1544 | ++xp->xp_pattern; |
| 1545 | } |
| 1546 | |
| 1547 | break; |
| 1548 | |
| 1549 | case CMD_function: |
| 1550 | case CMD_delfunction: |
| 1551 | xp->xp_context = EXPAND_USER_FUNC; |
| 1552 | xp->xp_pattern = arg; |
| 1553 | break; |
| 1554 | |
| 1555 | case CMD_echohl: |
| 1556 | set_context_in_echohl_cmd(xp, arg); |
| 1557 | break; |
| 1558 | #endif |
| 1559 | case CMD_highlight: |
| 1560 | set_context_in_highlight_cmd(xp, arg); |
| 1561 | break; |
| 1562 | #ifdef FEAT_CSCOPE |
| 1563 | case CMD_cscope: |
| 1564 | case CMD_lcscope: |
| 1565 | case CMD_scscope: |
| 1566 | set_context_in_cscope_cmd(xp, arg, ea.cmdidx); |
| 1567 | break; |
| 1568 | #endif |
| 1569 | #ifdef FEAT_SIGNS |
| 1570 | case CMD_sign: |
| 1571 | set_context_in_sign_cmd(xp, arg); |
| 1572 | break; |
| 1573 | #endif |
| 1574 | case CMD_bdelete: |
| 1575 | case CMD_bwipeout: |
| 1576 | case CMD_bunload: |
| 1577 | while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) |
| 1578 | arg = xp->xp_pattern + 1; |
| 1579 | // FALLTHROUGH |
| 1580 | case CMD_buffer: |
| 1581 | case CMD_sbuffer: |
| 1582 | case CMD_checktime: |
| 1583 | xp->xp_context = EXPAND_BUFFERS; |
| 1584 | xp->xp_pattern = arg; |
| 1585 | break; |
| 1586 | |
| 1587 | case CMD_USER: |
| 1588 | case CMD_USER_BUF: |
| 1589 | if (compl != EXPAND_NOTHING) |
| 1590 | { |
| 1591 | // EX_XFILE: file names are handled above |
| 1592 | if (!(ea.argt & EX_XFILE)) |
| 1593 | { |
| 1594 | #ifdef FEAT_MENU |
| 1595 | if (compl == EXPAND_MENUS) |
| 1596 | return set_context_in_menu_cmd(xp, cmd, arg, forceit); |
| 1597 | #endif |
| 1598 | if (compl == EXPAND_COMMANDS) |
| 1599 | return arg; |
| 1600 | if (compl == EXPAND_MAPPINGS) |
| 1601 | return set_context_in_map_cmd(xp, (char_u *)"map", |
| 1602 | arg, forceit, FALSE, FALSE, CMD_map); |
| 1603 | // Find start of last argument. |
| 1604 | p = arg; |
| 1605 | while (*p) |
| 1606 | { |
| 1607 | if (*p == ' ') |
| 1608 | // argument starts after a space |
| 1609 | arg = p + 1; |
| 1610 | else if (*p == '\\' && *(p + 1) != NUL) |
| 1611 | ++p; // skip over escaped character |
| 1612 | MB_PTR_ADV(p); |
| 1613 | } |
| 1614 | xp->xp_pattern = arg; |
| 1615 | } |
| 1616 | xp->xp_context = compl; |
| 1617 | } |
| 1618 | break; |
| 1619 | |
| 1620 | case CMD_map: case CMD_noremap: |
| 1621 | case CMD_nmap: case CMD_nnoremap: |
| 1622 | case CMD_vmap: case CMD_vnoremap: |
| 1623 | case CMD_omap: case CMD_onoremap: |
| 1624 | case CMD_imap: case CMD_inoremap: |
| 1625 | case CMD_cmap: case CMD_cnoremap: |
| 1626 | case CMD_lmap: case CMD_lnoremap: |
| 1627 | case CMD_smap: case CMD_snoremap: |
| 1628 | case CMD_tmap: case CMD_tnoremap: |
| 1629 | case CMD_xmap: case CMD_xnoremap: |
| 1630 | return set_context_in_map_cmd(xp, cmd, arg, forceit, |
| 1631 | FALSE, FALSE, ea.cmdidx); |
| 1632 | case CMD_unmap: |
| 1633 | case CMD_nunmap: |
| 1634 | case CMD_vunmap: |
| 1635 | case CMD_ounmap: |
| 1636 | case CMD_iunmap: |
| 1637 | case CMD_cunmap: |
| 1638 | case CMD_lunmap: |
| 1639 | case CMD_sunmap: |
| 1640 | case CMD_tunmap: |
| 1641 | case CMD_xunmap: |
| 1642 | return set_context_in_map_cmd(xp, cmd, arg, forceit, |
| 1643 | FALSE, TRUE, ea.cmdidx); |
| 1644 | case CMD_mapclear: |
| 1645 | case CMD_nmapclear: |
| 1646 | case CMD_vmapclear: |
| 1647 | case CMD_omapclear: |
| 1648 | case CMD_imapclear: |
| 1649 | case CMD_cmapclear: |
| 1650 | case CMD_lmapclear: |
| 1651 | case CMD_smapclear: |
| 1652 | case CMD_tmapclear: |
| 1653 | case CMD_xmapclear: |
| 1654 | xp->xp_context = EXPAND_MAPCLEAR; |
| 1655 | xp->xp_pattern = arg; |
| 1656 | break; |
| 1657 | |
| 1658 | case CMD_abbreviate: case CMD_noreabbrev: |
| 1659 | case CMD_cabbrev: case CMD_cnoreabbrev: |
| 1660 | case CMD_iabbrev: case CMD_inoreabbrev: |
| 1661 | return set_context_in_map_cmd(xp, cmd, arg, forceit, |
| 1662 | TRUE, FALSE, ea.cmdidx); |
| 1663 | case CMD_unabbreviate: |
| 1664 | case CMD_cunabbrev: |
| 1665 | case CMD_iunabbrev: |
| 1666 | return set_context_in_map_cmd(xp, cmd, arg, forceit, |
| 1667 | TRUE, TRUE, ea.cmdidx); |
| 1668 | #ifdef FEAT_MENU |
| 1669 | case CMD_menu: case CMD_noremenu: case CMD_unmenu: |
| 1670 | case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu: |
| 1671 | case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu: |
| 1672 | case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu: |
| 1673 | case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu: |
| 1674 | case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu: |
| 1675 | case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu: |
| 1676 | case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu: |
| 1677 | case CMD_tmenu: case CMD_tunmenu: |
| 1678 | case CMD_popup: case CMD_tearoff: case CMD_emenu: |
| 1679 | return set_context_in_menu_cmd(xp, cmd, arg, forceit); |
| 1680 | #endif |
| 1681 | |
| 1682 | case CMD_colorscheme: |
| 1683 | xp->xp_context = EXPAND_COLORS; |
| 1684 | xp->xp_pattern = arg; |
| 1685 | break; |
| 1686 | |
| 1687 | case CMD_compiler: |
| 1688 | xp->xp_context = EXPAND_COMPILER; |
| 1689 | xp->xp_pattern = arg; |
| 1690 | break; |
| 1691 | |
| 1692 | case CMD_ownsyntax: |
| 1693 | xp->xp_context = EXPAND_OWNSYNTAX; |
| 1694 | xp->xp_pattern = arg; |
| 1695 | break; |
| 1696 | |
| 1697 | case CMD_setfiletype: |
| 1698 | xp->xp_context = EXPAND_FILETYPE; |
| 1699 | xp->xp_pattern = arg; |
| 1700 | break; |
| 1701 | |
| 1702 | case CMD_packadd: |
| 1703 | xp->xp_context = EXPAND_PACKADD; |
| 1704 | xp->xp_pattern = arg; |
| 1705 | break; |
| 1706 | |
| 1707 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 1708 | case CMD_language: |
| 1709 | p = skiptowhite(arg); |
| 1710 | if (*p == NUL) |
| 1711 | { |
| 1712 | xp->xp_context = EXPAND_LANGUAGE; |
| 1713 | xp->xp_pattern = arg; |
| 1714 | } |
| 1715 | else |
| 1716 | { |
| 1717 | if ( STRNCMP(arg, "messages", p - arg) == 0 |
| 1718 | || STRNCMP(arg, "ctype", p - arg) == 0 |
| 1719 | || STRNCMP(arg, "time", p - arg) == 0) |
| 1720 | { |
| 1721 | xp->xp_context = EXPAND_LOCALES; |
| 1722 | xp->xp_pattern = skipwhite(p); |
| 1723 | } |
| 1724 | else |
| 1725 | xp->xp_context = EXPAND_NOTHING; |
| 1726 | } |
| 1727 | break; |
| 1728 | #endif |
| 1729 | #if defined(FEAT_PROFILE) |
| 1730 | case CMD_profile: |
| 1731 | set_context_in_profile_cmd(xp, arg); |
| 1732 | break; |
| 1733 | #endif |
| 1734 | case CMD_behave: |
| 1735 | xp->xp_context = EXPAND_BEHAVE; |
| 1736 | xp->xp_pattern = arg; |
| 1737 | break; |
| 1738 | |
| 1739 | case CMD_messages: |
| 1740 | xp->xp_context = EXPAND_MESSAGES; |
| 1741 | xp->xp_pattern = arg; |
| 1742 | break; |
| 1743 | |
| 1744 | case CMD_history: |
| 1745 | xp->xp_context = EXPAND_HISTORY; |
| 1746 | xp->xp_pattern = arg; |
| 1747 | break; |
| 1748 | #if defined(FEAT_PROFILE) |
| 1749 | case CMD_syntime: |
| 1750 | xp->xp_context = EXPAND_SYNTIME; |
| 1751 | xp->xp_pattern = arg; |
| 1752 | break; |
| 1753 | #endif |
| 1754 | |
| 1755 | case CMD_argdelete: |
| 1756 | while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) |
| 1757 | arg = xp->xp_pattern + 1; |
| 1758 | xp->xp_context = EXPAND_ARGLIST; |
| 1759 | xp->xp_pattern = arg; |
| 1760 | break; |
| 1761 | |
| 1762 | default: |
| 1763 | break; |
| 1764 | } |
| 1765 | return NULL; |
| 1766 | } |
| 1767 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1768 | void |
| 1769 | set_cmd_context( |
| 1770 | expand_T *xp, |
| 1771 | char_u *str, // start of command line |
| 1772 | int len, // length of command line (excl. NUL) |
| 1773 | int col, // position of cursor |
| 1774 | int use_ccline UNUSED) // use ccline for info |
| 1775 | { |
| 1776 | #ifdef FEAT_EVAL |
| 1777 | cmdline_info_T *ccline = get_cmdline_info(); |
| 1778 | #endif |
| 1779 | int old_char = NUL; |
| 1780 | char_u *nextcomm; |
| 1781 | |
| 1782 | // Avoid a UMR warning from Purify, only save the character if it has been |
| 1783 | // written before. |
| 1784 | if (col < len) |
| 1785 | old_char = str[col]; |
| 1786 | str[col] = NUL; |
| 1787 | nextcomm = str; |
| 1788 | |
| 1789 | #ifdef FEAT_EVAL |
| 1790 | if (use_ccline && ccline->cmdfirstc == '=') |
| 1791 | { |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1792 | // pass CMD_SIZE because there is no real command |
| 1793 | set_context_for_expression(xp, str, CMD_SIZE); |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1794 | } |
| 1795 | else if (use_ccline && ccline->input_fn) |
| 1796 | { |
| 1797 | xp->xp_context = ccline->xp_context; |
| 1798 | xp->xp_pattern = ccline->cmdbuff; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1799 | xp->xp_arg = ccline->xp_arg; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1800 | } |
| 1801 | else |
| 1802 | #endif |
| 1803 | while (nextcomm != NULL) |
| 1804 | nextcomm = set_one_cmd_context(xp, nextcomm); |
| 1805 | |
| 1806 | // Store the string here so that call_user_expand_func() can get to them |
| 1807 | // easily. |
| 1808 | xp->xp_line = str; |
| 1809 | xp->xp_col = col; |
| 1810 | |
| 1811 | str[col] = old_char; |
| 1812 | } |
| 1813 | |
| 1814 | /* |
| 1815 | * Expand the command line "str" from context "xp". |
| 1816 | * "xp" must have been set by set_cmd_context(). |
| 1817 | * xp->xp_pattern points into "str", to where the text that is to be expanded |
| 1818 | * starts. |
| 1819 | * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the |
| 1820 | * cursor. |
| 1821 | * Returns EXPAND_NOTHING when there is nothing to expand, might insert the |
| 1822 | * key that triggered expansion literally. |
| 1823 | * Returns EXPAND_OK otherwise. |
| 1824 | */ |
| 1825 | int |
| 1826 | expand_cmdline( |
| 1827 | expand_T *xp, |
| 1828 | char_u *str, // start of command line |
| 1829 | int col, // position of cursor |
| 1830 | int *matchcount, // return: nr of matches |
| 1831 | char_u ***matches) // return: array of pointers to matches |
| 1832 | { |
| 1833 | char_u *file_str = NULL; |
| 1834 | int options = WILD_ADD_SLASH|WILD_SILENT; |
| 1835 | |
| 1836 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 1837 | { |
| 1838 | beep_flush(); |
| 1839 | return EXPAND_UNSUCCESSFUL; // Something illegal on command line |
| 1840 | } |
| 1841 | if (xp->xp_context == EXPAND_NOTHING) |
| 1842 | { |
| 1843 | // Caller can use the character as a normal char instead |
| 1844 | return EXPAND_NOTHING; |
| 1845 | } |
| 1846 | |
| 1847 | // add star to file name, or convert to regexp if not exp. files. |
| 1848 | xp->xp_pattern_len = (int)(str + col - xp->xp_pattern); |
| 1849 | file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context); |
| 1850 | if (file_str == NULL) |
| 1851 | return EXPAND_UNSUCCESSFUL; |
| 1852 | |
| 1853 | if (p_wic) |
| 1854 | options += WILD_ICASE; |
| 1855 | |
| 1856 | // find all files that match the description |
| 1857 | if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL) |
| 1858 | { |
| 1859 | *matchcount = 0; |
| 1860 | *matches = NULL; |
| 1861 | } |
| 1862 | vim_free(file_str); |
| 1863 | |
| 1864 | return EXPAND_OK; |
| 1865 | } |
| 1866 | |
| 1867 | #ifdef FEAT_MULTI_LANG |
| 1868 | /* |
| 1869 | * Cleanup matches for help tags: |
| 1870 | * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first |
| 1871 | * tag matches it. Otherwise remove "@en" if "en" is the only language. |
| 1872 | */ |
| 1873 | static void |
| 1874 | cleanup_help_tags(int num_file, char_u **file) |
| 1875 | { |
| 1876 | int i, j; |
| 1877 | int len; |
| 1878 | char_u buf[4]; |
| 1879 | char_u *p = buf; |
| 1880 | |
| 1881 | if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n')) |
| 1882 | { |
| 1883 | *p++ = '@'; |
| 1884 | *p++ = p_hlg[0]; |
| 1885 | *p++ = p_hlg[1]; |
| 1886 | } |
| 1887 | *p = NUL; |
| 1888 | |
| 1889 | for (i = 0; i < num_file; ++i) |
| 1890 | { |
| 1891 | len = (int)STRLEN(file[i]) - 3; |
| 1892 | if (len <= 0) |
| 1893 | continue; |
| 1894 | if (STRCMP(file[i] + len, "@en") == 0) |
| 1895 | { |
| 1896 | // Sorting on priority means the same item in another language may |
| 1897 | // be anywhere. Search all items for a match up to the "@en". |
| 1898 | for (j = 0; j < num_file; ++j) |
| 1899 | if (j != i && (int)STRLEN(file[j]) == len + 3 |
| 1900 | && STRNCMP(file[i], file[j], len + 1) == 0) |
| 1901 | break; |
| 1902 | if (j == num_file) |
| 1903 | // item only exists with @en, remove it |
| 1904 | file[i][len] = NUL; |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | if (*buf != NUL) |
| 1909 | for (i = 0; i < num_file; ++i) |
| 1910 | { |
| 1911 | len = (int)STRLEN(file[i]) - 3; |
| 1912 | if (len <= 0) |
| 1913 | continue; |
| 1914 | if (STRCMP(file[i] + len, buf) == 0) |
| 1915 | { |
| 1916 | // remove the default language |
| 1917 | file[i][len] = NUL; |
| 1918 | } |
| 1919 | } |
| 1920 | } |
| 1921 | #endif |
| 1922 | |
| 1923 | /* |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame^] | 1924 | * Function given to ExpandGeneric() to obtain the possible arguments of the |
| 1925 | * ":behave {mswin,xterm}" command. |
| 1926 | */ |
| 1927 | static char_u * |
| 1928 | get_behave_arg(expand_T *xp UNUSED, int idx) |
| 1929 | { |
| 1930 | if (idx == 0) |
| 1931 | return (char_u *)"mswin"; |
| 1932 | if (idx == 1) |
| 1933 | return (char_u *)"xterm"; |
| 1934 | return NULL; |
| 1935 | } |
| 1936 | |
| 1937 | /* |
| 1938 | * Function given to ExpandGeneric() to obtain the possible arguments of the |
| 1939 | * ":messages {clear}" command. |
| 1940 | */ |
| 1941 | static char_u * |
| 1942 | get_messages_arg(expand_T *xp UNUSED, int idx) |
| 1943 | { |
| 1944 | if (idx == 0) |
| 1945 | return (char_u *)"clear"; |
| 1946 | return NULL; |
| 1947 | } |
| 1948 | |
| 1949 | static char_u * |
| 1950 | get_mapclear_arg(expand_T *xp UNUSED, int idx) |
| 1951 | { |
| 1952 | if (idx == 0) |
| 1953 | return (char_u *)"<buffer>"; |
| 1954 | return NULL; |
| 1955 | } |
| 1956 | |
| 1957 | /* |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1958 | * Do the expansion based on xp->xp_context and "pat". |
| 1959 | */ |
| 1960 | static int |
| 1961 | ExpandFromContext( |
| 1962 | expand_T *xp, |
| 1963 | char_u *pat, |
| 1964 | int *num_file, |
| 1965 | char_u ***file, |
| 1966 | int options) // WILD_ flags |
| 1967 | { |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1968 | regmatch_T regmatch; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 1969 | int ret; |
| 1970 | int flags; |
| 1971 | |
| 1972 | flags = EW_DIR; // include directories |
| 1973 | if (options & WILD_LIST_NOTFOUND) |
| 1974 | flags |= EW_NOTFOUND; |
| 1975 | if (options & WILD_ADD_SLASH) |
| 1976 | flags |= EW_ADDSLASH; |
| 1977 | if (options & WILD_KEEP_ALL) |
| 1978 | flags |= EW_KEEPALL; |
| 1979 | if (options & WILD_SILENT) |
| 1980 | flags |= EW_SILENT; |
| 1981 | if (options & WILD_ALLLINKS) |
| 1982 | flags |= EW_ALLLINKS; |
| 1983 | |
| 1984 | if (xp->xp_context == EXPAND_FILES |
| 1985 | || xp->xp_context == EXPAND_DIRECTORIES |
| 1986 | || xp->xp_context == EXPAND_FILES_IN_PATH) |
| 1987 | { |
| 1988 | // Expand file or directory names. |
| 1989 | int free_pat = FALSE; |
| 1990 | int i; |
| 1991 | |
| 1992 | // for ":set path=" and ":set tags=" halve backslashes for escaped |
| 1993 | // space |
| 1994 | if (xp->xp_backslash != XP_BS_NONE) |
| 1995 | { |
| 1996 | free_pat = TRUE; |
| 1997 | pat = vim_strsave(pat); |
| 1998 | for (i = 0; pat[i]; ++i) |
| 1999 | if (pat[i] == '\\') |
| 2000 | { |
| 2001 | if (xp->xp_backslash == XP_BS_THREE |
| 2002 | && pat[i + 1] == '\\' |
| 2003 | && pat[i + 2] == '\\' |
| 2004 | && pat[i + 3] == ' ') |
| 2005 | STRMOVE(pat + i, pat + i + 3); |
| 2006 | if (xp->xp_backslash == XP_BS_ONE |
| 2007 | && pat[i + 1] == ' ') |
| 2008 | STRMOVE(pat + i, pat + i + 1); |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | if (xp->xp_context == EXPAND_FILES) |
| 2013 | flags |= EW_FILE; |
| 2014 | else if (xp->xp_context == EXPAND_FILES_IN_PATH) |
| 2015 | flags |= (EW_FILE | EW_PATH); |
| 2016 | else |
| 2017 | flags = (flags | EW_DIR) & ~EW_FILE; |
| 2018 | if (options & WILD_ICASE) |
| 2019 | flags |= EW_ICASE; |
| 2020 | |
| 2021 | // Expand wildcards, supporting %:h and the like. |
| 2022 | ret = expand_wildcards_eval(&pat, num_file, file, flags); |
| 2023 | if (free_pat) |
| 2024 | vim_free(pat); |
| 2025 | #ifdef BACKSLASH_IN_FILENAME |
| 2026 | if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0) |
| 2027 | { |
| 2028 | int i; |
| 2029 | |
| 2030 | for (i = 0; i < *num_file; ++i) |
| 2031 | { |
| 2032 | char_u *ptr = (*file)[i]; |
| 2033 | |
| 2034 | while (*ptr != NUL) |
| 2035 | { |
| 2036 | if (p_csl[0] == 's' && *ptr == '\\') |
| 2037 | *ptr = '/'; |
| 2038 | else if (p_csl[0] == 'b' && *ptr == '/') |
| 2039 | *ptr = '\\'; |
| 2040 | ptr += (*mb_ptr2len)(ptr); |
| 2041 | } |
| 2042 | } |
| 2043 | } |
| 2044 | #endif |
| 2045 | return ret; |
| 2046 | } |
| 2047 | |
| 2048 | *file = (char_u **)""; |
| 2049 | *num_file = 0; |
| 2050 | if (xp->xp_context == EXPAND_HELP) |
| 2051 | { |
| 2052 | // With an empty argument we would get all the help tags, which is |
| 2053 | // very slow. Get matches for "help" instead. |
| 2054 | if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, |
| 2055 | num_file, file, FALSE) == OK) |
| 2056 | { |
| 2057 | #ifdef FEAT_MULTI_LANG |
| 2058 | cleanup_help_tags(*num_file, *file); |
| 2059 | #endif |
| 2060 | return OK; |
| 2061 | } |
| 2062 | return FAIL; |
| 2063 | } |
| 2064 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2065 | if (xp->xp_context == EXPAND_SHELLCMD) |
| 2066 | return expand_shellcmd(pat, num_file, file, flags); |
| 2067 | if (xp->xp_context == EXPAND_OLD_SETTING) |
| 2068 | return ExpandOldSetting(num_file, file); |
| 2069 | if (xp->xp_context == EXPAND_BUFFERS) |
| 2070 | return ExpandBufnames(pat, num_file, file, options); |
| 2071 | if (xp->xp_context == EXPAND_TAGS |
| 2072 | || xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 2073 | return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file); |
| 2074 | if (xp->xp_context == EXPAND_COLORS) |
| 2075 | { |
| 2076 | char *directories[] = {"colors", NULL}; |
| 2077 | return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file, |
| 2078 | directories); |
| 2079 | } |
| 2080 | if (xp->xp_context == EXPAND_COMPILER) |
| 2081 | { |
| 2082 | char *directories[] = {"compiler", NULL}; |
| 2083 | return ExpandRTDir(pat, 0, num_file, file, directories); |
| 2084 | } |
| 2085 | if (xp->xp_context == EXPAND_OWNSYNTAX) |
| 2086 | { |
| 2087 | char *directories[] = {"syntax", NULL}; |
| 2088 | return ExpandRTDir(pat, 0, num_file, file, directories); |
| 2089 | } |
| 2090 | if (xp->xp_context == EXPAND_FILETYPE) |
| 2091 | { |
| 2092 | char *directories[] = {"syntax", "indent", "ftplugin", NULL}; |
| 2093 | return ExpandRTDir(pat, 0, num_file, file, directories); |
| 2094 | } |
| 2095 | # if defined(FEAT_EVAL) |
| 2096 | if (xp->xp_context == EXPAND_USER_LIST) |
| 2097 | return ExpandUserList(xp, num_file, file); |
| 2098 | # endif |
| 2099 | if (xp->xp_context == EXPAND_PACKADD) |
| 2100 | return ExpandPackAddDir(pat, num_file, file); |
| 2101 | |
| 2102 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 2103 | if (regmatch.regprog == NULL) |
| 2104 | return FAIL; |
| 2105 | |
| 2106 | // set ignore-case according to p_ic, p_scs and pat |
| 2107 | regmatch.rm_ic = ignorecase(pat); |
| 2108 | |
| 2109 | if (xp->xp_context == EXPAND_SETTINGS |
| 2110 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
| 2111 | ret = ExpandSettings(xp, ®match, num_file, file); |
| 2112 | else if (xp->xp_context == EXPAND_MAPPINGS) |
| 2113 | ret = ExpandMappings(®match, num_file, file); |
| 2114 | # if defined(FEAT_EVAL) |
| 2115 | else if (xp->xp_context == EXPAND_USER_DEFINED) |
| 2116 | ret = ExpandUserDefined(xp, ®match, num_file, file); |
| 2117 | # endif |
| 2118 | else |
| 2119 | { |
| 2120 | static struct expgen |
| 2121 | { |
| 2122 | int context; |
| 2123 | char_u *((*func)(expand_T *, int)); |
| 2124 | int ic; |
| 2125 | int escaped; |
| 2126 | } tab[] = |
| 2127 | { |
| 2128 | {EXPAND_COMMANDS, get_command_name, FALSE, TRUE}, |
| 2129 | {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE}, |
| 2130 | {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE}, |
| 2131 | {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE}, |
| 2132 | {EXPAND_HISTORY, get_history_arg, TRUE, TRUE}, |
| 2133 | {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE}, |
| 2134 | {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE}, |
| 2135 | {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE}, |
| 2136 | {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE}, |
| 2137 | {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE}, |
| 2138 | # ifdef FEAT_EVAL |
| 2139 | {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE}, |
| 2140 | {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE}, |
| 2141 | {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE}, |
| 2142 | {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE}, |
| 2143 | # endif |
| 2144 | # ifdef FEAT_MENU |
| 2145 | {EXPAND_MENUS, get_menu_name, FALSE, TRUE}, |
| 2146 | {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE}, |
| 2147 | # endif |
| 2148 | # ifdef FEAT_SYN_HL |
| 2149 | {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE}, |
| 2150 | # endif |
| 2151 | # ifdef FEAT_PROFILE |
| 2152 | {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE}, |
| 2153 | # endif |
| 2154 | {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE}, |
| 2155 | {EXPAND_EVENTS, get_event_name, TRUE, TRUE}, |
| 2156 | {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE}, |
| 2157 | # ifdef FEAT_CSCOPE |
| 2158 | {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE}, |
| 2159 | # endif |
| 2160 | # ifdef FEAT_SIGNS |
| 2161 | {EXPAND_SIGN, get_sign_name, TRUE, TRUE}, |
| 2162 | # endif |
| 2163 | # ifdef FEAT_PROFILE |
| 2164 | {EXPAND_PROFILE, get_profile_name, TRUE, TRUE}, |
| 2165 | # endif |
| 2166 | # if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 2167 | {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE}, |
| 2168 | {EXPAND_LOCALES, get_locales, TRUE, FALSE}, |
| 2169 | # endif |
| 2170 | {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE}, |
| 2171 | {EXPAND_USER, get_users, TRUE, FALSE}, |
| 2172 | {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE}, |
| 2173 | }; |
| 2174 | int i; |
| 2175 | |
| 2176 | // Find a context in the table and call the ExpandGeneric() with the |
| 2177 | // right function to do the expansion. |
| 2178 | ret = FAIL; |
| 2179 | for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) |
| 2180 | if (xp->xp_context == tab[i].context) |
| 2181 | { |
| 2182 | if (tab[i].ic) |
| 2183 | regmatch.rm_ic = TRUE; |
| 2184 | ret = ExpandGeneric(xp, ®match, num_file, file, |
| 2185 | tab[i].func, tab[i].escaped); |
| 2186 | break; |
| 2187 | } |
| 2188 | } |
| 2189 | |
| 2190 | vim_regfree(regmatch.regprog); |
| 2191 | |
| 2192 | return ret; |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2193 | } |
| 2194 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2195 | /* |
| 2196 | * Expand a list of names. |
| 2197 | * |
| 2198 | * Generic function for command line completion. It calls a function to |
| 2199 | * obtain strings, one by one. The strings are matched against a regexp |
| 2200 | * program. Matching strings are copied into an array, which is returned. |
| 2201 | * |
| 2202 | * Returns OK when no problems encountered, FAIL for error (out of memory). |
| 2203 | */ |
| 2204 | int |
| 2205 | ExpandGeneric( |
| 2206 | expand_T *xp, |
| 2207 | regmatch_T *regmatch, |
| 2208 | int *num_file, |
| 2209 | char_u ***file, |
| 2210 | char_u *((*func)(expand_T *, int)), |
| 2211 | // returns a string from the list |
| 2212 | int escaped) |
| 2213 | { |
| 2214 | int i; |
| 2215 | int count = 0; |
| 2216 | int round; |
| 2217 | char_u *str; |
| 2218 | |
| 2219 | // do this loop twice: |
| 2220 | // round == 0: count the number of matching names |
| 2221 | // round == 1: copy the matching names into allocated memory |
| 2222 | for (round = 0; round <= 1; ++round) |
| 2223 | { |
| 2224 | for (i = 0; ; ++i) |
| 2225 | { |
| 2226 | str = (*func)(xp, i); |
| 2227 | if (str == NULL) // end of list |
| 2228 | break; |
| 2229 | if (*str == NUL) // skip empty strings |
| 2230 | continue; |
| 2231 | |
| 2232 | if (vim_regexec(regmatch, str, (colnr_T)0)) |
| 2233 | { |
| 2234 | if (round) |
| 2235 | { |
| 2236 | if (escaped) |
| 2237 | str = vim_strsave_escaped(str, (char_u *)" \t\\."); |
| 2238 | else |
| 2239 | str = vim_strsave(str); |
| 2240 | (*file)[count] = str; |
| 2241 | # ifdef FEAT_MENU |
| 2242 | if (func == get_menu_names && str != NULL) |
| 2243 | { |
| 2244 | // test for separator added by get_menu_names() |
| 2245 | str += STRLEN(str) - 1; |
| 2246 | if (*str == '\001') |
| 2247 | *str = '.'; |
| 2248 | } |
| 2249 | # endif |
| 2250 | } |
| 2251 | ++count; |
| 2252 | } |
| 2253 | } |
| 2254 | if (round == 0) |
| 2255 | { |
| 2256 | if (count == 0) |
| 2257 | return OK; |
| 2258 | *num_file = count; |
| 2259 | *file = ALLOC_MULT(char_u *, count); |
| 2260 | if (*file == NULL) |
| 2261 | { |
| 2262 | *file = (char_u **)""; |
| 2263 | return FAIL; |
| 2264 | } |
| 2265 | count = 0; |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | // Sort the results. Keep menu's in the specified order. |
| 2270 | if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS) |
| 2271 | { |
| 2272 | if (xp->xp_context == EXPAND_EXPRESSION |
| 2273 | || xp->xp_context == EXPAND_FUNCTIONS |
| 2274 | || xp->xp_context == EXPAND_USER_FUNC) |
| 2275 | // <SNR> functions should be sorted to the end. |
| 2276 | qsort((void *)*file, (size_t)*num_file, sizeof(char_u *), |
| 2277 | sort_func_compare); |
| 2278 | else |
| 2279 | sort_strings(*file, *num_file); |
| 2280 | } |
| 2281 | |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 2282 | #if defined(FEAT_SYN_HL) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2283 | // Reset the variables used for special highlight names expansion, so that |
| 2284 | // they don't show up when getting normal highlight names by ID. |
| 2285 | reset_expand_highlight(); |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 2286 | #endif |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2287 | |
| 2288 | return OK; |
| 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * Complete a shell command. |
| 2293 | * Returns FAIL or OK; |
| 2294 | */ |
| 2295 | static int |
| 2296 | expand_shellcmd( |
| 2297 | char_u *filepat, // pattern to match with command names |
| 2298 | int *num_file, // return: number of matches |
| 2299 | char_u ***file, // return: array with matches |
| 2300 | int flagsarg) // EW_ flags |
| 2301 | { |
| 2302 | char_u *pat; |
| 2303 | int i; |
| 2304 | char_u *path = NULL; |
| 2305 | int mustfree = FALSE; |
| 2306 | garray_T ga; |
| 2307 | char_u *buf = alloc(MAXPATHL); |
| 2308 | size_t l; |
| 2309 | char_u *s, *e; |
| 2310 | int flags = flagsarg; |
| 2311 | int ret; |
| 2312 | int did_curdir = FALSE; |
| 2313 | hashtab_T found_ht; |
| 2314 | hashitem_T *hi; |
| 2315 | hash_T hash; |
| 2316 | |
| 2317 | if (buf == NULL) |
| 2318 | return FAIL; |
| 2319 | |
| 2320 | // for ":set path=" and ":set tags=" halve backslashes for escaped |
| 2321 | // space |
| 2322 | pat = vim_strsave(filepat); |
| 2323 | for (i = 0; pat[i]; ++i) |
| 2324 | if (pat[i] == '\\' && pat[i + 1] == ' ') |
| 2325 | STRMOVE(pat + i, pat + i + 1); |
| 2326 | |
| 2327 | flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; |
| 2328 | |
| 2329 | if (pat[0] == '.' && (vim_ispathsep(pat[1]) |
| 2330 | || (pat[1] == '.' && vim_ispathsep(pat[2])))) |
| 2331 | path = (char_u *)"."; |
| 2332 | else |
| 2333 | { |
| 2334 | // For an absolute name we don't use $PATH. |
| 2335 | if (!mch_isFullName(pat)) |
| 2336 | path = vim_getenv((char_u *)"PATH", &mustfree); |
| 2337 | if (path == NULL) |
| 2338 | path = (char_u *)""; |
| 2339 | } |
| 2340 | |
| 2341 | // Go over all directories in $PATH. Expand matches in that directory and |
| 2342 | // collect them in "ga". When "." is not in $PATH also expand for the |
| 2343 | // current directory, to find "subdir/cmd". |
| 2344 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 2345 | hash_init(&found_ht); |
| 2346 | for (s = path; ; s = e) |
| 2347 | { |
| 2348 | # if defined(MSWIN) |
| 2349 | e = vim_strchr(s, ';'); |
| 2350 | # else |
| 2351 | e = vim_strchr(s, ':'); |
| 2352 | # endif |
| 2353 | if (e == NULL) |
| 2354 | e = s + STRLEN(s); |
| 2355 | |
| 2356 | if (*s == NUL) |
| 2357 | { |
| 2358 | if (did_curdir) |
| 2359 | break; |
| 2360 | // Find directories in the current directory, path is empty. |
| 2361 | did_curdir = TRUE; |
| 2362 | flags |= EW_DIR; |
| 2363 | } |
| 2364 | else if (STRNCMP(s, ".", (int)(e - s)) == 0) |
| 2365 | { |
| 2366 | did_curdir = TRUE; |
| 2367 | flags |= EW_DIR; |
| 2368 | } |
| 2369 | else |
| 2370 | // Do not match directories inside a $PATH item. |
| 2371 | flags &= ~EW_DIR; |
| 2372 | |
| 2373 | l = e - s; |
| 2374 | if (l > MAXPATHL - 5) |
| 2375 | break; |
| 2376 | vim_strncpy(buf, s, l); |
| 2377 | add_pathsep(buf); |
| 2378 | l = STRLEN(buf); |
| 2379 | vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); |
| 2380 | |
| 2381 | // Expand matches in one directory of $PATH. |
| 2382 | ret = expand_wildcards(1, &buf, num_file, file, flags); |
| 2383 | if (ret == OK) |
| 2384 | { |
| 2385 | if (ga_grow(&ga, *num_file) == FAIL) |
| 2386 | FreeWild(*num_file, *file); |
| 2387 | else |
| 2388 | { |
| 2389 | for (i = 0; i < *num_file; ++i) |
| 2390 | { |
| 2391 | char_u *name = (*file)[i]; |
| 2392 | |
| 2393 | if (STRLEN(name) > l) |
| 2394 | { |
| 2395 | // Check if this name was already found. |
| 2396 | hash = hash_hash(name + l); |
| 2397 | hi = hash_lookup(&found_ht, name + l, hash); |
| 2398 | if (HASHITEM_EMPTY(hi)) |
| 2399 | { |
| 2400 | // Remove the path that was prepended. |
| 2401 | STRMOVE(name, name + l); |
| 2402 | ((char_u **)ga.ga_data)[ga.ga_len++] = name; |
| 2403 | hash_add_item(&found_ht, hi, name, hash); |
| 2404 | name = NULL; |
| 2405 | } |
| 2406 | } |
| 2407 | vim_free(name); |
| 2408 | } |
| 2409 | vim_free(*file); |
| 2410 | } |
| 2411 | } |
| 2412 | if (*e != NUL) |
| 2413 | ++e; |
| 2414 | } |
| 2415 | *file = ga.ga_data; |
| 2416 | *num_file = ga.ga_len; |
| 2417 | |
| 2418 | vim_free(buf); |
| 2419 | vim_free(pat); |
| 2420 | if (mustfree) |
| 2421 | vim_free(path); |
| 2422 | hash_clear(&found_ht); |
| 2423 | return OK; |
| 2424 | } |
| 2425 | |
| 2426 | # if defined(FEAT_EVAL) |
| 2427 | /* |
| 2428 | * Call "user_expand_func()" to invoke a user defined Vim script function and |
| 2429 | * return the result (either a string or a List). |
| 2430 | */ |
| 2431 | static void * |
| 2432 | call_user_expand_func( |
| 2433 | void *(*user_expand_func)(char_u *, int, typval_T *), |
| 2434 | expand_T *xp, |
| 2435 | int *num_file, |
| 2436 | char_u ***file) |
| 2437 | { |
| 2438 | cmdline_info_T *ccline = get_cmdline_info(); |
| 2439 | int keep = 0; |
| 2440 | typval_T args[4]; |
| 2441 | sctx_T save_current_sctx = current_sctx; |
| 2442 | char_u *pat = NULL; |
| 2443 | void *ret; |
| 2444 | |
| 2445 | if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL) |
| 2446 | return NULL; |
| 2447 | *num_file = 0; |
| 2448 | *file = NULL; |
| 2449 | |
| 2450 | if (ccline->cmdbuff != NULL) |
| 2451 | { |
| 2452 | keep = ccline->cmdbuff[ccline->cmdlen]; |
| 2453 | ccline->cmdbuff[ccline->cmdlen] = 0; |
| 2454 | } |
| 2455 | |
| 2456 | pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len); |
| 2457 | |
| 2458 | args[0].v_type = VAR_STRING; |
| 2459 | args[0].vval.v_string = pat; |
| 2460 | args[1].v_type = VAR_STRING; |
| 2461 | args[1].vval.v_string = xp->xp_line; |
| 2462 | args[2].v_type = VAR_NUMBER; |
| 2463 | args[2].vval.v_number = xp->xp_col; |
| 2464 | args[3].v_type = VAR_UNKNOWN; |
| 2465 | |
| 2466 | current_sctx = xp->xp_script_ctx; |
| 2467 | |
| 2468 | ret = user_expand_func(xp->xp_arg, 3, args); |
| 2469 | |
| 2470 | current_sctx = save_current_sctx; |
| 2471 | if (ccline->cmdbuff != NULL) |
| 2472 | ccline->cmdbuff[ccline->cmdlen] = keep; |
| 2473 | |
| 2474 | vim_free(pat); |
| 2475 | return ret; |
| 2476 | } |
| 2477 | |
| 2478 | /* |
| 2479 | * Expand names with a function defined by the user. |
| 2480 | */ |
| 2481 | static int |
| 2482 | ExpandUserDefined( |
| 2483 | expand_T *xp, |
| 2484 | regmatch_T *regmatch, |
| 2485 | int *num_file, |
| 2486 | char_u ***file) |
| 2487 | { |
| 2488 | char_u *retstr; |
| 2489 | char_u *s; |
| 2490 | char_u *e; |
| 2491 | int keep; |
| 2492 | garray_T ga; |
| 2493 | int skip; |
| 2494 | |
| 2495 | retstr = call_user_expand_func(call_func_retstr, xp, num_file, file); |
| 2496 | if (retstr == NULL) |
| 2497 | return FAIL; |
| 2498 | |
| 2499 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 2500 | for (s = retstr; *s != NUL; s = e) |
| 2501 | { |
| 2502 | e = vim_strchr(s, '\n'); |
| 2503 | if (e == NULL) |
| 2504 | e = s + STRLEN(s); |
| 2505 | keep = *e; |
| 2506 | *e = NUL; |
| 2507 | |
| 2508 | skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0; |
| 2509 | *e = keep; |
| 2510 | |
| 2511 | if (!skip) |
| 2512 | { |
| 2513 | if (ga_grow(&ga, 1) == FAIL) |
| 2514 | break; |
| 2515 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s)); |
| 2516 | ++ga.ga_len; |
| 2517 | } |
| 2518 | |
| 2519 | if (*e != NUL) |
| 2520 | ++e; |
| 2521 | } |
| 2522 | vim_free(retstr); |
| 2523 | *file = ga.ga_data; |
| 2524 | *num_file = ga.ga_len; |
| 2525 | return OK; |
| 2526 | } |
| 2527 | |
| 2528 | /* |
| 2529 | * Expand names with a list returned by a function defined by the user. |
| 2530 | */ |
| 2531 | static int |
| 2532 | ExpandUserList( |
| 2533 | expand_T *xp, |
| 2534 | int *num_file, |
| 2535 | char_u ***file) |
| 2536 | { |
| 2537 | list_T *retlist; |
| 2538 | listitem_T *li; |
| 2539 | garray_T ga; |
| 2540 | |
| 2541 | retlist = call_user_expand_func(call_func_retlist, xp, num_file, file); |
| 2542 | if (retlist == NULL) |
| 2543 | return FAIL; |
| 2544 | |
| 2545 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 2546 | // Loop over the items in the list. |
| 2547 | for (li = retlist->lv_first; li != NULL; li = li->li_next) |
| 2548 | { |
| 2549 | if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) |
| 2550 | continue; // Skip non-string items and empty strings |
| 2551 | |
| 2552 | if (ga_grow(&ga, 1) == FAIL) |
| 2553 | break; |
| 2554 | |
| 2555 | ((char_u **)ga.ga_data)[ga.ga_len] = |
| 2556 | vim_strsave(li->li_tv.vval.v_string); |
| 2557 | ++ga.ga_len; |
| 2558 | } |
| 2559 | list_unref(retlist); |
| 2560 | |
| 2561 | *file = ga.ga_data; |
| 2562 | *num_file = ga.ga_len; |
| 2563 | return OK; |
| 2564 | } |
| 2565 | # endif |
| 2566 | |
| 2567 | /* |
| 2568 | * Expand color scheme, compiler or filetype names. |
| 2569 | * Search from 'runtimepath': |
| 2570 | * 'runtimepath'/{dirnames}/{pat}.vim |
| 2571 | * When "flags" has DIP_START: search also from 'start' of 'packpath': |
| 2572 | * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim |
| 2573 | * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': |
| 2574 | * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim |
| 2575 | * "dirnames" is an array with one or more directory names. |
| 2576 | */ |
| 2577 | static int |
| 2578 | ExpandRTDir( |
| 2579 | char_u *pat, |
| 2580 | int flags, |
| 2581 | int *num_file, |
| 2582 | char_u ***file, |
| 2583 | char *dirnames[]) |
| 2584 | { |
| 2585 | char_u *s; |
| 2586 | char_u *e; |
| 2587 | char_u *match; |
| 2588 | garray_T ga; |
| 2589 | int i; |
| 2590 | int pat_len; |
| 2591 | |
| 2592 | *num_file = 0; |
| 2593 | *file = NULL; |
| 2594 | pat_len = (int)STRLEN(pat); |
| 2595 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 2596 | |
| 2597 | for (i = 0; dirnames[i] != NULL; ++i) |
| 2598 | { |
| 2599 | s = alloc(STRLEN(dirnames[i]) + pat_len + 7); |
| 2600 | if (s == NULL) |
| 2601 | { |
| 2602 | ga_clear_strings(&ga); |
| 2603 | return FAIL; |
| 2604 | } |
| 2605 | sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); |
| 2606 | globpath(p_rtp, s, &ga, 0); |
| 2607 | vim_free(s); |
| 2608 | } |
| 2609 | |
| 2610 | if (flags & DIP_START) { |
| 2611 | for (i = 0; dirnames[i] != NULL; ++i) |
| 2612 | { |
| 2613 | s = alloc(STRLEN(dirnames[i]) + pat_len + 22); |
| 2614 | if (s == NULL) |
| 2615 | { |
| 2616 | ga_clear_strings(&ga); |
| 2617 | return FAIL; |
| 2618 | } |
| 2619 | sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); |
| 2620 | globpath(p_pp, s, &ga, 0); |
| 2621 | vim_free(s); |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | if (flags & DIP_OPT) { |
| 2626 | for (i = 0; dirnames[i] != NULL; ++i) |
| 2627 | { |
| 2628 | s = alloc(STRLEN(dirnames[i]) + pat_len + 20); |
| 2629 | if (s == NULL) |
| 2630 | { |
| 2631 | ga_clear_strings(&ga); |
| 2632 | return FAIL; |
| 2633 | } |
| 2634 | sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); |
| 2635 | globpath(p_pp, s, &ga, 0); |
| 2636 | vim_free(s); |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | for (i = 0; i < ga.ga_len; ++i) |
| 2641 | { |
| 2642 | match = ((char_u **)ga.ga_data)[i]; |
| 2643 | s = match; |
| 2644 | e = s + STRLEN(s); |
| 2645 | if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
| 2646 | { |
| 2647 | e -= 4; |
| 2648 | for (s = e; s > match; MB_PTR_BACK(match, s)) |
| 2649 | if (s < match || vim_ispathsep(*s)) |
| 2650 | break; |
| 2651 | ++s; |
| 2652 | *e = NUL; |
| 2653 | mch_memmove(match, s, e - s + 1); |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | if (ga.ga_len == 0) |
| 2658 | return FAIL; |
| 2659 | |
| 2660 | // Sort and remove duplicates which can happen when specifying multiple |
| 2661 | // directories in dirnames. |
| 2662 | remove_duplicates(&ga); |
| 2663 | |
| 2664 | *file = ga.ga_data; |
| 2665 | *num_file = ga.ga_len; |
| 2666 | return OK; |
| 2667 | } |
| 2668 | |
| 2669 | /* |
| 2670 | * Expand loadplugin names: |
| 2671 | * 'packpath'/pack/ * /opt/{pat} |
| 2672 | */ |
| 2673 | static int |
| 2674 | ExpandPackAddDir( |
| 2675 | char_u *pat, |
| 2676 | int *num_file, |
| 2677 | char_u ***file) |
| 2678 | { |
| 2679 | char_u *s; |
| 2680 | char_u *e; |
| 2681 | char_u *match; |
| 2682 | garray_T ga; |
| 2683 | int i; |
| 2684 | int pat_len; |
| 2685 | |
| 2686 | *num_file = 0; |
| 2687 | *file = NULL; |
| 2688 | pat_len = (int)STRLEN(pat); |
| 2689 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 2690 | |
| 2691 | s = alloc(pat_len + 26); |
| 2692 | if (s == NULL) |
| 2693 | { |
| 2694 | ga_clear_strings(&ga); |
| 2695 | return FAIL; |
| 2696 | } |
| 2697 | sprintf((char *)s, "pack/*/opt/%s*", pat); |
| 2698 | globpath(p_pp, s, &ga, 0); |
| 2699 | vim_free(s); |
| 2700 | |
| 2701 | for (i = 0; i < ga.ga_len; ++i) |
| 2702 | { |
| 2703 | match = ((char_u **)ga.ga_data)[i]; |
| 2704 | s = gettail(match); |
| 2705 | e = s + STRLEN(s); |
| 2706 | mch_memmove(match, s, e - s + 1); |
| 2707 | } |
| 2708 | |
| 2709 | if (ga.ga_len == 0) |
| 2710 | return FAIL; |
| 2711 | |
| 2712 | // Sort and remove duplicates which can happen when specifying multiple |
| 2713 | // directories in dirnames. |
| 2714 | remove_duplicates(&ga); |
| 2715 | |
| 2716 | *file = ga.ga_data; |
| 2717 | *num_file = ga.ga_len; |
| 2718 | return OK; |
| 2719 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2720 | |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2721 | /* |
| 2722 | * Expand "file" for all comma-separated directories in "path". |
| 2723 | * Adds the matches to "ga". Caller must init "ga". |
| 2724 | */ |
| 2725 | void |
| 2726 | globpath( |
| 2727 | char_u *path, |
| 2728 | char_u *file, |
| 2729 | garray_T *ga, |
| 2730 | int expand_options) |
| 2731 | { |
| 2732 | expand_T xpc; |
| 2733 | char_u *buf; |
| 2734 | int i; |
| 2735 | int num_p; |
| 2736 | char_u **p; |
| 2737 | |
| 2738 | buf = alloc(MAXPATHL); |
| 2739 | if (buf == NULL) |
| 2740 | return; |
| 2741 | |
| 2742 | ExpandInit(&xpc); |
| 2743 | xpc.xp_context = EXPAND_FILES; |
| 2744 | |
| 2745 | // Loop over all entries in {path}. |
| 2746 | while (*path != NUL) |
| 2747 | { |
| 2748 | // Copy one item of the path to buf[] and concatenate the file name. |
| 2749 | copy_option_part(&path, buf, MAXPATHL, ","); |
| 2750 | if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) |
| 2751 | { |
| 2752 | # if defined(MSWIN) |
| 2753 | // Using the platform's path separator (\) makes vim incorrectly |
| 2754 | // treat it as an escape character, use '/' instead. |
| 2755 | if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf))) |
| 2756 | STRCAT(buf, "/"); |
| 2757 | # else |
| 2758 | add_pathsep(buf); |
| 2759 | # endif |
| 2760 | STRCAT(buf, file); |
| 2761 | if (ExpandFromContext(&xpc, buf, &num_p, &p, |
| 2762 | WILD_SILENT|expand_options) != FAIL && num_p > 0) |
| 2763 | { |
| 2764 | ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
| 2765 | |
| 2766 | if (ga_grow(ga, num_p) == OK) |
| 2767 | { |
| 2768 | for (i = 0; i < num_p; ++i) |
| 2769 | { |
| 2770 | ((char_u **)ga->ga_data)[ga->ga_len] = |
| 2771 | vim_strnsave(p[i], (int)STRLEN(p[i])); |
| 2772 | ++ga->ga_len; |
| 2773 | } |
| 2774 | } |
| 2775 | |
| 2776 | FreeWild(num_p, p); |
| 2777 | } |
| 2778 | } |
| 2779 | } |
| 2780 | |
| 2781 | vim_free(buf); |
| 2782 | } |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2783 | |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 2784 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 66b5142 | 2019-08-18 21:44:12 +0200 | [diff] [blame] | 2785 | /* |
| 2786 | * "getcompletion()" function |
| 2787 | */ |
| 2788 | void |
| 2789 | f_getcompletion(typval_T *argvars, typval_T *rettv) |
| 2790 | { |
| 2791 | char_u *pat; |
| 2792 | expand_T xpc; |
| 2793 | int filtered = FALSE; |
| 2794 | int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH |
| 2795 | | WILD_NO_BEEP; |
| 2796 | |
| 2797 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 2798 | filtered = tv_get_number_chk(&argvars[2], NULL); |
| 2799 | |
| 2800 | if (p_wic) |
| 2801 | options |= WILD_ICASE; |
| 2802 | |
| 2803 | // For filtered results, 'wildignore' is used |
| 2804 | if (!filtered) |
| 2805 | options |= WILD_KEEP_ALL; |
| 2806 | |
| 2807 | ExpandInit(&xpc); |
| 2808 | xpc.xp_pattern = tv_get_string(&argvars[0]); |
| 2809 | xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); |
| 2810 | xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1])); |
| 2811 | if (xpc.xp_context == EXPAND_NOTHING) |
| 2812 | { |
| 2813 | if (argvars[1].v_type == VAR_STRING) |
| 2814 | semsg(_(e_invarg2), argvars[1].vval.v_string); |
| 2815 | else |
| 2816 | emsg(_(e_invarg)); |
| 2817 | return; |
| 2818 | } |
| 2819 | |
| 2820 | # if defined(FEAT_MENU) |
| 2821 | if (xpc.xp_context == EXPAND_MENUS) |
| 2822 | { |
| 2823 | set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE); |
| 2824 | xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); |
| 2825 | } |
| 2826 | # endif |
| 2827 | # ifdef FEAT_CSCOPE |
| 2828 | if (xpc.xp_context == EXPAND_CSCOPE) |
| 2829 | { |
| 2830 | set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope); |
| 2831 | xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); |
| 2832 | } |
| 2833 | # endif |
| 2834 | # ifdef FEAT_SIGNS |
| 2835 | if (xpc.xp_context == EXPAND_SIGN) |
| 2836 | { |
| 2837 | set_context_in_sign_cmd(&xpc, xpc.xp_pattern); |
| 2838 | xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); |
| 2839 | } |
| 2840 | # endif |
| 2841 | |
| 2842 | pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context); |
| 2843 | if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL)) |
| 2844 | { |
| 2845 | int i; |
| 2846 | |
| 2847 | ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP); |
| 2848 | |
| 2849 | for (i = 0; i < xpc.xp_numfiles; i++) |
| 2850 | list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1); |
| 2851 | } |
| 2852 | vim_free(pat); |
| 2853 | ExpandCleanup(&xpc); |
| 2854 | } |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 2855 | #endif // FEAT_EVAL |