gregf /.zshrc

###############################################################################
# Zsh settings file for Greg Fitzgerald 
#
# Most recent update: Wed Oct  8 15:24:37 2008
###############################################################################
# TODO
# . ~/.zsh/config
# . ~/.zsh/aliases
# . ~/.zsh/complete
# [[ -f ~/.localrc ]] && . ~/.localrc
# Split functions into ~/.zsh/functions/functioname
##############################################################################
# Test for an interactive shell. There is no need to set anything
# past this point for scp, and it's important to refrain from
# outputting anything in those cases.
##############################################################################
# by default, we want this to get set.
# Even for non-interactive, non-login shells.
if [ "`id -gn`" = "`id -un`" -a `id -u` -gt 99 ]; then
        umask 002
  else
        umask 022
fi

if [[ $- != *i* ]]; then
	# Shell is non-interactive.  Be done now
	return
fi
#################################################################################
# Create some default files/directories if they don't exist.
################################################################################
if ! [ -d ~/.ssh ]; then
    mkdir -p ~/.ssh
fi

if ! [ -f ~/.ssh/known_hosts ]; then
    touch ~/.ssh/known_hosts
fi

if ! [ -f ~/.viminfo ]; then
    touch ~/.viminfo
fi
##############################################################################
#       Clear screen on logout
##############################################################################
trap clear 0
#############################################################################
#       OS specific settings
#############################################################################
case `uname` in
	OpenBSD)
        # Enviroment Varibles
        export PKG_PATH=ftp://ftp.openbsd.org/pub/4.3/packages/`machine -a`/

	    # which version of ls should we use?
	    if [ -x /usr/local/bin/gls ]; then
		    alias ls='/usr/local/bin/gls -F --color --human-readable'
		    export LS_COLORS="no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;3:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:"
	    else
		    if [ -x /usr/local/bin/colorls ]; then
		        alias ls='/usr/local/bin/colorls -G -F'
		        export LSCOLORS="ExGxFxDxCxDxDxxbadacad"
		    fi
	    fi
	    # Aliases
        alias cvsup="cd /usr; cvs -d anoncvs@anoncvs1.usa.openbsd.org:/cvs checkout -P -rOPENBSD_4_3 src"
	    alias cvsrun="sudo cvsup -g -L 2 /etc/cvs-supfile"
        alias killall="pkill"
        alias shred="rm -P"
	;;
	FreeBSD)
        #SSH_ASKPASS="/usr/local/bin/ssh-askpass-gtk2"
        LSCOLORS=ExCxFxFxBxGxGxababaeae
        CLICOLOR=$LSCOLORS

        alias ls='ls -GF'
        alias portu="sudo csup -L 2 /etc/ports-cvsup"
        alias srcu="sudo csup -L 2 /etc/src-cvsup"
        export LANG="en_US.UTF-8"
        export LC_CTYPE="en_US.UTF-8"
        export LC_ALL="en_US.UTF-8"
   ;;
	Linux)
	    #export SSH_ASKPASS="/usr/bin/gtk2-ssh-askpass"

        if [[ -f /etc/gentoo-release ]]; then
            alias ms="mirrorselect -b10 -s5 -D"
            alias python-updater="python-updater -P paludis"

            export ECHANGELOG_USER="Greg Fitzgerald "
            export PALUDIS_OPTIONS="--show-reasons summary --dl-reinstall-scm weekly --log-level warning --dl-reinstall if-use-changed --show-use-descriptions changed"

            function paludis-scm {
                PALUDIS_OPTIONS="--dl-reinstall-scm daily"
                paludis $@
                PALUDIS_OPTIONS="--dl-reinstall-scm never"
            }

            function explainuseflag {
                sed -ne "s,^\([^ ]*:\)\?$1 - ,,p" \
                /usr/portage/profiles/use.desc \
                /usr/portage/profiles/use.local.desc
            }

            function edesc {
                cat *.ebuild | sed -ne 's-^DESCRIPTION="\(.*\)".*-\1-1p' | sort -u
            }

            function ewww {
                cat *.ebuild | sed -ne 's-^HOMEPAGE="\(.*\)".*-\1-1p' | sort -u
            }

            function rca {
                sudo /sbin/rc-update add $1 default
            }

            function rcd {
                sudo /sbin/rc-update del $1 default
            }

        fi

        # We Want utf8
        export LC_CTYPE=en_US.utf8
        export LC_ALL=en_US.UTF8

	    #aliases
        alias ls='ls -F --color --human-readable'

        if [ $HOST  = "gila" ]; then
          export RAILS_ENV="production"
        fi

        #############################################################################
        # colors for ls, etc.  Prefer ~/.dir_colors
        #############################################################################
        if [[ -f ~/.dir_colors ]]; then
            eval `dircolors -b ~/.dir_colors`
        else
            eval `dircolors -b /etc/DIR_COLORS`
        fi
	;;
