Skip to content

Commit 2bacc3e

Browse files
girishjichrisbra
authored andcommitted
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 <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent a250738 commit 2bacc3e

10 files changed

Lines changed: 86 additions & 12 deletions

File tree

runtime/doc/options.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*options.txt* For Vim version 9.1. Last change: 2025 Mar 01
1+
*options.txt* For Vim version 9.1. Last change: 2025 Mar 02
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -9566,7 +9566,10 @@ A jump table for the options with a short description can be found at |Q_op|.
95669566
"lastused" When completing buffer names and more than one buffer
95679567
matches, sort buffers by time last used (other than
95689568
the current buffer).
9569-
When there is only a single match, it is fully completed in all cases.
9569+
"noselect" Do not pre-select first menu item and start 'wildmenu'
9570+
if it is enabled.
9571+
When there is only a single match, it is fully completed in all cases
9572+
except when "noselect" is present.
95709573

95719574
Examples of useful colon-separated values:
95729575
"longest:full" Like "longest", but also start 'wildmenu' if it is
@@ -9589,7 +9592,11 @@ A jump table for the options with a short description can be found at |Q_op|.
95899592
:set wildmode=list,full
95909593
< List all matches without completing, then each full match >
95919594
:set wildmode=longest,list
9592-
< Complete longest common string, then list alternatives.
9595+
< Complete longest common string, then list alternatives >
9596+
:set wildmode=noselect:full
9597+
< Display 'wildmenu' without completing, then each full match >
9598+
:set wildmode=noselect:lastused,full
9599+
< Same as above, but sort buffers by time last used.
95939600
More info here: |cmdline-completion|.
95949601

95959602
*'wildoptions'* *'wop'*

runtime/doc/version9.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*version9.txt* For Vim version 9.1. Last change: 2025 Feb 23
1+
*version9.txt* For Vim version 9.1. Last change: 2025 Mar 02
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -41605,6 +41605,8 @@ Completion: ~
4160541605
"preinsert" - highlight to be inserted values
4160641606
- handle multi-line completion as expected
4160741607
- improved commandline completion for the |:hi| command
41608+
- New option value for 'wildoptions':
41609+
"noselect" - do not auto select an entry in the wildmenu
4160841610

4160941611
Options: ~
4161041612
- the default for 'commentstring' contains whitespace padding to have

