zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: File completion with **/ (with solution)
Date: Tue, 20 Sep 2016 17:56:23 -0700	[thread overview]
Message-ID: <160920175623.ZM32730@torch.brasslantern.com> (raw)

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.


             reply	other threads:[~2016-09-21  0:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-21  0:56 Bart Schaefer [this message]
2016-09-21  5:32 ` Bart Schaefer
2016-09-21 20:30   ` Bart Schaefer
     [not found] <160920175623.ZM32730__25904.0603101768$1474419485$gmane$org@torch.brasslantern.com>
2016-09-22 21:12 ` Daniel Shahaf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=160920175623.ZM32730@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).