esac

################################################################################
#      Enviroment Variables
################################################################################
unset MAILCHECK
if [[ -d /var/tmp/ccache ]]; then
    (( ${+CCACHE_DIR} )) || export CCACHE_DIR="/var/tmp/ccache"
    (( ${+CCACHE_SIZE} )) || export CCACHE_SIZE="2G"

fi
## (( ${+*} )) = if variable is set don't try to set it again
#(( ${+GREP_OPTIONS} )) || export GREP_OPTIONS="--color=auto -nsi"
(( ${+TZ} )) || export TZ="EST5EDT"
(( ${+MPD_HOST} )) || export MPD_HOST="127.0.0.1"
(( ${+MPD_PORT} )) || export MPD_PORT="6600"
(( ${+GIT_AUTHOR_EMAIL} )) || export GIT_AUTHOR_EMAIL="netzdamon@gmail.com"
(( ${+GIT_AUTHOR_NAME} )) || export GIT_AUTHOR_NAME="Greg Fitzgerald"
(( ${+GIT_COMMITTER_EMAIL} )) || export GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL

# Some things we want set regardless
export MANPAGER="most"
export PAGER="most"
export EDITOR="vim"
export VISUAL="vim"

################################################################################
# Resource Limits
################################################################################
limit stack 8192
#limit core unlimited
limit core 0
limit -s
###############################################################################
# Setup History Options
################################################################################
HISTSIZE=10000
HISTFILE=${HOME}/.history_zsh
SAVEHIST=10000
DIRSTACKSIZE=16
################################################################################
# Term Settings
################################################################################
#auto logout after timeout in seconds
TMOUT=1800

# if we are in X then disable TMOUT
case $TERM in
  xterm*|rxvt|(dt|k|E)term|rxvt-*)
    unset TMOUT
  ;;
esac

case $TERM in
    xterm*|rxvt|(dt|k|E)term|?rxvt-*)
	    # display user@host and full dir in *term title
	    precmd () { print -Pn  "\033]0;%n@%m %~\007" }
	    # display user@host and name of current process in *term title
	    preexec () { print -Pn "\033]0;%n@%m <$1> %~\007" }
	;;
    screen-*)
        # Set screen's window title to the command the user typed.
        preexec() { print -n '\ek'$1'\e\\' }
        # Restore a generic title if no program is running.
        precmd() { print -n '\ek'$HOST:$PWD'\e\\' }
        PROMPT_COMMAND='echo -ne "\033k$HOSTNAME\033\\"'
    ;;
esac

