# {{{ Keybindings
# a function to
# Use ^v to find out the keynames

man-command ()
{
	if [ -n "$BUFFER" ]
	then
		local FirstArg=`echo $BUFFER | awk '{print $1}'`
		man $FirstArg
	fi
}

sudo-command ()
{
	if [ -n "$BUFFER" ]
	then
		local FirstArg=`echo $BUFFER | awk '{print $1}'`
		if [ "$FirstArg" != "sudo" ]
		then
			BUFFER="sudo $BUFFER"
			zle end-of-line
		fi
	fi
}

move-pointer-to-next-arg ()
{
	if [ -n "$BUFFER" ]
	then
			CURSOR=`echo | awk '
			{ curpos=5; arg=$BUFFER; orig=arg;
			arg=substr(arg, curpos);
			if (substr(arg, 1, 1) == "-") arg=substr(arg, 2);
			curpos+=index(arg, "-");
			print curpos; }'`
	fi
}

move-pointer-after-command ()
{
	if [ -n "$BUFFER" ]
	then
		local LenOfFirstArg=`echo $BUFFER | awk '{ print length($1) }'`
		CURSOR=$LenOfFirstArg
	fi
}

add-accept-keywords ()
{
	if [ -n "$BUFFER" ]
	then
		BUFFER="ACCEPT_KEYWORDS=\"\" $BUFFER"
		CURSOR=17
	fi
}

zle -N man-command				# shows the man-page of the written command
zle -N sudo-command				# writes "sudo " in front of the buffer
zle -N move-pointer-after-command	# moves the pointer between command and first parameter
zle -N move-pointer-to-next-arg
zle -N add-accept-keywords

#bindkey -v # vi key bindings
bindkey -e # emacs key bindings
bindkey '^[[2~' overwrite-mode		# [Insert]
bindkey '^[[3~' delete-char		# [Delete]
bindkey '^[[5~' up-line-or-history	# [Page Up]
bindkey '^[[6~' down-line-or-history	# [Page Down]
bindkey '^[[1~' beginning-of-line	# [Begin]
bindkey '^[[4~' end-of-line		# [End]
bindkey '^[[A'	history-search-backward	# [Up]
bindkey '^[[B'	history-search-forward	# [Down]
bindkey '^[Oa'	history-incremental-search-backward # [Ctrl+Up]
bindkey '^[Ob'	history-incremental-search-forward  # [Ctrl+Down]
bindkey  '^[Oc'	 move-pointer-to-next-arg
bindkey '^[Od'  move-pointer-after-command
bindkey '^Z'	which-command
bindkey '^N'	man-command
bindkey '^S'	sudo-command
bindkey '^Y'	add-accept-keywords
bindkey '^U'	kill-whole-line
bindkey '^Q'	push-input
bindkey '^P'	yank
bindkey '^f'    forward-word
bindkey '^b'    backward-word

# Unbind c-s and c-q
stty -ixon

#}}}

# {{{ aliases and functions
#funktions

# help for aliases
aliashelp()
{
	echo '
	ALIAS			DESCRIPTION
	 cl		 goto and list directory
	 leo		 search/translate per leo with '$BROWSER'
	 wikipedia	 search per wikipedia with '$BROWSER'
	 disassemble	 disassemble given files
	 da		 Show the diskusage of the file/directory
	 daL		 same as above but with symlink-dereferencing
	 j		 show the runned (background-)jobs
	 CM		 `./configure && make`
	 CMI		 `./configure && make && make install`
	 cl		 go to $1 and show the contents
	 ls		 `ls --color=always`
	 lh		 `ls -lh`
	 ls_sizesorted	 long self explaining name... thx to the tabcompletion ^^
	 lad		 only show dot-directories
	 lsa		 only show dot-files
	 lss		 only files with setgid/setuid/sticky flag
	 lsl		 only symlinks
	 lsx		 only executables
	 lsw		 world-{readable,writable,executable} files
	 lsbig		 display the biggest files
	 lsd		 only show directories
	 lse		 only show empty directories
	 lsnew		 display the newest files
	 lsold		 display the oldest files
	 lssmall	 display the smallest files
	 sortfast    http://www.commandlinefu.com/commands/view/10313/fastest-sort
	'
}

cl()			{ cd $1 && ls -a }					# cd and ls
leo()			{ ${BROWSER} "http://dict.leo.org/?search=$*" }	# gimmie leo results
wikipedia()		{ ${BROWSER} "http://de.wikipedia.org/wiki/Spezial:Search/${(C)1}" }
disassemble()	{ gcc -pipe -S -o - -O -g $* | as -aldh -o /dev/null }	# self-explaining
regexp() {
	pcre_compile $1 && \
	pcre_match $2 && echo "regex matches" || echo "regex does not match"
}

