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