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