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