+function preexec() { # Hook
+ if { _we_are_in_gnu_screen } then
+ # Set the window title of GNU Screen.
+ local cmd=${1[(wr)^(*=*|sudo|nice|env|time|ssh|-*|[0-9]*)]}
+ printf "\ek$cmd\e\\"
+ fi
+}
+
+function precmd() { # Hook
+ local dirinfo="$(print_directory_info $(pwd))"
+
+ case $dirinfo in
+ "")
+ RPROMPT='%U%~%u';;
+ *)
+ RPROMPT="%U%~%u %U($dirinfo)%u";;
+ esac
+}
+
+function print_directory_info() {
+ local absdir=$1
+
+ if [[ -r $absdir/.git ]] then
+ # Git
+ git branch --no-color 2>/dev/null \
+ | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
+
+ elif [[ -r $absdir/.hg/branch ]] then
+ # Hg
+ local hgbranch=`cat $absdir/.hg/branch`
+ if [[ -e $absdir/.hg/unpushed ]] then
+ echo "*${hgbranch}*"
+ else
+ echo "$hgbranch"
+ fi
+
+ elif [[ -d $absdir/_darcs ]] then
+ # Darcs
+ basename $absdir
+
+ elif [[ -r $absdir/CVS/Repository ]] then
+ # CVS
+ cat $absdir/CVS/Repository \
+ | sed -e 's!\([^/]*\).*!\1!'
+
+ else
+ # Unknown.
+ if [[ $absdir = "/" ]] then
+ # This is the root directory so exit from the recursion.
+ echo
+ else
+ # Recurse to the parent dir.
+ print_directory_info $(dirname $absdir)
+ fi
+ fi
+}
+
+function ppgrep() {
+ local -a percol_opts
+
+ if [[ $# -gt 0 ]]; then
+ percol_opts=("--query" "$1")
+ fi
+
+ ps aux | percol "${percol_opts[@]}" | awk '{ print $2 }'
+}
+
+function ppkill() {
+ local -a ppgrep_opts
+
+ if [[ $1 =~ "^[^-]" ]]; then
+ ppgrep_opts=("$1")
+ shift
+ fi
+
+ ppgrep "${ppgrep_opts[@]}" | xargs kill "$@"
+}
+
+function percol_select_history() {
+ local -a tac_cmd
+
+ if which gtac >&/dev/null; then
+ tac_cmd=("gtac")
+ elif which tac >&/dev/null; then
+ tac_cmd=("tac")
+ else
+ tac_cmd=("cat")
+ fi
+
+ local selected
+ selected=$("${tac_cmd[@]}" "$HISTFILE" | \
+ sed 's/^: [0-9]*:[0-9]*;//' | \
+ percol --match-method regex --query "$LBUFFER")
+ if [[ $? -eq 0 ]]; then
+ BUFFER="$selected"
+ CURSOR="$#BUFFER" # move cursor to the end of line
+ zle redisplay
+ #zle accept-line # uncomment this to execute the selected one immediately.
+ else
+ zle redisplay
+ fi
+}
+if which percol >&/dev/null; then
+ zle -N percol_select_history
+ bindkey '^R' percol_select_history
+fi