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