compdef _gc gc
__gc_debug()
{
local file="$BASH_COMP_DEBUG_FILE"
if [[ -n ${file} ]]; then
echo "$*" >> "${file}"
fi
}
_gc()
{
local shellCompDirectiveError=1
local shellCompDirectiveNoSpace=2
local shellCompDirectiveNoFileComp=4
local shellCompDirectiveFilterFileExt=8
local shellCompDirectiveFilterDirs=16
local shellCompDirectiveKeepOrder=32
local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace keepOrder
local -a completions
__gc_debug "\n========= starting completion logic =========="
__gc_debug "CURRENT: ${CURRENT}, words[*]: ${words[*]}"
words=("${=words[1,CURRENT]}")
__gc_debug "Truncated words[*]: ${words[*]},"
lastParam=${words[-1]}
lastChar=${lastParam[-1]}
__gc_debug "lastParam: ${lastParam}, lastChar: ${lastChar}"
setopt local_options BASH_REMATCH
if [[ "${lastParam}" =~ '-.*=' ]]; then
flagPrefix="-P ${BASH_REMATCH}"
fi
requestComp="${words[1]} __complete ${words[2,-1]}"
if [ "${lastChar}" = "" ]; then
__gc_debug "Adding extra empty parameter"
requestComp="${requestComp} \"\""
fi
__gc_debug "About to call: eval ${requestComp}"
out=$(eval ${requestComp} 2>/dev/null)
__gc_debug "completion output: ${out}"
local lastLine
while IFS='\n' read -r line; do
lastLine=${line}
done < <(printf "%s\n" "${out[@]}")
__gc_debug "last line: ${lastLine}"
if [ "${lastLine[1]}" = : ]; then
directive=${lastLine[2,-1]}
local suffix
(( suffix=${#lastLine}+2))
out=${out[1,-$suffix]}
else
__gc_debug "No directive found. Setting do default"
directive=0
fi
__gc_debug "directive: ${directive}"
__gc_debug "completions: ${out}"
__gc_debug "flagPrefix: ${flagPrefix}"
if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
__gc_debug "Completion received error. Ignoring completions."
return
fi
local activeHelpMarker="_activeHelp_ "
local endIndex=${#activeHelpMarker}
local startIndex=$((${#activeHelpMarker}+1))
local hasActiveHelp=0
while IFS='\n' read -r comp; do
if [ "${comp[1,$endIndex]}" = "$activeHelpMarker" ];then
__gc_debug "ActiveHelp found: $comp"
comp="${comp[$startIndex,-1]}"
if [ -n "$comp" ]; then
compadd -x "${comp}"
__gc_debug "ActiveHelp will need delimiter"
hasActiveHelp=1
fi
continue
fi
if [ -n "$comp" ]; then
comp=${comp//:/\\:}
local tab="$(printf '\t')"
comp=${comp//$tab/:}
__gc_debug "Adding completion: ${comp}"
completions+=${comp}
lastComp=$comp
fi
done < <(printf "%s\n" "${out[@]}")
if [ $hasActiveHelp -eq 1 ]; then
if [ ${#completions} -ne 0 ] || [ $((directive & shellCompDirectiveNoFileComp)) -eq 0 ]; then
__gc_debug "Adding activeHelp delimiter"
compadd -x "--"
hasActiveHelp=0
fi
fi
if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then
__gc_debug "Activating nospace."
noSpace="-S ''"
fi
if [ $((directive & shellCompDirectiveKeepOrder)) -ne 0 ]; then
__gc_debug "Activating keep order."
keepOrder="-V"
fi
if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
local filteringCmd
filteringCmd='_files'
for filter in ${completions[@]}; do
if [ ${filter[1]} != '*' ]; then
filter="\*.$filter"
fi
filteringCmd+=" -g $filter"
done
filteringCmd+=" ${flagPrefix}"
__gc_debug "File filtering command: $filteringCmd"
_arguments '*:filename:'"$filteringCmd"
elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
local subdir
subdir="${completions[1]}"
if [ -n "$subdir" ]; then
__gc_debug "Listing directories in $subdir"
pushd "${subdir}" >/dev/null 2>&1
else
__gc_debug "Listing directories in ."
fi
local result
_arguments '*:dirname:_files -/'" ${flagPrefix}"
result=$?
if [ -n "$subdir" ]; then
popd >/dev/null 2>&1
fi
return $result
else
__gc_debug "Calling _describe"
if eval _describe $keepOrder "completions" completions $flagPrefix $noSpace; then
__gc_debug "_describe found some completions"
return 0
else
__gc_debug "_describe did not find completions."
__gc_debug "Checking if we should do file completion."
if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
__gc_debug "deactivating file completion"
return 1
else
__gc_debug "Activating file completion"
_arguments '*:filename:_files'" ${flagPrefix}"
fi
fi
fi
}
if [ "$funcstack[1]" = "_gc" ]; then
_gc
fi