1
0
mirror of git://git.code.sf.net/p/zsh/code synced 2024-10-03 17:31:33 +02:00
zsh/Completion/Core/_expand

155 lines
4.0 KiB
Plaintext
Raw Normal View History

1999-04-15 20:05:38 +02:00
#autoload
# This completer function is intended to be used as the first completer
# function and allows one to say more explicitly when and how the word
# from the line should be expanded than expand-or-complete.
# This function will allow other completer functions to be called if
# the expansions done produce no result or do not change the original
# word from the line.
2000-04-01 22:43:43 +02:00
setopt localoptions nullglob
[[ _matcher_num -gt 1 ]] && return 1
local exp word sort expr expl subd suf=" " force opt
(( $# )) &&
while getopts gsco opt; do
force="$force$opt"
done
if [[ "$funcstack[2]" = _prefix ]]; then
word="$IPREFIX$PREFIX$SUFFIX"
else
word="$IPREFIX$PREFIX$SUFFIX$ISUFFIX"
fi
2000-04-01 22:43:43 +02:00
# First, see if we should insert all *completions*.
if [[ "$force" = *c* ]] ||
{ zstyle -s ":completion:${curcontext}:" completions expr &&
[[ "${(e):-\$[$expr]}" -eq 1 ]] }; then
2000-04-01 22:43:43 +02:00
compstate[insert]=all
[[ "$curcontext" = expand-word:* ]] && _complete && return 0
2000-04-01 22:43:43 +02:00
return 1
fi
1999-04-15 20:05:38 +02:00
# In exp we will collect the expansion.
exp=("$word")
# First try substitution. That weird thing spanning multiple lines
# changes quoted spaces, tabs, and newlines into spaces and protects
# this function from aborting on parse errors in the expansion.
1999-04-15 20:05:38 +02:00
if [[ "$force" = *s* ]] ||
{ { zstyle -s ":completion:${curcontext}:" substitute expr ||
{ [[ "$curcontext" = expand-word:* ]] && expr=1 } } &&
[[ "${(e):-\$[$expr]}" -eq 1 ]] }; then
exp=( ${(f)"$(print -lR - ${(e)exp//\\[
]/ })"} ) 2>/dev/null
else
exp=( "${exp:s/\\\$/\$}" )
fi
1999-04-15 20:05:38 +02:00
# If the array is empty, store the original string again.
2000-04-01 22:43:43 +02:00
(( $#exp )) || exp=("$word")
subd=("$exp[@]")
1999-04-15 20:05:38 +02:00
# Now try globbing.
[[ "$force" = *g* ]] ||
{ { zstyle -s ":completion:${curcontext}:" glob expr ||
{ [[ "$curcontext" = expand-word:* ]] && expr=1 } } &&
[[ "${(e):-\$[$expr]}" -eq 1 ]] } &&
2000-04-01 22:43:43 +02:00
exp=( ${~exp} )
1999-04-15 20:05:38 +02:00
# If we don't have any expansions or only one and that is the same
# as the original string, we let other completers run.
2000-04-01 22:43:43 +02:00
(( $#exp )) || exp=("$subd[@]")
1999-04-15 20:05:38 +02:00
2000-04-01 22:43:43 +02:00
[[ $#exp -eq 1 && "$exp[1]" = "$word"(|\(N\)) ]] && return 1
1999-04-15 20:05:38 +02:00
2000-04-01 22:43:43 +02:00
# With subst-globs-only we bail out if there were no glob expansions,
# regardless of any substitutions
1999-04-15 20:05:38 +02:00
[[ "$force" = *o* ]] ||
{ zstyle -s ":completion:${curcontext}:" subst-globs-only expr &&
[[ "${(e):-\$[$expr]}" -eq 1 ]] } &&
[[ "$subd" = "$exp"(|\(N\)) ]] && return 1
1999-04-15 20:05:38 +02:00
2000-04-01 22:43:43 +02:00
# Now add as matches whatever the user requested.
1999-04-15 20:05:38 +02:00
2000-04-01 22:43:43 +02:00
zstyle -s ":completion:${curcontext}:" sort sort
1999-04-15 20:05:38 +02:00
2000-04-01 22:43:43 +02:00
[[ "$sort" = (yes|true|1|on) ]] && exp=( "${(@o)exp}" )
1999-04-15 20:05:38 +02:00
2000-04-01 22:43:43 +02:00
# If there is only one expansion, add a suitable suffix
if (( $#exp == 1 )); then
if [[ -d $exp && "$exp[1]" != */ ]]; then
suf=/
elif zstyle -T ":completion:${curcontext}:" add-space; then
suf=
fi
fi
1999-04-15 20:05:38 +02:00
2000-04-01 22:43:43 +02:00
if [[ -z "$compstate[insert]" ]] ;then
if [[ "$sort" = menu ]]; then
_description expansions expl expansions "o:$word"
else
_description -V expansions expl expansions "o:$word"
fi
1999-04-15 20:05:38 +02:00
compadd "$expl[@]" -UQ -qS "$suf" - "$exp[@]"
2000-04-01 22:43:43 +02:00
else
_tags all-expansions expansions original
1999-04-15 20:05:38 +02:00
if _requested all-expansions expl 'all expansions'; then
local disp dstr
1999-04-15 20:05:38 +02:00
if [[ $#exp -ge COLUMNS ]]; then
disp=( -ld dstr )
dstr=( "${(r:COLUMNS-5:)exp} ..." )
else
disp=()
fi
compadd "$disp[@]" "$expl[@]" -UQ -qS "$suf" - "$exp"
fi
2000-04-01 22:43:43 +02:00
if [[ $#exp -gt 1 ]] && _requested expansions; then
local i normal dir
2000-04-01 22:43:43 +02:00
if [[ "$sort" = menu ]]; then
_description expansions expl expansions "o:$word"
else
_description -V expansions expl expansions "o:$word"
fi
if zstyle -T ":completion:${curcontext}:" add-space; then
suf=' '
else
suf=
fi
normal=()
dir=()
for i in "$exp[@]"; do
if [[ -d "$i" && "$i" != */ ]]; then
dir=( "$dir[@]" "$i" )
else
normal=( "$normal[@]" "$i" )
fi
done
(( $#dir )) && compadd "$expl[@]" -UQ -qS/ - "$dir[@]"
(( $#normal )) && compadd "$expl[@]" -UQ -qS "$suf" - "$normal[@]"
1999-04-15 20:05:38 +02:00
fi
2000-04-01 22:43:43 +02:00
_requested original expl original && compadd "$expl[@]" -UQ - "$word"
1999-04-15 20:05:38 +02:00
compstate[insert]=menu
fi
return 0