+# Display possible log messages from ~/.xinitrc (if `xmessage` wasn't
+# installed). No race condition as xinitrc has finished before a shell is
+# executed and only one shell is started on login.
+if [[ -f ~/.xinitrc.errors ]]; then
+ echo "${fg_bold[red]}xinitrc failed!${fg_bold[default]}"
+ echo
+ cat ~/.xinitrc.errors
+ rm ~/.xinitrc.errors
+ echo
+fi
+
+# Run the following programs every 4 hours (and when zsh starts).
+PERIOD=14400
+periodic() {
+ # Display fortunes.
+ (( $+commands[fortune] )) && fortune -ac "${zshrc_fortune_arguments[@]}"
+ # Display reminders.
+ (( $+commands[rem] )) && [[ -f ~/.reminders ]] && rem -h
+}
+
+
+# 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, abort instead.
+ 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)
+
+
+# RELOAD SETTINGS
+
+zshenv_reload_time=0 # load before first command
+
+# Automatically source ~/.zsh/env.update when the file changes (and exists).
+# Can be used e.g. to update SSH_AGENT_PID and GPG_AGENT_INFO variables in
+# running zsh processes. Sourced immediately before executing shell commands
+# (preexec) to ensure the environment is always up to date.
+zshenv_reload_preexec() {
+ local file
+ file=~/.zsh/env.update
+
+ local stat
+ if ! zstat -A stat +mtime $file 2>/dev/null; then
+ return
+ fi
+ # File wasn't modified, nothing to do.
+ if [[ $stat -le $zshenv_reload_time ]]; then
+ return
+ fi
+ zshenv_reload_time=$EPOCHSECONDS
+
+ unsetopt warn_create_global
+ . $file
+ setopt warn_create_global
+}
+preexec_functions+=(zshenv_reload_preexec)