diff --git a/xclip.bash-completion b/xclip.bash-completion new file mode 100644 index 0000000..9803247 --- /dev/null +++ b/xclip.bash-completion @@ -0,0 +1,59 @@ +# -*- shell-script -*- +# bash completion for xclip, xclip-{copy,cut,paste}file. +# This file is part of the xclip program. +# This lists available TARGETS for -t, available selections for -se, +# available options for -, and files otherwise. + +_getselection() { + # Did the user specify which selection to use already? + # Look through words for "-selection" then return next word. + # (This affects which TARGETS are returned.) + local w nextandbreak + + for w in "${COMP_WORDS[@]}"; do + [[ -z "$nextandbreak" ]] || break + [[ $w == -se* ]] && nextandbreak=1 + done + + if [[ "$w" && $w != -se* ]]; then + echo "$w" + else + echo "Primary" + fi +} + +_xclip() +{ + local cur prev selection options + + COMPREPLY=() + cur=${COMP_WORDS[COMP_CWORD]} + + prev=${COMP_WORDS[COMP_CWORD-1]} + case $prev in + -t*) + selection=$( _getselection ) + options=$( xclip -selection $selection -target TARGETS 2>/dev/null ) + COMPREPLY=( $( compgen -W "$options" -- $cur ) ) + return 0 + ;; + -se*) + options="Primary Secondary Clipboard Buffer-cut" + COMPREPLY=( $( compgen -W "$options" -- ${cur^} ) ) + return 0 + ;; + esac + + # Begins with -, so command line option (or badly named file, I suppose) + if [[ "$cur" == -* ]]; then + options="-in -out -loops -display -help -selection" + options+=" -c -noutf8 -target -T -rmlastnl -version" + options+=" -silent -quiet -verbose" + COMPREPLY=( $( compgen -W "$options" -- $cur ) ) + return 0 + fi + + # Fall back to files + COMPREPLY=( $( compgen -f -- $cur ) ) +} +complete -F _xclip xclip