devshorts
devshorts

Reputation: 8872

Invoke custom function in zsh completion?

I can't seem to figure out a way to call a zsh completion function that I can tap into and provide a return result of available items. For example, I'd like to be able to call out to a web service and return back an array of potentials.

I've tried something like this:

#compdef test

local arguments


_run(){
    reply=(1 2 3)
}

arguments=(
'--test[foo]:bar:_run'
)

_arguments -s $arguments

If I put an echo in the _run function I can see it getting executed, but zsh always says there are no matches

Upvotes: 1

Views: 367

Answers (1)

devshorts
devshorts

Reputation: 8872

Took me a while to figure this out (and only because I stole it from the brew zsh completions file:

#compdef test

local arguments

_run(){
    val=(1 2 3)
    _wanted val expl "Items" compadd -a val
}

_biz(){
    val=(4 5 6)
    _wanted val expl "Biz" compadd -a val
}

local expl
local -a val

arguments=(
'--test[foo]:bar:_run'
'--biz[boo]:boo:_biz'
)

_arguments $arguments

Now you can do

$ test --test
 -- Items --
 1  2  3

and

$ test --test 2 --biz 4
-- Biz --
4  5  6

Upvotes: 2

Related Questions