dotfiles/home/private_dot_config/zsh/plugins/zexists/zexists.plugin.zsh

104 lines
2.5 KiB
Bash
Raw Normal View History

2023-02-21 09:44:47 -06:00
#!/usr/bin/env zsh
2023-02-20 12:55:32 -06:00
function source-zshcmdd {
setopt extended_glob
# glob search for the zshcmd.d dir
local -a zshcmdd=()
[[ -n "$ZSHCMDD" ]] && zshcmdd+=($ZSHCMDD(N))
[[ -n "$ZDOTDIR" ]] && zshcmdd+=(
$ZDOTDIR/exists.d/cmd(N)
)
zshcmdd+=(${ZDOTDIR:-$HOME}/zexists.d/cmd(N))
if ! (( $#zshcmdd )); then
echo >&2 "zexists: dir not found '${ZSHCMDD:-${ZDOTDIR:-$HOME}/zexists.d/cmd}'"
return 1
fi
local -a conf_files=("$zshcmdd[1]"/*.{sh,zsh}(N))
local rcfile
local antircfile
# sort and source conf files
for rcfile in ${(o)conf_files}; do
# ignore files that begin with a tilde and antircfiles
case ${rcfile:t} in 'anti'*|'~'*) continue;; esac
# source files only if exe with that name exists
if (( ! $+commands[{rcfile:t:r}] )); then
source "$rcfile"
else
# if it doesn't exist try the anti version
antircfile=${rcfile:h}/anti-${rcfile:t}
[ -f $antircfile ] && source $antircfile
fi
done
}
source-zshcmdd
function zshdir-decode {
# for now just remove the 'dir' part
2023-02-21 09:44:47 -06:00
echo ${${1//-SLASH-//}/-DOT-/\.}
2023-02-20 12:55:32 -06:00
}
function zshdir-encode {
# dir is hardcoded...
2023-02-21 09:44:47 -06:00
echo dir${${1//${HOME}\//}//\//-SLASH-}
2023-02-20 12:55:32 -06:00
}
function source-zshpathd {
setopt extended_glob
# glob search for the zshcmd.d dir
local -a zshpathd=()
[[ -n "$ZSHPATHD" ]] && zshpathd+=($ZSHPATH(N))
[[ -n "$ZDOTDIR" ]] && zshpathd+=(
$ZDOTDIR/zexists/path(N)
)
zshpathd+=(${ZDOTDIR:-$HOME}/zexists.d/path(N))
if ! (( $#zshpathd )); then
echo >&2 "zexists: dir not found '${ZSHPATHD:-${ZDOTDIR:-$HOME}/zexists.d/path}'"
return 1
fi
local -a conf_files=("$zshpathd[1]"/*(N))
local rcfile
local name
local pathtype
local directory
local antircfile # sort and source conf files
for rcfile in ${(o)conf_files}; do
if [[ ${rcfile:t} == "dir"* ]];then
pathtype='dir'
elif [[ ${rcfile:t} == "file"* ]];then
pathtype='file'
echo >&2 "zexists: file type not yet supported"
else
echo >&2 "zexists: unknown path type for ${rcfile}"
return 1
fi
# ignore files that begin with a tilde
case ${rcfile:t} in '~'*) continue;; esac
# remove 'dir' from name
2023-02-21 09:44:47 -06:00
name=${${rcfile:t}/${pathtype}-/}
directory=$HOME/$(zshdir-decode ${name})
2023-02-20 12:55:32 -06:00
# source files only if exe with that name exists
if [[ -d $directory ]]; then
source $rcfile
else
# if it doesn't exist try the anti version
antircfile=${rcfile:h}/anti-${pathtype}-${rcfile:t}
[[ -f $antircfile ]] && source $antircfile
fi
done
}
source-zshpathd
2023-02-21 09:44:47 -06:00
unset _envsubst