I'm trying to make use of zsh's completion widgets in my own scripts. At one point I'd like have access to the result of every completion that is active in zsh, or rather I want to obtain the final result of the completion chain.
function foo() {
do_unrelated_stuff()
# call zsh completion
comp_results=_main_complete_() # won't work like this, can only be called from within a completion function
if [[ "$#comp_results" -gt "0" ]]; then
# do something
echo "results:"
else
# do something else
echo "no results"
fi
}
I already thought of creating a custom completion function that just calls
_main_complete
(or whichever function is appropriate) and stores the results in an exported variable to make it accessible to other functions. However, this would also imply calling the custom completion function somehow – and I don't know how. Is it even possible to get "intermediate" completion results without performing the actual completion?
Best Answer
I'm not sure why zsh doesn't provide this functionality... Luckily someone made a hack to use the completion system pragmatically/non-interactively: https://github.com/Valodim/zsh-capture-completion
This only give access to the finished completion list. Ie. not the internal zsh objects (descriptions, etc.)
(A similar question: Is it possible to manually invoke a zsh completion function?)