airblade/vim-gitgutter

g:gitgutter_close_preview_on_escape is broken when exists('*nvim_open_win')

thisisrandy opened this issue · 2 comments

What is the latest commit SHA in your installed vim-gitgutter?

68f16eb

What vim/nvim version are you on?

NVIM v0.9.0

I noticed the the course of fixing #866 that g:gitgutter_close_preview_on_escape is broken for the exists('*nvim_open_win') case. The culprit is line 468 in hunk.vim, which sets up a buffer-local mapping for the current buffer instead of the floating window.

It should be an easy fix (use the <buffer=N> syntax as in the autocmd a few lines down), but I'm having trouble for some reason. Here's what I tried in place of the offending line:

execute "nnoremap <buffer=".winbufnr(s:winid)."> <silent> <Esc> :<C-U>call gitgutter#hunk#close_hunk_preview_window()<CR>"

I wasn't previously familiar with the <buffer=N> syntax, and either it's broken (unlikely) or I'm somehow managing to misuse it. I tried some sanity checking by making a simple mapping directly in a buffer where :echom bufnr() is 1

:nnoremap <buffer=1> <Esc> :echom "FOO!"<cr>

This doesn't work either!! Removing =1 of course does what I expect. Do you happen to know what exact manner of stupid I'm being? If you see my mistake, I'm happy to make a PR.

Hmm, maybe this was why I was having trouble removing the map once it had been used – it was actually in the wrong window/buffer.

The fix is just to jump to the floating window, set the map, then return to where we were. And then once the window is closed the map gets removed automatically :)

By the way, the <buffer=N> syntax is for autocommands not maps. See :help map-buffer and :help autocmd-buffer.

By the way, the <buffer=N> syntax is for autocommands not maps. See :help map-buffer and :help autocmd-buffer.

Yes, I see that now. Thanks.