Skip to content

Commit de4452c

Browse files
zeertzjqgirishji
andcommitted
vim-patch:9.1.1576: cannot easily trigger wildcard expansion
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 vim/vim@b486ed8 While at it, also make Ctrl-Z trigger search completion. Co-authored-by: Girish Palya <girishji@gmail.com>
1 parent ea2d226 commit de4452c

File tree

15 files changed

+240
-62
lines changed

15 files changed

+240
-62
lines changed

runtime/doc/cmdline.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ When repeating 'wildchar' or CTRL-N you cycle through the matches, eventually
451451
ending up back to what was typed. If the first match is not what you wanted,
452452
you can use <S-Tab> or CTRL-P to go straight back to what you typed.
453453

454+
See also |wildtrigger()|.
455+
454456
The 'wildmenu' option can be set to show the matches just above the command
455457
line.
456458

runtime/doc/news.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ VIMSCRIPT
294294
|cmdcomplete_info()| gets current cmdline completion info.
295295
|getcompletiontype()| gets command-line completion type for any string.
296296
|prompt_getinput()| gets current user-input in prompt-buffer.
297+
|wildtrigger()| triggers command-line expansion.
297298

298299
==============================================================================
299300
CHANGED FEATURES *news-changed*

runtime/doc/options.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7212,7 +7212,7 @@ A jump table for the options with a short description can be found at |Q_op|.
72127212
< 'wildchar' also enables completion in search pattern contexts such as
72137213
|/|, |?|, |:s|, |:g|, |:v|, and |:vim|. To insert a literal <Tab>
72147214
instead of triggering completion, type <C-V><Tab> or "\t".
7215-
See also |'wildoptions'|.
7215+
See also 'wildoptions' and |wildtrigger()|.
72167216

72177217
*'wildcharm'* *'wcm'*
72187218
'wildcharm' 'wcm' number (default 0)

runtime/doc/usr_41.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,7 @@ Mappings and Menus: *mapping-functions*
10361036
mapset() restore a mapping
10371037
menu_info() get information about a menu item
10381038
wildmenumode() check if the wildmode is active
1039+
wildtrigger() start wildcard expansion
10391040

10401041
Signs: *sign-functions*
10411042
sign_define() define or update a sign

runtime/doc/vimfn.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11878,6 +11878,33 @@ wildmenumode() *wildmenumode()*
1187811878
Return: ~
1187911879
(`any`)
1188011880

11881+
wildtrigger() *wildtrigger()*
11882+
Start wildcard expansion in the command-line, using the
11883+
behavior defined by the 'wildmode' and 'wildoptions' settings.
11884+
See |cmdline-completion|.
11885+
11886+
This function also enables completion in search patterns such
11887+
as |/|, |?|, |:s|, |:g|, |:v| and |:vimgrep|.
11888+
11889+
Unlike pressing 'wildchar' manually, this function does not
11890+
produce a beep when no matches are found and generally
11891+
operates more quietly. This makes it suitable for triggering
11892+
completion automatically, such as from an |:autocmd|.
11893+
*cmdline-autocompletion*
11894+
Example: To make the completion menu pop up automatically as
11895+
you type on the command line, use: >vim
11896+
autocmd CmdlineChanged [:/?] call wildtrigger()
11897+
set wildmode=noselect:lastused,full wildoptions=pum
11898+
<
11899+
To retain normal history navigation (up/down keys): >vim
11900+
cnoremap <Up> <C-U><Up>
11901+
cnoremap <Down> <C-U><Down>
11902+
<
11903+
Return value is always 0.
11904+
11905+
Return: ~
11906+
(`number`)
11907+
1188111908
win_execute({id}, {command} [, {silent}]) *win_execute()*
1188211909
Like `execute()` but in the context of window {id}.
1188311910
The window will temporarily be made the current window,

runtime/lua/vim/_meta/options.lua

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

runtime/lua/vim/_meta/vimfn.lua

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

runtime/syntax/vim.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ syn match vimUserAutoEvent contained "\<\h\w*\>" skipwhite nextgroup=vimUserAuto
9999

100100
" Highlight commonly used Groupnames {{{2
101101
" GEN_SYN_VIM: vimGroup, START_STR='syn keyword vimGroup contained', END_STR=''
102-
syn keyword vimGroup contained Added Boolean Changed Character Comment Conditional Constant Debug Define Delimiter Error Exception Float Function Identifier Ignore Include Keyword Label Macro Number Operator PreCondit PreProc Removed Repeat Special SpecialChar SpecialComment Statement StorageClass String Structure Tag Todo Type Typedef Underlined
102+
syn keyword vimGroup contained Added Bold BoldItalic Boolean Changed Character Comment Conditional Constant Debug Define Delimiter Error Exception Float Function Identifier Ignore Include Italic Keyword Label Macro Number Operator PreCondit PreProc Removed Repeat Special SpecialChar SpecialComment Statement StorageClass String Structure Tag Todo Type Typedef Underlined
103103

104104
" Default highlighting groups {{{2
105105
" GEN_SYN_VIM: vimHLGroup, START_STR='syn keyword vimHLGroup contained', END_STR=''

src/nvim/cmdexpand.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ int nextwild(expand_T *xp, int type, int options, bool escape)
254254
{
255255
CmdlineInfo *const ccline = get_cmdline_info();
256256
char *p;
257+
bool from_wildtrigger_func = options & WILD_FUNC_TRIGGER;
257258

258259
if (xp->xp_numfiles == -1) {
259260
pre_incsearch_pos = xp->xp_pre_incsearch_pos;
@@ -280,17 +281,24 @@ int nextwild(expand_T *xp, int type, int options, bool escape)
280281
return FAIL;
281282
}
282283

284+
int i = (int)(xp->xp_pattern - ccline->cmdbuff);
285+
assert(ccline->cmdpos >= i);
286+
xp->xp_pattern_len = (size_t)ccline->cmdpos - (size_t)i;
287+
288+
// Skip showing matches if prefix is invalid during wildtrigger()
289+
if (from_wildtrigger_func && xp->xp_context == EXPAND_COMMANDS
290+
&& xp->xp_pattern_len == 0) {
291+
return FAIL;
292+
}
293+
283294
// If cmd_silent is set then don't show the dots, because redrawcmd() below
284295
// won't remove them.
285-
if (!cmd_silent && !(ui_has(kUICmdline) || ui_has(kUIWildmenu))) {
296+
if (!cmd_silent && !from_wildtrigger_func
297+
&& !(ui_has(kUICmdline) || ui_has(kUIWildmenu))) {
286298
msg_puts("..."); // show that we are busy
287299
ui_flush();
288300
}
289301

290-
int i = (int)(xp->xp_pattern - ccline->cmdbuff);
291-
assert(ccline->cmdpos >= i);
292-
xp->xp_pattern_len = (size_t)ccline->cmdpos - (size_t)i;
293-
294302
if (type == WILD_NEXT || type == WILD_PREV
295303
|| type == WILD_PAGEUP || type == WILD_PAGEDOWN
296304
|| type == WILD_PUM_WANT) {

src/nvim/cmdexpand.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ enum {
4242
BUF_DIFF_FILTER = 0x2000,
4343
WILD_KEEP_SOLE_ITEM = 0x4000,
4444
WILD_MAY_EXPAND_PATTERN = 0x8000,
45+
WILD_FUNC_TRIGGER = 0x10000, ///< called from wildtrigger()
4546
};
4647

4748
#ifdef INCLUDE_GENERATED_DECLARATIONS

0 commit comments

Comments
 (0)