AUDIO_PLAYER="open"
VIDEO_PLAYER="open"
ARCHIVE_FORMATS=(7z bz2 gz xz tar tbz tbz2 tgz txz tar.gz tar.bz2 tar.xz rar zip)
AUDIO_PLAYER_FORMATS=(aac flac it m4a mod mp3 mpc ogg sid spc wma wv xm)
VIDEO_PLAYER_FORMATS=(avi divx flv mkv mov mpeg mp4 mpg ogm wmv)
EDITOR="vim"
PAGER="less"
GREP=""
GREP_COLOUR="auto"
FGREP=""
LS="ls"
LS_COLOUR="auto"
LS_ON_CD="yes"
LS_ON_INIT="no"
LS_COLORS='di=34:ow=44;37:tw=44;37:st=44;37:ex=31:su=7;31:sg=7;31:ln=33:or=7;33:pi=32:do=32:bd=35:cd=35:so=32:*.bz2=36:*.dmg=36:*.gz=36:*.rar=36:*.xz=36:*.zip=36:*.orig=90:*~=90'
FEMALE="no"	# Whether the user is female ;)
LISTMAX=32768
WORDCHARS="*?[];!#~"

bindkey -v
# history-incremental-search-backward is much more useful than
# _history-complete-*
bindkey "^R" history-incremental-search-backward
# Add a few other emacs bindings for convenience, as it's sometimes quicker to
# use those than to switch between modes.
bindkey "^A" beginning-of-line
bindkey "^E" end-of-line
bindkey "^D" delete-char-or-list
# backward-kill-word behaves more like vim, whereas vi-backward-kill-word is
# the vi behaviour.
bindkey "^W" backward-kill-word

setopt no_bg_nice
setopt no_nomatch
setopt extended_glob
setopt autocd
setopt auto_pushd

autoload -U compinit
compinit -C

# Ignore case on completion
zstyle ':completion:*' matcher-list 'm:{A-Z}={a-z} m:{a-z}={A-Z}'

if [ "$GREP" -o "$GREP_COLOUR" ]; then
	[ -z "$GREP" ] && GREP="grep"
	[ ! -z "$GREP_COLOUR" -a "$GREP_COLOUR[1]" != "-" ] && \
		GREP_COLOUR="--colour=$GREP_COLOUR"
	alias grep="$GREP $GREP_COLOUR"
fi

if [ "$FGREP" -o "$GREP_COLOUR" ]; then
	[ -z "$FGREP" ] && FGREP="fgrep"
	[ ! -z "$GREP_COLOUR" -a "$GREP_COLOUR[1]" != "-" ] && \
		GREP_COLOUR="--colour=$GREP_COLOUR"
	alias fgrep="$FGREP $GREP_COLOUR"
fi

if [ "$LS" -o "$LS_COLOUR" ]; then
	[ -z "$LS" ] && LS="ls"
	[ ! -z "$LS_COLOUR" -a "$LS_COLOUR[1]" != "-" ] && \
		LS_COLOUR="--color=$LS_COLOUR"
	alias ls="$LS $LS_COLOUR"
fi

if [ "$LS_ON_CD" -a "$LS_ON_CD" != "no" ]; then
	chpwd() {
		ls
	}
fi

if [ "$TERM_PROGRAM" = "Apple_Terminal" ]; then
	update_terminal_cwd() {
		printf '\e]7;%s\a' "file://$HOSTNAME${PWD// /%20}"
	}
else
	update_terminal_cwd() {
	}
fi

set_prompt() {
	if [ "$FEMALE" = "yes" ]; then
		PS1="%m:%c$@%B%(!.#.%{$(print "\e[1;35m")%}♥"
		PS1+="%{$(print "\e[0m")%})%b "
	else
		PS1="%m:%c$@%B%(!.#.$)%b "
	fi
	PS2="%B>%b "
	RPS1="%(1j.%{$(print "\e[1;33m")%}%j%{$(print "\e[0m")%}.)"
	RPS1+="%(?..%(1j. .)%{$(print "\e[1;31m")%}%?%{$(print "\e[0m")%})"
	RPS2="%(1_.%{$(print "\e[1;30m")%}(%_%)%{$(print "\e[0m")%}.)"
}
set_prompt

_precmd() {
	update_terminal_cwd

	local branch_name
	branch_name="$(git symbolic-ref HEAD 2>/dev/null)"
	branch_name="${branch_name##refs/heads/}"
	if [ ! -z "$branch_name" -a "$branch_name" != "master" ]; then
		local branch
		branch="%{$(print "\e[1;30m")%}("
		branch+="%{$(print "\e[0;36m")%}$branch_name"
		branch+="%{$(print "\e[1;30m")%})%{$(print "\e[0m")%}"
		set_prompt "$branch"
	else
		set_prompt
	fi
}

case $TERM in
	aterm|Eterm|rxvt*|uxterm*|xterm*)

		# Use set_title if you want to change the term title
		set_title() {
			# Without this, precmd would override it
			precmd() {
				_precmd
			}
			print -Pn "\e]0;$@\a"
		}

		unset_title() {
			precmd() {
				print -Pn "\e]0;%m\a"
				_precmd
			}
		}

		unset_title
		;;
	*)
		precmd() {
			_precmd
		}
		;;
esac

extr() {
	for i in $@; do
		case $i in
			*.7z)
				7za x $i
				;;
			*.tar)
				tar xfv $i
				;;
			*.tbz | *.tbz2 | *.tar.bz2)
				bzcat $i | tar xfv -
				;;
			*.tgz | *.tar.gz)
				gzcat $i | tar xfv -
				;;
			*.txz | *.tar.xz)
				xzcat $i | tar xfv -
				;;
			*.rar)
				unrar x $i
				;;
			*.zip)
				unzip $i
				;;
			# These have to be the last for obvious reasons
			*.bz2)
				bunzip2 $i
				;;
			*.gz)
				gunzip $i
				;;
			*.xz)
				unxz $i
				;;
			*)
				echo "$i: Unknown file type"
				false
				;;
		esac
	done
}

sprunge() {
	curl -F "sprunge=<-" http://sprunge.us
}

for ext in $ARCHIVE_FORMATS; do alias -s $ext=extr; done

[ "$AUDIO_PLAYER" ] && \
	for ext in $AUDIO_PLAYER_FORMATS; do alias -s $ext=$AUDIO_PLAYER; done

[ "$VIDEO_PLAYER" ] && \
	for ext in $VIDEO_PLAYER_FORMATS; do alias -s $ext=$VIDEO_PLAYER; done

[ "$LS_ON_INIT" = "yes" ] && ls

unset AUDIO_PLAYER VIDEO_PLAYER
unset ARCHIVE_FORMATS AUDIO_PLAYER_FORMATS VIDEO_PLAYER_FORMATS
unset FGREP GREP GREP_COLOUR LS LS_COLOUR LS_ON_CD LS_ON_INIT
export EDITOR LS_COLORS
