#!/usr/bin/zsh
# -*- mode: shell-script -*-
#
# .zshrc - customisations for the all-powerful shell
#
# the latest copy of this file can be found at 
#   http://repose.cx/conf
#

typeset -U path
path=(~/.bin ~/Muse /sbin /usr/sbin $path)

typeset -U cdpath
cdpath=(. $cdpath)

setopt nobeep                  # i hate beeps
setopt noautomenu              # don't cycle completions
setopt autocd                  # change to dirs without cd
#setopt autopushd               # automatically append dirs to the push/pop list
setopt pushdignoredups         # and don't duplicate them
setopt cdablevars              # avoid the need for an explicit $
setopt nocheckjobs             # don't warn me about bg processes when exiting
setopt nohup                   # and don't kill them, either
#setopt listpacked              # compact completion lists
setopt nolisttypes             # show types in completion
setopt dvorak                  # with spelling correction, assume dvorak kb
setopt extendedglob            # weird & wacky pattern matching - yay zsh!
setopt completeinword          # not just at the end
setopt alwaystoend             # when complete from middle, move cursor
setopt correct                 # spelling correction
setopt nopromptcr              # don't add \n which overwrites cmds with no \n
setopt histverify              # when using ! cmds, confirm first
setopt interactivecomments     # escape commands so i can use them later
setopt printexitvalue          # alert me if something's failed

# woo! so glad i found this. jump to each element in a path with m-f m-b, same
# for kill-word, etc.
export WORDCHARS=''            

# history related stuff.
export HISTSIZE=5000
export SAVEHIST=5000
export HISTFILE=~/.zsh_history
setopt hist_ignore_dups        # ignore same commands run twice+
setopt appendhistory           # don't overwrite history 

# prompt (if running screen, show window #)
if [ x$WINDOW != x ]; then
    export PS1="%m[$WINDOW]%# "
else
    export PS1='%m%# '
fi

# set up some directory variables. i can then do cd ms to land in my music dir
# emacs understands these too.
export m=~/Media/music
export uni=~/Lib/uni
export code=~/Lib/code
export work=~/Lib/work
: ~m ~uni ~code ~work

# preexec hook shows command in title as it's running, and precmd sets it to
# something else when it's done. this should work with screen and
# gnome-terminal2/multi-gnome-terminal

case $TERM in
    xterm*|screen*)
        preexec () {
            export CURRENTCMD="$1"
            if [ x$WINDOW != x ]; then
                print -Pn "\ek$1\e\\"
            else
                print -Pn "\e]0;$1\a"
            fi
        }
        precmd () {
            if [[ ! -z $CURRENTCMD ]]; then
                if [ x$WINDOW != x ]; then
                    print -Pn "\ek($CURRENTCMD)\e\\"
                else
                    print -Pn "\e]0;($CURRENTCMD)\a"
                fi
            fi
        }
    ;;
esac

# log out all my ephemeral shells, except the last one
export TMOUT=60

TRAPALRM () {
    if [ `pwd` = ~ ] && [ x$WINDOW != x ] && (( $WINDOW != 0 )); then
        exit 0
    fi
}

[[ "$WINDOW" = 0 ]] && setopt ignore_eof

#trap '(( $WINDOW == 0 )) && echo "Last window - type logout" && return 0' EXIT

# this is defined in ~/.bin/e now
# 
# i have gnuclient set to load files in my current emacs buffer, so i'd like to
# jump to it afterwards. i use a program "e" to edit files in this manner.
# e() {
#     sawfish-client -f de-gnuserv-activate > /dev/null
#     # now run gnuclient. we'll be returned to this window when c-x # is hit in
#     # emacs
#     gnuclient $1
# }

export CVSROOT=~/Lib/CVS


# If running interactively, then:
if [ "$PS1" ]; then
    # colour ls
    eval `dircolors`
    alias ls='ls --color=auto'
    
    # use ssh for rysnc
    export RSYNC_RSH=ssh

    # used to have arch-dependent flags, but i kept forgetting to unset cflags
    # before compiling stuff for another box. i don't compile much anymore.
    export CFLAGS="-O2 -fomit-frame-pointer -pipe"

    export SmallEiffel="/usr/lib/smalleiffel/sys/system.se"
    export PYTHONPATH="/home/resolve/.python"
    export PYTHONSTARTUP="/home/resolve/.pystartup"
    export SAVEDIR="/home/resolve/.news/"
    export ORGANISATION="Damien Elmes"

    export TEXINPUTS=::/usr/local/lib/texmf

    export VISUAL=e
    export EDITOR=$VISUAL

    # colours in man output, emacs like bindings. nifty.
    export PAGER=most

    # anti aliasing in the two toolkits
    export QT_XFT=1
    export GDK_USE_XFT=1

    alias go-wl="sudo ifdown eth0; sudo /etc/init.d/pcmcia start"
    alias go-eth="sudo /etc/init.d/pcmcia stop; sudo ifup eth0" 
    alias nw="sfsagent resolve@respite; ls .mail > /dev/null 2>&1"
    
    # display reminders
    [ -f .r ] && cat .r

    umask 022

#
# completion tweaking
#

# complete hostnames out of ssh's ~/.ssh/known_hosts
    autoload -U compinit; compinit
    zstyle ':completion:*' use-cache on
    zstyle ':completion:*' users resolve
    hosts=(${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[0-9]*}%%\ *}%%,*})
    zstyle ':completion:*:hosts' hosts $hosts

# use dircolours in completion listings
    zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}

# allow approximate matching
    zstyle ':completion:*' completer _complete _match _approximate
    zstyle ':completion:*:match:*' original only
    zstyle ':completion:*:approximate:*' max-errors 1 numeric
    zstyle ':completion:*' auto-description 'Specify: %d'
    zstyle ':completion:*' format 'Completing %d'
    zstyle ':completion:*' verbose true
    zstyle ':completion:*:functions' ignored-patterns '_*'
    zstyle ':completion:*:*:(^rm):*:*files' ignored-patterns \
        '*?.(o|c~|zwc)' '*?~'

    bindkey "\C-w" kill-region

fi

# evaluate work related stuff
[ -f .workrc ] && . ~/.workrc

if [ x$TERM = xscreen ]; then
    export TERM=xterm
fi
