Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[RFC][PATCH] Add zrestart()



Attached patch adds the zrestart() function that was discussed
previously in workers/48408 and before (and also after).

It was suggested originally that `zle -fn` be used to validate the
dotfiles. However, I discovered that this can fail needlessly, for
example, if a dotfile references dynamically named directories.
Instead, to verify that the shell can restart without errors, I start
an interactive subshell with an empty command and capture its return
value and standard error.

Likewise, it was suggested at some point that some form of `zsh -l &&
exit` be used instead of `exec zsh`. However, it was later brought up
that, if the user would restart the shell many times this way, this
could potentially exhaust the available process IDs. Thus, I use `exec
zsh` after all. Hopefully, the validation above provides enough
safeguards. At least the user doesn't have to worry about mistyping
`zsh` as the argument to `exec` this way.
From f0fbd0f7769d675620168d4109a9a1e7410f9e78 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@xxxxxxxxx>
Date: Sat, 24 Apr 2021 23:34:47 +0300
Subject: [PATCH] Add zrestart()

---
 Doc/Zsh/contrib.yo      |  5 +++++
 Functions/Misc/zrestart | 42 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 Functions/Misc/zrestart

diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index 8bf1a208e..efd190f7a 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -4649,6 +4649,11 @@ See `Recompiling Functions'
 ifzman(above)\
 ifnzman((noderef(Utilities))).
 )
+findex(zrestart)
+item(tt(zrestart))(
+This function tests whether the shell is able to restart without error and, if 
+so, restarts the shell.
+)
 findex(zstyle+)
 item(tt(zstyle+) var(context) var(style) var(value) [ tt(+) var(subcontext) var(style) var(value) ... ])(
 This makes defining styles a bit simpler by using a single `tt(+)' as a
diff --git a/Functions/Misc/zrestart b/Functions/Misc/zrestart
new file mode 100644
index 000000000..1fc17c03b
--- /dev/null
+++ b/Functions/Misc/zrestart
@@ -0,0 +1,42 @@
+#
+# Restarts the shell if and only if it can restart without error.
+#
+
+emulate -LR zsh
+{
+  # Some users export $ZDOTDIR, which can mess things up.
+  local zdotdir=$ZDOTDIR
+  unset ZDOTDIR
+  
+  print 'Validating...'
+  
+  # Try if the shell can start up without errors. Passing an empty command 
+  # ensures that the subshell exits immediately after executing all dotfiles.
+  # We suppress standard out, since we're interested in standard error only.
+  setopt multios
+  local err="$(zsh --interactive --monitor --zle -c '' 2>&1 > /dev/null)"
+  local -i ret=$?
+  
+  if (( ret )) || [[ -n $err ]]; then
+    [[ -n $err ]] &&
+        print -ru2 -- "$err"
+    print -nu2 'Validation failed'
+    (( ret )) &&
+        print -nu2 " with exit status $ret"
+    [[ -n $err ]] &&
+        print -nu2 '. Please fix the error(s) above'
+    print -u2 '.'
+    print -u2 'Restart aborted.'
+    
+    (( ret )) ||
+        (( ret = 64 ))  # EX_DATAERR; see `man 3 sysexits`.
+        
+    return ret
+    
+  else
+    print 'Restarting...'
+    exec zsh
+  fi
+} always {
+  ZDOTDIR=$zdotdir
+}
-- 
2.31.1



Messages sorted by: Reverse Date, Date, Thread, Author