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

Re: Separate cdpath elements in path-directories completion

Am Sonntag, den 19. Februar 2023 um 18:32 schrieb Oliver Kiddle:
[ moving to -workers and cc: Sebastian ]

Sebastian Stark wrote:
Thanks for showing me the right direction. I ended up with the
following, in case somebody wants that feature too. I can control the
behaviour by adding

   "zstyle ':completion:*:directories' cdpath-sections true"

to my config.

I realise that you posted this "in case somebody wants that feature
too" rather than as an explicit contribution but if we include this in
the distributed _cd, it'd be rather easier to use. For that, there's a
couple of changes I would suggest:

I wouldn't mind trying to get this in shape for an explicit contribution, if I can be of any help with it.

cd completion otherwise uses the tag "path-directories" for directories
from $CDPATH. It is always better if you can vary the context but reuse
an existing style name. In this case, the separate-sections style would
be appropriate. This would then need to be:

 zstyle ':completion:*:path-directories' separate-sections true

This makes much more sense indeed. And helped me understand zstyle better.

Also, I would probably suggest expanding the description to "directory
in $elem" instead of just "$elem" as here:

+        alt+=( "path-directories-$elem:$elem:_path_files -W $elem -/" )
+      done
+    else
+      (( $#tmpcdpath )) &&
+        alt=( 'path-directories:directory in cdpath:_path_files -W tmpcdpath -/' )

I'm also unsure about including $elem in the tag. It will probably work
fine but doesn't exactly conform with usual tag naming conventions.
Perhaps just an index for the position in the cdpath array. Does anyone
else have an opinion on that?

I think a tag should be a) unique and b) easily addressable via zstyle.

The index in $CDPATH would satisfy these requirements. Although if we had some normalized form of the directory here, e. g. by replacing all non-alphanumeric characters with '-', one could address an individual path by its name instead of its index, making my zstyling immune to the order in $CDPATH. For example for cdpath=(~/git /some/other/dir):

  zstyle ':completion:*:path-directories-1' format "Git Repositories:"

as opposed to:

  zstyle ':completion:*:path-directories---git' format "Git Repositories:"

The latter being slightly opaque syntax unfortunately.

Updated patch using the index and the other suggestions added:

--- /usr/share/zsh/functions/Completion/Zsh/_cd	2023-02-08 23:22:07.000000000 +0100
+++ .zfunc/_cd	2023-02-21 22:45:35.731260952 +0100
@@ -70,8 +70,15 @@


-    (( $#tmpcdpath )) &&
-      alt=( 'path-directories:directory in cdpath:_path_files -W tmpcdpath -/' )
+    if zstyle -t ":completion:${curcontext}:path-directories" separate-sections; then
+      local elem
+      for elem in $tmpcdpath; do
+        alt+=( "path-directories-${tmpcdpath[(ie)$elem]}:directory in $elem:_path_files -W $elem -/" )
+      done
+    else
+      (( $#tmpcdpath )) &&
+        alt=( 'path-directories:directory in cdpath:_path_files -W tmpcdpath -/' )
+    fi

     # With cdablevars, we can complete foo as if ~foo/
     if [[ -o cdablevars && -n "$PREFIX" && "$PREFIX" != <-> ]]; then


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