1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-09-11 13:01:28 +02:00

allow subscripts for compadd -[ak]; new style for history completion; better list-colors handling (12029)

This commit is contained in:
Sven Wischnowsky 2000-06-22 08:42:36 +00:00
parent 44b34667f8
commit b9a533f382
19 changed files with 209 additions and 93 deletions

View file

@ -1,3 +1,17 @@
2000-06-22 Sven Wischnowsky <wischnow@zsh.org>
* 12029: Completion/Builtins/_arrays, Completion/Builtins/_zstyle,
Completion/Commands/_bash_completions,
Completion/Commands/_history_complete_word,
Completion/Core/_history, Completion/Core/_main_complete,
Completion/Core/_parameters, Completion/Core/_setup,
Completion/Core/_tags, Completion/Debian/_apt,
Completion/X/_x_extension, Doc/Zsh/compsys.yo, Doc/Zsh/compwid.yo,
Src/zsh.h, Src/Zle/compcore.c, Src/Zle/compctl.c,
Src/Zle/complist.c, Src/Zle/computil.c: allow subscripts for
compadd -[ak]; new style for history completion; better
list-colors handling
2000-06-22 Clint Adams <schizo@debian.org>
* 12027: Completion/User/_mailboxes: fix splitting problem in

View file

@ -1,3 +1,5 @@
#defcomp shift
#compdef shift
complist -A
local expl
_wanted arrays expl array compadd -k "parameters[(R)*array*~*local*]"

View file

@ -136,7 +136,7 @@ while [[ -n $state ]]; do
ctop=cz
fi
_wanted styles expl style \
compadd -M 'r:|-=* r:|=*' - ${(k)styles[(R)[^:]#[$ctop][^:]#:*]}
compadd -M 'r:|-=* r:|=*' -k "styles[(R)[^:]#[$ctop][^:]#:*]"
;;
style-arg)

View file

@ -33,8 +33,8 @@ local key=$KEYS[-1] expl
case $key in
'!') _main_complete _command_names
;;
'$') _main_complete - _wanted parameters expl 'exported parameters' \
compadd - "${(@k)parameters[(R)*export*]}"
'$') _main_complete - parameters _wanted parameters expl 'exported parameters' \
compadd -k 'parameters[(R)*export*]'
;;
'@') _main_complete _hosts
;;

View file

