]> Sergey Matveev's repositories - zsh-autoenv.git/blobdiff - README.md
Simplify and lower LoC
[zsh-autoenv.git] / README.md
index 1913190c10f6652602fb0cf4ffa3c9108211e41b..c0036cef2da23337006c4ac3b1107435ef360c29 100644 (file)
--- a/README.md
+++ b/README.md
@@ -8,6 +8,10 @@ typically used in project root directories.
 It handles "enter" and leave" events, nesting, and stashing of
 variables (overwriting and restoring).
 
+## Requirements
+
+- Zsh version 4.3.10 or later.
+
 ## Features
 
 - Support for enter and leave events, which can use the same file.
@@ -17,7 +21,7 @@ variables (overwriting and restoring).
   unknown `.autoenv.zsh` file, and remembers whitelisted files by their
   hashed content.
 - Test suite.
-- Written in Zsh.
+- Written in/for Zsh.
 
 ### Variable stashing
 
@@ -119,13 +123,13 @@ event was handled?
 
 Default: `1`
 
-### AUTOENV\_DISABLED
+### AUTOENV_DISABLED
 
 (Temporarily) disable zsh-autoenv. This gets looked at in the chpwd handler.
 
 Default: 0
 
-### AUTOENV\_DEBUG
+### AUTOENV_DEBUG
 
 Set debug level. If enabled (> 0) it will print information to stderr.
 
@@ -145,6 +149,24 @@ zsh-autoenv works automatically once installed.
 You can use ``autoenv-edit`` to edit the nearest/current autoenv files.
 It will use ``$AUTOENV_EDITOR``, ``$EDITOR``, or ``vim`` for editing.
 
+## Helper functions
+
+The following helper functions are available:
+
+### autoenv_append_path
+
+Appends path(s) to `$path` (`$PATH`), if they are not in there already.
+
+### autoenv_prepend_path
+
+Prepends path(s) to `$path` (`$PATH`), if they are not in there already.
+
+### autoenv_remove_path
+
+Removes path(s) from `$path` (`$PATH`).
+
+Returns 0 in case `$path` has changed, 1 otherwise.
+
 ## Recipes
 
 ### Automatically activate Python virtualenvs
@@ -169,20 +191,31 @@ if [[ $autoenv_event == 'enter' ]]; then
 
     setopt localoptions extendedglob
     local -a venv
-    venv=(./(../)#.venv(NY1:a))
+    venv=(./(../)#.venv(NY1:A))
 
     if [[ -n "$_ZSH_ACTIVATED_VIRTUALENV" && -n "$VIRTUAL_ENV" ]]; then
       if ! (( $#venv )) || [[ "$_ZSH_ACTIVATED_VIRTUALENV" != "$venv[1]" ]]; then
         unset _ZSH_ACTIVATED_VIRTUALENV
         echo "De-activating virtualenv: ${(D)VIRTUAL_ENV}" >&2
-        deactivate
+
+        # Simulate "deactivate", but handle $PATH better (remove VIRTUAL_ENV).
+        if ! autoenv_remove_path $VIRTUAL_ENV/bin; then
+          echo "warning: ${VIRTUAL_ENV}/bin not found in \$PATH" >&2
+        fi
+
+        # NOTE: does not handle PYTHONHOME/_OLD_VIRTUAL_PYTHONHOME
+        unset _OLD_VIRTUAL_PYTHONHOME
+        # NOTE: does not handle PS1/_OLD_VIRTUAL_PS1
+        unset _OLD_VIRTUAL_PS1
+        unset VIRTUAL_ENV
       fi
     fi
 
     if [[ -z "$VIRTUAL_ENV" ]]; then
       if (( $#venv )); then
         echo "Activating virtualenv: ${(D)venv}" >&2
-        source $venv[1]/bin/activate
+        export VIRTUAL_ENV=$venv[1]
+        autoenv_prepend_path $VIRTUAL_ENV/bin
         _ZSH_ACTIVATED_VIRTUALENV="$venv[1]"
       fi
     fi