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