patch 9.1.1166: command-line auto-completion hard with wildmenu
Problem: command-line auto-completion hard with wildmenu
Solution: implement "noselect" wildoption value (Girish Palya)
When `noselect` is present in `wildmode` and 'wildmenu' is enabled, the
completion menu appears without pre-selecting the first item.
This change makes it easier to implement command-line auto-completion,
where the menu dynamically appears as characters are typed, and `<Tab>`
can be used to manually select an item. This can be achieved by
leveraging the `CmdlineChanged` event to insert `wildchar(m)`,
triggering completion menu.
Without this change, auto-completion using the 'wildmenu' mechanism is
not feasible, as it automatically inserts the first match, preventing
dynamic selection.
The following Vimscript snippet demonstrates how to configure
auto-completion using `noselect`:
```vim
vim9script
set wim=noselect:lastused,full wop=pum wcm=<C-@> wmnu
autocmd CmdlineChanged : timer_start(0, function(CmdComplete, [getcmdline()]))
def CmdComplete(cur_cmdline: string, timer: number)
var [cmdline, curpos] = [getcmdline(), getcmdpos()]
if cur_cmdline ==# cmdline # Avoid completing each character in keymaps and pasted text
&& !pumvisible() && curpos == cmdline->len() + 1
if cmdline[curpos - 2] =~ '[\w*/:]' # Reduce noise by completing only selected characters
feedkeys("\<C-@>", "ti")
set eventignore+=CmdlineChanged # Suppress redundant completion attempts
timer_start(0, (_) => {
getcmdline()->substitute('\%x00$', '', '')->setcmdline() # Remove <C-@> if no completion items exist
set eventignore-=CmdlineChanged
})
endif
endif
enddef
```
fixes: #16551
closes: #16759
Signed-off-by: Girish Palya <girishji@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/ex_getln.c b/src/ex_getln.c
index 93d612a..ff1b3eb 100644
--- a/src/ex_getln.c
+++ b/src/ex_getln.c
@@ -913,6 +913,8 @@
if (wim_flags[wim_index] & WIM_BUFLASTUSED)
options |= WILD_BUFLASTUSED;
+ if (wim_flags[0] & WIM_NOSELECT)
+ options |= WILD_KEEP_SOLE_ITEM;
if (xp->xp_numfiles > 0) // typed p_wc at least twice
{
// if 'wildmode' contains "list" may still need to list
@@ -958,14 +960,15 @@
// when more than one match, and 'wildmode' first contains
// "list", or no change and 'wildmode' contains "longest,list",
// list all matches
- if (res == OK && xp->xp_numfiles > 1)
+ if (res == OK
+ && xp->xp_numfiles > ((wim_flags[wim_index] & WIM_NOSELECT) ? 0 : 1))
{
// a "longest" that didn't do anything is skipped (but not
// "list:longest")
if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
wim_index = 1;
if ((wim_flags[wim_index] & WIM_LIST)
- || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0))
+ || (p_wmnu && (wim_flags[wim_index] & (WIM_FULL | WIM_NOSELECT))))
{
if (!(wim_flags[0] & WIM_LONGEST))
{
@@ -974,7 +977,7 @@
p_wmnu = 0;
// remove match
- nextwild(xp, WILD_PREV, 0, escape);
+ nextwild(xp, WILD_PREV, 0 | (options & ~WIM_NOSELECT), escape);
p_wmnu = p_wmnu_save;
}
(void)showmatches(xp, p_wmnu
@@ -983,7 +986,8 @@
*did_wild_list = TRUE;
if (wim_flags[wim_index] & WIM_LONGEST)
nextwild(xp, WILD_LONGEST, options, escape);
- else if (wim_flags[wim_index] & WIM_FULL)
+ else if ((wim_flags[wim_index] & WIM_FULL)
+ && !(wim_flags[wim_index] & WIM_NOSELECT))
nextwild(xp, WILD_NEXT, options, escape);
}
else
@@ -2716,6 +2720,8 @@
new_wim_flags[idx] |= WIM_LIST;
else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
new_wim_flags[idx] |= WIM_BUFLASTUSED;
+ else if (i == 8 && STRNCMP(p, "noselect", 8) == 0)
+ new_wim_flags[idx] |= WIM_NOSELECT;
else
return FAIL;
p += i;