zsh-users
 help / color / mirror / code / Atom feed
* _arguments '(-o --option)'[...] functionality without calling _arguments
@ 2023-09-13  9:06 Adam Szkoda
  2023-09-13 11:41 ` Oliver Kiddle
  0 siblings, 1 reply; 4+ messages in thread
From: Adam Szkoda @ 2023-09-13  9:06 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 976 bytes --]

Hi ZSH hackers!

I'm trying to achieve a certain quite specific completion effect.  I
basically want to steal it from the cargo completion script.  I can't find
it in the source code of the _arguments function.

Rust's cargo completion script has this line in it: _arguments '(-p
--package)'{-p+,--package=}'[specify package to
build]:package:_cargo_package_names'

This results in very nice and compact completions where --package and -p
are grouped in a single line and share the same description, yet each of
them can be selected separately.  This is great.

Yet I can't find the code in _arguments that's responsible for this.  I'm
suspecting it's delegating to the comparguments builtin ([1])?

Is it possible to achieve that by calling compadd directly?  Maybe by
calling comparguments directly somehow?

Thanks in advance!
— adaszko

[1]
https://github.com/zsh-users/zsh/blob/95269147fcbd85961d652ab419bb168a8aafcd14/Src/Zle/computil.c#L2590

[-- Attachment #2: Type: text/html, Size: 1337 bytes --]

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

* Re: _arguments '(-o --option)'[...] functionality without calling _arguments
  2023-09-13  9:06 _arguments '(-o --option)'[...] functionality without calling _arguments Adam Szkoda
@ 2023-09-13 11:41 ` Oliver Kiddle
  2023-09-13 14:52   ` Adam Szkoda
  0 siblings, 1 reply; 4+ messages in thread
From: Oliver Kiddle @ 2023-09-13 11:41 UTC (permalink / raw)
  To: Adam Szkoda; +Cc: zsh-users

Adam Szkoda wrote:
> Rust's cargo completion script has this line in it: _arguments '(-p --package)'
> {-p+,--package=}'[specify package to build]:package:_cargo_package_names'
>
> This results in very nice and compact completions where --package and -p are
> grouped in a single line and share the same description, yet each of them can
> be selected separately.  This is great.
>
> Yet I can't find the code in _arguments that's responsible for this.  I'm
> suspecting it's delegating to the comparguments builtin ([1])?

_describe does the same thing for words that are not options. It's
implementation is in C code that is shared with that used by
comparguments. It doesn't have to be done in C, however - compadd's
interface would allow you to achieve the same thing from shell code if
you wanted but that is more involved so check whether _describe meets
your needs first.

If you're looking at other completion functions for inspiration, I would
recommend looking at those that come with zsh rather than those other
projects include. Cargo's is fairly good but external projects often
include completions that are auto-generated, converted from bash or
otherwise less well written.

Oliver


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

* Re: _arguments '(-o --option)'[...] functionality without calling _arguments
  2023-09-13 11:41 ` Oliver Kiddle
@ 2023-09-13 14:52   ` Adam Szkoda
  2023-09-14 11:14     ` Adam Szkoda
  0 siblings, 1 reply; 4+ messages in thread
From: Adam Szkoda @ 2023-09-13 14:52 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: zsh-users

On Wed, Sep 13, 2023 at 1:41 PM Oliver Kiddle <opk@zsh.org> wrote:
> _describe does the same thing for words that are not options. It's
> implementation is in C code that is shared with that used by
> comparguments. It doesn't have to be done in C, however - compadd's
> interface would allow you to achieve the same thing from shell code if
> you wanted but that is more involved so check whether _describe meets
> your needs first.

Great, here's what I found out.  What works with _arguments:

cmd.zsh:
    _cmd () {
        _arguments '(-p --package)'{-p+,--package=}'[specify package
to build]:package:_cargo_package_names'
        return 0
    }
    compdef _cmd cmd

% source cmd.zsh
% cmd <TAB> # does what I want

With _describe, the same effect is achieved with:

cmd.zsh:
    _cmd () {
        local -a args=("-p" "--package")
        local -a descrs=("-p:specify package to build"
"-package:specify package to build")
        _describe '' descrs args
        return 0
    }
    compdef _cmd cmd


% source cmd.zsh
% cmd <TAB> # also does what I want

So it works (provided list-grouped zstyle is set to true but that's totally
fine). I'd be curious however to know how to get the same effect with plain
compadd.

> If you're looking at other completion functions for inspiration, I would
> recommend looking at those that come with zsh rather than those other
> projects include. Cargo's is fairly good but external projects often
> include completions that are auto-generated, converted from bash or
> otherwise less well written.

Thanks, that's good to know!

> Oliver
Thank you for taking a look at this, Oliver!
— Adam


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

* Re: _arguments '(-o --option)'[...] functionality without calling _arguments
  2023-09-13 14:52   ` Adam Szkoda
@ 2023-09-14 11:14     ` Adam Szkoda
  0 siblings, 0 replies; 4+ messages in thread
From: Adam Szkoda @ 2023-09-14 11:14 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: zsh-users

> So it works (provided list-grouped zstyle is set to true but that's totally
> fine). I'd be curious however to know how to get the same effect with plain
> compadd.

I tried it more thoroughly and _describe turned out to be sufficient,
so the compadd solution is no longer necessary.

Thanks!
— Adam


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

end of thread, other threads:[~2023-09-14 11:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-13  9:06 _arguments '(-o --option)'[...] functionality without calling _arguments Adam Szkoda
2023-09-13 11:41 ` Oliver Kiddle
2023-09-13 14:52   ` Adam Szkoda
2023-09-14 11:14     ` Adam Szkoda

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