From b6298c0ca0116884400245978abf3b771be5914e Mon Sep 17 00:00:00 2001 From: Jan Larres Date: Mon, 17 Oct 2016 22:57:15 +1300 Subject: [PATCH] Use window ID functionality if available, ref #351 When opening the Tagbar window the window numbers can change so that restoring the window history is not possible. Use the new window ID functionality if it is available to make that possible. --- autoload/tagbar.vim | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/autoload/tagbar.vim b/autoload/tagbar.vim index 6a2aa7d..4d5adb5 100644 --- a/autoload/tagbar.vim +++ b/autoload/tagbar.vim @@ -1816,9 +1816,17 @@ function! s:OpenWindow(flags) abort return endif - let prevwinnr = winnr() - call s:goto_win('p', 1) - let pprevwinnr = winnr() + " Use the window ID if the functionality exists, this is more reliable + " since the window number can change due to the Tagbar window opening + if exists('*win_getid') + let prevwinid = win_getid() + call s:goto_win('p', 1) + let pprevwinid = win_getid() + else + let prevwinnr = winnr() + call s:goto_win('p', 1) + let pprevwinnr = winnr() + endif call s:goto_win('p', 1) " This is only needed for the CorrectFocusOnStartup() function @@ -1865,8 +1873,21 @@ function! s:OpenWindow(flags) abort call s:HighlightTag(g:tagbar_autoshowtag != 2, 1, curline) if !(g:tagbar_autoclose || autofocus || g:tagbar_autofocus) - call s:goto_win(pprevwinnr, 1) - call s:goto_win(prevwinnr) + if exists('*win_getid') + noautocmd call win_gotoid(pprevwinid) + call win_gotoid(prevwinid) + else + " If the Tagbar winnr is identical to one of the saved values + " then that means that the window numbers have changed. + " Just jump back to the previous window since we won't be able to + " restore the window history. + if winnr() == pprevwinnr || winnr() == prevwinnr + call s:goto_win('p') + else + call s:goto_win(pprevwinnr, 1) + call s:goto_win(prevwinnr) + endif + endif endif call s:debug('OpenWindow finished')