skywind3000 / quickmenu.vim

A nice customizable popup menu for vim
MIT License
278 stars 12 forks source link

Neovim automatic menus #7

Open tjdevries opened 7 years ago

tjdevries commented 7 years ago

Hello again :)

Neovim recently added a function menu_get. I'm not sure if you use Neovim or not, but I think this could be useful for quickmenu. I made an example of what could be done to auto generate "quickmenus" from built-in menus.

For example, with Vimwiki, you can generate a menu automatically that looks like this:

image

let s:debug = v:false

" Function to handle creating a menu entry for an actual mapping
function! s:handle_maps(menu, mode) abort
  if !has_key(a:menu, 'mappings')
    return
  endif

  if !has_key(a:menu.mappings, a:mode)
    return
  endif

  let rhs = a:menu.mappings[a:mode].rhs
  let rhs = substitute(rhs, "\r", '', 'g')
  let rhs = substitute(rhs, "\n", '', 'g')

  if s:debug
    echo 'MENU.RHS: ' string(rhs)
  endif

  call quickmenu#append(a:menu.name, rhs)
endfunction

" Recursive function to check for menus and handle menus
function! s:handle_menu(menu, mode) abort
  if has_key(a:menu, 'submenus')
    if s:debug
      echo 'MENU: ' . string(a:menu.name)
    endif

    call quickmenu#append('# ' . a:menu.name, '')
    call s:handle_maps(a:menu, a:mode)

    for submenu in a:menu.submenus
      call s:handle_menu(submenu, a:mode)
    endfor
  else
    call s:handle_maps(a:menu, a:mode)
  endif
endfunction

" Entry point for making menus
function! Menu2Quick(name, mode) abort
  let menu_list = menu_get(a:name, a:mode)

  call quickmenu#reset()
  call quickmenu#header(a:name)

  for menu in menu_list
    call s:handle_menu(menu, a:mode)
  endfor

  call quickmenu#toggle(0)
endfunction

Would you be interested in including this in your plugin? If not, that's fine. If so, I can make a pull request for it and clean things up.

shaform commented 5 years ago

Is there any documentation about menu_get? I am interested in automatic menus as well.