sleepuntil()	{	#TODO
	usage="Usage: $0 Stunden [Minuten [Sekunden]]\nBeispiel:\n $0 6\n(schlaeft bis um 6)"
	if [ "$1" = "" ]; then echo $usage
	else
		h="`date '+%H'`"
		m="`date '+%M'`"
		s="`date '+%S'`"

		if [ "$1" = "" ]; then echo $usage; exit 1; fi
		if [ "$2" = "" ]; then 2=0; fi
		if [ "$3" = "" ]; then 3=0; fi

		if [ $h -gt $1 ]; then
			echo "24-($1-$h)"
			let "hdiff=24-($1-$h)"
		else
			echo "$h-$1"
			let "hdiff=$h-$1"
		fi

		if [ $m -gt $2 ]; then
			echo "60-($m-$2)"
			let "mdiff=60-($m-$2)"
		else
			echo "$m-$2"
			let "mdiff=$m-$1"
		fi

		if [ $s -gt $3 ]; then
			echo "60-($s-$3)"
			let "sdiff=24-($s-$3)"
		else
			echo "$s-$3"
			let "sdiff=$s-$3"
		fi

		diff=$[$hdiff*3600+$mdiff*60+$sdiff]
		echo "... schlafe fuer ${diff}s (${hdiff}h${mdiff}m${sdiff}s) ..."
		sleep 1
	fi
}

test-for-ftp-mv ()
{
z=0
orig=$@
dateien=""
ordner=""
goal=""

while [ $# -gt 0 ]
do
	let z++
	if [ $# -ne 1 ]
	then
		[ -f "$1" ] && dateien="$dateien\n$1"
		[ -d "$1" ] && ordner="$ordner\n$1"
		[ -d "$1" -o -f "$1" ] || echo "\"$1\" existiert nicht und wird ignoriert!"
	else
		goal=$1
	fi
	shift
done

zmodload -i zsh/pcre
if pcre_compile "^/home/ftp" && pcre_match $goal
then
	for o in `echo -e $ordner`
	do
		sudo mv $o $goal
		sudo chmod 555 $goal$o
		sudo chown ftp:ftp $goal$o
	done
	for d in `echo -e $dateien`
	do
		sudo mv $d $goal
		sudo chmod 444 $goal`basename $d`
		sudo chown ftp:ftp $goal`basename $d`
	done
else
	arg=""
	for x in `echo -e $ordner $dateien $goal`
	do
		arg="$arg $x"
	done
	mv $arg
fi
}

# normal aliases
alias da='du -sch'
alias daL='du -schL'
alias j='jobs -l'
alias CM='./configure && make'
alias CMI='./configure && make && make install'
alias ls='ls --color=always'
alias lh='ls -lh'
alias ls_sizesorted='ls -lSrah'
alias lad='ls -d .*(/)'                # only show dot-directories
alias lsa='ls -a .*(.)'                # only show dot-files
alias lss='ls -l *(s,S,t)'             # only files with setgid/setuid/sticky flag
alias lsl='ls -l *(@[1,10])'           # only symlinks
alias lsx='ls -l *(*[1,10])'           # only executables
alias lsw='ls -ld *(R,W,X.^ND/)'       # world-{readable,writable,executable} files
alias lsbig="ls -flh *(.OL[1,10])"     # display the biggest files
alias lsd='ls -d ^.*(/)'               # only show directories
alias lse='ls -d *(/^F)'               # only show empty directories
alias lsnew="ls -drl *(Dom[1,10])"     # display the newest files and directories
alias lsold="ls -rtlh *(Dom[1,10])"    # display the oldest files and directories
alias lssmall="ls -Srl *(.oL[1,10])"   # display the smallest files
alias mtr="mtr -t"                     # a better traceroute (no gtk please)
alias screensaver='clear; for ((;;)); do echo -ne "\033[$((1+RANDOM%LINES));$((1+RANDOM%COLUMNS))H\033[$((RANDOM%2));3$((RANDOM%8))m$((RANDOM%10))"; sleep 0.1 ; done'  # credits to lkj@commandlinefu.com
alias sortfast="sort -S$(($(sed '/MemT/!d;s/[^0-9]*//g' /proc/meminfo)/1024-200)) --parallel=$(($(grep -c '^proc' /proc/cpuinfo)*2))"
alias gitpush="hipchat-notify.sh && git push $@"
#alias mv="test-for-ftp-mv";
# TODO!!!

# global aliases
alias -g BG='& exit'
alias -g C='|wc -l'
alias -g G='|grep'
alias -g H='|head'
alias -g Hl=' --help |& less -r'
alias -g L='|less'
alias -g LL='|& less -r'
alias -g M='|most'
alias -g N='&>/dev/null'
alias -g R='| tr A-z N-za-m'
alias -g SL='| sort | less'
alias -g S='| sort'
alias -g T='|tail'
# }}}

# vim: set foldmethod=marker tabstop=8
