]> Sergey Matveev's repositories - zsh-autoenv.git/blobdiff - autoenv.zsh
Add autoenv-edit function to edit current env files (#68)
[zsh-autoenv.git] / autoenv.zsh
index c7a2999be5ea2f4d8348dcdf960f2e55b3605631..195c0da95c600aef18a2f979b894b74315be1611 100644 (file)
@@ -355,6 +355,37 @@ _autoenv_get_file_upwards() {
   done
 }
 
+autoenv-edit() {
+  local env_file
+  local -a files
+  local -A check
+  check[enter]=$AUTOENV_FILE_ENTER
+  if [[ "$AUTOENV_FILE_ENTER" != "$AUTOENV_FILE_LEAVE" ]]; then
+    check[leave]=$AUTOENV_FILE_LEAVE
+  fi
+  local f t
+  for t f in ${(kv)check}; do
+    env_file="$f"
+    if ! [[ -f $env_file ]]; then
+      env_file=$(_autoenv_get_file_upwards . $f)
+      if [[ -z $env_file ]]; then
+        echo "No $f file found ($t)." >&2
+        continue
+      fi
+      if ! [[ $AUTOENV_LOOK_UPWARDS == 1 ]]; then
+        echo "Note: found $env_file, but AUTOENV_LOOK_UPWARDS is disabled."
+      fi
+    fi
+    files+=($env_file)
+  done
+  if [[ -z "$files" ]]; then
+    return 1
+  fi
+  echo "Editing $files.."
+  local editor
+  editor="${${AUTOENV_EDITOR:-$EDITOR}:-vim}"
+  eval $editor "$files"
+}
 
 _autoenv_chpwd_handler() {
   _autoenv_debug "Calling chpwd handler: PWD=$PWD"