mirror of
https://github.com/Tarrasch/zsh-autoenv.git
synced 2024-11-21 15:00:59 +02:00
Merge 279eab90d2
into f5951dd0cf
This commit is contained in:
commit
10f32ab50e
@ -146,6 +146,8 @@ Default: `0`
|
|||||||
|
|
||||||
zsh-autoenv works automatically once installed.
|
zsh-autoenv works automatically once installed.
|
||||||
|
|
||||||
|
### autoenv-edit
|
||||||
|
|
||||||
You can use ``autoenv-edit`` to edit the nearest/current autoenv files.
|
You can use ``autoenv-edit`` to edit the nearest/current autoenv files.
|
||||||
It will use ``$AUTOENV_EDITOR``, ``$EDITOR``, or ``vim`` for editing.
|
It will use ``$AUTOENV_EDITOR``, ``$EDITOR``, or ``vim`` for editing.
|
||||||
|
|
||||||
|
56
autoenv.zsh
56
autoenv.zsh
@ -393,19 +393,51 @@ _autoenv_get_file_upwards() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Helper function to edit enter/leave files.
|
||||||
autoenv-edit() {
|
autoenv-edit() {
|
||||||
emulate -L zsh
|
emulate -L zsh
|
||||||
local env_file
|
|
||||||
local -a files
|
|
||||||
local -A check
|
local -A check
|
||||||
check[enter]=$AUTOENV_FILE_ENTER
|
check[enter]=$AUTOENV_FILE_ENTER
|
||||||
if [[ "$AUTOENV_FILE_ENTER" != "$AUTOENV_FILE_LEAVE" ]]; then
|
if [[ "$AUTOENV_FILE_ENTER" != "$AUTOENV_FILE_LEAVE" ]]; then
|
||||||
check[leave]=$AUTOENV_FILE_LEAVE
|
check[leave]=$AUTOENV_FILE_LEAVE
|
||||||
fi
|
fi
|
||||||
local f t
|
|
||||||
|
local -a opts
|
||||||
|
zmodload -i zsh/zutil
|
||||||
|
zparseopts -D -a opts c h l
|
||||||
|
if (( $opts[(I)-h] )); then
|
||||||
|
echo "Edit or create files (${(j:, :)check}) in DIR."
|
||||||
|
echo 'DIR defaults to the current directory (looking upward).'
|
||||||
|
echo "$0 [-h] [-c] [-l] [DIR]"
|
||||||
|
echo ' -h: this help'
|
||||||
|
echo ' -c: create new files (uses non-existing files with the editor)'
|
||||||
|
echo ' -l: list files to be edited'
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
local create=$(( opts[(I)-c] > 0 ))
|
||||||
|
local use_dir=0 dir
|
||||||
|
if [[ -n $1 ]]; then
|
||||||
|
use_dir=1
|
||||||
|
if [[ "$1" == . ]]; then
|
||||||
|
dir=
|
||||||
|
else
|
||||||
|
dir=${1#/}/
|
||||||
|
fi
|
||||||
|
elif (( create )); then
|
||||||
|
use_dir=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local f t env_file
|
||||||
|
local -a files
|
||||||
for t f in ${(kv)check}; do
|
for t f in ${(kv)check}; do
|
||||||
env_file="$f"
|
env_file=${dir}${f}
|
||||||
if ! [[ -f $env_file ]]; then
|
if (( use_dir )); then
|
||||||
|
if ! (( create )) && ! [[ -f $env_file ]]; then
|
||||||
|
echo "No $f file found ($t)." >&2
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
elif ! [[ -f $env_file ]]; then
|
||||||
env_file=$(_autoenv_get_file_upwards . $f)
|
env_file=$(_autoenv_get_file_upwards . $f)
|
||||||
if [[ -z $env_file ]]; then
|
if [[ -z $env_file ]]; then
|
||||||
echo "No $f file found ($t)." >&2
|
echo "No $f file found ($t)." >&2
|
||||||
@ -418,12 +450,18 @@ autoenv-edit() {
|
|||||||
files+=($env_file)
|
files+=($env_file)
|
||||||
done
|
done
|
||||||
if [[ -z "$files" ]]; then
|
if [[ -z "$files" ]]; then
|
||||||
|
echo "Use -c to create the file(s)." >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
echo "Editing $files.."
|
|
||||||
local editor
|
if (( opts[(I)-l] > 0 )); then
|
||||||
editor="${${AUTOENV_EDITOR:-$EDITOR}:-vim}"
|
printf '%s\n' $files
|
||||||
eval $editor "$files"
|
else
|
||||||
|
echo "Editing $files.."
|
||||||
|
local editor
|
||||||
|
editor="${${AUTOENV_EDITOR:-$EDITOR}:-vim}"
|
||||||
|
eval $editor "$files"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_autoenv_chpwd_handler() {
|
_autoenv_chpwd_handler() {
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
$ autoenv-edit
|
$ autoenv-edit
|
||||||
No .autoenv.zsh file found (enter).
|
No .autoenv.zsh file found (enter).
|
||||||
No .autoenv_leave.zsh file found (leave).
|
No .autoenv_leave.zsh file found (leave).
|
||||||
|
Use -c to create the file(s).
|
||||||
[1]
|
[1]
|
||||||
|
|
||||||
$ touch .autoenv.zsh
|
$ touch .autoenv.zsh
|
||||||
@ -66,3 +67,21 @@ Note with AUTOENV_LOOK_UPWARDS=0
|
|||||||
$ autoenv-edit
|
$ autoenv-edit
|
||||||
Note: found ../.autoenv.zsh, but AUTOENV_LOOK_UPWARDS is disabled.
|
Note: found ../.autoenv.zsh, but AUTOENV_LOOK_UPWARDS is disabled.
|
||||||
Editing ../.autoenv.zsh .autoenv_leave.zsh..
|
Editing ../.autoenv.zsh .autoenv_leave.zsh..
|
||||||
|
|
||||||
|
autoenv-edit -c edits non-existing files.
|
||||||
|
|
||||||
|
$ EDITOR=true
|
||||||
|
$ AUTOENV_FILE_ENTER=enter-file AUTOENV_FILE_LEAVE=leave-file autoenv-edit -c
|
||||||
|
Editing enter-file leave-file..
|
||||||
|
|
||||||
|
$ AUTOENV_FILE_ENTER=enter-file AUTOENV_FILE_LEAVE=leave-file autoenv-edit -c .
|
||||||
|
Editing enter-file leave-file..
|
||||||
|
|
||||||
|
$ AUTOENV_FILE_ENTER=enter-file AUTOENV_FILE_LEAVE=leave-file autoenv-edit -c ..
|
||||||
|
Editing ../enter-file ../leave-file..
|
||||||
|
|
||||||
|
autoenv-edit -l lists files.
|
||||||
|
|
||||||
|
$ AUTOENV_LOOK_UPWARDS=1 autoenv-edit -l
|
||||||
|
../.autoenv.zsh
|
||||||
|
.autoenv_leave.zsh
|
||||||
|
Loading…
Reference in New Issue
Block a user