From 230a9f4678abb53f24ba6d303d6788e0bf942586 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 17 Apr 2022 19:39:32 +0530 Subject: [PATCH] Implement option to control clone sourcing strategies --- kitty/launch.py | 16 ++++------ shell-integration/bash/kitty.bash | 21 +++++++++++-- .../kitty-shell-integration.fish | 30 +++++++++++++++++-- shell-integration/zsh/kitty-integration | 25 ++++++++++++---- 4 files changed, 71 insertions(+), 21 deletions(-) diff --git a/kitty/launch.py b/kitty/launch.py index 02b067c14..bee67c0ff 100644 --- a/kitty/launch.py +++ b/kitty/launch.py @@ -340,6 +340,7 @@ class ForceWindowLaunch: force_window_launch = ForceWindowLaunch() +non_window_launch_types = 'background', 'clipboard', 'primary' def launch( @@ -348,7 +349,6 @@ def launch( args: List[str], target_tab: Optional[Tab] = None, force_target_tab: bool = False, - base_env: Optional[Dict[str, str]] = None, active: Optional[Window] = None, is_clone_launch: str = '', ) -> Optional[Window]: @@ -365,11 +365,7 @@ def launch( if opts.os_window_title == 'current': tm = boss.active_tab_manager opts.os_window_title = get_os_window_title(tm.os_window_id) if tm else None - if base_env is not None: - env = base_env.copy() - env.update(get_env(opts)) - else: - env = get_env(opts, active_child) + env = get_env(opts, active_child) kw: LaunchKwds = { 'allow_remote_control': opts.allow_remote_control, 'cwd_from': None, @@ -450,7 +446,7 @@ def launch( if exe: final_cmd[0] = exe kw['cmd'] = final_cmd - if force_window_launch and opts.type not in ('background', 'clipboard', 'primary'): + if force_window_launch and opts.type not in non_window_launch_types: opts.type = 'window' if opts.type == 'overlay' and active: kw['overlay_for'] = active.id @@ -575,7 +571,7 @@ class CloneCmd: # some people export these. We want the shell rc files to recreate them 'PS0', 'PS1', 'PS2', 'PS3', 'PS4', 'RPS1', 'PROMPT_COMMAND', 'SHLVL', # conda state env vars - 'CONDA_SHLVL', 'CONDA_PREFIX', 'CONDA_EXE', 'CONDA_PROMPT_MODIFIER', 'CONDA_EXE', 'CONDA_PYTHON_EXE', + 'CONDA_SHLVL', 'CONDA_PREFIX', 'CONDA_PROMPT_MODIFIER', 'CONDA_EXE', 'CONDA_PYTHON_EXE', '_CE_CONDA', '_CE_M', # skip SSH environment variables 'SSH_CLIENT', 'SSH_CONNECTION', 'SSH_ORIGINAL_COMMAND', 'SSH_TTY', 'SSH2_TTY', }} @@ -591,7 +587,7 @@ def clone_and_launch(msg: str, window: Window) -> None: c.opts.cwd = c.cwd c.opts.copy_colors = True c.opts.copy_env = False - if c.opts.type in ('clipboard', 'primary', 'background'): + if c.opts.type in non_window_launch_types: c.opts.type = 'window' if c.env and c.env.get('PATH') and c.env.get('VIRTUAL_ENV'): # only pass VIRTUAL_ENV if it is currently active @@ -628,4 +624,4 @@ def clone_and_launch(msg: str, window: Window) -> None: cmdline[0] = window.child.final_exe if cmdline and cmdline == [window.child.final_exe] + window.child.argv[1:]: cmdline = window.child.unmodified_argv - launch(get_boss(), c.opts, cmdline, base_env=c.env, active=window, is_clone_launch=is_clone_launch) + launch(get_boss(), c.opts, cmdline, active=window, is_clone_launch=is_clone_launch) diff --git a/shell-integration/bash/kitty.bash b/shell-integration/bash/kitty.bash index e37f1ed15..886d95622 100644 --- a/shell-integration/bash/kitty.bash +++ b/shell-integration/bash/kitty.bash @@ -264,12 +264,27 @@ _ksi_main() { builtin eval "${KITTY_IS_CLONE_LAUNCH}" builtin hash -r 2> /dev/null 1> /dev/null builtin local venv="${VIRTUAL_ENV}/bin/activate" - if [ -n "${VIRTUAL_ENV}" -a -r "$venv" ]; then + builtin local sourced="" + _ksi_s_is_ok() { + [[ -z "$sourced" && "$KITTY_CLONE_SOURCE_STRATEGIES" == *",$1,"* ]] && return 0 + return 1 + } + + if _ksi_s_is_ok "venv" && [ -n "${VIRTUAL_ENV}" -a -r "$venv" ]; then + sourced="y" builtin unset VIRTUAL_ENV . "$venv" - elif [ -n "${CONDA_DEFAULT_ENV}" ] && builtin command -v conda >/dev/null 2>/dev/null && [ "${CONDA_DEFAULT_ENV}" != "$orig_conda_env" ]; then + fi; if _ksi_s_is_ok "conda" && [ -n "${CONDA_DEFAULT_ENV}" ] && builtin command -v conda >/dev/null 2>/dev/null && [ "${CONDA_DEFAULT_ENV}" != "$orig_conda_env" ]; then + sourced="y" conda activate "${CONDA_DEFAULT_ENV}" + fi; if _ksi_s_is_ok "env_var" && [[ -n "${KITTY_CLONE_SOURCE_CODE}" ]]; then + sourced="y" + eval "${KITTY_CLONE_SOURCE_CODE}" + fi; if _ksi_s_is_ok "path" && [[ -r "${KITTY_CLONE_SOURCE_PATH}" ]]; then + sourced="y" + builtin source "${KITTY_CLONE_SOURCE_PATH}" fi + builtin unset -f _ksi_s_is_ok # Ensure PATH has no duplicate entries if [ -n "$PATH" ]; then builtin local old_PATH=$PATH:; PATH= @@ -285,7 +300,7 @@ _ksi_main() { PATH=${PATH#:} fi fi - builtin unset KITTY_IS_CLONE_LAUNCH + builtin unset KITTY_IS_CLONE_LAUNCH KITTY_CLONE_SOURCE_STRATEGIES } _ksi_main builtin unset -f _ksi_main diff --git a/shell-integration/fish/vendor_conf.d/kitty-shell-integration.fish b/shell-integration/fish/vendor_conf.d/kitty-shell-integration.fish index ddb45e2bc..17c677900 100644 --- a/shell-integration/fish/vendor_conf.d/kitty-shell-integration.fish +++ b/shell-integration/fish/vendor_conf.d/kitty-shell-integration.fish @@ -113,17 +113,41 @@ function __ksi_schedule --on-event fish_prompt -d "Setup kitty integration after set -l orig_conda_env "$CONDA_DEFAULT_ENV" eval "$KITTY_IS_CLONE_LAUNCH" set -l venv "$VIRTUAL_ENV/bin/activate.fish" - if test -n "$VIRTUAL_ENV" -a -r "$venv" + set -g _ksi_sourced 'n' + function _ksi_s_is_ok + if test "$_ksi_sourced" = 'n' + and string match -q -- "*,$argv[1],*" "$KITTY_CLONE_SOURCE_STRATEGIES" + return 0 + end + return 1 + end + if _ksi_s_is_ok "venv" + and test -n "$VIRTUAL_ENV" -a -r "$venv" + set _ksi_sourced "y" set -e VIRTUAL_ENV _OLD_FISH_PROMPT_OVERRIDE # activate.fish stupidly exports _OLD_FISH_PROMPT_OVERRIDE source "$venv" - else if test -n "$CONDA_DEFAULT_ENV" + end + if _ksi_s_is_ok "conda" + and test -n "$CONDA_DEFAULT_ENV" and type -q conda and test "$CONDA_DEFAULT_ENV" != "$orig_conda_env" + set _ksi_sourced "y" # for some reason that I cant be bothered to figure out this doesnt take effect # conda activate $_ksi_pre_rc_conda_default_env eval ($CONDA_EXE shell.fish activate $CONDA_DEFAULT_ENV) end - set --erase KITTY_IS_CLONE_LAUNCH + if _ksi_s_is_ok "env_var" + and test -n "$KITTY_CLONE_SOURCE_CODE" + set _ksi_sourced "y" + eval "$KITTY_CLONE_SOURCE_CODE" + end + if _ksi_s_is_ok "path" + and test -r "$KITTY_CLONE_SOURCE_PATH" + set _ksi_sourced "y" + source "$KITTY_CLONE_SOURCE_PATH" + end + set --erase KITTY_IS_CLONE_LAUNCH KITTY_CLONE_SOURCE_STRATEGIES _ksi_sourced + functions --erase _ksi_s_is_ok end end diff --git a/shell-integration/zsh/kitty-integration b/shell-integration/zsh/kitty-integration index b2ab656bd..194a51e4a 100644 --- a/shell-integration/zsh/kitty-integration +++ b/shell-integration/zsh/kitty-integration @@ -354,16 +354,31 @@ _ksi_deferred_init() { builtin eval "${KITTY_IS_CLONE_LAUNCH}" builtin hash -r 2> /dev/null 1> /dev/null builtin local venv="${VIRTUAL_ENV}/bin/activate" - if [ -n "${VIRTUAL_ENV}" -a -r "$venv" ]; then + builtin local sourced="" + _ksi_s_is_ok() { + [[ -z "$sourced" && "$KITTY_CLONE_SOURCE_STRATEGIES" == *",$1,"* ]] && return 0 + return 1 + } + + if _ksi_s_is_ok "venv" && [[ -n "${VIRTUAL_ENV}" && -r "$venv" ]]; then + sourced="y" builtin unset VIRTUAL_ENV - . "$venv" - elif [[ -n "${CONDA_DEFAULT_ENV}" && (( $+commands[conda] )) && "${CONDA_DEFAULT_ENV}" != "$orig_conda_env" ]]; then + builtin source "$venv" + fi; if _ksi_s_is_ok "conda" && [[ -n "${CONDA_DEFAULT_ENV}" && (( $+commands[conda] )) && "${CONDA_DEFAULT_ENV}" != "$orig_conda_env" ]]; then + sourced="y" conda activate "${CONDA_DEFAULT_ENV}" + fi; if _ksi_s_is_ok "env_var" && [[ -n "${KITTY_CLONE_SOURCE_CODE}" ]]; then + sourced="y" + eval "${KITTY_CLONE_SOURCE_CODE}" + fi; if _ksi_s_is_ok "path" && [[ -r "${KITTY_CLONE_SOURCE_PATH}" ]]; then + sourced="y" + builtin source "${KITTY_CLONE_SOURCE_PATH}" fi + builtin unfunction _ksi_s_is_ok # Ensure PATH has no duplicate entries - typeset -U path + builtin typeset -U path fi - builtin unset KITTY_IS_CLONE_LAUNCH + builtin unset KITTY_IS_CLONE_LAUNCH KITTY_CLONE_SOURCE_STRATEGIES # Unfunction _ksi_deferred_init to save memory. Don't unfunction # kitty-integration though because decent public functions aren't supposed to