From 0f92983d1e6a008d587063739cef3897d053c0be Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 11 Aug 2017 14:15:46 +0200 Subject: [PATCH 1/1] Add autoenv-edit function to edit current env files (#68) --- README.md | 7 +++++ autoenv.zsh | 31 ++++++++++++++++++++ tests/autoenv-edit.t | 70 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 tests/autoenv-edit.t diff --git a/README.md b/README.md index e6cbaf7..fc86547 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,13 @@ Enable debugging. Multiple levels are supported (max 2). Default: `0` +## Usage + +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. + ## Recipes ### Automatically activate Python virtualenvs diff --git a/autoenv.zsh b/autoenv.zsh index c7a2999..195c0da 100644 --- a/autoenv.zsh +++ b/autoenv.zsh @@ -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" diff --git a/tests/autoenv-edit.t b/tests/autoenv-edit.t new file mode 100644 index 0000000..52c3d07 --- /dev/null +++ b/tests/autoenv-edit.t @@ -0,0 +1,70 @@ + $ source $TESTDIR/setup.zsh || return 1 + + $ set | sort > before + + $ export EDITOR=echo + + $ autoenv-edit + No .autoenv.zsh file found (enter). + No .autoenv_leave.zsh file found (leave). + [1] + + $ touch .autoenv.zsh + $ autoenv-edit + No .autoenv_leave.zsh file found (leave). + Editing .autoenv.zsh.. + .autoenv.zsh + + $ AUTOENV_FILE_LEAVE=$AUTOENV_FILE_ENTER + $ autoenv-edit + Editing .autoenv.zsh.. + .autoenv.zsh (glob) + + $ mkdir sub + $ cd -q sub + $ autoenv-edit + Editing ../.autoenv.zsh.. + ../.autoenv.zsh + +Supports command with args for EDITOR. + + $ export EDITOR='printf file:%s\\n' + $ autoenv-edit + Editing ../.autoenv.zsh.. + file:../.autoenv.zsh + +Supports alias for EDITOR. + + $ alias myeditor_alias='printf file:%s' + $ export EDITOR=myeditor_alias + $ autoenv-edit + Editing ../.autoenv.zsh.. + file:../.autoenv.zsh (no-eol) + +Falls back to "vim" for EDITOR. + + $ alias vim='printf vim_file:%s' + $ unset EDITOR + $ autoenv-edit + Editing ../.autoenv.zsh.. + vim_file:../.autoenv.zsh (no-eol) + +Note with AUTOENV_LOOK_UPWARDS=0 + + $ EDITOR=true + $ AUTOENV_LOOK_UPWARDS=0 + $ autoenv-edit + Note: found ../.autoenv.zsh, but AUTOENV_LOOK_UPWARDS is disabled. + Editing ../.autoenv.zsh.. + + $ AUTOENV_FILE_LEAVE=.autoenv_leave.zsh + $ touch ../$AUTOENV_FILE_LEAVE + $ autoenv-edit + Note: found ../.autoenv.zsh, but AUTOENV_LOOK_UPWARDS is disabled. + Note: found ../.autoenv_leave.zsh, but AUTOENV_LOOK_UPWARDS is disabled. + Editing ../.autoenv.zsh ../.autoenv_leave.zsh.. + + $ touch $AUTOENV_FILE_LEAVE + $ autoenv-edit + Note: found ../.autoenv.zsh, but AUTOENV_LOOK_UPWARDS is disabled. + Editing ../.autoenv.zsh .autoenv_leave.zsh.. -- 2.44.0