vim-patch:9.1.1576: cannot easily trigger wildcard expansion (#35022)

Problem:  cannot easily trigger wildcard expansion
Solution: Introduce wildtrigger() function
          (Girish Palya)

This PR introduces a new `wildtrigger()` function.

See `:h wildtrigger()`

`wildtrigger()` behaves like pressing the `wildchar,` but provides a
more refined and controlled completion experience:

- Suppresses beeps when no matches are found.
- Avoids displaying irrelevant completions (like full command lists)
  when the prefix is insufficient or doesn't match.
- Skips completion if the typeahead buffer has pending input or if a
  wildmenu is already active.
- Does not print "..." before completion.

This is an improvement on the `feedkeys()` based autocompletion script
given in vim/vim#16759.

closes: vim/vim#17806

b486ed8266

While at it, also make Ctrl-Z trigger search completion.

Co-authored-by: Girish Palya <girishji@gmail.com>
This commit is contained in:
zeertzjq
2025-07-23 06:12:50 +08:00
committed by GitHub
parent 8b5d8dfc73
commit 9377db2545
15 changed files with 243 additions and 65 deletions

View File

@@ -1147,8 +1147,11 @@ static int command_line_wildchar_complete(CommandLineState *s)
res = OK; // don't insert 'wildchar' now
}
} else { // typed p_wc first time
if (s->c == p_wc || s->c == p_wcm) {
if (s->c == p_wc || s->c == p_wcm || s->c == K_WILD || s->c == Ctrl_Z) {
options |= WILD_MAY_EXPAND_PATTERN;
if (s->c == K_WILD) {
options |= WILD_FUNC_TRIGGER;
}
s->xpc.xp_pre_incsearch_pos = s->is_state.search_start;
}
s->wim_index = 0;
@@ -1442,11 +1445,22 @@ static int command_line_execute(VimState *state, int key)
}
}
// Completion for 'wildchar' or 'wildcharm' key.
if ((s->c == p_wc && !s->gotesc && KeyTyped) || s->c == p_wcm || s->c == Ctrl_Z) {
if (command_line_wildchar_complete(s) == CMDLINE_CHANGED) {
// Completion for 'wildchar', 'wildcharm', and wildtrigger()
if ((s->c == p_wc && !s->gotesc && KeyTyped) || s->c == p_wcm || s->c == K_WILD
|| s->c == Ctrl_Z) {
if (s->c == K_WILD) {
emsg_silent++; // Silence the bell
}
int res = command_line_wildchar_complete(s);
if (s->c == K_WILD) {
emsg_silent--;
}
if (res == CMDLINE_CHANGED) {
return command_line_changed(s);
}
if (s->c == K_WILD) {
return command_line_not_changed(s);
}
}
s->gotesc = false;
@@ -4907,3 +4921,27 @@ void get_user_input(const typval_T *const argvars, typval_T *const rettv, const
need_wait_return = false;
msg_didout = false;
}
/// "wildtrigger()" function
void f_wildtrigger(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
if (!(State & MODE_CMDLINE) || char_avail()
|| (wild_menu_showing != 0 && wild_menu_showing != WM_LIST)
|| cmdline_pum_active()) {
return;
}
int cmd_type = get_cmdline_type();
if (cmd_type == ':' || cmd_type == '/' || cmd_type == '?') {
// Add K_WILD as a single special key
uint8_t key_string[4];
key_string[0] = K_SPECIAL;
key_string[1] = KS_EXTRA;
key_string[2] = KE_WILD;
key_string[3] = NUL;
// Insert it into the typeahead buffer
ins_typebuf((char *)key_string, REMAP_NONE, 0, true, false);
}
}