Skip to content

Commit

Permalink
load: Use LOCAL_OPTIONS (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmc3 committed Mar 6, 2024
1 parent 90deef1 commit 53bb25f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 55 deletions.
59 changes: 59 additions & 0 deletions functions/__antidote_load_prep
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/zsh

### Prep to load
#function __antidote_load_prep {
emulate -L zsh; setopt local_options $_adote_funcopts

# pass in bundle file, read from zstyle, or use default .zsh_plugins.txt
local bundlefile="$1"
if [[ -z "$bundlefile" ]]; then
zstyle -s ':antidote:bundle' file 'bundlefile' ||
bundlefile=${ZDOTDIR:-$HOME}/.zsh_plugins.txt
fi

# pass in static file, read from zstyle, change extension, or use default .zsh_plugins.zsh
local staticfile="$2"
if [[ -z "$staticfile" ]]; then
zstyle -s ':antidote:static' file 'staticfile'
if [[ -z "$staticfile" ]]; then
if [[ -z "$bundlefile:t:r" ]]; then
staticfile=${bundlefile}.zsh
else
staticfile=${bundlefile:r}.zsh
fi
fi
fi

if [[ ! -e "$bundlefile" ]]; then
# the files can't have the same name
print -ru2 -- "antidote: bundle file not found '$bundlefile'."
return 1
elif [[ "$bundlefile" == "$staticfile" ]]; then
# the files can't have the same name
print -ru2 -- "antidote: bundle file and static file are the same '$bundlefile'."
return 1
fi

# regenerate the static file based on whether the bundle file is newer and whether
# antidote home exists and is ready to be loaded
local force_bundle=0
if ! zstyle -t ':antidote:load:checkfile' disabled; then
local loadable_check_path="$(antidote-home)/.antidote.load"
if [[ ! -e $loadable_check_path ]]; then
force_bundle=1
[[ -d $loadable_check_path:h ]] || mkdir -p $loadable_check_path:h
touch $loadable_check_path
fi
fi

if [[ ! $staticfile -nt $bundlefile ]] || [[ $force_bundle -eq 1 ]]; then
antidote bundle <"$bundlefile" >|"$staticfile"
if [[ -r "${staticfile}.zwc" ]] && ! zstyle -t ':antidote:static' zcompile; then
command rm -f -- "${staticfile}.zwc"
fi
fi

# tell antidote-load what to source
typeset -g REPLY=$staticfile
#print $REPLY
#}
69 changes: 14 additions & 55 deletions functions/antidote-load
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,22 @@
# usage: antidote load [-h|--help] [<bundlefile> [<staticfile>]]
#
#function antidote-load {
setopt extended_glob

local o_help
zparseopts $_adote_zparopt_flags -- h=o_help -help=h || return 1

if (( $#o_help )); then
if [[ "$1" == (-h|--help) ]]; then
antidote-help load
return
fi

# pass in bundle file, read from zstyle, or use default .zsh_plugins.txt
local bundlefile="$1"
if [[ -z "$bundlefile" ]]; then
zstyle -s ':antidote:bundle' file 'bundlefile' ||
bundlefile=${ZDOTDIR:-$HOME}/.zsh_plugins.txt
fi

# pass in static file, read from zstyle, change extension, or use default .zsh_plugins.zsh
local staticfile="$2"
if [[ -z "$staticfile" ]]; then
zstyle -s ':antidote:static' file 'staticfile'
if [[ -z "$staticfile" ]]; then
if [[ -z "$bundlefile:t:r" ]]; then
staticfile=${bundlefile}.zsh
else
staticfile=${bundlefile:r}.zsh
fi
fi
fi

if [[ ! -e "$bundlefile" ]]; then
# the files can't have the same name
print -ru2 -- "antidote: bundle file not found '$bundlefile'."
return 1
elif [[ "$bundlefile" == "$staticfile" ]]; then
# the files can't have the same name
print -ru2 -- "antidote: bundle file and static file are the same '$bundlefile'."
return 1
fi

# regenerate the static file based on whether the bundle file is newer and whether
# antidote home exists and is ready to be loaded
local force_bundle=0
if ! zstyle -t ':antidote:load:checkfile' disabled; then
local loadable_check_path="$(antidote-home)/.antidote.load"
if [[ ! -e $loadable_check_path ]]; then
force_bundle=1
[[ -d $loadable_check_path:h ]] || mkdir -p $loadable_check_path:h
touch $loadable_check_path
fi
fi

if [[ ! $staticfile -nt $bundlefile ]] || [[ $force_bundle -eq 1 ]]; then
antidote bundle <"$bundlefile" >|"$staticfile"
if [[ -r "${staticfile}.zwc" ]] && zstyle -T ':antidote:static' zcompile; then
command rm -f -- "${staticfile}.zwc"
fi
fi
source "$staticfile"
# We can't use LOCAL_OPTIONS because sourcing plugins means we'd lose any Zsh options
# set in those plugins, so we delegate all the work to __antidote_load_prep where
# we can safely use LOCAL_OPTIONS. For this function, we should do the bare minimum
# so the user can set whatever crazy Zsh options they want, and antidote doesn't need
# to concern itself with that.
#
# "Is your house on fire, Clark? No, Aunt Bethany, those are the user's Zsh options."
#
typeset -g REPLY=
__antidote_load_prep "$@" || return 1
[[ -f "$REPLY" ]] || return 2
source "$REPLY"
unset REPLY
#}

0 comments on commit 53bb25f

Please sign in to comment.