+autoload -Uz colors && colors
+
+# Necessary for $EPOCHSECONDS, the UNIX time.
+zmodload zsh/datetime
+
+# Some shortcuts for colors. The %{...%} tells zsh that the data in between
+# doesn't need any space, necessary for correct prompt drawing.
+local red="%{${fg[red]}%}"
+local blue="%{${fg[blue]}%}"
+local green="%{${fg[green]}%}"
+local yellow="%{${fg[yellow]}%}"
+local default="%{${fg[default]}%}"
+
+# vcs_info was added in 4.3.9 but it works in earlier versions too. So load it
+# if the necessary files are available in ~/.zsh/functions/vcs_info (often a
+# symbolic link to current checkout of Zsh's sources).
+if [[ $ZSH_VERSION == (4.3.<9->*|4.<4->*|<5->*) ||
+ -d ~/.zsh/functions/vcs_info ]]; then
+ # Update fpath to allow loading the vcs_info functions.
+ if [[ -d ~/.zsh/functions/vcs_info ]]; then
+ fpath=(~/.zsh/functions/vcs_info/
+ ~/.zsh/functions/vcs_info/Backends
+ $fpath)
+ fi
+
+ # Load vcs_info to display information about version control repositories.
+ autoload -Uz vcs_info
+ # Only look for certain VCS.
+ zstyle ':vcs_info:*' enable git
+ # Check the repository for changes so they can be used in %u/%c (see
+ # below). This comes with a speed penalty for bigger repositories.
+ zstyle ':vcs_info:*' check-for-changes yes
+
+ # Set style of vcs_info display. The current branch (green) and VCS (blue)
+ # is displayed. If there is an special action going on (merge, rebase)
+ # it's also displayed (red). Also display if there are unstaged or staged
+ # (%u/%c) changes.
+ if [[ $ZSH_VERSION == (4.3.<11->*|4.<4->*|<5->*) ||
+ -d ~/.zsh/functions/vcs_info ]]; then
+ zstyle ':vcs_info:*' formats \
+ "(${green}%b%u%c${default}:${blue}%s${default}%m)" \
+ "${green}%u%c${default}"
+ zstyle ':vcs_info:*' actionformats \
+ "(${green}%b%u%c${default}/${red}%a${default}:${blue}%s${default}%m)" \
+ "${green}%u%c${default}"
+ else
+ # In older versions %u and %c are not defined yet and are not
+ # correctly expanded.
+ zstyle ':vcs_info:*' formats \
+ "(${green}%b${default}:${blue}%s${default})"
+ zstyle ':vcs_info:*' actionformats \
+ "(${green}%b${default}/${red}%a${default}:${blue}%s${default})"
+ fi
+ # Set style for formats/actionformats when unstaged (%u) and staged (%c)
+ # changes are detected in the repository; check-for-changes must be set to
+ # true for this to work. Thanks to Bart Trojanowski
+ # (http://jukie.net/~bart/blog/pimping-out-zsh-prompt) for the idea
+ # (2010-03-11 00:20 CET).
+ zstyle ':vcs_info:*' unstagedstr '¹'
+ zstyle ':vcs_info:*' stagedstr '²'
+
+ # Default to run vcs_info. If possible we prevent running it later for
+ # speed reasons. If set to a non empty value vcs_info is run.
+ zshrc_force_run_vcs_info=1
+
+ # Cache system inspired by Bart Trojanowski
+ # (http://jukie.net/~bart/blog/pimping-out-zsh-prompt).
+ zstyle ':vcs_info:*+pre-get-data:*' hooks pre-get-data
+ +vi-pre-get-data() {
+ # Only Git and Mercurial support and need caching. Abort if any other
+ # VCS is used.
+ [[ "$vcs" != git && "$vcs" != hg ]] && return
+
+ # If the shell just started up or we changed directories (or for other
+ # custom reasons) we must run vcs_info.
+ if [[ -n $zshrc_force_run_vcs_info ]]; then
+ zshrc_force_run_vcs_info=
+ return
+ fi
+
+ # Don't run vcs_info by default to speed up the shell.
+ ret=1
+ # If a git/hg command was run then run vcs_info as the status might
+ # need to be updated.
+ case "$(fc -ln $(($HISTCMD-1)))" in
+ git* | g\ *)
+ ret=0
+ ;;
+ hg*)
+ ret=0
+ ;;
+ esac
+ }
+
+ # Display number of WIP stashes (this excludes manually named commits
+ # which might be used for something else), thanks to
+ # http://eseth.org/2010/git-in-zsh.html (viewed on 2013-04-27) for the
+ # idea to display the stash count.
+ function +vi-git-stashes() {
+ if [[ -s ${hook_com[base]/.git/refs/stash} ]]; then
+ local -a stashes
+ # Thanks to Valodim in #zsh on Freenode (2013-07-01 14:14 CEST)
+ # for the solution to "grep" the output with (M) and :#(...).
+ stashes=( ${(M)${(f)"$(git stash list 2>/dev/null)"}:#(*WIP*)} )
+
+ if [[ ${#stashes} -gt 0 ]]; then
+ hook_com[misc]+=" ${yellow}${#stashes}s${default}"
+ fi
+ fi
+ }
+
+ # Apply hooks to Git.
+ zstyle ':vcs_info:git*+set-message:*' hooks git-stashes
+
+ # Must run vcs_info when changing directories.
+ prompt_chpwd() {
+ zshrc_force_run_vcs_info=1
+ }
+ chpwd_functions+=(prompt_chpwd)
+
+ # Used by prompt code below to determine if vcs_info should be run.
+ zshrc_use_vcs_info=1
+else
+ zshrc_use_vcs_info=
+fi
+
+typeset -a zshrc_longrun_data
+zshrc_longrun_data=()
+# Display runtime in seconds for long running programs (> 60 seconds) and send
+# a bell to notify me.
+zshrc_longrun_preexec() {
+ local program="$3"
+
+ # Handle fg.
+ local REPLY
+ zshrc_resolve_fg_to_resumed_job_name "$program"
+ program="$REPLY"
+
+ # Don't track the time for certain (possible) long running processes which
+ # need no automatic notification.
+ for ignore in elinks man mutt vim; do
+ case $program in
+ $ignore | $ignore\ *)
+ zshrc_longrun_data=()
+ return
+ ;;
+ esac
+ done
+
+ zshrc_longrun_data=("$program" $EPOCHSECONDS)
+}
+zshrc_longrun_precmd() {
+ # No previous timestamp available or disabled for this command, ignore.
+ if [[ -z $zshrc_longrun_data ]]; then
+ return
+ fi
+
+ local difference=$(( EPOCHSECONDS - zshrc_longrun_data[2] ))
+ if [[ $difference -gt 60 ]]; then
+ echo
+ echo -n "${fg[yellow]}"
+ echo -n "~> ${(V)zshrc_longrun_data[1]} took $difference seconds."
+ echo -n "${fg[default]}"
+ echo "\a" # send bell
+ fi
+
+ # Clear status. Prevents displaying old status information when pressing
+ # enter with an empty command line.
+ zshrc_longrun_data=()
+}
+preexec_functions+=(zshrc_longrun_preexec)
+precmd_functions+=(zshrc_longrun_precmd)