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

File completion with **/ (with solution)



This started out as an email to ponder/grumble about why _match can't
resolve a recursive glob.  Along the way I discovered a way to accomplish
what I wanted ... but I'll leave the leading expository stuff for context,
having taken the time to write it.

Using the _match completer, I can do (in the zsh source tree)

% ls C*/*/*/_main<TAB>

to get the expected result of

% ls Completion/Base/Core/_main_complete

On the other hand, if I do

% ls C*/**/_main<TAB>

I get no result.

If I have the _expand completer and manually append a "*" before hitting
TAB, I also get the desired result.  Or if I have _complete and do

ls C///_main<TAB>

then _path_files does magic for me, but I have to use the exact correct
number of slashes, which is precisely what I want to avoid doing.

The "problem" with _match is that it doesn't change the way that possible
completions are generated; it only changes the way the word on the line
is compared to the possible completions to select the candidate matches.

It was at this point that I re-read the doc for the _user_expand completer
for the first time in years.

_user_expand looks up the array-valued style "user-expand" and steps
through the array until one of the elements yields at least one possible
completion, by assigning it to the $reply array.  You can read the doc
to find out what the elements look like.  There are several things about
_user_expand that could be improved with features of more recent zsh,
and the doc doesn't cover all the other styles it recognized / tags it
uses, so that needs update too.  However, it's perfect for solving the
problem I just described:

  _glob_expand() { reply=( $~1* ) }
  zstyle :completion::user-expand:: user-expand _glob_expand
  zstyle :completion::user-expand:: tag-order expansions all-expansions
  zstyle ':completion:*' completer _expand _complete _match _user_expand

The other thing it might be useful to call out in the doc is to note that
because _user_expand is called AS a completer, it has access to more of
the zstyle $context when it is called than when the "completer" style is
looked up.  This could be handy when deciding what to assign to $reply.

It would mostly work to include other completers / completion functions
as elements of the user-expand array, but note that they'll ALL be tried
(rather than only tried until at least one finds a match), because the
functions normally won't assign to $reply and that is currently the only
way to stop walking the elements.

Daniel, this might be the "better answer" to your _match question, too.



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