Skip to content

Commit

Permalink
patch 9.0.1958: cannot complete option values
Browse files Browse the repository at this point in the history
Problem:  cannot complete option values
Solution: Add completion functions for several options

Add cmdline tab-completion for setting string options

Add tab-completion for setting string options on the cmdline using
`:set=` (along with `:set+=` and `:set-=`).

The existing tab completion for setting options currently only works
when nothing is typed yet, and it only fills in with the existing value,
e.g. when the user does `:set diffopt=<Tab>` it will be completed to
`set diffopt=internal,filler,closeoff` and nothing else. This isn't too
useful as a user usually wants auto-complete to suggest all the possible
values, such as 'iblank', or 'algorithm:patience'.

For set= and set+=, this adds a new optional callback function for each
option that can be invoked when doing completion. This allows for each
option to have control over how completion works. For example, in
'diffopt', it will suggest the default enumeration, but if `algorithm:`
is selected, it will further suggest different algorithm types like
'meyers' and 'patience'. When using set=, the existing option value will
be filled in as the first choice to preserve the existing behavior. When
using set+= this won't happen as it doesn't make sense.

For flag list options (e.g. 'mouse' and 'guioptions'), completion will
take into account existing typed values (and in the case of set+=, the
existing option value) to make sure it doesn't suggest duplicates.

For set-=, there is a new `ExpandSettingSubtract` function which will
handle flag list and comma-separated options smartly, by only suggesting
values that currently exist in the option.

Note that Vim has some existing code that adds special handling for
'filetype', 'syntax', and misc dir options like 'backupdir'. This change
preserves them as they already work, instead of converting to the new
callback API for each option.

closes: #13182

Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yee Cheng Chin <ychin.git@gmail.com>
  • Loading branch information
ychin authored and chrisbra committed Sep 29, 2023
1 parent 3695d0e commit 900894b
Show file tree
Hide file tree
Showing 29 changed files with 2,590 additions and 731 deletions.
20 changes: 15 additions & 5 deletions runtime/doc/cmdline.txt
Expand Up @@ -517,16 +517,26 @@ example, to match only files that end in ".c": >
:e *.c$
This will not match a file ending in ".cpp". Without the "$" it does match.

The old value of an option can be obtained by hitting 'wildchar' just after
the '='. For example, typing 'wildchar' after ":set dir=" will insert the
current value of 'dir'. This overrules file name completion for the options
that take a file name.

If you would like using <S-Tab> for CTRL-P in an xterm, put this command in
your .cshrc: >
xmodmap -e "keysym Tab = Tab Find"
And this in your .vimrc: >
:cmap <Esc>[1~ <C-P>
< *complete-set-option*
When setting an option using |:set=|, the old value of an option can be
obtained by hitting 'wildchar' just after the '='. For example, typing
'wildchar' after ":set dir=" will insert the current value of 'dir'. This
overrules file name completion for the options that take a file name.

When using |:set=|, |:set+=|, or |:set^=|, string options that have
pre-defined names or syntax (e.g. 'diffopt', 'listchars') or are a list of
single-character flags (e.g. 'shortmess') will also present a list of possible
values for completion when using 'wildchar'.

When using |:set-=|, comma-separated options like 'diffopt' or 'backupdir'
will show each item separately. Flag list options like 'shortmess' will show
both the entire old value and the individual flags. Otherwise completion will
just fill in with the entire old value.

==============================================================================
3. Ex command-lines *cmdline-lines*
Expand Down
9 changes: 7 additions & 2 deletions runtime/doc/options.txt
Expand Up @@ -71,15 +71,17 @@ achieve special effects. These options come in three forms:
'ttytype'
Warning: This may have a lot of side effects.

*:set-args* *E487* *E521*
*:set-args* *:set=* *E487* *E521*
:se[t] {option}={value} or
:se[t] {option}:{value}
Set string or number option to {value}.
For numeric options the value can be given in decimal,
hex (preceded with 0x) or octal (preceded with '0').
The old value can be inserted by typing 'wildchar' (by
default this is a <Tab> or CTRL-E if 'compatible' is
set). See |cmdline-completion|.
set). Many string options with fixed syntax and names
also support completing known values. See
|cmdline-completion| and |complete-set-option|.
White space between {option} and '=' is allowed and
will be ignored. White space between '=' and {value}
is not allowed.
Expand Down Expand Up @@ -113,6 +115,9 @@ achieve special effects. These options come in three forms:
When the option is a list of flags, {value} must be
exactly as they appear in the option. Remove flags
one by one to avoid problems.
The individual values from a comma separated list or
list of flags can be inserted by typing 'wildchar'.
See |complete-set-option|.
Also see |:set-args| above.

The {option} arguments to ":set" may be repeated. For example: >
Expand Down
2 changes: 2 additions & 0 deletions runtime/doc/tags
Expand Up @@ -3208,6 +3208,7 @@ $quote eval.txt /*$quote*
:set-inv options.txt /*:set-inv*
:set-termcap options.txt /*:set-termcap*
:set-verbose options.txt /*:set-verbose*
:set= options.txt /*:set=*
:set^= options.txt /*:set^=*
:set_env options.txt /*:set_env*
:setf options.txt /*:setf*
Expand Down Expand Up @@ -6484,6 +6485,7 @@ complete-items insert.txt /*complete-items*
complete-popup insert.txt /*complete-popup*
complete-popuphidden insert.txt /*complete-popuphidden*
complete-script-local-functions cmdline.txt /*complete-script-local-functions*
complete-set-option cmdline.txt /*complete-set-option*
complete_CTRL-E insert.txt /*complete_CTRL-E*
complete_CTRL-Y insert.txt /*complete_CTRL-Y*
complete_add() builtin.txt /*complete_add()*
Expand Down
10 changes: 10 additions & 0 deletions src/autocmd.c
Expand Up @@ -2737,6 +2737,16 @@ get_event_name(expand_T *xp UNUSED, int idx)
return (char_u *)event_names[idx - augroups.ga_len].name;
}

/*
* Function given to ExpandGeneric() to obtain the list of event names. Don't
* include groups.
*/
char_u *
get_event_name_no_group(expand_T *xp UNUSED, int idx)
{
return (char_u *)event_names[idx].name;
}


