From 2ba944b16242f579f8996f0c4cb732e5ffe53434 Mon Sep 17 00:00:00 2001 From: Jan Larres Date: Thu, 23 Feb 2012 20:42:35 +1300 Subject: [PATCH] Correct focus on startup under certain circumstances, closes #60 --- autoload/tagbar.vim | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/autoload/tagbar.vim b/autoload/tagbar.vim index 5fc0557..fd2aae6 100644 --- a/autoload/tagbar.vim +++ b/autoload/tagbar.vim @@ -967,6 +967,8 @@ function! s:CreateAutocommands() \ s:AutoUpdate(fnamemodify(expand(''), ':p')) autocmd BufDelete,BufUnload,BufWipeout * call \ s:CleanupFileinfo(fnamemodify(expand(''), ':p')) + + autocmd VimEnter * call s:CorrectFocusOnStartup() augroup END let s:autocommands_done = 1 @@ -1525,6 +1527,9 @@ function! s:OpenWindow(flags) return endif + " This is only needed for the CorrectFocusOnStartup() function + let s:last_autofocus = autofocus + if !s:Init() return 0 endif @@ -1697,6 +1702,23 @@ function! s:ZoomWindow() endif endfunction +" s:CorrectFocusOnStartup() {{{2 +" For whatever reason the focus will be on the Tagbar window if +" tagbar#autoopen is used with a FileType autocommand on startup and +" g:tagbar_left is set. This should work around it by jumping to the window of +" the current file after startup. +function! s:CorrectFocusOnStartup() + if bufwinnr('__Tagbar__') != -1 && !g:tagbar_autofocus && !s:last_autofocus + let curfile = s:known_files.getCurrent() + if !empty(curfile) && curfile.fpath != fnamemodify(bufname('%'), ':p') + let winnr = bufwinnr(curfile.fpath) + if winnr != -1 + call s:winexec(winnr . 'wincmd w') + endif + endif + endif +endfunction + " Tag processing {{{1 " s:ProcessFile() {{{2 " Execute ctags and put the information into a 'FileInfo' object