From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15256 invoked by alias); 29 Jun 2018 15:55:00 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 23520 Received: (qmail 15793 invoked by uid 1010); 29 Jun 2018 15:55:00 -0000 X-Qmail-Scanner-Diagnostics: from 195.159.176.226 by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(195.159.176.226):SA:0(2.6/5.0):. Processed in 0.478796 secs); 29 Jun 2018 15:55:00 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: ** X-Spam-Status: No, score=2.6 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_MUA_MOZILLA,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,NML_ADSP_CUSTOM_MED,RDNS_NONE autolearn=no autolearn_force=no version=3.4.1 X-Envelope-From: gcszu-zsh-users@m.gmane.org X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | X-Injected-Via-Gmane: http://gmane.org/ To: zsh-users@zsh.org From: "Scott Frazer (scfrazer)" Subject: Re: Completion of two items separated by a dash Date: Fri, 29 Jun 2018 11:54:43 -0400 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@blaine.gmane.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 In-Reply-To: Content-Language: en-US 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', 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= _values "second" $second else local first= _values -s - "first" $first fi ;; esac } _foo "$@"