/
magic
42 lines (35 loc) · 912 Bytes
/
magic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# vim:filetype=zsh
_MAGIC_FILE=".zshrc"
_LAST_DIRECTORY=""
# Magically source files when changing directories. When changing to another
# directory, search the directory and its parents for a file called ".zshrc"
# and source it, if found. Useful to set stuff for projects
# automatically, like exporting PERL5LIB, generating ctags files, setting up
# project-specific aliases etc.
function chpwd; {
DIRECTORY="$PWD"
FOUND=""
while true; do
[[ $PWD = '/' || $PWD == $HOME ]] && break
if [ -f "./$_MAGIC_FILE" ]; then
FOUND="$PWD"
break
fi
builtin cd -q ..
done
if [[ -z $FOUND ]]; then
if [[ ! -z $_LAST_DIRECTORY ]]; then
if [[ ! -z $functions[unmagic] ]]; then
unmagic
functions[unmagic]=""
fi
_LAST_DIRECTORY=""
fi
else
if [[ $FOUND != $_LAST_DIRECTORY ]]; then
source "$FOUND/$_MAGIC_FILE"
_LAST_DIRECTORY="$PWD"
fi
fi
builtin cd -q "$DIRECTORY"
}