Skip to content

Instantly share code, notes, and snippets.

@skanev
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skanev/25eb993d59c49f26035c to your computer and use it in GitHub Desktop.
Save skanev/25eb993d59c49f26035c to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
c_dir=~/.zsh/contexts
c_contexts=($c_dir/*(:t))
c-chpwd() {
local matches
matches=(${${(s:/:)PWD}:*c_contexts})
if [[ $#matches -gt 1 ]]; then
print "WARNING: multiple contexts match: ${(j:, :)matches}"
return
fi
if [[ $#matches -eq 0 && -n $c_current ]]; then
c-unload
elif [[ $#matches -eq 1 ]]; then
c-load ${matches[1]}
fi
}
c-load() {
if [[ "$c_current" == $1 ]]; then
return
fi
local file=$c_dir/$1
c_functions=${(z)$(awk \
'/^[-a-zA-Z0-9_]+[[:space:]]*\(\)[[:space:]]*{/ { gsub("\\(\\)", "", $1); print $1; } \
/^function/ { gsub("\\(\\)", "", $2); print $2; }' \
$file)}
c_current=$1
source $file
}
c-unload() {
for fn in $c_functions; do
unfunction $fn
done
c_functions=
c_current=
}
chpwd_functions=(${chpwd_functions[@]} "c-chpwd")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment