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