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