# Zsh configuration file.
-# Copyright (C) 2011-2012 Simon Ruderich
+# Copyright (C) 2011-2013 Simon Ruderich
#
# This file is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
source_debug '. ~/.zsh/rc'
+# Warn when creating global variables from inside a function. Needs to be set
+# before declaring a function.
+setopt warn_create_global
+
+
# HELPER FUNCTIONS
# Return the name of the program which is called in the foreground with `fg`.
# Use Vi(m) style key bindings.
bindkey -v
-# Use jj and jk to exit insert mode.
-bindkey 'jj' vi-cmd-mode
+# Use jk to exit insert mode (jj is too slow).
bindkey 'jk' vi-cmd-mode
# I don't need the arrow keys, I use ^N and ^P for this (see below).
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)"
+ "(${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)"
+ "(${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)"
+ "(${green}%b${default}:${blue}%s${default})"
zstyle ':vcs_info:*' actionformats \
- "($green%b$default/$red%a$default:$blue%s$default)"
+ "(${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
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
vcs_info
else
vcs_info_msg_0_=
+ vcs_info_msg_1_=
fi
# Setup. Create variables holding the formatted content.
# Current directory in yellow, truncated if necessary (WIDTH is replaced
# below).
local directory="${yellow}%WIDTH<..<%~%<<${default}"
+ # Minimal information about the VCS, only displayed if there are
+ # unstaged/staged changes.
+ local vcs_staged="${vcs_info_msg_1_}"
# Information about the VCS in this directory.
local vcs="${vcs_info_msg_0_}"
# Combine them to create the prompt.
+ local top_left="${vcs_staged}"
local top_right="${vcs}(${seconds})"
local width_top_prefix=${#${(S%%)top_prefix//$~zero/}}
+ local width_top_left=${#${(S%%)top_left//$~zero/}}
local width_top_right=${#${(S%%)top_right//$~zero/}}
# Calculate the maximum width of ${top_left}. -2 are the braces of
# ${top_left}, -1 is one separator from ${top_separator} (we want at least
# one between left and right parts).
local top_left_width_max=$((
- COLUMNS - $width_top_prefix - 2 - 1 - $width_top_right
+ COLUMNS - $width_top_prefix
+ - $width_top_left - 2
+ - 1
+ - $width_top_right
))
# Truncate directory if necessary.
- local top_left="(${directory/WIDTH/${top_left_width_max}})"
- local width_top_left=${#${(S%%)top_left//$~zero/}}
+ top_left="(${directory/WIDTH/${top_left_width_max}})${top_left}"
+ width_top_left=${#${(S%%)top_left//$~zero/}}
# Calculate the width of the top prompt to fill the middle with "-".
local width=$((
# Is set to a non empty value to reset the window name in the next
# precmd() call.
zshrc_window_reset=yes
- # Is set to a non empty value when the shell is running as root.
- if [[ $UID -eq 0 ]]; then
- zshrc_window_root=yes
- fi
zshrc_window_preexec() {
# Get the program name with its arguments.
# Add an exclamation mark at the beginning if running with sudo or if
# running zsh as root.
- if [[ -n $program_sudo || -n $zshrc_window_root ]]; then
+ if [[ -n $program_sudo || $UID -eq 0 ]]; then
program_name=!$program_name
fi
fi
# Prepend prefixes like in window_preexec().
- if [[ -n $zshrc_window_root ]]; then
+ if [[ $UID -eq 0 ]]; then
name="!$name"
fi
if [[ -n $SSH_CONNECTION ]]; then
}
+# RESTART SETTINGS
+
+zmodload -F zsh/stat b:zstat
+
+# Remember startup time. Used to perform automatic restarts when ~/.zshrc is
+# modified.
+zshrc_startup_time=$EPOCHSECONDS
+
+# Automatically restart Zsh if ~/.zshrc was modified.
+zshrc_restart_precmd() {
+ local stat
+ if ! zstat -A stat +mtime ~/.zshrc; then
+ return
+ fi
+
+ # ~/.zshrc wasn't modified, nothing to do.
+ if [[ $stat -le $zshrc_startup_time ]]; then
+ return
+ fi
+
+ local startup
+ strftime -s startup '%Y-%m-%d %H:%M:%S' "$zshrc_startup_time"
+
+ echo -n "${fg[magenta]}"
+ echo -n "~/.zshrc modified since startup ($startup) ... "
+ echo -n "${fg[default]}"
+
+ if [[ -n $zshrc_disable_restart ]]; then
+ echo 'automatic restart disabled.'
+ return
+ fi
+
+ # Don't exec if we have background processes because execing will let us
+ # lose control over them.
+ if [[ ${#${(k)jobstates}} -ne 0 ]]; then
+ echo 'active background jobs!'
+ return
+ fi
+
+ # Try to start a new interactive shell. If it fails, something is wrong.
+ # Don't kill our current session by execing it.
+ zsh -i -c 'exit 42'
+ if [[ $? -ne 42 ]]; then
+ echo -n "${fg_bold[red]}"
+ echo 'failed to start new zsh!'
+ echo -n "${fg_bold[default]}"
+ return
+ fi
+
+ echo 'restarting zsh.'
+ echo
+
+ exec zsh
+}
+precmd_functions+=(zshrc_restart_precmd)
+
+
source_debug '. ~/.zsh/rc (done)'
# vim: ft=zsh