1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-09-18 15:21:16 +02:00
zsh/Completion/Core/_expand
1999-11-05 14:04:17 +00:00

140 lines
4.1 KiB
Text

#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.
local exp word="$PREFIX$SUFFIX" group=-V expl expl2 disp
# First, see if we should insert all *completions*.
if [[ -n "$compconfig[expand_completions]" &&
"${(e):-\$[$compconfig[expand_substitute]]}" -eq 1 ]]; then
compstate[insert]=all
return 1
fi
# Do this only for the first global matcher.
[[ "$compstate[matcher]" -le 1 ]] || return 1
# 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.
[[ -z "$compconfig[expand_substitute]" ||
"${(e):-\$[$compconfig[expand_substitute]]}" -eq 1 ]] &&
exp=( "${(e)exp//\\[
]/ }" )
# If the array is empty, store the original string again.
[[ -z "$exp" ]] && exp=("$word")
# Now try globbing.
[[ -z "$compconfig[expand_glob]" ||
"${(e):-\$[$compconfig[expand_glob]]}" -eq 1 ]] &&
exp=( ${~exp}(N) )
# If we don't have any expansions or only one and that is the same
# as the original string, we let other completers run.
[[ $#exp -eq 0 ||
( $#exp -eq 1 && "$exp[1]" = "$word"(|\(N\)) ) ]] && return 1
# Get the options for adding the original string and `all'-string.
if [[ "$compconfig[expand_original]" = *show* ]]; then
if [[ -n "$compconfig[description_format]" ]]; then
expl=(-X "${compconfig[description_format]//\\%d/original}")
else
expl=()
fi
else
expl=(-n)
fi
if [[ -n "$compconfig[expand_menu]" &&
"$compconfig[expand_menu]" != *only* &&
"$compconfig[expand_menu]" = *showall* ]]; then
if [[ -n "$compconfig[description_format]" ]]; then
expl2=(-ld disp -X "${compconfig[description_format]//\\%d/all words}")
else
expl2=(-ld disp )
fi
disp=( "$exp" )
if [[ ${#disp[1]} -gt COLUMNS-5 ]]; then
disp=( "${disp[1][1,COLUMNS-5]}..." )
fi
else
expl2=(-n)
fi
# Quote the results and remove unnecessary quotes before `='s.
exp=( "${(@)${(@)${(@q)exp}//\\\\=/=}/#=/\\=}" )
# We have expansions, should we menucomplete them?
if [[ -z "$compconfig[expand_menu]" ]]; then
# No, so if the user only wants a list, we add the strings
# separately. Otherwise we add the whole array as one string,
# probably also adding the original string.
if [[ -z "$compstate[insert]" ]]; then
compadd -U -V _expand -Q - "$exp[@]"
else
[[ -n "$compconfig[expand_original]" &&
"$compconfig[expand_original]" != *last* ]] &&
compadd "$expl[@]" -UQ -V _expand_original - "$word"
compadd -UQ -V _expand - "$exp"
[[ -n "$compconfig[expand_original]" &&
"$compconfig[expand_original]" = *last* ]] &&
compadd "$expl[@]" -UQ -V _expand_original - "$word"
compstate[insert]=menu
fi
else
# Sorting? We just use a different group type then.
[[ "$compconfig[expand_menu]" = *sort* ]] && group=-J
# Now add the expansion string, probably also adding the original
# and/or the string containing all expanded string.
[[ -n "$compconfig[expand_original]" &&
"$compconfig[expand_original]" != *last* ]] &&
compadd "$expl[@]" -UQ -V _expand_original - "$word"
[[ $#exp -ne 1 && "$compconfig[expand_menu]" = *last* &&
"$compconfig[expand_menu]" != *only* ]] &&
compadd "$expl2[@]" -UQ -V _expand_all - "$exp"
if [[ -z "$compconfig[expand_prompt]" ]]; then
compadd -UQ $group _expand - "$exp[@]"
else
compadd -UQ -X "${compconfig[expand_prompt]//\\%o/$word}" \
$group _expand - "$exp[@]"
fi
[[ $#exp -ne 1 && "$compconfig[expand_menu]" != *last* &&
"$compconfig[expand_menu]" != *only* ]] &&
compadd "$expl2[@]" -UQ -V _expand_all - "$exp"
[[ -n "$compconfig[expand_original]" &&
"$compconfig[expand_original]" = *last* ]] &&
compadd "$expl[@]" -UQ -V _expand_original - "$word"
compstate[insert]=menu
fi
return 0