]> Sergey Matveev's repositories - zk.zsh.git/commitdiff
getrel without subshell calls
authorSergey Matveev <stargrave@stargrave.org>
Sun, 13 Mar 2022 10:35:44 +0000 (13:35 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 13 Mar 2022 10:35:44 +0000 (13:35 +0300)
zk.zsh

diff --git a/zk.zsh b/zk.zsh
index 94a670067926cfcc7aeca94a164cf4204fb39882..5a46d999208819990a59ed9c152d83c38debec84 100755 (executable)
--- a/zk.zsh
+++ b/zk.zsh
@@ -51,7 +51,22 @@ for p (${(k)backs}) {
 
 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() {