Adding print -l ${(u)foobar} workaround for pre-4.2.0 shells
authorFrank Terbeck <ft@grml.org>
Wed, 26 Mar 2008 19:21:48 +0000 (20:21 +0100)
committerFrank Terbeck <ft@grml.org>
Wed, 26 Mar 2008 19:21:48 +0000 (20:21 +0100)
This also enabled the dirstack magic for such shells again.
Also, this patch only sets defaults for DIRSTACKSIZE and introduces
a new variable DIRSTACKFILE (which defaults to ~/.zdirs). That way
you may change these defaults in your .zshenv for example.

debian/changelog
etc/zsh/zshrc

index 595693f..7f0972c 100644 (file)
@@ -1,3 +1,9 @@
+grml-etc-core (0.3.51) unstable; urgency=low
+
+  * zshrc: Add persistent dirstack support for shells older than 4.2.0
+
+ -- Frank Terbeck <ft@grml.org>  Wed, 26 Mar 2008 13:57:12 +0100
+
 grml-etc-core (0.3.50) unstable; urgency=low
 
   [ Michael Prokop ]
index b414c93..9a2a6c8 100644 (file)
@@ -258,6 +258,29 @@ salias() {
     return 0
 }
 
+# a "print -l ${(u)foo}"-workaround for pre-4.2.0 shells
+# usage: uprint foo
+#   Where foo is the *name* of the parameter you want printed.
+#   Note that foo is no typo; $foo would be wrong here!
+if ! is42 ; then
+    uprint () {
+        local -a u
+        local w
+        local parameter=${1}
+
+        if [[ -z ${parameter} ]] ; then
+            printf 'usage: uprint <parameter>\n'
+            return 1
+        fi
+
+        for w in ${(P)parameter} ; do
+            [[ -z ${(M)u:#${w}} ]] && u=( ${u} ${w} )
+        done
+
+        builtin print -l ${u}
+    }
+fi
+
 # Check if we can read given files and source those we can.
 xsource() {
     if (( ${#argv} < 1 )) ; then
@@ -931,21 +954,21 @@ isgrmlcd && SAVEHIST=1000 || SAVEHIST=10000 # useful for setopt append_history
 
 # dirstack handling {{{
 
-# TODO: the is42 tests are in place, because (u) requires at least 4.2.0
-#       of zsh. We also disable loading potentielly old .zdir files when
-#       starting a pre-4.2.0 zsh.
-#       Implementing a workaround-(u) for older shells is the obvious
-#       solution here, if we want this for all supported shells.
+DIRSTACKSIZE=${DIRSTACKSIZE:-20}
+DIRSTACKFILE=${DIRSTACKFILE:-${HOME}/.zdirs}
 
-DIRSTACKSIZE=20
-if is42 && [[ -f ~/.zdirs ]] && [[ ${#dirstack[*]} -eq 0 ]] ; then
-    dirstack=( ${(f)"$(< ~/.zdirs)"} )
+if [[ -f ${DIRSTACKFILE} ]] && [[ ${#dirstack[*]} -eq 0 ]] ; then
+    dirstack=( ${(f)"$(< $DIRSTACKFILE)"} )
     # "cd -" won't work after login by just setting $OLDPWD, so
     [[ -d $dirstack[0] ]] && cd $dirstack[0] && cd $OLDPWD
 fi
 
 chpwd() {
-    is42 && builtin print -l ${(u)dirstack} >! ~/.zdirs
+    if is42 ; then
+        builtin print -l ${(u)dirstack} >! ${DIRSTACKFILE}
+    else
+        uprint dirstack >! ${DIRSTACKFILE}
+    fi
 }
 
 # }}}