@ -7,20 +7,26 @@
#
# Available styles:
#
# :history-words:list -- display lists of available matches
# :history-words:stop -- prevent looping at beginning and end of matches
# during menu-completion
# :history-words:sort -- sort matches lexically (default is to sort by age)
# :history-words:remove-all-dups --
# remove /all/ duplicate matches rather than just
# consecutives
#
# list -- display lists of available matches
# stop -- prevent looping at beginning and end of matches during
# menu-completion
# sort -- sort matches lexically (default is to sort by age)
# remove-all-dups --
# remove /all/ duplicate matches rather than just consecutives
# range -- range of history words to complete
_history_complete_word () {
setopt localoptions nullglob rcexpandparam extendedglob
unsetopt markdirs globsubst shwordsplit nounset ksharrays
local expl direction stop
local expl direction stop curcontext="$curcontext"
local max slice hmax=$#historywords
if [[ -z "$curcontext" ]]; then
curcontext=history-words:::
else
curcontext="history-words${curcontext#*:}"
fi
if [[ $WIDGET = *newer ]]; then
direction=newer
@ -32,6 +38,19 @@ _history_complete_word () {
zstyle -t ":completion:${curcontext}:history-words" list || compstate[list]=''
if zstyle -s ":completion:${curcontext}:history-words" range max; then
if [[ $max = *:* ]]; then
slice=${max#*:}
max=${max%:*}
else
slice=$max
fi
[[ max -gt hmax ]] && max=$hmax
else
max=$hmax
slice=$max
fi
if [[ -n "$compstate[old_list]" &&
( -n "$stop" || "$compstate[insert]" = menu ) ]] ; then
# array of matches is newest -> oldest (reverse of history order)
@ -66,11 +85,11 @@ _history_complete_word () {
_history_complete_word_gen_matches
fi
[[ -n "$compstate[nmatches]" ]]
(( $compstate[nmatches] ))
}
_history_complete_word_gen_matches () {
local opt h_words
local opt beg=2
[[ -n "$_hist_stop" ]] && PREFIX="$_hist_old_prefix"
@ -90,9 +109,11 @@ _history_complete_word_gen_matches () {
SUFFIX="$SUFFIX$ISUFFIX"
ISUFFIX=
h_words=( "${(@)historywords[2,-1]}" )
_wanted "$opt" history-words expl 'history word' \
compadd -Q -a h_words
while [[ $compstate[nmatches] -eq 0 && beg -lt max ]]; do
_main_complete - history _wanted "$opt" history-words expl 'history word' \
compadd -Q -a 'historywords[beg,beg+slice]'
(( beg+=slice ))
done
zstyle -t ":completion:${curcontext}:history-words" list ||
compstate[list]=

View file

@ -11,12 +11,12 @@
#
# Available styles:
#
# :history-words:sort -- sort matches lexically (default is to sort by age)
# :history-words:remove-all-dups --
# remove /all/ duplicate matches rather than just
# consecutives
# sort -- sort matches lexically (default is to sort by age)
# remove-all-dups --
# remove /all/ duplicate matches rather than just consecutives
# range -- range of history words to complete
local opt expl h_words
local opt expl max slice hmax=$#historywords beg=2
if zstyle -t ":completion:${curcontext}:" remove-all-dups; then
opt=-
@ -30,6 +30,19 @@ else
opt="${opt}V"
fi
if zstyle -s ":completion:${curcontext}:" range max; then
if [[ $max = *:* ]]; then
slice=${max#*:}
max=${max%:*}
else
slice=$max
fi
[[ max -gt hmax ]] && max=$hmax
else
max=$hmax
slice=$max
fi
PREFIX="$IPREFIX$PREFIX"
IPREFIX=
SUFFIX="$SUFFIX$ISUFFIX"
@ -37,5 +50,11 @@ ISUFFIX=
# We skip the first element of historywords so the current word doesn't
# interfere with the completion
h_words=( "${(@)historywords[2,-1]}" )
_wanted "$opt" history-words expl 'history word' compadd -Q -a h_words
while [[ $compstate[nmatches] -eq 0 && beg -lt max ]]; do
_wanted "$opt" history-words expl 'history word' \
compadd -Q -a 'historywords[beg,beg+slice]'
(( beg+=slice ))
done
(( $compstate[namtches] ))

View file

@ -34,7 +34,7 @@ local func funcs ret=1 tmp _compskip format nm call match \
_saved_insert="${compstate[insert]}" \
_saved_colors="$ZLS_COLORS"
typeset -U _lastdescr _comp_ignore
typeset -U _lastdescr _comp_ignore _comp_colors
[[ -z "$curcontext" ]] && curcontext=:::
@ -263,7 +263,11 @@ fi
( "$_comp_force_list" = ?* && nm -ge _comp_force_list ) ]] &&
compstate[list]="${compstate[list]//messages} force"
[[ "$compstate[old_list]" = keep ]] && ZLS_COLORS="$_saved_colors"
if [[ "$compstate[old_list]" = keep ]]; then
ZLS_COLORS="$_saved_colors"
else
ZLS_COLORS="${(j.:.)_comp_colors}"
fi
# Now call the post-functions.

View file

@ -1,8 +1,8 @@
#autoload
# This should be used to complete parameter names if you need some of the
# extra options of compadd. It first tries to complete only non-local
# parameters. All arguments are given to compadd.
# extra options of compadd. It completes only non-local parameters.
compadd "$@" - "${(@)${(@)${(@)${(@f)$(typeset)}:#*local *\=*}%%\=*}##* }" ||
compadd "$@" - "${(@)${(@)${(@f)$(typeset)}%%\=*}##* }"
local expl
_wanted parameters expl parameter compadd "$@" -k 'parameters[(R)^*local*]'

View file

@ -7,16 +7,10 @@ local val nm="$compstate[nmatches]"
if zstyle -a ":completion:${curcontext}:$1" list-colors val; then
zmodload -i zsh/complist
if [[ "$1" = default ]]; then
ZLS_COLORS="${(j.:.)${(@)val:gs/:/\\\:}}"
_comp_colors=( "$val[@]" )
else
local simple grouped
simple=( "(${2})${(@)^val:#\(*\)*}" )
grouped=( "${(M@)val:#\(*\)*}" )
simple="${(j.:.)simple}:"
grouped="${(j.:.)grouped}:"
[[ "$ZLS_COLORS" != *${simple}* ]] && ZLS_COLORS="${simple}$ZLS_COLORS"
[[ "$ZLS_COLORS" != *${grouped}* ]] && ZLS_COLORS="${grouped}$ZLS_COLORS"
_comp_colors=( "$_comp_colors[@]"
"(${2})${(@)^val:#\(*\)*}" "${(M@)val:#\(*\)*}" )
fi
# Here is the problem mentioned in _main_complete.

View file

@ -30,18 +30,8 @@ if (( $# )); then
[[ "$1" = -(|-) ]] && shift
if zstyle -a ":completion:${curcontext}:" group-order order; then
local name
for name in "$order[@]"; do
compadd -J "$name"
compadd -V "$name"
compadd -J "$name" -1
compadd -V "$name" -1
compadd -J "$name" -2
compadd -V "$name" -2
done
fi
zstyle -a ":completion:${curcontext}:" group-order order &&
compgroups "$order[@]"
# Set and remember offered tags.

View file

@ -469,7 +469,7 @@ _apt-config () {
-- \
/$'shell\0'/ \
\( \
/$'[^\0]#\0'/ ':parameters:shell variable to assign:compadd "$expl[@]" - "${(@k)parameters}"' \
/$'[^\0]#\0'/ ':parameters:shell variable to assign:_parameters' \
/$'[^\0]#\0'/ ':values:configuration key:compadd "$expl[@]" - ${${(f)"$(apt-config dump 2>&1)"}% *}' \
\) \# \| \
/$'dump\0'/ \| \

View file

@ -15,5 +15,5 @@ else
[[ "$1" = - ]] && shift
_wanted extensions expl 'X extensions' \
compadd "$@" -M 'm:{a-z}={A-Z} r:|-=* r:|=*' - _xe_cache
compadd "$@" -M 'm:{a-z}={A-Z} r:|-=* r:|=*' -a _xe_cache
fi

View file

@ -386,6 +386,22 @@ ifnzman(noderef(The zsh/zutil Module))).
When looking up styles the completion system uses full context names,
including the tag.
To have more control over when certain values for styles are used one
can use the special parameters available in completion widgets (see
ifzman(see zmanref(zshcompwid))\
ifnzman(noderef(Completion Widgets)))\
) and the tt(-e) option to tt(zstyle) that makes the value be
evaluated when looked up. For example, to make the tt(completer)
style have a different value when completion for the tt(cvs) command,
one could use the tt(words) special array:
example(zstyle -e ':completion:*' completer '
if [[ $words[1] = cvs ]]; then
reply=(_complete)
else
reply=(_complete _approximate)
fi')
Styles determine such things as how the matches are generated; some of them
correspond to shell options (for example, the use of menu completion), but
styles provide more specific control. They can have any number of strings as
@ -1732,6 +1748,19 @@ is any, and if it is different from the word on the line.
)
enditem()
)
kindex(range, completion style)
item(tt(range))(
This is used by the tt(_history) completer and the
tt(_history_complete_word) bindable command to decide which words
should be completed. It may be set to a number, var(N), to say that
only the last var(N) words from the history should be completed. The
value may also be of the form `var(max)tt(:)var(slice)'. This means
that first the last var(slice) words will be completed. If that
yields no matches, the var(slice) words before those will be tried and
so on, until either at least one match is generated or var(max) words
have been tried. The default is to complete all words from the
history at once.
)
kindex(remove-all-dups, completion style)
item(tt(remove-all-dups))(
The tt(_history_complete_word) bindable command and the tt(_history)

View file

@ -485,11 +485,14 @@ Like tt(-i), but gives an ignored suffix.
)
item(tt(-a))(
With this flag the var(words) are taken as names of arrays and the
possible matches are their values.
possible matches are their values. If only some elements of the
arrays are needed, the var(words) may also contain subscripts, as in
`tt(foo[2,-1])'.
)
item(tt(-k))(
With this flag the var(words) are taken as names of associative arrays
and the possible matches are their keys.
and the possible matches are their keys. As for tt(-a), the
var(words) may also contain subscripts, as in `tt(foo[(R)*bar*])'.
)
item(tt(-d) var(array))(
This adds per-match display strings. The var(array) should contain one

View file

@ -1553,14 +1553,17 @@ get_user_var(char *nam)
}
static char **
get_user_keys(char *nam)
get_data_arr(char *name, int keys)
{
char **ret;
struct value vbuf;
Value v;
if ((ret = gethkparam(nam)))
return (incompfunc ? arrdup(ret) : ret);
if (!(v = fetchvalue(&vbuf, &name, 1,
(keys ? SCANPM_WANTKEYS : SCANPM_WANTVALS) |
SCANPM_MATCHMANY)))
return NULL;
return NULL;
return getarrvalue(v);
}
/* This is used by compadd to add a couple of matches. The arguments are
@ -1586,9 +1589,10 @@ addmatches(Cadata dat, char **argv)
Patprog cp = NULL, *pign = NULL;
LinkList aparl = NULL, oparl = NULL, dparl = NULL;
Brinfo bp, bpl = brbeg, obpl, bsl = brend, obsl;
Heap oldheap;
if (!*argv) {
SWITCHHEAPS(compheap) {
SWITCHHEAPS(oldheap, compheap) {
/* Select the group in which to store the matches. */
gflags = (((dat->aflags & CAF_NOSORT ) ? CGF_NOSORT : 0) |
((dat->aflags & CAF_UNIQALL) ? CGF_UNIQALL : 0) |
@ -1602,7 +1606,7 @@ addmatches(Cadata dat, char **argv)
}
if (dat->mesg)
addmesg(dat->mesg);
} SWITCHBACKHEAPS;
} SWITCHBACKHEAPS(oldheap);
return 1;
}
@ -1638,7 +1642,7 @@ addmatches(Cadata dat, char **argv)
/* Switch back to the heap that was used when the completion widget
* was invoked. */
SWITCHHEAPS(compheap) {
SWITCHHEAPS(oldheap, compheap) {
if ((doadd = (!dat->apar && !dat->opar && !dat->dpar))) {
if (dat->aflags & CAF_MATCH)
hasmatched = 1;
@ -1887,17 +1891,22 @@ addmatches(Cadata dat, char **argv)
obpl = bpl;
obsl = bsl;
if (dat->aflags & CAF_ARRAYS) {
arrays = argv;
argv = NULL;
while (*arrays && (!(argv = ((dat->aflags & CAF_KEYS) ?
get_user_keys(*arrays) :
get_user_var(*arrays))) || !*argv))
Heap oldheap2;
SWITCHHEAPS(oldheap2, oldheap) {
arrays = argv;
argv = NULL;
while (*arrays &&
(!(argv = get_data_arr(*arrays,
(dat->aflags & CAF_KEYS))) ||
!*argv))
arrays++;
arrays++;
arrays++;
if (!argv) {
ms = NULL;
argv = &ms;
}
if (!argv) {
ms = NULL;
argv = &ms;
}
} SWITCHBACKHEAPS(oldheap2);
}
if (dat->ppre)
ppl = strlen(dat->ppre);
@ -1994,17 +2003,22 @@ addmatches(Cadata dat, char **argv)
free_cline(lc);
}
if ((dat->aflags & CAF_ARRAYS) && !argv[1]) {
argv = NULL;
while (*arrays && (!(argv = ((dat->aflags & CAF_KEYS) ?
get_user_keys(*arrays) :
get_user_var(*arrays))) || !*argv))
Heap oldheap2;
SWITCHHEAPS(oldheap2, oldheap) {
argv = NULL;
while (*arrays &&
(!(argv = get_data_arr(*arrays,
(dat->aflags & CAF_KEYS))) ||
!*argv))
arrays++;
arrays++;
arrays++;
if (!argv) {
ms = NULL;
argv = &ms;
}
argv--;
if (!argv) {
ms = NULL;
argv = &ms;
}
argv--;
} SWITCHBACKHEAPS(oldheap2);
}
}
if (dat->apar)
@ -2015,7 +2029,7 @@ addmatches(Cadata dat, char **argv)
set_list_array(dat->dpar, dparl);
if (dat->exp)
addexpl();
} SWITCHBACKHEAPS;
} SWITCHBACKHEAPS(oldheap);
/* We switched back to the current heap, now restore the stack of
* matchers. */

View file

@ -2268,13 +2268,14 @@ static int cdepth = 0;
static int
makecomplistctl(int flags)
{
Heap oldheap;
int ret;
if (cdepth == MAX_CDEPTH)
return 0;
cdepth++;
SWITCHHEAPS(compheap) {
SWITCHHEAPS(oldheap, compheap) {
int ooffs = offs, lip, lp;
char *str = comp_str(&lip, &lp, 0), *t;
char *os = cmdstr, **ow = clwords, **p, **q, qc;
@ -2333,7 +2334,7 @@ makecomplistctl(int flags)
clwords = ow;
clwnum = on;
clwpos = op;
} SWITCHBACKHEAPS;
} SWITCHBACKHEAPS(oldheap);
cdepth--;
return ret;

View file

@ -1750,8 +1750,6 @@ domenuselect(Hookdef dummy, Chdata dat)
}
setwish = wasnext = 0;
getk:
if (!(cmd = getkeycmd()) || cmd == Th(z_sendbreak)) {
zbeep();
break;

View file

@ -3200,7 +3200,7 @@ cfp_matcher_pats(char *matcher, char *add)
}
}
if (*add) {
char *ret = "", buf[259], *oadd = add;
char *ret = "", buf[259];
for (mp = ms; *add; add++, mp++) {
if (!(m = *mp)) {
@ -3661,6 +3661,32 @@ bin_compfiles(char *nam, char **args, char *ops, int func)
return 1;
}
static int
bin_compgroups(char *nam, char **args, char *ops, int func)
{
Heap oldheap;
char *n;
SWITCHHEAPS(oldheap, compheap) {
while ((n = *args++)) {
endcmgroup(NULL);
begcmgroup(n, 0);
endcmgroup(NULL);
begcmgroup(n, CGF_NOSORT);
endcmgroup(NULL);
begcmgroup(n, CGF_UNIQALL);
endcmgroup(NULL);
begcmgroup(n, CGF_NOSORT|CGF_UNIQCON);
endcmgroup(NULL);
begcmgroup(n, CGF_UNIQALL);
endcmgroup(NULL);
begcmgroup(n, CGF_NOSORT|CGF_UNIQCON);
}
} SWITCHBACKHEAPS(oldheap);
return 0;
}
static struct builtin bintab[] = {
BUILTIN("compdescribe", 0, bin_compdescribe, 3, -1, 0, NULL, NULL),
BUILTIN("comparguments", 0, bin_comparguments, 1, -1, 0, NULL, NULL),
@ -3670,6 +3696,7 @@ static struct builtin bintab[] = {
BUILTIN("comptry", 0, bin_comptry, 0, -1, 0, NULL, NULL),
BUILTIN("compfmt", 0, bin_compfmt, 2, -1, 0, NULL, NULL),
BUILTIN("compfiles", 0, bin_compfiles, 1, -1, 0, NULL, NULL),
BUILTIN("compgroups", 0, bin_compgroups, 1, -1, 0, NULL, NULL),
};

View file

@ -1629,8 +1629,8 @@ struct heap {
# define NEWHEAPS(h) do { Heap _switch_oldheaps = h = new_heaps(); do
# define OLDHEAPS while (0); old_heaps(_switch_oldheaps); } while (0);
# define SWITCHHEAPS(h) do { Heap _switch_oldheaps = switch_heaps(h); do
# define SWITCHBACKHEAPS while (0); switch_heaps(_switch_oldheaps); } while (0);
# define SWITCHHEAPS(o, h) do { o = switch_heaps(h); do
# define SWITCHBACKHEAPS(o) while (0); switch_heaps(o); } while (0);
/****************/
/* Debug macros */