gvim popup menu item only in Nerdtree window
Question
I have a following menu item
nnoremenu 1.120 PopUp.Add\ &To\ &&Path :call NERDTreeAddNodeToPath()<CR>
but I want it to show only in Nerdtree window, how do I do it?
Solution
I think I've cracked this, but it's not as pretty as I had hoped. I have written a function NERDTreeMenuToggle()
which adds the line to the popup menu if the current buffer is filetype "nerdtree", otherwise the line is removed from the menu:
function! NERDTreeMenuToggle()
if &ft == "nerdtree"
nnoremenu 1.120 PopUp.Add\ To\ Path :call NERDTreeAddNodeToPath()<CR>
else
silent! nunmenu PopUp.Add\ To\ Path
endif
endfunction
Note that nunmenu
must be called silently to suppress errors if the menu item doesn't exist. Next we must set this function to be called on the MenuPopup
event (i.e. just before the menu is displaued).
au! MenuPopup * :call NERDTreeMenuToggle()
That seems to do the trick (although I am not a regular user of NERDTree, so I may be missing some subtleties.
Another approach would be to add the line to menu in a new filetype plugin (just put the nnoremenu
line in a file called nerdtree.vim in $VIM/vimfiles/ftplugin) but this still leaves the problem of removing the line for non-NERDTree buffers, so I think my solution above is slightly cleaner.