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