Skip to content

Instantly share code, notes, and snippets.

@sooop
Last active August 25, 2022 14:28
Show Gist options
  • Save sooop/84b8f2cc676383d6b5f884d47e2b4bcc to your computer and use it in GitHub Desktop.
Save sooop/84b8f2cc676383d6b5f884d47e2b4bcc to your computer and use it in GitHub Desktop.
vim8 popup menu with custom filter function
vim9script
var x = '[A]pple [B]anana [C]herry [O]range'->split()
def MyMenuHandler(winid: number, result: any)
popup_notification("you selected " .. x[result - 1] .. '.', { pos: 'center', time: 1000 })
enddef
def MyMenuFilter(winid: number, key: string): bool
var i = match('AaBbCcOo', key)
if i >= 0
popup_close(winid, 1 + (i / 2))
return true
endif
return popup_filter_menu(winid, key)
enddef
popup_menu(x, { pos: 'center', filter: MyMenuFilter, callback: MyMenuHandler })
let s:x = '[A]pple [B]anana [C]herry [O]range'->split()
function! s:my_menu_handler(winid, result) abort
call popup_notification("you selected " . s:x[a:result-1] . '.',
\#{pos:'center', time:1000})
endfunction
function! s:my_menu_filter(winid, key) abort
let i = stridx('AaBbCcOo', a:key)
if i >= 0
echom i/2
return popup_close(a:winid, 1 + (i/2))
endif
return popup_filter_menu(a:winid, a:key)
endfunction
let w = popup_menu(s:x, #{
\pos: 'center',
\filter:function('s:my_menu_filter'),
\callback:function('s:my_menu_handler')
\})
@sooop
Copy link
Author

sooop commented Apr 26, 2021

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment