From 8f571fe4e59269127ba06b45a4217ffcfe9212f4 Mon Sep 17 00:00:00 2001 From: Ken Lai Date: Wed, 22 Jul 2015 17:45:21 +0800 Subject: [PATCH] 1. set ENV_NAME to current folder name instead of an empty string when .venv file is not found 2. optimized activate & deactivated logic --- .../virtualenvwrapper.plugin.zsh | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh index f5f37ff6..1ea8956a 100644 --- a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh +++ b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh @@ -53,27 +53,26 @@ if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then elif [[ "$PROJECT_ROOT" != "." ]]; then ENV_NAME="${PROJECT_ROOT:t}" else - ENV_NAME="" + FOLDER_NAME=`pwd` + ENV_NAME=`basename "$FOLDER_NAME"` fi - if [[ "$ENV_NAME" != "" ]]; then - # Activate the environment only if it is not already active - if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then - if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then - workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME" - elif [[ -e "$ENV_NAME/bin/activate" ]]; then - source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME" - fi + # Activate the environment only if it is not already active + if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then + if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then + workon "$ENV_NAME" && export CD_VIRTUAL_ENV=`pwd` + elif [[ -e "$ENV_NAME/bin/activate" ]]; then + source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV=`pwd` + elif [[ `pwd` != "$CD_VIRTUAL_ENV"* ]]; then + # We've just left the repo, deactivate the environment + # Note: this only happens if the virtualenv was activated automatically + deactivate && unset CD_VIRTUAL_ENV fi - elif [[ -n $CD_VIRTUAL_ENV && -n $VIRTUAL_ENV ]]; then - # We've just left the repo, deactivate the environment - # Note: this only happens if the virtualenv was activated automatically - deactivate && unset CD_VIRTUAL_ENV fi unset PROJECT_ROOT unset WORKON_CWD fi } - + # Append workon_cwd to the chpwd_functions array, so it will be called on cd # http://zsh.sourceforge.net/Doc/Release/Functions.html if ! (( $chpwd_functions[(I)workon_cwd] )); then