]> git.ipfire.org Git - thirdparty/vim.git/commit
patch 9.1.1166: command-line auto-completion hard with wildmenu v9.1.1166
authorGirish Palya <girishji@gmail.com>
Sun, 2 Mar 2025 21:55:57 +0000 (22:55 +0100)
committerChristian Brabandt <cb@256bit.org>
Sun, 2 Mar 2025 22:02:42 +0000 (23:02 +0100)
commit2bacc3e5fb3569e0fd98e129cb1e422ca18b80a6
tree9fbccf80d4f977be4cf2739a1e1b744e2470ad19
parenta250738303f85132335d69fd1936501b189eebce
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>
runtime/doc/options.txt
runtime/doc/version9.txt
src/cmdexpand.c
src/ex_getln.c
src/option.h
src/optionstr.c
src/testdir/gen_opt_test.vim
src/testdir/test_cmdline.vim
src/version.c
src/vim.h