# HG changeset patch # User Frank Terbeck # Date 1196887836 -3600 # Node ID 6125ddcf0131b5aa71ab99f2fe9ee0d63247f559 # Parent 3e70aced743451fc640bdf92e1db867fdc5a0682 Introducing check_com() to check for the existence of commands This makes use of features, that require zsh v3.1.7+. usage: check_com [-c] If -c is given, check_com() only checks for commands. Else it checks for the given word in reserved words, commands, aliases, builtins and functions. If used correctly, check_com() does not output anything, so no '>/dev/null 2>&1' stuff is needed. diff -r 3e70aced7434 -r 6125ddcf0131 etc/skel/.zshrc --- a/etc/skel/.zshrc Tue Dec 04 19:06:09 2007 +0100 +++ b/etc/skel/.zshrc Wed Dec 05 21:50:36 2007 +0100 @@ -23,17 +23,17 @@ # autoloading stuff {{{ # associate types and extensions (be aware with perl scripts and anwanted behaviour!) -# type zsh-mime-setup &>/dev/null || { autoload zsh-mime-setup && zsh-mime-setup } +# check_com zsh-mime-setup || { autoload zsh-mime-setup && zsh-mime-setup } # alias -s pl='perl -S' # }}} # completion system {{{ # just make sure it is loaded in this file too - type compinit &>/dev/null || { autoload -U compinit && compinit } + check_com compinit || { autoload -U compinit && compinit } # }}} # make sure isgrmlsmall is defined {{{ - type isgrmlsmall &>/dev/null || function isgrmlsmall () { return 1 } + check_com isgrmlsmall || function isgrmlsmall () { return 1 } # }}} ## variables {{{ @@ -48,10 +48,10 @@ if [[ -z "$BROWSER" ]] ; then if [[ -n "$DISPLAY" ]] ; then #v# If X11 is running - [[ -x $(type firefox) ]] && export BROWSER=firefox + check_com -c firefox && export BROWSER=firefox else #v# If no X11 is running - [[ -x $(type w3m) ]] && export BROWSER=w3m + check_com -c w3m && export BROWSER=w3m fi fi #v# @@ -142,7 +142,7 @@ alias conkeror='firefox -chrome chrome://conkeror/content' # arch/tla stuff - if type -p tla &>/dev/null ; then + if check_com -c tla ; then #a2# Execute \kbd{tla what-changed --diffs | less} alias tdi='tla what-changed --diffs | less' #a2# Execute \kbd{tla-buildpackage} @@ -197,7 +197,7 @@ #a2# Execute \kbd{mkdir -o} alias md='mkdir -p' - [[ -x $(type ipython) ]] && alias ips='ipython -p sh' + check_com -c ipython && alias ips='ipython -p sh' # console stuff #a2# Execute \kbd{mplayer -vo fbdev} @@ -214,7 +214,7 @@ alias insecscp='scp -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null"' # Use 'g' instead of 'git': - type g &>/dev/null || alias g='git' + check_com g || alias g='git' # use colors when browsing man pages, but only if not using LESS_TERMCAP_* from /etc/zsh/zshenv: if [[ -z "$LESS_TERMCAP_md" ]] ; then @@ -222,22 +222,22 @@ fi # check whether Debian's package management (dpkg) is running - if type salias &>/dev/null ; then + if check_com salias ; then #a2# Check whether a dpkg instance is currently running salias check_dpkg_running="dpkg_running" fi # work around non utf8 capable software in utf environment via $LANG and luit - if type isutfenv &>/dev/null && type luit &>/dev/null; then - if [[ -x $(type mrxvt) ]] ; then + if check_com isutfenv && check_com luit ; then + if check_com -c mrxvt ; then isutfenv && [[ -n "$LANG" ]] && alias mrxvt="LANG=${LANG/(#b)(*)[.@]*/$match[1].iso885915} luit mrxvt" fi - if [[ -x $(type aterm) ]] ; then + if check_com -c aterm ; then isutfenv && [[ -n "$LANG" ]] && alias aterm="LANG=${LANG/(#b)(*)[.@]*/$match[1].iso885915} luit aterm" fi - if [[ -x $(type centericq) ]] ; then + if check_com -c centericq ; then isutfenv && [[ -n "$LANG" ]] && alias centericq="LANG=${LANG/(#b)(*)[.@]*/$match[1].iso885915} luit centericq" fi fi @@ -278,7 +278,7 @@ wodeb () { ${=BROWSER} "http://packages.debian.org/search?keywords=$1&searchon=contents&suite=${2:=unstable}§ion=all" } #m# f4 gex() Exact search via Google - which google &>/dev/null && gex () { google "\"[ $1]\" $*" } # exact search at google + check_com google && gex () { google "\"[ $1]\" $*" } # exact search at google # misc #f5# Backup \kbd{file {\rm to} file\_timestamp} @@ -337,7 +337,7 @@ #f5# \kbd{cd} to /usr/share/doc/\textit{package} doc() { cd /usr/share/doc/$1 && ls } _doc() { _files -W /usr/share/doc -/ } - type compdef &>/dev/null && compdef _doc doc + check_com compdef && compdef _doc doc #f5# Make screenshot sshot() { @@ -904,7 +904,7 @@ # highlight important stuff in diff output, usage example: hg diff | hidiff #m# a2 hidiff \kbd{histring} oneliner for diffs - [[ -x $(type histring) ]] && \ + check_com -c histring && \ alias hidiff="histring -fE '^Comparing files .*|^diff .*' | histring -c yellow -fE '^\-.*' | histring -c green -fE '^\+.*'" # rename pictures based on information found in exif headers @@ -973,7 +973,7 @@ # hl() highlighted less # http://ft.bewatermyfriend.org/comp/data/zsh/zfunct.html - if [[ -x $(type highlight) ]] ; then + if check_com -c highlight ; then function hl() { local theme lang theme=${HL_THEME:-""} @@ -1105,7 +1105,7 @@ # }}} # mercurial related stuff {{{ - if type -p hg &>/dev/null ; then + if check_com -c hg ; then # gnu like diff for mercurial # http://www.selenic.com/mercurial/wiki/index.cgi/TipsAndTricks #f5# GNU like diff for mercurial @@ -1186,7 +1186,7 @@ # Work around ion/xterm resize bug. #if [[ "$SHLVL" -eq 1 ]]; then -# if [[ -x $(type resize) ]]; then +# if check_com -c resize ; then # eval `resize \n' >&2 + return 1 + fi + + if (( comonly > 0 )) ; then + [[ -n ${commands[$1]} ]] && return 0 + return 1 + fi + + if [[ -n ${commands[$1]} ]] \ + || [[ -n ${functions[$1]} ]] \ + || [[ -n ${aliases[$1]} ]] \ + || [[ -n ${reswords[$1]} ]] ; then + + return 0 + fi + + return 1 + } +#}}} # check for potentially old files in 'completion.d' {{{ setopt extendedglob @@ -231,7 +267,7 @@ # {{{ set some variables #v# - if type -p vim &>/dev/null ; then + if check_com -c vim ; then export EDITOR=${EDITOR:-vim} else export EDITOR=${EDITOR:-vi} @@ -244,7 +280,7 @@ export SHELL='/bin/zsh' # color setup for ls: - [[ -x $(which dircolors) ]] && eval `dircolors -b` + check_com -c dircolors && eval `dircolors -b` # set width of man pages to 80 for more convenient reading # (( ${+MANWIDTH} )) || export MANWIDTH=80 @@ -633,7 +669,7 @@ fi # {{{ display battery status on right side of prompt via running 'BATTERY=1 zsh' if [[ -n "$BATTERY" ]] ; then - if [[ -x $(which acpi) ]] ; then + if check_com -c acpi ; then PERCENT="${(C)${(s| |)$(acpi 2>/dev/null)}[4]}" [[ -z "$PERCENT" ]] && PERCENT='acpi not present' if [[ "${PERCENT%%%}" -lt 20 ]] ; then @@ -933,14 +969,14 @@ fi alias 0 &>/dev/null || functions 0 &>/dev/null || alias 0='return 0' # for really lazy people like mika: - type S &>/dev/null || alias S='screen' - type s &>/dev/null || alias s='ssh' + check_com S &>/dev/null || alias S='screen' + check_com s &>/dev/null || alias s='ssh' # get top 10 shell commands: alias top10='print -l ? ${(o)history%% *} | uniq -c | sort -nr | head -n 10' # truecrypt; use e.g. via 'truec /dev/ice /mnt/ice' or 'truec -i' - if [[ -x $(which truecrypt) ]] ; then + if check_com -c truecrypt ; then if isutfenv ; then alias truec='truecrypt --mount-options "rw,sync,dirsync,users,uid=1000,gid=users,umask=077,utf8" ' else @@ -1052,14 +1088,14 @@ Enjoy your grml system with the zsh!$res fi # sort installed Debian-packages by size - if [[ -x $(which grep-status) ]] ; then + if check_com -c grep-status ; then #a3# List installed Debian-packages sorted by size alias debs-by-size='grep-status -FStatus -sInstalled-Size,Package -n "install ok installed" | paste -sd " \n" | sort -rn' fi # if cdrecord is a symlink (to wodim) or isn't present at all warn: - if [[ -L /usr/bin/cdrecord || ! -x $(which cdrecord) ]] ; then - if [[ -x $(which wodim) ]] ; then + if [[ -L /usr/bin/cdrecord ]] || ! check_com -c cdrecord ; then + if check_com -c wodim ; then alias cdrecord="echo 'cdrecord is not provided under its original name by Debian anymore. See #377109 in the BTS of Debian for more details. @@ -1068,7 +1104,7 @@ Please use the wodim binary instead' ; r fi # get_tw_cli has been renamed into get_3ware - if [[ -x $(which get_3ware) ]] ; then + if check_com -c get_3ware ; then get_tw_cli() { echo 'Warning: get_tw_cli has been renamed into get_3ware. Invoking get_3ware for you.'>&2 get_3ware @@ -1076,7 +1112,7 @@ Please use the wodim binary instead' ; r fi # I hate lacking backward compatibility, so provide an alternative therefore - if ! [[ -x $(which apache2-ssl-certificate) ]] ; then + if ! check_com -c apache2-ssl-certificate ; then apache2-ssl-certificate(){ print 'Debian does not ship apache2-ssl-certificate anymore (see #398520). :(' @@ -1266,19 +1302,19 @@ If you want to use xinit anyway please c fi } - if [[ -x $(which 915resolution) ]] ; then + if check_com -c 915resolution ; then alias 855resolution='echo -e "Please use 915resolution as resolution modify tool for Intel graphic chipset."; return -1' fi #a1# Output version of running grml alias grml-version='cat /etc/grml_version' - if [[ -x $(which rebuildfstab) ]] ; then + if check_com -c rebuildfstab ; then #a1# Rebuild /etc/fstab alias grml-rebuildfstab='rebuildfstab -v -r -config' fi - if [[ -x $(which grml-debootstrap) ]] ; then + if check_com -c grml-debootstrap ; then alias debian2hd='print "Installing debian to harddisk is possible via using grml-debootstrap." ; return 1' fi } @@ -1341,7 +1377,7 @@ If you want to use xinit anyway please c } # fast manual access - if type -p qma &>/dev/null ; then + if check_com qma ; then #f1# View the zsh manual manzsh() { qma zshall "$1" } compdef _man qma @@ -1352,7 +1388,7 @@ If you want to use xinit anyway please c # manzsh() { man zshall | $MYLESS -p $1 ; } fi - if [[ -x $(which most) ]] ; then + if check_com -c most ; then #f1# View Debian's changelog of a given package dchange() { if [[ -r /usr/share/doc/${1}/changelog.Debian.gz ]] ; then @@ -1360,7 +1396,7 @@ If you want to use xinit anyway please c elif [[ -r /usr/share/doc/${1}/changelog.gz ]] ; then most /usr/share/doc/${1}/changelog.gz else - if type -p aptitude &>/dev/null ; then + if check_com -c aptitude ; then echo "No changelog for package $1 found, using aptitude to retrieve it." if isgrml ; then aptitude -t unstable changelog ${1} @@ -1475,7 +1511,7 @@ If you want to use xinit anyway please c } alias help-zshglob=H-Glob - type -p qma &>/dev/null && alias ?='qma zshall' + check_com -c qma && alias ?='qma zshall' # grep for running process, like: 'any vim' any() {