mirror of
https://github.com/kovidgoyal/kitty
synced 2026-06-08 22:28:24 +02:00
Minor robustness improvements to zsh shell integration
This commit is contained in:
@@ -68,23 +68,32 @@ def setup_fish_env(env: Dict[str, str]) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def is_new_zsh_install(env: Dict[str, str]) -> bool:
|
def is_new_zsh_install(env: Dict[str, str]) -> bool:
|
||||||
|
# if ZDOTDIR is empty, zsh will read user rc files from /
|
||||||
|
# if there aren't any, it'll run zsh-newuser-install
|
||||||
|
# the latter will bail if there are rc files in $HOME
|
||||||
zdotdir = env.get('ZDOTDIR')
|
zdotdir = env.get('ZDOTDIR')
|
||||||
base = zdotdir or os.path.expanduser('~')
|
if not zdotdir:
|
||||||
|
zdotdir = os.path.expanduser('~')
|
||||||
|
if zdotdir == '~':
|
||||||
|
return True
|
||||||
for q in ('.zshrc', '.zshenv', '.zprofile', '.zlogin'):
|
for q in ('.zshrc', '.zshenv', '.zprofile', '.zlogin'):
|
||||||
if os.path.exists(os.path.join(base, q)):
|
if os.path.exists(os.path.join(zdotdir, q)):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def setup_zsh_env(env: Dict[str, str]) -> None:
|
def setup_zsh_env(env: Dict[str, str]) -> None:
|
||||||
zdotdir = env.get('ZDOTDIR')
|
|
||||||
base = zdotdir or os.path.expanduser('~')
|
|
||||||
if is_new_zsh_install(env):
|
if is_new_zsh_install(env):
|
||||||
# dont prevent zsh-newuser-install from running
|
# dont prevent zsh-newuser-install from running
|
||||||
|
# zsh-newuser-install never runs as root but we assume that it does
|
||||||
return
|
return
|
||||||
|
zdotdir = env.get('ZDOTDIR')
|
||||||
if zdotdir is not None:
|
if zdotdir is not None:
|
||||||
env['KITTY_ORIG_ZDOTDIR'] = zdotdir
|
env['KITTY_ORIG_ZDOTDIR'] = zdotdir
|
||||||
env['KITTY_ZSH_BASE'] = base
|
else:
|
||||||
|
# KITTY_ORIG_ZDOTDIR can be set at this point if, for example, the global
|
||||||
|
# zshenv overrides ZDOTDIR; we try to limit the damage in this case
|
||||||
|
env.pop('KITTY_ORIG_ZDOTDIR', None)
|
||||||
env['ZDOTDIR'] = os.path.join(shell_integration_dir, 'zsh')
|
env['ZDOTDIR'] = os.path.join(shell_integration_dir, 'zsh')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,32 @@
|
|||||||
if [[ -o interactive && -v ZDOTDIR && -r "$ZDOTDIR/kitty.zsh" ]]; then source "$ZDOTDIR/kitty.zsh"; fi
|
# Don't use [[ -v ... ]] because it doesn't work in zsh < 5.4.
|
||||||
if [[ -v KITTY_ORIG_ZDOTDIR ]]; then
|
if (( ${+KITTY_ORIG_ZDOTDIR} )); then
|
||||||
export ZDOTDIR="$KITTY_ORIG_ZDOTDIR"
|
# Normally ZDOTDIR shouldn't be exported but it was in the environment
|
||||||
|
# of Kitty, so we export it.
|
||||||
|
export ZDOTDIR=$KITTY_ORIG_ZDOTDIR
|
||||||
unset KITTY_ORIG_ZDOTDIR
|
unset KITTY_ORIG_ZDOTDIR
|
||||||
else
|
else
|
||||||
unset ZDOTDIR
|
unset ZDOTDIR
|
||||||
fi
|
fi
|
||||||
if [[ -v KITTY_ZSH_BASE && -r "$KITTY_ZSH_BASE/.zshenv" ]]; then source "$KITTY_ZSH_BASE/.zshenv"; fi
|
|
||||||
unset KITTY_ZSH_BASE
|
# Use try-always to have the right error code.
|
||||||
|
{
|
||||||
|
# Zsh treats empty $ZDOTDIR as if it was "/". We do the same.
|
||||||
|
#
|
||||||
|
# Source the user's zshenv before sourcing kitty.zsh because the former
|
||||||
|
# might set fpath and other things without which kitty.zsh won't work.
|
||||||
|
#
|
||||||
|
# Use typeset in case we are in a function with warn_create_global in
|
||||||
|
# effect. Unlikely but better safe than sorry.
|
||||||
|
typeset _ksi_source=${ZDOTDIR-~}/.zshenv
|
||||||
|
# Zsh ignores unreadable rc files. We do the same.
|
||||||
|
# Zsh ignores rc files that are directories, and so does source.
|
||||||
|
[[ ! -r $_ksi_source ]] || source -- "$_ksi_source"
|
||||||
|
} always {
|
||||||
|
if [[ -o interactive ]]; then
|
||||||
|
# ${(%):-%x} is the path to the current file.
|
||||||
|
# On top of it we add :a:h to get the directory.
|
||||||
|
typeset _ksi_source=${${(%):-%x}:A:h}/kitty.zsh
|
||||||
|
[[ ! -r $_ksi_source ]] || source -- "$_ksi_source"
|
||||||
|
fi
|
||||||
|
unset _ksi_source
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user