Skip to content

Neovim automatic menus #7

@tjdevries

Description

@tjdevries

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions