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