73 lines
1.8 KiB
Plaintext
Raw Normal View History

2009-05-21 01:38:13 +02:00
# bash completion for GNU tar
2009-06-08 21:22:43 +03:00
have tar && {
2009-05-21 01:38:13 +02:00
_tar()
{
2009-10-04 19:42:50 +02:00
local cur ext regex tar untar
2009-05-21 01:38:13 +02:00
2009-10-04 19:42:50 +02:00
COMPREPLY=()
_get_comp_words_by_ref cur
2009-05-21 01:38:13 +02:00
2009-10-04 19:42:50 +02:00
if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=( $( compgen -W 'c t x u r d A' -- "$cur" ) )
return 0
fi
2009-05-21 01:38:13 +02:00
case ${COMP_WORDS[1]} in
2010-09-19 13:15:07 +03:00
?(-)[cr]*f)
_filedir
return 0
;;
+([^IZzJjy])f)
ext='@(@(tar|gem|spkg)?(.@(Z|[gx]z|bz?(2)|lzma))|t@([glx]z|bz?(2)))'
regex='\(\(tar\|gem\|spkg\)\(\.\(Z\|[gx]z\|bz?(2)\|lzma\)\)\?\|t\([glx]z\|bz2\?\)\)'
2010-09-19 13:15:07 +03:00
;;
*[Zz]*f)
ext='@(t?(ar.)|gem.|spkg.)@(gz|Z)'
regex='\(t\(ar\.\)\?\|gem\.\|spkg\.\)\(gz\|Z\)'
2010-09-19 13:15:07 +03:00
;;
*[Ijy]*f)
ext='@(@(t?(ar.)|gem.)bz?(2)|spkg)'
regex='\(\(t\(ar\.\)\?\|gem\.\)bz2\?\|spkg\)'
2010-09-19 13:15:07 +03:00
;;
*[J]*f)
ext='@(t?(ar.)|gem.|spkg.)@(lz?(ma)|xz)'
regex='\(t\(ar\.\)\?\|gem\.\|spkg\.\)\(lzma\|xz\)\?'
2010-09-19 13:15:07 +03:00
;;
*)
_filedir
return 0
;;
2009-10-04 19:42:50 +02:00
esac
2009-05-21 01:38:13 +02:00
2009-10-04 19:42:50 +02:00
if [[ "$COMP_LINE" == *$ext' ' ]]; then
# complete on files in tar file
#
# get name of tar file from command line
tar=$( sed -e 's/^.* \([^ ]*'$regex'\) .*$/\1/' <<<"$COMP_LINE" )
2009-10-04 19:42:50 +02:00
# devise how to untar and list it
untar=t${COMP_WORDS[1]//[^Izjyf]/}
2009-05-21 01:38:13 +02:00
local IFS=$'\n'
COMPREPLY=( $( compgen -W "$( printf '%s\n' $( tar $untar $tar \
2009-10-04 19:42:50 +02:00
2>/dev/null ) )" -- "$cur" ) )
return 0
fi
2009-05-21 01:38:13 +02:00
2009-10-04 19:42:50 +02:00
# file completion on relevant files
_filedir "$ext"
2009-05-21 01:38:13 +02:00
2009-10-04 19:42:50 +02:00
return 0
2009-05-21 01:38:13 +02:00
}
[ -n "${COMP_TAR_INTERNAL_PATHS:-}" ] && complete -F _tar -o dirnames tar ||
complete -F _tar tar
2009-06-08 21:22:43 +03:00
}
# Local variables:
# mode: shell-script
2009-10-04 19:42:50 +02:00
# sh-basic-offset: 4
# sh-indent-comment: t
2009-10-04 19:42:50 +02:00
# indent-tabs-mode: nil
# End:
2009-10-04 19:42:50 +02:00
# ex: ts=4 sw=4 et filetype=sh