quickmenu.vim icon indicating copy to clipboard operation
quickmenu.vim copied to clipboard

Neovim automatic menus

Open tjdevries opened this issue 8 years ago • 1 comments

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.

tjdevries avatar Aug 09 '17 23:08 tjdevries

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

shaform avatar Dec 22 '18 01:12 shaform