# Enable flow control, prevents ^k^s from locking up my screen session.
stty -ixon
################################################################################
#       Default Aliases
################################################################################
alias xlog="sudo grep --binary-files=without-match --color -nsie '(EE)' -e '(WW)' /var/log/Xorg.0.log"
alias which="whence"
alias sd='export DISPLAY=:0.0'
alias cpan="perl -MCPAN -e shell"
alias cup='cvs -z3 update -Pd'
alias mv='nocorrect /bin/mv'
alias rm='nocorrect /bin/rm -i'
alias shred='nocorrect ionice -c3 /usr/bin/shred -fuz'
alias wipe='nocorrect ionice -c3 /usr/bin/wipe -l2 -v -x -r'
alias man='nocorrect man'
alias mkdir='nocorrect /bin/mkdir -p'
alias find='noglob find'
alias wget="wget -c"
alias lsd='ls -Fld *(-/DN)'
alias weather="/home/gregf/code/bin/forecast/forecast.rb"
alias ncmpc="ncmpc -c"
alias fixdbus="sudo -s dbus-uuidgen --ensure"
alias wp="feh --bg-scale"
alias m="nice -n1 mplayer -af volnorm -stop-xscreensaver"
alias ml="nice -n1 mplayer -loop 0 -af volnorm -stop-xscreensaver"
alias e="gvim"
alias v="vim"
alias t="thunar"
alias p="nice -n 19 paludis"
alias repo='cd /var/paludis/repositories'
alias scm='cd /home/gregf/code/scm/'
alias sc='script/console'
alias ss='script/server'
alias sp='script/plugin'
alias db='script/dbconsole'
alias sgmo="sg model $@"
alias sgmi="sg migration $@"
alias setmid='rm -f db/*.sqlite3 ; rake db:migrate && rake admin:create'
alias a='autotest -rails'
alias tlog='tail -f log/development.log'
alias rst='touch tmp/restart.txt'
alias scaffold='script/generate shoulda_scaffold'
alias migrate='rake db:migrate db:test:clone'
alias gi='sudo gem install --no-ri --include-dependencies'
alias giti="vim .gitignore"
alias gs='gem search -b'
alias g='gthumb'
alias vmware='VMWARE_USE_SHIPPED_GTK=yes /opt/vmware/workstation/bin/vmware'
alias burniso='wodim -v dev=/dev/cdrw'
alias burndvdiso='growisofs -speed=8 -dvd-compat -Z /dev/dvdrw=$1'
alias usepretend='sudo paludis -ip --dl-reinstall if-use-changed'
alias usedo='sudo paludis -i --dl-reinstall if-use-changed'
alias ketchup='ketchup -G'
alias installed="eix -I --nocolor -c > /tmp/installed.txt && most /tmp/installed.txt && rm /tmp/installed.txt"
alias biosinfo='sudo dmidecode'
alias pwgen='pwgen -sBnc 10'
alias la="ls -a"
alias l="ls"
alias f-spot='dbus-launch f-spot'
alias d="devtodo -A"
alias zkbd="zsh /usr/share/zsh/4.3.4/functions/Misc/zkbd"
alias gnp="git-notpushed"
alias s="sudo"
alias k="killall"
alias cap='/usr/X11R6/bin/cap'
alias poweroff='sudo poweroff'
alias reboot='sudo reboot'
alias oa='openarena'
alias savage="~/code/bin/savage/savage.sh"
alias wcyy="mplayer http://68.142.81.164:80/citadelcc_WCYY_FM\?MSWMExt\=.asf"
alias wkit="mplayer http://64.92.199.73/WKIT-FM"
alias dropcache='sudo echo 3 > /proc/sys/vm/drop_caches'
alias deploy='cap deploy:migrations'
alias lock='alock -auth pam -bg blank:color=black'
alias paludis='nice -n 19 paludis'
alias lsnoext="ls | grep -v '\.'"
alias cleanliferea="sqlite3 ~/.liferea_1.4/liferea.db vacuum"
alias urb='urbanterror +connect 208.43.15.167:27960'
alias starcraft=' wine ~/.wine/drive_c/Program\ Files/Starcraft/StarCraft.exe'
alias ipager='k ipager; sleep 1; ipager &'
alias devilspie='k devilspie; sleep 1; devilspie &'
alias gis="git status | grep --color=always '^[^a-z]\+\(new file:\|modified:\)' | cut -d'#' -f2-"
alias latestkernel='lynx -dump http://kernel.org/kdist/finger_banner'
alias update-eix='ionice -c3 update-eix'
alias dosbox='dosbox -conf ~/.dosbox.conf -fulscreen'
alias ports='netstat --inet -pln'
################################################################################
# Functions and Completion
################################################################################
# Magically quote urls
autoload -U url-quote-magic
zle -N self-insert url-quote-magic

