compadd doesn't work when called from nested functions

260 views Asked by At

I have some custom completion I've been working on and am stuck. It extends existing completion scripts with some custom options. The full file can be found here https://github.com/rothgar/k/blob/zsh-completion/completions/zsh/k

I have a custom function called __k_handle_kspace which looks at the current word and does a basic case statement and calls another function. (pasting code without comments and extra options here)

__k_handle_kspace() {
    cur="${words[$CURRENT]}"

    case $cur in
    +* )
        __k_kspace_parse_config_contexts
        ;;
    @* )
        __k_kspace_parse_config_clusters
esac

When I set compdef __k_handle_kspace k this works great and all tab completion is exactly what I want. The full __k_kspace_parse_config_* function can be found here

The completion by default uses __start_k which calls __k_handle_word which then calls my __k_handle_kspace function.

When I set compdef __start_k k I can see my functions being called (using set -x for debugging) and compadd being the last thing called but no tab completion is shown.

When I use the default completion I also have to change the cur variable to cur="${words[$(($CURRENT -1))]}" in my __k_handle_kspace function.

I cant figure out if there's a variable I need to set/return from my function or rules around when compadd can be called to return completion values.


1

There are 1 answers

1
Marlon Richert On BEST ANSWER

The completion code you're extending is based on bashcompinit. As a result of this, you need to write your code as a Bash completion function. This means you should add your completion matches to the array COMPREPLY. Because that array is empty when your function returns, _bash_complete reports to Zsh's _main_complete that it has failed.

So, in short: Add your completion matches to COMPREPLY, instead of using compadd, and that should fix it.