Skip to content

Commit 900894b

Browse files
ychinchrisbra
authored andcommitted
patch 9.0.1958: cannot complete option values
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>
1 parent 3695d0e commit 900894b

29 files changed

+2590
-731
lines changed

runtime/doc/cmdline.txt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -517,16 +517,26 @@ example, to match only files that end in ".c": >
517517
:e *.c$
518518
This will not match a file ending in ".cpp". Without the "$" it does match.
519519

520-
The old value of an option can be obtained by hitting 'wildchar' just after
521-
the '='. For example, typing 'wildchar' after ":set dir=" will insert the
522-
current value of 'dir'. This overrules file name completion for the options
523-
that take a file name.
524-
525520
If you would like using <S-Tab> for CTRL-P in an xterm, put this command in
526521
your .cshrc: >
527522
xmodmap -e "keysym Tab = Tab Find"
528523
And this in your .vimrc: >
529524
:cmap <Esc>[1~ <C-P>
525+
< *complete-set-option*
526+
When setting an option using |:set=|, the old value of an option can be
527+
obtained by hitting 'wildchar' just after the '='. For example, typing
528+
'wildchar' after ":set dir=" will insert the current value of 'dir'. This
529+
overrules file name completion for the options that take a file name.
530+
531+
When using |:set=|, |:set+=|, or |:set^=|, string options that have
532+
pre-defined names or syntax (e.g. 'diffopt', 'listchars') or are a list of
533+
single-character flags (e.g. 'shortmess') will also present a list of possible
534+
values for completion when using 'wildchar'.
535+
536+
When using |:set-=|, comma-separated options like 'diffopt' or 'backupdir'
537+
will show each item separately. Flag list options like 'shortmess' will show
538+
both the entire old value and the individual flags. Otherwise completion will
539+
just fill in with the entire old value.
530540

531541
==============================================================================
532542
3. Ex command-lines *cmdline-lines*

runtime/doc/options.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,17 @@ achieve special effects. These options come in three forms:
7171
'ttytype'
7272
Warning: This may have a lot of side effects.
7373

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

118123
The {option} arguments to ":set" may be repeated. For example: >

runtime/doc/tags

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3208,6 +3208,7 @@ $quote eval.txt /*$quote*
32083208
:set-inv options.txt /*:set-inv*
32093209
:set-termcap options.txt /*:set-termcap*
32103210
:set-verbose options.txt /*:set-verbose*
3211+
:set= options.txt /*:set=*
32113212
:set^= options.txt /*:set^=*
32123213
:set_env options.txt /*:set_env*
32133214
:setf options.txt /*:setf*
@@ -6484,6 +6485,7 @@ complete-items insert.txt /*complete-items*
64846485
complete-popup insert.txt /*complete-popup*
64856486
complete-popuphidden insert.txt /*complete-popuphidden*
64866487
complete-script-local-functions cmdline.txt /*complete-script-local-functions*
6488+
complete-set-option cmdline.txt /*complete-set-option*
64876489
complete_CTRL-E insert.txt /*complete_CTRL-E*
64886490
complete_CTRL-Y insert.txt /*complete_CTRL-Y*
64896491
complete_add() builtin.txt /*complete_add()*

src/autocmd.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,6 +2737,16 @@ get_event_name(expand_T *xp UNUSED, int idx)
27372737
return (char_u *)event_names[idx - augroups.ga_len].name;
27382738
}
27392739

2740+
/*
2741+
* Function given to ExpandGeneric() to obtain the list of event names. Don't
2742+
* include groups.
2743+
*/
2744+
char_u *
2745+
get_event_name_no_group(expand_T *xp UNUSED, int idx)
2746+
{
2747+
return (char_u *)event_names[idx].name;
2748+
}
2749+
27402750

27412751
#if defined(FEAT_EVAL) || defined(PROTO)
27422752
/*

src/clipboard.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,7 @@ did_set_clipboard(optset_T *args UNUSED)
12661266

12671267
for (p = p_cb; *p != NUL; )
12681268
{
1269+
// Note: Keep this in sync with p_cb_values.
12691270
if (STRNCMP(p, "unnamed", 7) == 0 && (p[7] == ',' || p[7] == NUL))
12701271
{
12711272
new_unnamed |= CLIP_UNNAMED;

src/cmdexpand.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515

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

18-
static int ExpandGeneric(char_u *pat, expand_T *xp, regmatch_T *regmatch,
19-
char_u ***matches, int *numMatches,
20-
char_u *((*func)(expand_T *, int)), int escaped);
2118
static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
2219
static char_u *showmatches_gettail(char_u *s);
2320
static int expand_showtail(expand_T *xp);
@@ -54,6 +51,8 @@ cmdline_fuzzy_completion_supported(expand_T *xp)
5451
&& xp->xp_context != EXPAND_FILETYPE
5552
&& xp->xp_context != EXPAND_HELP
5653
&& xp->xp_context != EXPAND_OLD_SETTING
54+
&& xp->xp_context != EXPAND_STRING_SETTING
55+
&& xp->xp_context != EXPAND_SETTING_SUBTRACT
5756
&& xp->xp_context != EXPAND_OWNSYNTAX
5857
&& xp->xp_context != EXPAND_PACKADD
5958
&& xp->xp_context != EXPAND_RUNTIME
@@ -3093,6 +3092,10 @@ ExpandFromContext(
30933092
if (xp->xp_context == EXPAND_SETTINGS
30943093
|| xp->xp_context == EXPAND_BOOL_SETTINGS)
30953094
ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
3095+
else if (xp->xp_context == EXPAND_STRING_SETTING)
3096+
ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3097+
else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3098+
ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
30963099
else if (xp->xp_context == EXPAND_MAPPINGS)
30973100
ret = ExpandMappings(pat, &regmatch, numMatches, matches);
30983101
#if defined(FEAT_EVAL)
@@ -3121,7 +3124,7 @@ ExpandFromContext(
31213124
*
31223125
* Returns OK when no problems encountered, FAIL for error (out of memory).
31233126
*/
3124-
static int
3127+
int
31253128
ExpandGeneric(
31263129
char_u *pat,
31273130
expand_T *xp,
@@ -3226,6 +3229,7 @@ ExpandGeneric(
32263229
// applies to the completion context. Menus and scriptnames should be kept
32273230
// in the specified order.
32283231
if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
3232+
&& xp->xp_context != EXPAND_STRING_SETTING
32293233
&& xp->xp_context != EXPAND_MENUS
32303234
&& xp->xp_context != EXPAND_SCRIPTNAMES)
32313235
sort_matches = TRUE;

src/diff.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,6 +2266,7 @@ diffopt_changed(void)
22662266
p = p_dip;
22672267
while (*p != NUL)
22682268
{
2269+
// Note: Keep this in sync with p_dip_values
22692270
if (STRNCMP(p, "filler", 6) == 0)
22702271
{
22712272
p += 6;
@@ -2343,6 +2344,7 @@ diffopt_changed(void)
23432344
}
23442345
else if (STRNCMP(p, "algorithm:", 10) == 0)
23452346
{
2347+
// Note: Keep this in sync with p_dip_algorithm_values.
23462348
p += 10;
23472349
if (STRNCMP(p, "myers", 5) == 0)
23482350
{

src/ex_getln.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,6 +2650,7 @@ check_opt_wim(void)
26502650

26512651
for (p = p_wim; *p; ++p)
26522652
{
2653+
// Note: Keep this in sync with p_wim_values.
26532654
for (i = 0; ASCII_ISALPHA(p[i]); ++i)
26542655
;
26552656
if (p[i] != NUL && p[i] != ',' && p[i] != ':')

src/highlight.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3814,6 +3814,7 @@ highlight_changed(void)
38143814
if (attr > HL_ALL) // Combination with ':' is not allowed.
38153815
return FAIL;
38163816

3817+
// Note: Keep this in sync with expand_set_highlight().
38173818
switch (*p)
38183819
{
38193820
case 'b': attr |= HL_BOLD;

src/indent.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@ briopt_check(win_T *wp)
871871
p = wp->w_p_briopt;
872872
while (*p != NUL)
873873
{
874+
// Note: Keep this in sync with p_briopt_values
874875
if (STRNCMP(p, "shift:", 6) == 0
875876
&& ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6])))
876877
{

src/mbyte.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5782,3 +5782,16 @@ f_charclass(typval_T *argvars, typval_T *rettv UNUSED)
57825782
rettv->vval.v_number = mb_get_class(argvars[0].vval.v_string);
57835783
}
57845784
#endif
5785+
5786+
/*
5787+
* Function given to ExpandGeneric() to obtain the possible arguments of the
5788+
* encoding options.
5789+
*/
5790+
char_u *
5791+
get_encoding_name(expand_T *xp UNUSED, int idx)
5792+
{
5793+
if (idx >= (int)(sizeof(enc_canon_table) / sizeof(enc_canon_table[0])))
5794+
return NULL;
5795+
5796+
return (char_u*)enc_canon_table[idx].name;
5797+
}

0 commit comments

Comments
 (0)