diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index 547c6bb..eaa6b75 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -75,6 +75,16 @@ if [ -z "$no_shell" ]; then fi case "$shell" in -fish ) echo ". \"$(pyenv-prefix "${versions}")/bin/activate.fish\"" ;; -* ) echo "source \"$(pyenv-prefix "${versions}")/bin/activate\"" ;; +fish ) + cat </dev/null 2>&1 && deactivate;";; +fish ) + cat </dev/null 2>&1; then + export PYENV_DEACTIVATE="\$VIRTUAL_ENV"; + deactivate; +fi; +EOS + ;; esac if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then # Backward compatibility issue # https://github.com/yyuu/pyenv-virtualenv/issues/26 - echo "pyenv shell --unset" + echo "pyenv shell --unset;" fi diff --git a/bin/pyenv-virtualenv-init b/bin/pyenv-virtualenv-init index cdf6f8d..8e0b11c 100755 --- a/bin/pyenv-virtualenv-init +++ b/bin/pyenv-virtualenv-init @@ -63,53 +63,84 @@ if [ -z "$print" ]; then fi case "$shell" in -bash ) +fish ) + echo "setenv PYENV_VIRTUALENV_INIT 1;" + ;; +* ) + echo "export PYENV_VIRTUALENV_INIT=1;" + ;; +esac + +case "$shell" in +fish ) + cat </dev/null); or true + end + else + if [ "\$PYENV_DEACTIVATE" != (pyenv prefix) ] + eval (pyenv sh-activate 2>/dev/null); or true + end + end +end +EOS + ;; +ksh ) + cat </dev/null || true +EOS + ;; +esac + +if [[ "$shell" != "fish" ]]; then + cat </dev/null || true + fi + else + if [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then + pyenv activate 2>/dev/null || true + fi fi }; +EOS + + case "$shell" in + bash ) + cat </dev/null); or true - end -end -EOS - ;; -zsh ) - cat </dev/null || true - fi -} + ;; + zsh ) + cat </dev/null 2>&1 && deactivate; +if declare -f deactivate 1>/dev/null 2>&1; then + export PYENV_DEACTIVATE="\$VIRTUAL_ENV"; + deactivate; +fi; EOS } @@ -24,8 +27,11 @@ EOS assert_success assert_output </dev/null 2>&1 && deactivate; -pyenv shell --unset +if declare -f deactivate 1>/dev/null 2>&1; then + export PYENV_DEACTIVATE="\$VIRTUAL_ENV"; + deactivate; +fi; +pyenv shell --unset; EOS } @@ -36,7 +42,10 @@ EOS assert_success assert_output </dev/null || true + if [ -n "\$VIRTUAL_ENV" ]; then + if [ "x\`pyenv version-name\`" = "xsystem" ]; then + pyenv deactivate || true + return 0 + fi + if [ "x\$VIRTUAL_ENV" != "x\`pyenv prefix\`" ]; then + pyenv deactivate || true + pyenv activate 2>/dev/null || true + fi + else + if [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then + pyenv activate 2>/dev/null || true + fi fi }; if ! [[ "\$PROMPT_COMMAND" =~ _pyenv_virtualenv_hook ]]; then @@ -50,11 +58,19 @@ EOS assert_output </dev/null); or true + if [ -n "\$VIRTUAL_ENV" ] + if [ (pyenv version-name) = "system" ] + eval (pyenv sh-deactivate); or true + return 0 + end + if [ "\$VIRTUAL_ENV" != (pyenv prefix) ] + eval (pyenv sh-deactivate); or true + eval (pyenv sh-activate 2>/dev/null); or true + end + else + if [ "\$PYENV_DEACTIVATE" != (pyenv prefix) ] + eval (pyenv sh-activate 2>/dev/null); or true + end end end EOS @@ -64,15 +80,23 @@ EOS run pyenv-virtualenv-init - zsh assert_success assert_output </dev/null || true + if [ -n "\$VIRTUAL_ENV" ]; then + if [ "x\`pyenv version-name\`" = "xsystem" ]; then + pyenv deactivate || true + return 0 + fi + if [ "x\$VIRTUAL_ENV" != "x\`pyenv prefix\`" ]; then + pyenv deactivate || true + pyenv activate 2>/dev/null || true + fi + else + if [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then + pyenv activate 2>/dev/null || true + fi fi -} +}; typeset -a precmd_functions if [[ -z \$precmd_functions[(r)_pyenv_virtualenv_hook] ]]; then precmd_functions+=_pyenv_virtualenv_hook;