compctl -g '*(-/)' + -g '.*(-/)' -v cd pushd rmdir dirs chdir

# completion for "man" by Gossamer  980827
compctl -f -x 'S[1][2][3][4][5][6][7][8][9]' -k '(1 2 3 4 5 6 7 8 9)' \
  - 'R[[1-9nlo]|[1-9](|[a-z]),^*]' -K 'match-man' \
  - 's[-M],c[-1,-M]' -g '*(-/)' \
  - 's[-P],c[-1,-P]' -c \
  - 's[-S],s[-1,-S]' -k '( )' \
  - 's[-]' -k '(a d f h k t M P)' \
  - 'p[1,-1]' -c + -K 'match-man' \
  -- man

if [ -f ~/.ssh/known_hosts ]; then
    local _myhosts
    _myhosts=( ${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[0-9]*}%%\ *}%%,*} )
    zstyle ':completion:*' hosts $_myhosts
fi

################################################################################
# Custom Functions
################################################################################

function clamscan {
    mkdir -p /tmp/virus
    nice -n 19 clamscan --verbose --max-recursion=20 -m -i --detect-pua --move=/tmp/virus -r
    cd /tmp/virus
    echo -n "Viruses:"
    ls
}

function qemiso {
    kvm -boot d -m 384 -smp 2 -localtime \
    -net nic \
    -net user \
    -no-fd-bootchk -no-acpi -cdrom $1
}

function qemimg {
    qemu-img create -f qcow2 $i.qcow 10G
}

function qeminst {
    echo "Supply disk.img and path to iso or drive for installing from"
    kvm -boot d -m 384 -smp 2 -localtime \
    -net nic \
    -net user \
    -no-fd-bootchk -no-acpi \
    -hda ~/qemu/$1.qcow -cdrom $2
}

function qemrun {
    echo "Supply drive image to boot from"
    kvm -boot c -m 512 -smp 2 -localtime \
    -net nic \
    -net user \
    -no-fd-bootchk -no-acpi -hda ~/qemu/$1.qcow
}

function pskill {
	kill -9 `pgrep $1`
	echo "slaughtered."
}


function kscreen {
    echo -ne "\017"
    reset
}

function spell { echo "$@" | aspell -a }

function calc() { echo "$*" | bc; }

