73 lines
1.6 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=()
cur=`_get_cword`
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
2009-10-04 19:42:50 +02:00
case "${COMP_WORDS[1]}" in
?(-)[cr]*f)
_filedir
return 0
;;
+([^IZzJjy])f)
ext='t@(ar?(.@(Z|gz|bz?(2)|lz?(ma)))|gz|bz?(2)|lz?(ma))'
regex='t\(ar\(\.\(Z\|gz\|bz2\?\|lzma\|xz\)\)\?\|gz\|bz2\?\|lzma\|xz\)'
;;
*[Zz]*f)
ext='t?(ar.)@(gz|Z)'
regex='t\(ar\.\)\?\(gz\|Z\)'
;;
*[Ijy]*f)
ext='t?(ar.)bz?(2)'
regex='t\(ar\.\)\?bz2\?'
;;
*[J]*f)
ext='t?(ar.)@(lz?(ma)|xz)'
regex='t\(ar\.\)\?\(lzma\|xz\)\?'
;;
*)
_filedir
return 0
;;
2009-05-21 01:38:13 +02:00
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=$( echo "$COMP_LINE" | sed -e 's/^.* \([^ ]*'$regex'\) .*$/\1/' )
# devise how to untar and list it
untar=t${COMP_WORDS[1]//[^Izjyf]/}
2009-05-21 01:38:13 +02:00
2009-10-04 19:42:50 +02:00
COMPREPLY=( $( compgen -W "$( echo $( tar $untar $tar \
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 -o filenames 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