#if defined(FEAT_EVAL) || defined(PROTO)
/*
Expand Down
1 change: 1 addition & 0 deletions src/clipboard.c
Expand Up @@ -1266,6 +1266,7 @@ did_set_clipboard(optset_T *args UNUSED)

for (p = p_cb; *p != NUL; )
{
// Note: Keep this in sync with p_cb_values.
if (STRNCMP(p, "unnamed", 7) == 0 && (p[7] == ',' || p[7] == NUL))
{
new_unnamed |= CLIP_UNNAMED;
Expand Down
12 changes: 8 additions & 4 deletions src/cmdexpand.c
Expand Up @@ -15,9 +15,6 @@

static int cmd_showtail; // Only show path tail in lists ?

static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch,
char_u ***matches, int *numMatches,
char_u *((*func)(expand_T *, int)), int escaped);
static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
static char_u *showmatches_gettail(char_u *s);
static int expand_showtail(expand_T *xp);
Expand Down Expand Up @@ -54,6 +51,8 @@ cmdline_fuzzy_completion_supported(expand_T *xp)
&& xp->xp_context != EXPAND_FILETYPE
&& xp->xp_context != EXPAND_HELP
&& xp->xp_context != EXPAND_OLD_SETTING
&& xp->xp_context != EXPAND_STRING_SETTING
&& xp->xp_context != EXPAND_SETTING_SUBTRACT
&& xp->xp_context != EXPAND_OWNSYNTAX
&& xp->xp_context != EXPAND_PACKADD
&& xp->xp_context != EXPAND_RUNTIME
Expand Down Expand Up @@ -3093,6 +3092,10 @@ ExpandFromContext(
if (xp->xp_context == EXPAND_SETTINGS
|| xp->xp_context == EXPAND_BOOL_SETTINGS)
ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
else if (xp->xp_context == EXPAND_STRING_SETTING)
ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
else if (xp->xp_context == EXPAND_MAPPINGS)
ret = ExpandMappings(pat, &regmatch, numMatches, matches);
#if defined(FEAT_EVAL)
Expand Down Expand Up @@ -3121,7 +3124,7 @@ ExpandFromContext(
*
* Returns OK when no problems encountered, FAIL for error (out of memory).
*/
static int
int
ExpandGeneric(
char_u *pat,
expand_T *xp,
Expand Down Expand Up @@ -3226,6 +3229,7 @@ ExpandGeneric(
// applies to the completion context. Menus and scriptnames should be kept
// in the specified order.
if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
&& xp->xp_context != EXPAND_STRING_SETTING
&& xp->xp_context != EXPAND_MENUS
&& xp->xp_context != EXPAND_SCRIPTNAMES)
sort_matches = TRUE;
Expand Down
2 changes: 2 additions & 0 deletions src/diff.c
Expand Up @@ -2266,6 +2266,7 @@ diffopt_changed(void)
p = p_dip;
while (*p != NUL)
{
// Note: Keep this in sync with p_dip_values
if (STRNCMP(p, "filler", 6) == 0)
{
p += 6;
Expand Down Expand Up @@ -2343,6 +2344,7 @@ diffopt_changed(void)
}
else if (STRNCMP(p, "algorithm:", 10) == 0)
{
// Note: Keep this in sync with p_dip_algorithm_values.
p += 10;
if (STRNCMP(p, "myers", 5) == 0)
{
Expand Down
1 change: 1 addition & 0 deletions src/ex_getln.c
Expand Up @@ -2650,6 +2650,7 @@ check_opt_wim(void)

for (p = p_wim; *p; ++p)
{
// Note: Keep this in sync with p_wim_values.
for (i = 0; ASCII_ISALPHA(p[i]); ++i)
;
if (p[i] != NUL && p[i] != ',' && p[i] != ':')
Expand Down
1 change: 1 addition & 0 deletions src/highlight.c
Expand Up @@ -3814,6 +3814,7 @@ highlight_changed(void)
if (attr > HL_ALL) // Combination with ':' is not allowed.
return FAIL;

// Note: Keep this in sync with expand_set_highlight().
switch (*p)
{
case 'b': attr |= HL_BOLD;
Expand Down
1 change: 1 addition & 0 deletions src/indent.c
Expand Up @@ -871,6 +871,7 @@ briopt_check(win_T *wp)
p = wp->w_p_briopt;
while (*p != NUL)
{
// Note: Keep this in sync with p_briopt_values
if (STRNCMP(p, "shift:", 6) == 0
&& ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6])))
{
Expand Down
13 changes: 13 additions & 0 deletions src/mbyte.c
Expand Up @@ -5782,3 +5782,16 @@ f_charclass(typval_T *argvars, typval_T *rettv UNUSED)
rettv->vval.v_number = mb_get_class(argvars[0].vval.v_string);
}
#endif

/*
* Function given to ExpandGeneric() to obtain the possible arguments of the
* encoding options.
*/
char_u *
get_encoding_name(expand_T *xp UNUSED, int idx)
{
if (idx >= (int)(sizeof(enc_canon_table) / sizeof(enc_canon_table[0])))
return NULL;

return (char_u*)enc_canon_table[idx].name;
}

0 comments on commit 900894b

Please sign in to comment.