Skip to content

Commit

Permalink
Extract GetFileWinnr() function
Browse files Browse the repository at this point in the history
  • Loading branch information
majutsushi committed Feb 1, 2014
1 parent e872e67 commit 0ead333
Showing 1 changed file with 42 additions and 29 deletions.
71 changes: 42 additions & 29 deletions autoload/tagbar.vim
Expand Up @@ -3613,48 +3613,61 @@ function! s:GetTagInfo(linenr, ignorepseudo) abort
return taginfo
endfunction

" s:GetFileWinnr() {{{2
" Get the number of the window that has Tagbar's current file loaded into it,
" or 0 if no window has loaded it. It tries the previous window first, if that
" does not have the correct buffer loaded it will look for the first one with
" the correct buffer in it.
function! s:GetFileWinnr(fileinfo) abort
let filewinnr = 0
let prevwinnr = winnr("#")

if winbufnr(prevwinnr) == a:fileinfo.bufnr &&
\ !getwinvar(prevwinnr, '&previewwindow')
let filewinnr = prevwinnr
else
" Search for the first real window that has the correct buffer loaded
" in it. Similar to bufwinnr() but skips the previewwindow.
for i in range(1, winnr('$'))
call s:goto_win(i, 1)
if bufnr('%') == a:fileinfo.bufnr && !&previewwindow
let filewinnr = winnr()
break
endif
endfor

call s:goto_tagbar(1)
endif

return filewinnr
endfunction

" s:GotoFileWindow() {{{2
" Try to switch to the window that has Tagbar's current file loaded in it, or
" open the file in a window otherwise.
" open the file in an existing window otherwise.
function! s:GotoFileWindow(fileinfo, ...) abort
let noauto = a:0 > 0 ? a:1 : 0

let tagbarwinnr = bufwinnr('__Tagbar__')

call s:goto_win('p', noauto)
let filewinnr = s:GetFileWinnr(a:fileinfo)

let filebufnr = bufnr(a:fileinfo.fpath)
if bufnr('%') != filebufnr || &previewwindow
" Search for the first real window that has the correct buffer loaded
" in it. Similar to bufwinnr() but skips the previewwindow.
let found = 0
" If there is no window with the correct buffer loaded then load it
" into the first window that has a non-special buffer in it.
if filewinnr == 0
for i in range(1, winnr('$'))
call s:goto_win(i, 1)
if bufnr('%') == filebufnr && !&previewwindow
let found = 1
if &buftype == '' && !&previewwindow
execute 'buffer ' . a:fileinfo.bufnr
break
endif
endfor

" If there is no window with the correct buffer loaded then load it
" into the first window that has a non-special buffer in it.
if !found
for i in range(1, winnr('$'))
call s:goto_win(i, 1)
if &buftype == '' && !&previewwindow
execute 'buffer ' . filebufnr
break
endif
endfor
endif

" To make ctrl-w_p work we switch between the Tagbar window and the
" correct window once
call s:goto_win(tagbarwinnr, noauto)
call s:goto_win('p', noauto)
else
call s:goto_win(filewinnr, 1)
endif

return winnr()
" To make ctrl-w_p work we switch between the Tagbar window and the
" correct window once
call s:goto_tagbar(noauto)
call s:goto_win('p', noauto)
endfunction

" s:IsValidFile() {{{2
Expand Down

0 comments on commit 0ead333

Please sign in to comment.