]> Sergey Matveev's repositories - zk.zsh.git/blobdiff - zk.zsh
Simpler deduplication
[zk.zsh.git] / zk.zsh
diff --git a/zk.zsh b/zk.zsh
index 94a670067926cfcc7aeca94a164cf4204fb39882..5326cb08b0f3e5d18cc6adf7807f24a14395d99a 100755 (executable)
--- a/zk.zsh
+++ b/zk.zsh
@@ -34,24 +34,34 @@ for p (${(k)pages}) {
 }
 
 # Deduplicate all references
-typeset -A ws
 for p (${(k)links}) {
-    ws=()
-    for w (${=${links[$p]}}) {
-        ws[$w]=1
-        backs[$w]="$p ${backs[$w]}"
-    }
-    links[$p]=${(k)ws}
+    local ws=(${(u)=links[$p]})
+    links[$p]=${(j: :)ws}
+    for w ($ws) backs[$w]="$p ${backs[$w]}"
 }
 for p (${(k)backs}) {
-    ws=()
-    for w (${=${backs[$p]}}) ws[$w]=1
-    backs[$p]=${(k)ws}
+    local ws=(${(u)=backs[$p]})
+    backs[$p]=${(j: :)ws}
 }
 
 autoload -U relative
 getrel() {
-    relative $2:a $1:h
+    # nearly the copy-paste of Functions/Misc/relative
+    local dst=$2:a
+    local src=$1:h:a
+    local -a cur abs
+    cur=(${(s:/:)src})
+    abs=(${(s:/:)dst:h} $dst:t)
+    integer i=1
+    while [[ i -le $#abs && $abs[i] == $cur[i] ]] ; do
+        ((++i > $#cur)) && {
+            REPLY=${(j:/:)abs[i,-1]}
+            return
+        }
+    done
+    src=${(j:/:)cur[i,-1]/*/..}
+    dst=${(j:/:)abs[i,-1]}
+    REPLY=$src${dst:+/$dst}
 }
 
 genhtml() {