zsh-users
 help / color / mirror / code / Atom feed
* Completion of two items separated by a dash
@ 2018-06-28 19:40 Scott Frazer (scfrazer)
  2018-06-28 20:03 ` Eric Cook
  0 siblings, 1 reply; 3+ messages in thread
From: Scott Frazer (scfrazer) @ 2018-06-28 19:40 UTC (permalink / raw)
  To: zsh-users

I am trying to write a completion function for a command that takes two 
items separated by a dash, and the second item depends on the first. 
For example, suppose possible full completions were:

foo-bar
foo-baz
abc-def
abc-xyz

Ideally, the completion function would first present options "foo" and 
"abc", then after the user chooses one it inserts the "-', and when you 
hit tab again it presents either "bar" and "baz" or "def" and "xyz".

For this simple example I could just show all four, but in real life 
there are many first items and many second items and the list would be huge.

I have written some simple completions before, but this has me stumped. 
Is there a way to do this?

Thanks,
Scott


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

* Re: Completion of two items separated by a dash
  2018-06-28 19:40 Completion of two items separated by a dash Scott Frazer (scfrazer)
@ 2018-06-28 20:03 ` Eric Cook
  2018-06-29 15:54   ` Scott Frazer (scfrazer)
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Cook @ 2018-06-28 20:03 UTC (permalink / raw)
  To: zsh-users

On 06/28/2018 03:40 PM, Scott Frazer (scfrazer) wrote:
> I am trying to write a completion function for a command that takes two items separated by a dash, and the second item depends on the first. For example, suppose possible full completions were:
> 
> foo-bar
> foo-baz
> abc-def
> abc-xyz
> 
> Ideally, the completion function would first present options "foo" and "abc", then after the user chooses one it inserts the "-', and when you hit tab again it presents either "bar" and "baz" or "def" and "xyz".
> 
> For this simple example I could just show all four, but in real life there are many first items and many second items and the list would be huge.
> 
> I have written some simple completions before, but this has me stumped. Is there a way to do this?
> 
> Thanks,
> Scott
> 


_foo() {
  local -a ary
  ary=( foo-bar foo-baz abc-def abc-xyz )
  _arguments '1:description:_multi_parts -- - ary'
}
compdef _foo foo


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

* Re: Completion of two items separated by a dash
  2018-06-28 20:03 ` Eric Cook
@ 2018-06-29 15:54   ` Scott Frazer (scfrazer)
  0 siblings, 0 replies; 3+ messages in thread
From: Scott Frazer (scfrazer) @ 2018-06-29 15:54 UTC (permalink / raw)
  To: zsh-users

On 6/28/2018 4:03 PM, Eric Cook wrote:
> On 06/28/2018 03:40 PM, Scott Frazer (scfrazer) wrote:
>> I am trying to write a completion function for a command that takes two items separated by a dash, and the second item depends on the first. For example, suppose possible full completions were:
>>
>> foo-bar
>> foo-baz
>> abc-def
>> abc-xyz
>>
>> Ideally, the completion function would first present options "foo" and "abc", then after the user chooses one it inserts the "-', and when you hit tab again it presents either "bar" and "baz" or "def" and "xyz".
>>
>> For this simple example I could just show all four, but in real life there are many first items and many second items and the list would be huge.
>>
>> I have written some simple completions before, but this has me stumped. Is there a way to do this?
>>
>> Thanks,
>> Scott
>>
> 
> 
> _foo() {
>    local -a ary
>    ary=( foo-bar foo-baz abc-def abc-xyz )
>    _arguments '1:description:_multi_parts -- - ary'
> }
> compdef _foo foo
> 

Thanks!  This was tantalizingly close, but _multi_parts wasn't handling 
completion of the first part the way I prefer.  For example, if possible 
completions were:

foo-abc
foobar-xyz

and I type 'foo<TAB>', I like completion to give me the two options 
'foo' and 'foobar', but _multi_parts would see a 'complete' match and 
insert 'foo-' without asking about the ambiguity.

I did finally get things they way I wanted.  If anyone comes upon this 
thread in the future, here is basically what I ended up with:

_foo() {

     local curcontext="$curcontext" state

     _arguments -C '*: :->foo' && return 0

     case "$state" in
         foo)
             if compset -P '*[-]'; then
                 local first=${IPREFIX%-}
                 local second=<do something using $first to get values>
                 _values "second" $second
             else
                 local first=<do something here to get values>
                 _values -s - "first" $first
             fi
             ;;
     esac
}

_foo "$@"


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

end of thread, other threads:[~2018-06-29 15:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-28 19:40 Completion of two items separated by a dash Scott Frazer (scfrazer)
2018-06-28 20:03 ` Eric Cook
2018-06-29 15:54   ` Scott Frazer (scfrazer)

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