zsh-users
 help / color / mirror / code / Atom feed
* Multipart autocomplete, zstyle, and cd
@ 2021-12-06  1:28 Zach Riggle
  2021-12-06  4:47 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Zach Riggle @ 2021-12-06  1:28 UTC (permalink / raw)
  To: Zsh Users

I would like to add a new autocomplete section to the tab-completion
of "cd".  I have some of the pieces of this working, but haven't
figured out how to hook it up correctly.

#-------------------------------- BACKGROUND ---------------------------------
I have a zsh autoloadable function, zshz-cd-completion.  Its
implementation is not relevant, except that it outputs a list of
directory names in a specific order.

For now, let's assume zsh-cd-completion emits the following lines:

    Downloads/
    bin/
    Library/
    Desktop/

#-------------------------------- COMPLETION ---------------------------------
I have completion working for a placeholder command, foobarbaz.  This
is done with a file, _foobarbaz, also in $fpath.  It is very
straightforward, and works as intended.

    #compdef foobarbaz

    _arguments \
        '*: :->custom'

    case "$state" in
        custom)
            local -a dirs=( $(zshz-cd-completion) )

            # Old: Does not preserve ordering
            # _values "popular directories" $dirs

            _wanted -V popular-directories expl 'popular directories' \
                compadd -Q -a dirs
            ;;
    esac

#-------------------------- COMPLETION ENHANCEMENT ---------------------------
This completion works well, though I would like to know how to get the
completion to not do word-separated completion and upon e.g.

    $ foobarbaz bi<tab>

Turns into...

    $ foobarbaz bin/

... to not add a space, but instead allow further specification e.g.,

    $ foobarbaz bin/subdir

#-------------------------- CURRENT CD AUTOCOMPLETE --------------------------
I would like to know how to get these results added to "cd"
autocompletion.  I expect that I can do this from zstyle.  Currently,
this is how things are configured:

    $ zstyle -L | grep :cd:
    zstyle ':completion:*:*:cd:*:directory-stack' menu yes select
    zstyle ':completion:*:*:cd:*' tag-order local-directories
directory-stack path-directories

When I do "cd <tab>", I get a single section, "-- local directory --".

#-------------------------- ADDING A SECTION TO CD ---------------------------
I tried my very best to comprehend Section 20 "Completion System" of
the documentation, and various other distillations on Github and so on
-- but this is the most complex documentation I've come across, and
the various "magic" that powers it is too complex to read through.

Any pointers would be useful -- it's not clear if I need to define a
new tag somehow (via _tags?) or modify the _cd function and put my
modified copy earlier in $fpath.  I hope / expect that this is
something that can be MOSTLY achieved via zstyle, but I'm not clear
how.

Zach Riggle


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Multipart autocomplete, zstyle, and cd
  2021-12-06  1:28 Multipart autocomplete, zstyle, and cd Zach Riggle
@ 2021-12-06  4:47 ` Bart Schaefer
  2021-12-06  8:53   ` zzapper
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2021-12-06  4:47 UTC (permalink / raw)
  To: Zach Riggle; +Cc: Zsh Users

On Sun, Dec 5, 2021 at 5:29 PM Zach Riggle <zachriggle@gmail.com> wrote:
>
>             _wanted -V popular-directories expl 'popular directories' \
>                 compadd -Q -a dirs
> #-------------------------- COMPLETION ENHANCEMENT ---------------------------
> This completion works well, though I would like to know how to get the
> completion to not do word-separated completion and upon e.g.
>
>     $ foobarbaz bi<tab>
>
> ... to not add a space, but instead allow further specification e.g.,

You should add the -f option to compadd, so that it knows the results
are file names (which includes directories).  I suspect you also do
not really need/want the -Q option.

You should also NOT include the trailing slash in the values returned
from zshz-cd-completion.  Either -f will cause compadd to figure that
out, or you should be passing the options -S "/" and -q to compadd (if
the strings returned by zshz-cd-completion might not refer to existing
directories).

> #-------------------------- CURRENT CD AUTOCOMPLETE --------------------------
> I would like to know how to get these results added to "cd"
> autocompletion.
>
> zstyle ':completion:*:*:cd:*' tag-order local-directories directory-stack path-directories
>
> When I do "cd <tab>", I get a single section, "-- local directory --".

With that tag-order, you will only see path-directories if $cdpath is
non-empty and there are no local directories.  Is that what you
wanted?  (Directory-stack completes shorthands like "+1" so it
normally doesn't matter where it appears in tag-order as long as it's
there at all.  [Someone please demonstrate how I've got this wrong, if
I have.])

> #-------------------------- ADDING A SECTION TO CD ---------------------------
>
> Any pointers would be useful -- it's not clear if I need to define a
> new tag somehow (via _tags?) or modify the _cd function and put my
> modified copy earlier in $fpath.  I hope / expect that this is
> something that can be MOSTLY achieved via zstyle, but I'm not clear
> how.

There are a bunch of ways depending on the effect you want to get ...
probably the easiest would be to use compdef to put a wrapper around
_cd that also calls your function.  Something like this:

compdef '_alternative \
  "popular-directories:popular directories:( $(zshz-cd-completion) )" \
  ::_cd' \
  cd

Caveat, this will misbehave if zshz-cd-completion outputs strings
containing double quotes or equal signs.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Multipart autocomplete, zstyle, and cd
  2021-12-06  4:47 ` Bart Schaefer
@ 2021-12-06  8:53   ` zzapper
  0 siblings, 0 replies; 3+ messages in thread
From: zzapper @ 2021-12-06  8:53 UTC (permalink / raw)
  To: zsh-users


On 06/12/2021 04:47, Bart Schaefer wrote:
> On Sun, Dec 5, 2021 at 5:29 PM Zach Riggle <zachriggle@gmail.com> wrote:
>>              _wanted -V popular-directories expl 'popular directories' \
>>                  compadd -Q -a dirs
>> #-------------------------- COMPLETION ENHANCEMENT ---------------------------
>> This completion works well, though I would like to know how to get the
>> completion to not do word-separated completion and upon e.g.
>>
>>      $ foobarbaz bi<tab>
>>
>> ... to not add a space, but instead allow further specification e.g.,
> You should add the -f option to compadd, so that it knows the results
> are file names (which includes directories).  I suspect you also do
> not really need/want the -Q option.
>
> You should also NOT include the trailing slash in the values returned
> from zshz-cd-completion.  Either -f will cause compadd to figure that
> out, or you should be passing the options -S "/" and -q to compadd (if
> the strings returned by zshz-cd-completion might not refer to existing
> directories).

Ah the dreaded 'optionally' unwanted trailing space when I want to do two stage completion.
Will see if this advice can help me

zzapper



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-12-06  8:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-06  1:28 Multipart autocomplete, zstyle, and cd Zach Riggle
2021-12-06  4:47 ` Bart Schaefer
2021-12-06  8:53   ` zzapper

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).