aboutsummaryrefslogtreecommitdiffstats
path: root/bundle/git-blamer/autoload
diff options
context:
space:
mode:
authorTeddy Wing2018-03-25 15:59:44 +0200
committerTeddy Wing2018-03-25 16:13:09 +0200
commit019805f339a0774574d97f138ea2b2c9425db0b4 (patch)
treec1a06a4545b1cdb20cce875012ec30555cdc9070 /bundle/git-blamer/autoload
parentf8efc45d741883eb1566b9973fe1a24a79061b97 (diff)
downloaddotvim-019805f339a0774574d97f138ea2b2c9425db0b4.tar.bz2
git-blamer: Refocus original window when blamer buffer is closed
Previously, when the GitBlamer window was closed, the window it was opened from would not be re-focused. Because of this, you couldn't just pick up where you left off, you had to run a window command first. This change re-focuses the original window automatically, bringing you right where you were before opening GitBlamer. Unfortunately, this seemingly simple want is not easy to do in VimL. There's a `WinLeave` autocmd, but that event gets executed _before_ leaving a window. Similarly, the `BufWinLeave` autocmd we already had gets executed before the buffer is removed from the window. Because these events happen before the window is left, running the `wincmd w` command doesn't work. We need a way to run the command after the window is closed, allowing us to work with the new window layout and window numbers. In order to run `wincmd w` at the proper time, we add a new autocmd upon closing the GitBlamer window that gets executed when any window is entered. That autocmd refocuses the correct window and removes itself, having served its purpose. I used a tab variable to store the window number because it was a way to allow multiple GitBlamer instances in different tabs. Otherwise, the autocmd would be attached to a window that isn't correct in other tabs. Also, augroups can't have dynamic names, so we can't create them programmatically, preventing us from having multiple autocmds with different window numbers for different tabs. The disadvantage of using a tab variable is that window focus restoration doesn't work when multiple GitBlamers are opened in a single tab.
Diffstat (limited to 'bundle/git-blamer/autoload')
-rw-r--r--bundle/git-blamer/autoload/git_blamer.vim21
1 files changed, 20 insertions, 1 deletions
diff --git a/bundle/git-blamer/autoload/git_blamer.vim b/bundle/git-blamer/autoload/git_blamer.vim
index dc41bca..df9832e 100644
--- a/bundle/git-blamer/autoload/git_blamer.vim
+++ b/bundle/git-blamer/autoload/git_blamer.vim
@@ -7,6 +7,7 @@ function! git_blamer#Blame()
let l:line_number = line('.')
let l:buffer_name = shellescape(bufname('%'))
let l:buffer_number = bufnr('%')
+ let t:git_blamer_window_number = winnr()
let restore = ''
if &l:wrap
@@ -42,5 +43,23 @@ function! git_blamer#Blame()
nnoremap <buffer> q :q<CR>
" Restore starting file's scrollbind on exit
- autocmd BufWinLeave <buffer> execute b:git_blamer_restore
+ autocmd BufWinLeave <buffer>
+ \ execute b:git_blamer_restore
+ \ | call s:FocusOriginalWindow()
+endfunction
+
+function! s:FocusOriginalWindow()
+ augroup git_blamer
+ autocmd!
+
+ autocmd WinEnter *
+ \ execute t:git_blamer_window_number . 'wincmd w'
+ \ | call s:RemoveWindowFocusAutocmd()
+ augroup END
+endfunction
+
+function! s:RemoveWindowFocusAutocmd()
+ augroup git_blamer
+ autocmd!
+ augroup END
endfunction