]> ruderich.org/simon Gitweb - config/dotfiles.git/commitdiff
zsh/rc: Restart automatically once ~/.zshrc changes.
authorSimon Ruderich <simon@ruderich.org>
Sat, 13 Jul 2013 10:57:11 +0000 (12:57 +0200)
committerSimon Ruderich <simon@ruderich.org>
Sat, 13 Jul 2013 10:57:11 +0000 (12:57 +0200)
zsh/rc

diff --git a/zsh/rc b/zsh/rc
index 096dfabb326a5f80d73e1e429b429216cf6cd2b2..7f5b1b0cc0eb52b3ed41ea7eb068ce61920bcbf2 100644 (file)
--- a/zsh/rc
+++ b/zsh/rc
@@ -914,6 +914,63 @@ periodic() {
 }
 
 
+# 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