Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Directory completion acts as if CHASE_LINKS is set



On Sep 5,  1:23pm, Jan Larres wrote:
}
} I recently noticed that completion of directories acts as if the 
} CHASE_LINKS option is set, that is if you are in a symlinked directory 
} and you want to complete its siblings then it will complete using the 
} physical siblings instead of the ones from the directory that the 
} symlink resides in.

"cd" itself is smart enough to interpret "../foo" as a physical path when
using it as a symbolic path does not work, so the completion list in this
example is incomplete rather than incorrect, which is probably why no one
has ever noticed.

In any case, this is a problem inherent in the way globbing interprets
"../" -- filename generation does not obey the same rules as "cd" path
resolution.

torch% print ../*(/)
../bar ../foo
torch% print $PWD:h/*(/)
/tmp/cdtest/foo/baz

Completion uses globbing to generate the target file names, so this has
to be fixed up somehow in _cd before _path_files is called.

This patch is a step in the right direction, but has the side-effect of
expanding ".." into $PWD:h which is probably not desirable.  There may
be other nuances [particularly with completion in the middle of a word]
that are not handled.  So I'm not going to commit it, just presenting it
for purposes of discussion.

diff --git a/Completion/Zsh/Command/_cd b/Completion/Zsh/Command/_cd
index 476947f..9c82a2f 100644
--- a/Completion/Zsh/Command/_cd
+++ b/Completion/Zsh/Command/_cd
@@ -51,6 +51,14 @@ else
     _directory_stack && ret=0
   fi
 
+  if [[ $PREFIX = (|*/)..(|/*) ]]; then
+    local tmpprefix
+    # Use cd in a subshell to properly [not] resolve symlinks
+    tmpprefix=$(cd $PREFIX >&/dev/null && print $PWD) ||
+      tmpprefix=$(cd $PREFIX:h >&/dev/null && print $PWD/$PREFIX:t)
+    [[ -n $tmpprefix ]] && PREFIX=$tmpprefix
+  fi
+
   if [[ $PREFIX != (\~|/|./|../)* ]]; then
     local tmpcdpath alt
 

-- 
Barton E. Schaefer



Messages sorted by: Reverse Date, Date, Thread, Author