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