From f0fbd0f7769d675620168d4109a9a1e7410f9e78 Mon Sep 17 00:00:00 2001 From: Marlon Richert 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