@@ -57,12 +57,24 @@ compinit -C # Ignore case on completion zstyle ':completion:*' matcher-list 'm:{A-Z}={a-z} m:{a-z}={A-Z}' +__has_command() { + for i in $@; do + which $i &>/dev/null && return 0 + done + return 1 +} __find_command() { - which "$1" &>/dev/null && echo "$1" || echo "$2" + for i in $@; do + if __has_command $i; then + echo $i + return 0 + fi + done + return 1 } if [ -n "$grep" -o -n "$grep_color" ]; then : ${grep:=$(__find_command ggrep grep)} [ -n "$grep_color" -a "$grep_color[1]" != - ] && @@ -196,12 +208,12 @@ for ext in 7z lha tar tbz tbz2 tar.bz2 tgz tar.gz txz tar.xz rar zip bz2 gz xz do alias -s "$ext=extr" done -[ "$EDITOR" = vim ] && alias vi=vim -which gpg2 &>/dev/null && alias gpg=gpg2 +__has_command vim && alias vi=vim +__has_command gpg2 && alias gpg=gpg2 make() { case "$PWD" in # pkgsrc needs bmake and does not like MAKEFLAGS including -j. */pkgsrc*) @@ -211,26 +223,27 @@ =make $@ ;; esac } -alias tmux="tmux -f $XDG_CONFIG_HOME/tmux/tmux.conf" +__has_command tmux && alias tmux="tmux -f $XDG_CONFIG_HOME/tmux/tmux.conf" + +if ! __has_command colordiff; then + colordiff() { + red="$(tput setaf 1)" + green="$(tput setaf 2)" + reset="$(tput sgr0)" + sed "s/^+.*$/$green&$reset/" | sed "s/^-.*$/$red&$reset/" + } +fi -if which fossil &>/dev/null; then +if __has_command fossil; then alias fl="fossil" alias flc="fossil changes --differ" fld() { - if which colordiff &>/dev/null; then - fossil diff -N "$@" | colordiff | less -FRX - else - red="$(tput setaf 1)" - green="$(tput setaf 2)" - reset="$(tput sgr0)" - fossil diff -N "$@" | sed "s/^+.*$/$green&$reset/" | - sed "s/^-.*$/$red&$reset/" | less -FRX - fi + fossil diff -N "$@" | colordiff | less -FRX } flgrep() { local ret=1 fossil changes --all . | sed 's/^.* //' | while read file; do @@ -297,52 +310,50 @@ --export-marks=$gitdir/git.marks \ $=importmarks_git } fi -if which cvs &>/dev/null; then +if __has_command cvs; then cvsd() { - if which colordiff &>/dev/null; then - cvs diff -uN "$@" | colordiff | less -FRX - else - red="$(tput setaf 1)" - green="$(tput setaf 2)" - reset="$(tput sgr0)" - cvs diff -uN "$@" | sed "s/^+.*$/$green&$reset/" | - sed "s/^-.*$/$red&$reset/" | less -FRX - fi + cvs diff -uN "$@" | colordiff | less -FRX } fi # pkgsrc does not like MAKEFLAGS including -j -which pkg_chk &>/dev/null && alias pkg_chk="MAKEFLAGS= pkg_chk" -which pkg_rolling-replace &>/dev/null && +__has_command pkg_chk && + alias pkg_chk="MAKEFLAGS= pkg_chk" +__has_command pkg_rolling-replace && alias pkg_rolling-replace="MAKEFLAGS= pkg_rolling-replace" -which url2pkg &>/dev/null && alias url2pkg="MAKEFLAGS= url2pkg" +__has_command url2pkg && + alias url2pkg="MAKEFLAGS= url2pkg" # mpv does not like locales that use , as decimal point. -alias mpv="LC_ALL=C mpv" - -alias gpg-ssh="SSH_AUTH_SOCK=$GNUPGHOME/S.gpg-agent.ssh ssh" -alias gpg-ssh-add="SSH_AUTH_SOCK=$GNUPGHOME/S.gpg-agent.ssh ssh-add" -alias gpg-sftp="SSH_AUTH_SOCK=$GNUPGHOME/S.gpg-agent.ssh sftp" - -pwnk() { - local clipboard - if which pbcopy &>/dev/null; then - clipboard=pbcopy - elif which xclip &>/dev/null; then - clipboard=xclip - else - echo "No clipboard handler found!" - return 1 - fi - - cryptopassphrase $@ | tr -d '\n' | $clipboard -} -alias pw="pwnk -k ~/.cryptopassphrase.key" -alias pws="pwnk -k ~/.cryptopassphrase-server.key" +__has_command mpv && alias mpv="LC_ALL=C mpv" + +if __has_command gpg gpg2; then + alias gpg-ssh="SSH_AUTH_SOCK=$GNUPGHOME/S.gpg-agent.ssh ssh" + alias gpg-ssh-add="SSH_AUTH_SOCK=$GNUPGHOME/S.gpg-agent.ssh ssh-add" + alias gpg-sftp="SSH_AUTH_SOCK=$GNUPGHOME/S.gpg-agent.ssh sftp" +fi + +if __has_command cryptopassphrase; then + pwnk() { + local clipboard + if __has_command pbcopy; then + clipboard=pbcopy + elif __has_command xclip; then + clipboard=xclip + else + echo "No clipboard handler found!" + return 1 + fi + + cryptopassphrase $@ | tr -d '\n' | $clipboard + } + alias pw="pwnk -k ~/.cryptopassphrase.key" + alias pws="pwnk -k ~/.cryptopassphrase-server.key" +fi ixio() { curl -F 'f:1=<-' ix.io } 0x0st() { curl -F'file=@-' https://0x0.st } cross() { @@ -395,11 +406,11 @@ echo "Usage: cross amiga m68k|ppc" 1>&2 return 1 fi ;; ios | iossim) - if ! which xcrun &>/dev/null; then + if ! __has_command xcrun; then echo -n "Can only cross-compile for iOS " 1>&2 echo "on a Mac with Xcode!" 1>&2 return 1 fi