1 ################################################################################
2 # Stash/unstash support for per-directory variables
4 # Adopted for zsh-autoenv.
6 # Copyright (c) 2009,2012 Dave Olszewski <cxreg@pobox.com>
7 # http://github.com/cxreg/smartcd
9 # This code is released under GPL v2 and the Artistic License, and
10 # may be redistributed under the terms of either.
13 # This library allows you to save the current value of a given environment
14 # variable in a temporary location, so that you can modify it, and then
15 # later restore its original value.
17 # Note that you will need to be in the same directory you were in when you
18 # stashed in order to successfully unstash. This is because the temporary
19 # variable is derived from your current working directory's path.
23 # export PATH=/something/else
27 # Note that this was written for use with, and works very well with,
28 # smartcd. See the documentation there for examples.
30 # An alternate usage is `autostash' which will trigger autounstash when
31 # leaving the directory, if combined with smartcd. This reduces the amount
32 # of explicit configuration you need to provide:
35 # export PATH=/something/else
37 # You may also do both operations on line line, leaving only the very succinct
39 # autostash PATH=/something/else
41 # If you attempt to stash the same value twice, a warning will be displayed
42 # and the second stash will not occur. To make it happen anyway, pass -f
43 # as the first argument to stash.
47 # You have already stashed FOO, please specify "-f" if you want to overwrite another stashed value
51 # This rule is a bit different if you are assigning a value and the variable
52 # has already been stashed. In that case, the new value will be assigned, but
53 # the stash will not be overwritten. This allows for non-conflicting chained
56 ################################################################################
60 if [[ $1 == "-f" ]]; then
65 if [[ $1 == "alias" && $2 == *=* ]]; then
67 local _stashing_alias_assign=1
71 local stash_expression=$1
72 local stash_which=${stash_expression%%'='*}
73 local stash_name=$(_mangle_var $stash_which)
75 # Extract the value and make it double-quote safe
76 local stash_value=${stash_expression#*'='}
77 stash_value=${stash_value//\\/\\\\}
78 stash_value=${stash_value//\"/\\\"}
79 stash_value=${stash_value//\`/\\\`}
80 stash_value=${stash_value//\$/\\\$}
82 if [[ ( -n "$(eval echo '$__varstash_alias__'$stash_name)" ||
83 -n "$(eval echo '$__varstash_function__'$stash_name)" ||
84 -n "$(eval echo '$__varstash_array__'$stash_name)" ||
85 -n "$(eval echo '$__varstash_export__'$stash_name)" ||
86 -n "$(eval echo '$__varstash_variable__'$stash_name)" ||
87 -n "$(eval echo '$__varstash_nostash__'$stash_name)" )
90 if [[ -z $already_stashed && ${already_stashed-_} == "_" ]]; then
91 local already_stashed=1
96 if [[ $stash_which == $stash_expression ]]; then
97 if [[ -z $run_from_smartcd ]]; then
98 echo "You have already stashed $stash_which, please specify \"-f\" if you want to overwrite another stashed value"
101 # Skip remaining work if we're not doing an assignment
107 # Handle any alias that may exist under this name
108 if [[ -z $already_stashed ]]; then
109 local alias_def="$(eval alias $stash_which 2>/dev/null)"
110 if [[ -n $alias_def ]]; then
111 alias_def=${alias_def#alias }
112 eval "__varstash_alias__$stash_name=\"$alias_def\""
116 if [[ $stash_which != $stash_expression && -n $_stashing_alias_assign ]]; then
117 eval "alias $stash_which=\"$stash_value\""
120 # Handle any function that may exist under this name
121 if [[ -z $already_stashed ]]; then
122 local function_def="$(declare -f $stash_which)"
123 if [[ -n $function_def ]]; then
124 # make function definition quote-safe. because we are going to evaluate the
125 # source with "echo -e", we need to double-escape the backslashes (so 1 -> 4)
126 function_def=${function_def//\\/\\\\\\\\}
127 function_def=${function_def//\"/\\\"}
128 function_def=${function_def//\`/\\\`}
129 function_def=${function_def//\$/\\\$}
130 eval "__varstash_function__$stash_name=\"$function_def\""
135 # Handle any variable that may exist under this name
136 local vartype="$(declare -p $stash_which 2>/dev/null)"
137 if [[ -n $vartype ]]; then
138 if [[ $vartype == 'typeset -a'* ]]; then
139 # varible is an array
140 if [[ -z $already_stashed ]]; then
141 eval "__varstash_array__$stash_name=(\"\${$stash_which""[@]}\")"
144 elif [[ $vartype == "export "* || $vartype == 'typeset -x'* ]]; then
145 # variable is exported
146 if [[ -z $already_stashed ]]; then
147 eval "export __varstash_export__$stash_name=\"\$$stash_which\""
149 if [[ $stash_which != $stash_expression && -z $_stashing_alias_assign ]]; then
150 eval "export $stash_which=\"$stash_value\""
154 if [[ -z $already_stashed ]]; then
155 eval "__varstash_variable__$stash_name=\"\$$stash_which\""
157 if [[ $stash_which != $stash_expression && -z $_stashing_alias_assign ]]; then
158 eval "$stash_which=\"$stash_value\""
165 if [[ -z $stashed ]]; then
166 # Nothing in the variable we're stashing, but make a note that we stashed so we
167 # do the right thing when unstashing. Without this, we take no action on unstash
169 # Zsh bug sometimes caues
170 # (eval):1: command not found: __varstash_nostash___tmp__home_dolszewski_src_smartcd_RANDOM_VARIABLE=1
171 # fixed in zsh commit 724fd07a67f, version 4.3.14
172 if [[ -z $already_stashed ]]; then
173 eval "export __varstash_nostash__$stash_name=1"
176 # In the case of a previously unset variable that we're assigning too, export it
177 if [[ $stash_which != $stash_expression && -z $_stashing_alias_assign ]]; then
178 eval "export $stash_which=\"$stash_value\""
183 unset -v _stashing_alias_assign
187 function get_autostash_array_name() {
188 local autostash_name=$(_mangle_var AUTOSTASH)
189 # Create a scalar variable linked to an array (for exporting).
190 local autostash_array_name=${(L)autostash_name}
191 if ! (( ${(P)+autostash_array_name} )); then
192 # Conditionally set it, to prevent error with Zsh 4.3:
193 # can't tie already tied scalar: ...
194 typeset -xT $autostash_name $autostash_array_name
196 ret=$autostash_array_name
199 function autostash() {
200 local run_from_autostash=1
201 while [[ -n $1 ]]; do
202 if [[ $1 == "alias" && $2 == *=* ]]; then
204 local _stashing_alias_assign=1
207 local already_stashed=
209 if [[ -z $already_stashed ]]; then
210 local ret varname=${1%%'='*}
211 get_autostash_array_name
212 eval "$ret=(\$$ret \$varname)"
215 unset -v _stashing_alias_assign
220 while [[ -n $1 ]]; do
221 local unstash_which=$1
222 if [[ -z $unstash_which ]]; then
226 local unstash_name=$(_mangle_var $unstash_which)
228 # This bit is a little tricky. Here are the rules:
229 # 1) unstash any alias, function, or variable which matches
230 # 2) if one or more matches, but not all, delete any that did not
231 # 3) if none match but nostash is found, delete all
232 # 4) if none match and nostash not found, do nothing
235 if [[ -n "$(eval echo \$__varstash_alias__$unstash_name)" ]]; then
236 eval "alias $(eval echo \$__varstash_alias__$unstash_name)"
237 unset __varstash_alias__$unstash_name
239 local unstashed_alias=1
242 # Unstash any function
243 if [[ -n "$(eval echo \$__varstash_function__$unstash_name)" ]]; then
244 eval "function $(eval echo -e \"\$__varstash_function__$unstash_name\")"
245 unset __varstash_function__$unstash_name
247 local unstashed_function=1
250 # Unstash any variable
251 if [[ -n "$(declare -p __varstash_array__$unstash_name 2>/dev/null)" ]]; then
252 eval "$unstash_which=(\"\${__varstash_array__$unstash_name""[@]}\")"
253 unset __varstash_array__$unstash_name
255 local unstashed_variable=1
256 elif [[ -n "$(declare -p __varstash_export__$unstash_name 2>/dev/null)" ]]; then
257 eval "export $unstash_which=\"\$__varstash_export__$unstash_name\""
258 unset __varstash_export__$unstash_name
260 local unstashed_variable=1
261 elif [[ -n "$(declare -p __varstash_variable__$unstash_name 2>/dev/null)" ]]; then
262 # Unset variable first to reset export
263 unset -v $unstash_which
264 eval "$unstash_which=\"\$__varstash_variable__$unstash_name\""
265 unset __varstash_variable__$unstash_name
267 local unstashed_variable=1
270 # Unset any values which did not exist at time of stash
271 local nostash="$(eval echo \$__varstash_nostash__$unstash_name)"
272 unset __varstash_nostash__$unstash_name
273 if [[ ( -n "$nostash" && -z "$unstashed" ) || ( -n "$unstashed" && -z "$unstashed_alias" ) ]]; then
274 unalias $unstash_which 2>/dev/null
276 if [[ ( -n "$nostash" && -z "$unstashed" ) || ( -n "$unstashed" && -z "$unstashed_function" ) ]]; then
277 unset -f $unstash_which 2>/dev/null
279 if [[ ( -n "$nostash" && -z "$unstashed" ) || ( -n "$unstashed" && -z "$unstashed_variable" ) ]]; then
280 # Don't try to unset illegal variable names
281 # Using substitution to avoid using regex, which might fail to load on Zsh (minimal system).
282 if [[ ${unstash_which//[^a-zA-Z0-9_]/} == $unstash_which && $unstash_which != [0-9]* ]]; then
283 unset -v $unstash_which
291 function autounstash() {
292 # If there is anything in (mangled) variable AUTOSTASH, then unstash it
294 get_autostash_array_name
295 if (( ${#${(P)ret}} > 0 )); then
296 local run_from_autounstash=1
297 for autounstash_var in ${(P)ret}; do
298 unstash $autounstash_var
304 function _mangle_var() {
305 local mangle_var_where="${varstash_dir:-$PWD}"
306 mangle_var_where=${mangle_var_where//[^A-Za-z0-9]/_}
307 local mangled_name=${1//[^A-Za-z0-9]/_}
308 echo "_tmp_${mangle_var_where}_${mangled_name}"
311 # vim: filetype=zsh autoindent expandtab shiftwidth=4 softtabstop=4