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