zsh-workers
 help / color / mirror / code / Atom feed
* [RFC][PATCH] Add zrestart()
@ 2021-04-24 20:50 Marlon Richert
  2021-04-26  3:22 ` Daniel Shahaf
  0 siblings, 1 reply; 16+ messages in thread
From: Marlon Richert @ 2021-04-24 20:50 UTC (permalink / raw)
  To: Zsh hackers list

[-- Attachment #1: Type: text/plain, Size: 938 bytes --]

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.

[-- Attachment #2: 0001-Add-zrestart.txt --]
[-- Type: text/plain, Size: 2256 bytes --]

From f0fbd0f7769d675620168d4109a9a1e7410f9e78 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@gmail.com>
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


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2021-05-09 22:52 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-24 20:50 [RFC][PATCH] Add zrestart() Marlon Richert
2021-04-26  3:22 ` Daniel Shahaf
2021-04-26 19:03   ` Marlon Richert
2021-04-26 19:29     ` Daniel Shahaf
2021-04-26 23:54       ` Bart Schaefer
2021-04-27 11:42         ` Marlon Richert
2021-04-27 11:49           ` Roman Perepelitsa
2021-04-27 17:49             ` Marlon Richert
2021-04-27 17:57               ` Marlon Richert
2021-04-27 18:37                 ` Bart Schaefer
2021-04-29 13:58           ` Daniel Shahaf
2021-04-27 11:37       ` Marlon Richert
2021-04-29 14:12         ` Daniel Shahaf
2021-04-30 17:27           ` Marlon Richert
2021-05-09 20:59             ` Lawrence Velázquez
2021-05-09 22:52               ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).