#zdep options
### vim:ft=zsh:foldmethod=marker
###
### zsh's old completion system
###
### Frank Terbeck <ft@bewatermyfriend.org>
### Last-Modified: Mon Jan 21 00:54:44 2008
###
### URI: <http://ft.bewatermyfriend.org/comp/zsh.html>
###

### simple completions {{{

### complete only dirs (or symlinks to dirs in some cases) for certain commands
compctl -g '*(/)' rmdir dircmp
compctl -g '*(-/)' cd chdir dirs pushd

### hosts completion for a few commands
compctl -k hosts ftp lftp ncftp ssh w3m lynx links elinks nc telnet rlogin host
compctl -k hosts -P '@' finger

### kill
compctl -j -P '%' + -s '`ps ax | tail -n+2 | cut -c1-5`' + -x 's[-] p[1]' -k "($signals[1,-3])" -- kill

### su
compctl -u -x 'w[2,-c] p[3,-1]' -l '' -- su

### id passwd
compctl -u id passwd

### mount
function mounttab() {
    reply=(`egrep "^/.*" /etc/fstab | awk '{print $1}'`)
}
compctl -K mounttab mount

### umount
function umounttab() {
    reply=(`mount | cut -d' ' -f3`)
}
compctl -K umounttab umount

# filetypes based completion
compctl -g '*.Z *.gz *.tgz' + -g '*' zcat zless zgrep gunzip gzip
compctl -g '*.bz2' + -g '*' bzip2 bunzip2
compctl -g '*.tar.Z *.tar.gz *.tgz *.tar.bz2' + -g '*' tar
compctl -g '*.zip *.ZIP' + -g '*' unzip zip
compctl -g '*(-/) *.pl *.PL *.cgi *.pm *.PM *.t *.xpl' perl
compctl -g '*(-/) *.pl *.PL *.pm *.PM *.pod' -K perldoc pod
compctl -g '*(-/) *.rb' ruby
compctl -g '*(-/) *.py *.pyc' python
compctl -g '*(-/) *.c' splint

### }}}
### screen {{{
function listscreens() {
    reply=(`screen -ls | grep 'tached' | sed -e 's/  //' | sed -e 's/  .*//'`)
}
compctl -K listscreens screen

### }}}
### make {{{
function makeentry() {
    local a
    local mfile
    read -cA a
    mfile=(GNUmakefile makefile Makefile)
    while [ ! -z $a[0] ]; do
        shift a
        case $a[0in
            -f)
                shift a;
                mfile=$(a[0])
                ;;
        esac
    done
    while [ ! -z "$mfile[0]" ]; do
        if [ -f $mfile[0] ]; then
            reply=(`egrep '^[^#. ][^=       ]*:' $mfile[0] | cut -d: -f1`)
            break
        else
            shift mfile
        fi
    done
}
compctl -K makeentry -x 'c[-1,-f]' -f -- make
### }}}
### ssh slogin sftp scp {{{
function remote_files() {
    local a
    read -cA a
    reply=(`ssh ${a[-1]%%:*} "echo ${a[-1]#*:]*/(/N) ${a[-1]#*:}*(.N)"`)
}
function sshhosts() {
    reply=(`cut -d' ' -f1 ~/.ssh/known_hosts | cut -d, -f1`)
}
compctl -K sshhosts -k hosts -x 'c[-1,-1]' -u -- ssh slogin sftp
compctl                                                                 \
    -k sshhosts -S ':'                                                  \
    -g '*(-/) *' -S ' '                                                 \
    -u -S '@'                                                           \
    -x 'n[-1,@]' -K sshhosts -S ':'                                     \
    - 's[-]' -k '(a A q Q p r v B C L S o P c i)'                       \
    - 'c[-1,-S]' -X '' -f                                               \
    - 'c[-1,-l]' -u                                                     \
    - 'c[-1,-o]' -X ''                                                  \
    - 'c[-1,-P]' -X ''                                                  \
    - 'c[-1,-c]' -X '' -k '(idea blowfish des 3des arcfour tss none)'   \
    - 'c[-1,-i]' -X '' -f                                               \
    - 'n[-1,:]' -S '' -K remote_files                                   \
    - 'C[0,[./]*] ' -f                                                  \
    -g '*(-/) *' -S ' ' -- scp
### }}}
### manpage comletion {{{
function man_glob() {
    local a mp MP
    MP=${MANPATH:-/usr/share/man:/usr/X11R6/man:/usr/local/man:/usr/local/share/man}
    mp=(${(s+:+)MP})
    read -cA a
    case $a[2in
        1|2|3|4|5|6|7|8|9)
            reply=(${^mp}/man$a[2]/$1*$2(N:t:fr))
            ;;
        *)
            reply=(${^mp}/man*/$1*$2(N:t:fr))
            ;;
    esac
}
compctl -K man_glob man whatis apropos
### }}}
### completion for some builtins (taken from examples) {{{
compctl -z -P '%' bg
compctl -j -P '%' fg jobs disown
compctl -j -P '%' + -s '`ps -x | tail +2 | cut -c1-5`' wait
compctl -A shift
compctl -cAF type whence where which
compctl -m -x 'W[1,-*d*]' -n - 'W[1,-*a*]' -a - 'W[1,-*f*]' -F -- unhash
compctl -m -q -S '=' -x 'W[1,-*d*] n[1,=]' -g '*(-/)' - 'W[1,-*d*]' -n -q -S '=' - 'n[1,=]' -g '*(*)' -- hash
compctl -F functions unfunction
compctl -k '(al dc dl do le up al bl cd ce cl cr dc dl do ho is le ma nd nl se so up)' echotc
compctl -a {,un}alias
compctl -v getln getopts read unset vared
compctl -v -S '=' -q declare export integer local readonly typeset
compctl -eB -x 'p[1] s[-]' -k '(a f m r)' - 'C[1,-*a*]' -ea - 'C[1,-*f*]' -eF - 'C[-1,-*r*]' -ew -- disable
compctl -dB -x 'p[1] s[-]' -k '(a f m r)' - 'C[1,-*a*]' -da - 'C[1,-*f*]' -dF - 'C[-1,-*r*]' -dw -- enable
compctl -k "(${(j: :)${(f)$(limit)}%% *})" limit unlimit
compctl -l '' -x 'p[1]' -f -- . source
compctl -s '$(setopt 2>/dev/null)' + -o + -x 's[no]' -o -- unsetopt
compctl -s '$(unsetopt)' + -o + -x 's[no]' -o -- setopt
compctl -s '${^fpath}/*(N:t)' autoload
compctl -s '${^module_path}/*(N:t:r)' -x 'W[1,-*(a*u|u*a)*],W[1,-*a*]p[3,-1]' -B - 'W[1,-*u*]' -s '$(zmodload)' -- zmodload
compctl -b bindkey
compctl -c -x 'C[-1,-*k]' -A - 'C[-1,-*K]' -F -- compctl
compctl -x 'C[-1,-*e]' -c - 'C[-1,-[ARWI]##]' -f -- fc
compctl -x 'p[1]' - 'p[2,-1]' -l '' -- sched
compctl -x 'C[-1,[+-]o]' -o - 'c[-1,-A]' -A -- set
compctl -l '' nohup noglob exec nice eval time sudo
### }}}