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