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