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