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