]> Sergey Matveev's repositories - zsh-autoenv.git/blobdiff - autoenv.zsh
Fix being loaded from a function (antigen)
[zsh-autoenv.git] / autoenv.zsh
index db77553cdaaff4ec5593a719906671934a577f35..14c4a7a2e114277d0e23eb88e56165047e7c0c9b 100644 (file)
@@ -2,6 +2,7 @@
 # https://github.com/joshuaclayton/dotfiles/blob/master/zsh_profile.d/autoenv.zsh
 
 export AUTOENV_ENV_FILENAME=$HOME/.env_auth
+[ -e $AUTOENV_ENV_FILENAME ] || touch $AUTOENV_ENV_FILENAME
 
 # Name of file to look for when entering directories.
 : ${AUTOENV_FILE_ENTER:=.env}
@@ -41,18 +42,21 @@ autoenv_source_parent() {
 
 # Internal functions. {{{
 # Internal: stack of entered (and handled) directories. {{{
-typeset -a _autoenv_stack_entered
-typeset -A _autoenv_stack_entered_mtime
+typeset -g -a _autoenv_stack_entered
+_autoenv_stack_entered=()
+# -g: make it global, this is required when used with antigen.
+typeset -g -A _autoenv_stack_entered_mtime
+_autoenv_stack_entered_mtime=()
 
 # Add an entry to the stack, and remember its mtime.
 _autoenv_stack_entered_add() {
   local env_file=$1
 
-  _autoenv_debug "[stack] adding: $env_file" 2
-
   # Remove any existing entry.
   _autoenv_stack_entered_remove $env_file
 
+  _autoenv_debug "[stack] adding: $env_file" 2
+
   # Append it to the stack, and remember its mtime.
   _autoenv_stack_entered+=($env_file)
   _autoenv_stack_entered_mtime[$env_file]=$(_autoenv_get_file_mtime $env_file)
@@ -96,7 +100,7 @@ _autoenv_debug() {
     return
   fi
   # Load zsh color support.
-  if [[ -z $colors ]]; then
+  if [[ -z $color ]]; then
     autoload colors
     colors
   fi
@@ -146,7 +150,7 @@ _autoenv_authorized_env_file() {
 _autoenv_authorize() {
   local env_file=${1:A}
   _autoenv_deauthorize $env_file
-  _autoenv_hash_pair $env_file >> $AUTOENV_ENV_FILENAME
+  _autoenv_hash_pair $env_file >>| $AUTOENV_ENV_FILENAME
 }
 
 # Deauthorize a given filename, by removing it from the auth file.
@@ -155,7 +159,7 @@ _autoenv_authorize() {
 _autoenv_deauthorize() {
   local env_file=${1:A}
   if [[ -s $AUTOENV_ENV_FILENAME ]]; then
-    echo "$(\grep -vF :${env_file}: $AUTOENV_ENV_FILENAME)" > $AUTOENV_ENV_FILENAME
+    echo "$(\grep -vF :${env_file}: $AUTOENV_ENV_FILENAME)" >| $AUTOENV_ENV_FILENAME
   fi
 }