I have many menu entries like these:
nnoremenu <silent> 94.015.10 &MyMenu.Test\ :call Test("%","keyw2",keyw3")<CR>
vnoremenu <silent> 94.015.10 &MyMenu.Test\ :<C-U>call Test("'<,'>","keyw2",keyw3")<CR>
One for normal mode 'nnoremenu' and
One for visual mode 'vnoremenu'
With the same keywords except the first one ("%","'<,'>")
Is there no way to merge them together?
p.e. is it possible to do this:
an <silent> 94.015.10 &MyMenu.Test\ :call Test("","keyw2",keyw3")<CR>
and check within the function if normal mode or visual mode is active?
When you use :an
, the visual mode is automatically aborted via <C-C>
. This means that there is no way to retrieve the mode any more (and your <C-U>
prefix is not necessary); if you need the mode information, you have to keep the two different menu definitions.
If you would like to reduce the code duplication, you have to utilize other means, e.g. metaprogramming (that is, building and :executing
the menu definition commands in a loop).
Edit: On request, here's an example of how I would tackle this to avoid the duplication:
for [mode, range] in [['n', '%'], ['v', "'<,'>"]]
execute printf('%snoremenu <silent> 94.015.10 &MyMenu.Test\ :call Test(%s,"keyw2","keyw3")<CR>', mode, string(range))
endfor