function date {
    if [ $# = 0 ]; then
        # format: saturday, december 21, 2002 06:46:38 pm est
        command date +"%a, %b %e %Y %I:%M:%S%P %Z"
    else
        # execute real `date'
        command date $@
    fi
}

#function to clean up web permissions.
# TODO Move this to a script
function fixwww {
DIRPERM="0755"
FILEPERM="0644"
USER="gregf"
GROUP="gregf"
echo " * Are you in the correct directory? (yes/no)"
pwd
 read input
  if [ "$input" = "yes" ]; then
    echo " ! Fixing up WWW Permissions"
    echo "   Fixing File Permissions"
    find . -type f -print0 | xargs -0 chmod $FILEPERM
	  find . -type f -print0 | xargs -0 chown $USER:$GROUP
    echo "   Fixing Directory Permissions"
    find . -type d -print0 | xargs -0 chmod $DIRPERM
	  find . -type d -print0 | xargs -0 chown $USER:$GROUP
     echo " * done."
  else
     echo " ! Invalid response."
 fi
}

function mcdrom {
    local mounted
    local cpwd
    mounted=$(grep cdrom /etc/mtab)
    if [[ $mounted = "" ]];then
        mount /cdrom
        echo "-- mounted cdrom --"
        cd /cdrom ; ls
        else
        cpwd=$(pwd|grep cdrom)
        if [[ $cpwd = "" ]];then
            umount /cdrom
            echo "-- umounted cdrom --"
            else
            cd;umount /cdrom
            echo "-- umounted cdrom --"
            pwd
            eject
        fi
    fi
}

function extractrar {
    FILE=$(basename "$1" .rpm)
    rpm2tar "$1"
    mkdir -p ${FILE}
    tar -xvf ${FILE}.tar -C ${FILE}
}

function createlzma {
    lzma -q -9 $1
}

function extractlzma {
    TARFILE=$(basename $1 .lzma)
    unlzma $1
    mkdir -p ${TARFILE}
    tar xvf $TARFILE.tar -C ${TARFILE}
}

function open {
        if [[ -f "$1" ]]
        then
                case "$1" in
                      (*.tar.bz2) tar -xvjf "$1" ;;
                      (*.tar.gz) tar -xvzf "$1" ;;
                      (*.ace) unace e "$1" ;;
                      (*.rar) unrar x "$1" ;;
                      (*.deb) ar -x "$1" ;;
                      (*.bz2) bzip2 -d "$1" ;;
                      (*.lzh) lha x "$1" ;;
                      (*.gz) gunzip -d "$1" ;;
                      (*.tar) tar -xvf "$1" ;;
                      (*.rpm) extractrar "$1" ;;
                      (*.lzma) extractlzma "$1" ;;
                      (*.tgz) gunzip -d "$1" ;;
                      (*.tbz2) tar -jxvf "$1" ;;
                      (*.zip) unzip "$1" ;;
                      (*.Z) uncompress "$1" ;;
                      (*.shar) sh "$1" ;;
                      (*) echo "'"$1"' Error. Please go away" ;;
                esac
        else
                echo "'"$1"' is not a valid file"
        fi
}

function mps { /bin/ps $@ -u $USER -o pid,%cpu,%mem,command ; }

function ech {
    CHPTH=`eix --only-names -e $1`
    most /usr/portage/$CHPTH/ChangeLog
}

function junk {
    scp -r $* web:~/blog/shared/stuff/
}

function dotfile {
    scp -r $* web:~/blog/shared/dotfiles/
}

function pcache {
    paludis --regenerate-installable-cache
    paludis --regenerate-installed-cache
}

function isocdrom {
    dd if=/dev/cdrom of=$1 bs=2048
}

function fix-paludis-perms {
    mkdirs=(/usr/portage/.cache/names /usr/portage/distfiles /var/tmp/paludis /etc/paludis)
    fixdirs=(/usr/portage /var/tmp/paludis /etc/paludis)
    repodir="/var/paludis/repositories"
    for dir in $mkdirs;
        do
            mkdir -p $dir
        done
    for dir in $fixdirs;
        do
            chgrp -R paludisbuild $dir
            find $dir -type d -exec chmod g+wrx {} \;
        done
    cd $repodir
    if [ $? == 0 ]; then
        for repo in `ls --color=never -Fd *(-/DN)`;
            do
                    mkdir -p $repo/.cache/names
                    chgrp -R paludisbuild $repo
                    find $repo -type d -exec chmod g+wrx {} \;
            done
    fi
    pcache # see function above
}

function confcat {
    grep -vh '^[[:space:]]*#' "$@" | grep -v '^$'
}

function sg {
  local last_path=`script/generate $@ | awk '/db\/migrate\//{print $NF}'`
  if [ $last_path ]; then $EDITOR $last_path && rake db:migrate; fi
}

function http_headers {
    curl='whence curl'
    curl -I -L $@ ;
}

function mktar {
    tar jcvf "${1%%/}.tar.bz2" "${1%%/}/"
}

function sanitize {
  chmod -R u=rwX,go=rX "$@"
  chown -R ${USER}:users "$@"
}

# Stolen:
# http://cinderwick.ca/files/configs/bashrc

# remindme - a simple reminder
# usage: remindme