src/cmdexpand.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ nextwild(
286286
{
287287
int use_options = options |
288288
WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
289+
if (use_options & WILD_KEEP_SOLE_ITEM)
290+
use_options &= ~WILD_KEEP_SOLE_ITEM;
291+
289292
if (escape)
290293
use_options |= WILD_ESCAPE;
291294

@@ -340,7 +343,7 @@ nextwild(
340343

341344
if (xp->xp_numfiles <= 0 && p2 == NULL)
342345
beep_flush();
343-
else if (xp->xp_numfiles == 1)
346+
else if (xp->xp_numfiles == 1 && !(options & WILD_KEEP_SOLE_ITEM))
344347
// free expanded pattern
345348
(void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
346349

src/ex_getln.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,8 @@ cmdline_wildchar_complete(
913913

914914
if (wim_flags[wim_index] & WIM_BUFLASTUSED)
915915
options |= WILD_BUFLASTUSED;
916+
if (wim_flags[0] & WIM_NOSELECT)
917+
options |= WILD_KEEP_SOLE_ITEM;
916918
if (xp->xp_numfiles > 0) // typed p_wc at least twice
917919
{
918920
// if 'wildmode' contains "list" may still need to list
@@ -958,14 +960,15 @@ cmdline_wildchar_complete(
958960
// when more than one match, and 'wildmode' first contains
959961
// "list", or no change and 'wildmode' contains "longest,list",
960962
// list all matches
961-
if (res == OK && xp->xp_numfiles > 1)
963+
if (res == OK
964+
&& xp->xp_numfiles > ((wim_flags[wim_index] & WIM_NOSELECT) ? 0 : 1))
962965
{
963966
// a "longest" that didn't do anything is skipped (but not
964967
// "list:longest")
965968
if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
966969
wim_index = 1;
967970
if ((wim_flags[wim_index] & WIM_LIST)
968-
|| (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0))
971+
|| (p_wmnu && (wim_flags[wim_index] & (WIM_FULL | WIM_NOSELECT))))
969972
{
970973
if (!(wim_flags[0] & WIM_LONGEST))
971974
{
@@ -974,7 +977,7 @@ cmdline_wildchar_complete(
974977
p_wmnu = 0;
975978

976979
// remove match
977-
nextwild(xp, WILD_PREV, 0, escape);
980+
nextwild(xp, WILD_PREV, 0 | (options & ~WIM_NOSELECT), escape);
978981
p_wmnu = p_wmnu_save;
979982
}
980983
(void)showmatches(xp, p_wmnu
@@ -983,7 +986,8 @@ cmdline_wildchar_complete(
983986
*did_wild_list = TRUE;
984987
if (wim_flags[wim_index] & WIM_LONGEST)
985988
nextwild(xp, WILD_LONGEST, options, escape);
986-
else if (wim_flags[wim_index] & WIM_FULL)
989+
else if ((wim_flags[wim_index] & WIM_FULL)
990+
&& !(wim_flags[wim_index] & WIM_NOSELECT))
987991
nextwild(xp, WILD_NEXT, options, escape);
988992
}
989993
else
@@ -2716,6 +2720,8 @@ check_opt_wim(void)
27162720
new_wim_flags[idx] |= WIM_LIST;
27172721
else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
27182722
new_wim_flags[idx] |= WIM_BUFLASTUSED;
2723+
else if (i == 8 && STRNCMP(p, "noselect", 8) == 0)
2724+
new_wim_flags[idx] |= WIM_NOSELECT;
27192725
else
27202726
return FAIL;
27212727
p += i;

src/option.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ typedef enum {
369369
#define WIM_LONGEST 0x02
370370
#define WIM_LIST 0x04
371371
#define WIM_BUFLASTUSED 0x08
372+
#define WIM_NOSELECT 0x10
372373

373374
// flags for the 'wildoptions' option
374375
// each defined char should be unique over all values.

src/optionstr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm"
9494
#endif
9595
static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
9696
// Note: Keep this in sync with check_opt_wim()
97-
static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", NULL};
97+
static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", "noselect", NULL};
9898
static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
9999
#ifdef FEAT_WAK
100100
static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};

src/testdir/gen_opt_test.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ let test_values = {
321321
\ 'bs'],
322322
\ ['xxx']],
323323
\ 'wildmode': [['', 'full', 'longest', 'list', 'lastused', 'list:full',
324+
\ 'noselect', 'noselect,full', 'noselect:lastused,full',
324325
\ 'full,longest', 'full,full,full,full'],
325326
\ ['xxx', 'a4', 'full,full,full,full,full']],
326327
\ 'wildoptions': [['', 'tagfile', 'pum', 'fuzzy'], ['xxx']],

src/testdir/test_cmdline.vim

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,16 +2170,52 @@ func Wildmode_tests()
21702170
call assert_equal('AAA AAAA AAAAA', g:Sline)
21712171
call assert_equal('"b A', @:)
21722172

2173+
" When 'wildmenu' is not set, 'noselect' completes first item
2174+
set wildmode=noselect
2175+
call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
2176+
call assert_equal('"MyCmd oneA', @:)
2177+
2178+
" When 'noselect' is present, do not complete first <tab>.
2179+
set wildmenu
2180+
set wildmode=noselect
2181+
call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
2182+
call assert_equal('"MyCmd o', @:)
2183+
call feedkeys(":MyCmd o\t\t\<C-B>\"\<CR>", 'xt')
2184+
call assert_equal('"MyCmd o', @:)
2185+
call feedkeys(":MyCmd o\t\t\<C-Y>\<C-B>\"\<CR>", 'xt')
2186+
call assert_equal('"MyCmd o', @:)
2187+
2188+
" When 'full' is present, complete after first <tab>.
2189+
set wildmode=noselect,full
2190+
call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
2191+
call assert_equal('"MyCmd o', @:)
2192+
call feedkeys(":MyCmd o\t\t\<C-B>\"\<CR>", 'xt')
2193+
call assert_equal('"MyCmd oneA', @:)
2194+
call feedkeys(":MyCmd o\t\t\t\<C-B>\"\<CR>", 'xt')
2195+
call assert_equal('"MyCmd oneB', @:)
2196+
call feedkeys(":MyCmd o\t\t\t\<C-Y>\<C-B>\"\<CR>", 'xt')
2197+
call assert_equal('"MyCmd oneB', @:)
2198+
2199+
" 'noselect' has no effect when 'longest' is present.
2200+
set wildmode=noselect:longest
2201+
call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
2202+
call assert_equal('"MyCmd one', @:)
2203+
2204+
" Complete 'noselect' value in 'wildmode' option
2205+
set wildmode&
2206+
call feedkeys(":set wildmode=n\t\<C-B>\"\<CR>", 'xt')
2207+
call assert_equal('"set wildmode=noselect', @:)
2208+
call feedkeys(":set wildmode=\t\t\t\t\t\<C-B>\"\<CR>", 'xt')
2209+
call assert_equal('"set wildmode=noselect', @:)
2210+
21732211
" when using longest completion match, matches shorter than the argument
21742212
" should be ignored (happens with :help)
21752213
set wildmode=longest,full
2176-
set wildmenu
21772214
call feedkeys(":help a*\t\<C-B>\"\<CR>", 'xt')
21782215
call assert_equal('"help a', @:)
21792216
" non existing file
21802217
call feedkeys(":e a1b2y3z4\t\<C-B>\"\<CR>", 'xt')
21812218
call assert_equal('"e a1b2y3z4', @:)
2182-
set wildmenu&
21832219

21842220
" Test for longest file name completion with 'fileignorecase'
21852221
" On MS-Windows, file names are case insensitive.
@@ -2199,6 +2235,21 @@ func Wildmode_tests()
21992235
set fileignorecase&
22002236
endif
22012237

2238+
" If 'noselect' is present, single item menu should not insert item
2239+
func! T(a, c, p)
2240+
return "oneA"
2241+
endfunc
2242+
command! -nargs=1 -complete=custom,T MyCmd
2243+
set wildmode=noselect,full
2244+
call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
2245+
call assert_equal('"MyCmd o', @:)
2246+
call feedkeys(":MyCmd o\t\t\<C-B>\"\<CR>", 'xt')
2247+
call assert_equal('"MyCmd oneA', @:)
2248+
" 'nowildmenu' should make 'noselect' ineffective
2249+
set nowildmenu
2250+
call feedkeys(":MyCmd o\t\<C-B>\"\<CR>", 'xt')
2251+
call assert_equal('"MyCmd oneA', @:)
2252+
22022253
%argdelete
22032254
delcommand MyCmd
22042255
delfunc T

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
1166,
707709
/**/
708710
1165,
709711
/**/

src/vim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ extern int (*dyn_libintl_wputenv)(const wchar_t *envstring);
881881
#define WILD_NOERROR 0x800 // sets EW_NOERROR
882882
#define WILD_BUFLASTUSED 0x1000
883883
#define BUF_DIFF_FILTER 0x2000
884+
#define WILD_KEEP_SOLE_ITEM 0x4000
884885

885886
// Flags for expand_wildcards()
886887
#define EW_DIR 0x01 // include directory names

0 commit comments

Comments
 (0)