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