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