For now I have the feeling the answer is no, but I'm asking to make sure that's the case, and to know if people has found workarounds or can suggest good approaches.
From :help popup_menu
I've found this example
func ColorSelected(id, result)
" use a:result
endfunc
call popup_menu(['red', 'green', 'blue'], #{
\ callback: 'ColorSelected',
\ })
and tried it out, chainging the commented line to echo a:result
, thus discovering that a:result
is actually the 1-based index of the item into the list that is the first argument to popup_menu
.
Indeed, at :help popup-callback
I read that (my emphasis)
The callback is invoked with two arguments: the ID of the popup window and the result, which could be an index in the popup lines, or whatever was passed as the second argument of
popup_close()
.
that seems to confirm my understanding.
So if I wanted the callback to echo
back red
/green
/blue
, what options would I have?
I'd be tempted to say that the only way to accomplish the task, without introducing code/data duplication, is to have a g:
lobal variable that both the call to popup_menu
and the definition of the callback share. Something like this:
let g:pumcolors = ['red', 'green', 'blue']
func ColorSelected(id, result)
echo 'choice made: ' .. a:result .. ' which is ' .. g:pumcolors[a:result - 1]
endfunc
call popup_menu(g:pumcolors, #{ callback: 'ColorSelected', })
So is this the only way? Or am I missing some obvious approach?