]> Sergey Matveev's repositories - zsh-autoenv.git/blobdiff - lib/varstash
minor: sync lib/varstash
[zsh-autoenv.git] / lib / varstash
index 320649c70e187c66d5549523b427a34b8009c716..94c063d28d3513b9a286ee456279eb43fc19da4e 100644 (file)
@@ -34,7 +34,7 @@
 #       autostash PATH
 #       export PATH=/something/else
 #
-#   You may also do both operations on line line, leaving only the very succinct
+#   You may also do both operations in one line, resulting in the very succinct
 #
 #       autostash PATH=/something/else
 #
 #
 ################################################################################
 
-
-# Library functions, from smartcd's lib/core/arrays. {{{
-function apush() {
-    local var=$1; shift
-    eval "$var=(\${$var[@]} \"\$@\")"
-}
-
-function alen() {
-    local var=$1
-
-    if [[ -n $var ]]; then
-        eval "echo \${#$var[@]}"
-    fi
-}
-
-function afirst() {
-    setopt localoptions && setopt ksharrays
-    local var=$1
-
-    if [[ -n $var ]] && (( $(eval "echo \${#$var[@]}") >= 1 )); then
-        eval "echo \"\${$var""[0]}\""
-    fi
-}
-
-function ashift() {
-    setopt localoptions && setopt ksharrays
-    local var=$1
-
-    local _ashift_return=
-
-    if [[ -n $var ]] && (( $(eval "echo \${#$var[@]}") >= 1 )); then
-        eval "_ashift_return=\"\${$var""[0]}\""
-        eval "$var""[0]=()"
-
-        echo "$_ashift_return"
-    fi
-}
-# }}}
-
-
 function stash() {
     if [[ $1 == "-f" ]]; then
         local force=1; shift
     fi
 
     while [[ -n $1 ]]; do
-        if [[ $1 == "alias" && $2 =~ "=" ]]; then
+        if [[ $1 == "alias" && $2 == *=* ]]; then
             shift
             local _stashing_alias_assign=1
             continue
@@ -174,21 +134,16 @@ function stash() {
         # Handle any variable that may exist under this name
         local vartype="$(declare -p $stash_which 2>/dev/null)"
         if [[ -n $vartype ]]; then
-            if [[ -n $ZSH_VERSION ]]; then
-                local pattern="^typeset"
-            else
-                local pattern="^declare"
-            fi
-            if [[ $vartype =~ $pattern" -a" ]]; then
+            if [[ $vartype == 'typeset -a'* ]]; then
                 # varible is an array
                 if [[ -z $already_stashed ]]; then
                     eval "__varstash_array__$stash_name=(\"\${$stash_which""[@]}\")"
                 fi
 
-            elif [[ $vartype =~ $pattern" -x" ]]; then
+            elif [[ $vartype == "export "* || $vartype == 'typeset -x'* ]]; then
                 # variable is exported
                 if [[ -z $already_stashed ]]; then
-                    eval "__varstash_export__$stash_name=\"\$$stash_which\""
+                    eval "export __varstash_export__$stash_name=\"\$$stash_which\""
                 fi
                 if [[ $stash_which != $stash_expression && -z $_stashing_alias_assign ]]; then
                     eval "export $stash_which=\"$stash_value\""
@@ -214,7 +169,7 @@ function stash() {
             # (eval):1: command not found: __varstash_nostash___tmp__home_dolszewski_src_smartcd_RANDOM_VARIABLE=1
             # fixed in zsh commit 724fd07a67f, version 4.3.14
             if [[ -z $already_stashed ]]; then
-                eval "__varstash_nostash__$stash_name=1"
+                eval "export __varstash_nostash__$stash_name=1"
             fi
 
             # In the case of a previously unset variable that we're assigning too, export it
@@ -228,10 +183,22 @@ function stash() {
     done
 }
 
+function get_autostash_array_name() {
+    local autostash_name=$(_mangle_var AUTOSTASH)
+    # Create a scalar variable linked to an array (for exporting).
+    local autostash_array_name=${(L)autostash_name}
+    if ! (( ${(P)+autostash_array_name} )); then
+        # Conditionally set it, to prevent error with Zsh 4.3:
+        # can't tie already tied scalar: ...
+        typeset -xT $autostash_name $autostash_array_name
+    fi
+    ret=$autostash_array_name
+}
+
 function autostash() {
     local run_from_autostash=1
     while [[ -n $1 ]]; do
-        if [[ $1 == "alias" && $2 = *=* ]]; then
+        if [[ $1 == "alias" && $2 == *=* ]]; then
             shift
             local _stashing_alias_assign=1
         fi
@@ -239,9 +206,9 @@ function autostash() {
         local already_stashed=
         stash "$1"
         if [[ -z $already_stashed ]]; then
-            local autostash_name=$(_mangle_var AUTOSTASH)
-            local varname=${1%%'='*}
-            apush $autostash_name "$varname"
+            local ret varname=${1%%'='*}
+            get_autostash_array_name
+            eval "$ret=(\$$ret \$varname)"
         fi
         shift
         unset -v _stashing_alias_assign
@@ -310,7 +277,8 @@ function unstash() {
         fi
         if [[ ( -n "$nostash" && -z "$unstashed" ) || ( -n "$unstashed" && -z "$unstashed_variable" ) ]]; then
             # Don't try to unset illegal variable names
-            if ! [[ $unstash_which =~ [^a-zA-Z0-9_] || $unstash_which =~ ^[0-9] ]]; then
+            # Using substitution to avoid using regex, which might fail to load on Zsh (minimal system).
+            if [[ ${unstash_which//[^a-zA-Z0-9_]/} == $unstash_which && $unstash_which != [0-9]* ]]; then
                 unset -v $unstash_which
             fi
         fi
@@ -321,15 +289,14 @@ function unstash() {
 
 function autounstash() {
     # If there is anything in (mangled) variable AUTOSTASH, then unstash it
-    local autounstash_name=$(_mangle_var AUTOSTASH)
-    if (( $(alen $autounstash_name) > 0 )); then
+    local ret
+    get_autostash_array_name
+    if (( ${#${(P)ret}} > 0 )); then
         local run_from_autounstash=1
-        while (( $(alen $autounstash_name) > 0 )); do
-            local autounstash_var=$(afirst $autounstash_name)
-            ashift $autounstash_name >/dev/null
+        for autounstash_var in ${(P)ret}; do
             unstash $autounstash_var
         done
-        unset $autounstash_name
+        unset $ret
     fi
 }
 
@@ -340,4 +307,4 @@ function _mangle_var() {
     echo "_tmp_${mangle_var_where}_${mangled_name}"
 }
 
-# vim: filetype=sh autoindent expandtab shiftwidth=4 softtabstop=4
+# vim: filetype=zsh autoindent expandtab shiftwidth=4 softtabstop=4