1
0
mirror of https://github.com/gryf/tagbar.git synced 2025-12-18 12:00:23 +01:00

Abstract out window commands without autocommands

This commit is contained in:
Jan Larres
2012-01-31 20:58:49 +13:00
parent 52fe2b7c3a
commit 8bb4a3c111

View File

@@ -813,6 +813,8 @@ endfunction
function! s:RestoreSession()
call s:LogDebugMessage('Restoring session')
let curfile = fnamemodify(bufname('%'), ':p')
let tagbarwinnr = bufwinnr('__Tagbar__')
if tagbarwinnr == -1
" Tagbar wasn't open in the saved session, nothing to do
@@ -820,7 +822,7 @@ function! s:RestoreSession()
else
let in_tagbar = 1
if winnr() != tagbarwinnr
execute tagbarwinnr . 'wincmd w'
call s:winexec(tagbarwinnr . 'wincmd w')
let in_tagbar = 0
endif
endif
@@ -829,12 +831,10 @@ function! s:RestoreSession()
call s:InitWindow(g:tagbar_autoclose)
" Leave the Tagbar window and come back so the update event gets triggered
wincmd p
execute tagbarwinnr . 'wincmd w'
call s:AutoUpdate(curfile)
if !in_tagbar
wincmd p
call s:winexec('wincmd p')
endif
endfunction
@@ -1411,12 +1411,14 @@ function! s:OpenWindow(flags)
let jump = a:flags =~# 'j'
let autoclose = a:flags =~# 'c'
let curfile = fnamemodify(bufname('%'), ':p')
" If the tagbar window is already open check jump flag
" Also set the autoclose flag if requested
let tagbarwinnr = bufwinnr('__Tagbar__')
if tagbarwinnr != -1
if winnr() != tagbarwinnr && jump
execute tagbarwinnr . 'wincmd w'
call s:winexec(tagbarwinnr . 'wincmd w')
if autoclose
let w:autoclose = autoclose
endif
@@ -1442,13 +1444,10 @@ function! s:OpenWindow(flags)
call s:InitWindow(autoclose)
wincmd p
call s:AutoUpdate(curfile)
" Jump back to the tagbar window if autoclose or autofocus is set. Can't
" just stay in it since it wouldn't trigger the update event
if g:tagbar_autoclose || autofocus || g:tagbar_autofocus
let tagbarwinnr = bufwinnr('__Tagbar__')
execute tagbarwinnr . 'wincmd w'
if !(g:tagbar_autoclose || autofocus || g:tagbar_autofocus)
call s:winexec('wincmd p')
endif
endfunction
@@ -1529,35 +1528,29 @@ function! s:CloseWindow()
if winbufnr(2) != -1
let filebufnr = bufnr(s:known_files.getCurrent().fpath)
" Ignore BufEnter to prevent MiniBufExplorer from interfering
let eventignore_save = &eventignore
set eventignore=BufEnter
" Other windows are open, only close the tagbar one
close
call s:winexec('close')
" Try to jump to the correct window after closing
wincmd p
call s:winexec('wincmd p')
if bufnr('%') != filebufnr
let filewinnr = bufwinnr(filebufnr)
if filewinnr != -1
execute filewinnr . 'wincmd w'
call s:winexec(filewinnr . 'wincmd w')
endif
endif
let &eventignore = eventignore_save
endif
else
" Go to the tagbar window, close it and then come back to the
" original window
let curbufnr = bufnr('%')
execute tagbarwinnr . 'wincmd w'
call s:winexec(tagbarwinnr . 'wincmd w')
close
" Need to jump back to the original window only if we are not
" already in that window
let winnum = bufwinnr(curbufnr)
if winnr() != winnum
exe winnum . 'wincmd w'
call s:winexec(winnum . 'wincmd w')
endif
endif
@@ -2114,7 +2107,7 @@ function! s:RenderContent(...)
else
let in_tagbar = 0
let prevwinnr = winnr()
execute tagbarwinnr . 'wincmd w'
call s:winexec(tagbarwinnr . 'wincmd w')
endif
if !empty(s:known_files.getCurrent()) &&
@@ -2177,7 +2170,7 @@ function! s:RenderContent(...)
let &eventignore = eventignore_save
if !in_tagbar
execute prevwinnr . 'wincmd w'
call s:winexec(prevwinnr . 'wincmd w')
endif
endfunction
@@ -2386,19 +2379,15 @@ function! s:HighlightTag()
let s:last_highlight_tline = tagline
endif
let eventignore_save = &eventignore
set eventignore=all
let tagbarwinnr = bufwinnr('__Tagbar__')
let prevwinnr = winnr()
execute tagbarwinnr . 'wincmd w'
call s:winexec(tagbarwinnr . 'wincmd w')
match none
" No tag above cursor position so don't do anything
if tagline == 0
execute prevwinnr . 'wincmd w'
let &eventignore = eventignore_save
call s:winexec(prevwinnr . 'wincmd w')
redraw
return
endif
@@ -2421,9 +2410,7 @@ function! s:HighlightTag()
let pattern = '/^\%' . tagline . 'l\s*' . foldpat . '[-+# ]\zs[^( ]\+\ze/'
execute 'match TagbarHighlight ' . pattern
execute prevwinnr . 'wincmd w'
let &eventignore = eventignore_save
call s:winexec(prevwinnr . 'wincmd w')
redraw
endfunction
@@ -2440,22 +2427,18 @@ function! s:JumpToTag(stay_in_tagbar)
let tagbarwinnr = winnr()
" Ignore BufEnter to prevent MiniBufExplorer from interfering
let eventignore_save = &eventignore
set eventignore=BufEnter
" This elaborate construct will try to switch to the correct
" buffer/window; if the buffer isn't currently shown in a window it will
" open it in the first window with a non-special buffer in it
wincmd p
call s:winexec('wincmd p')
let filebufnr = bufnr(taginfo.fileinfo.fpath)
if bufnr('%') != filebufnr
let filewinnr = bufwinnr(filebufnr)
if filewinnr != -1
execute filewinnr . 'wincmd w'
call s:winexec(filewinnr . 'wincmd w')
else
for i in range(1, winnr('$'))
execute i . 'wincmd w'
call s:winexec(i . 'wincmd w')
if &buftype == ''
execute 'buffer ' . filebufnr
break
@@ -2464,8 +2447,8 @@ function! s:JumpToTag(stay_in_tagbar)
endif
" To make ctrl-w_p work we switch between the Tagbar window and the
" correct window once
execute tagbarwinnr . 'wincmd w'
wincmd p
call s:winexec(tagbarwinnr . 'wincmd w')
call s:winexec('wincmd p')
endif
" Mark current position so it can be jumped back to
@@ -2510,11 +2493,9 @@ function! s:JumpToTag(stay_in_tagbar)
redraw
let &eventignore = eventignore_save
if a:stay_in_tagbar
call s:HighlightTag()
execute tagbarwinnr . 'wincmd w'
call s:winexec(tagbarwinnr . 'wincmd w')
elseif g:tagbar_autoclose || autoclose
call s:CloseWindow()
else
@@ -2746,20 +2727,24 @@ endfunction
function! s:AutoUpdate(fname)
call s:LogDebugMessage('AutoUpdate called on ' . a:fname)
" Get the filetype of the file we're about to process
let bufnr = bufnr(a:fname)
let ftype = getbufvar(bufnr, '&filetype')
" Don't do anything if tagbar is not open or if we're in the tagbar window
let tagbarwinnr = bufwinnr('__Tagbar__')
if tagbarwinnr == -1 || &filetype == 'tagbar'
if tagbarwinnr == -1 || ftype == 'tagbar'
call s:LogDebugMessage('Tagbar window not open or in Tagbar window')
return
endif
" Only consider the main filetype in cases like 'python.django'
let ftype = get(split(&filetype, '\.'), 0, '')
call s:LogDebugMessage('Vim filetype: ' . &filetype .
\ ', sanitized filetype: ' . ftype)
let sftype = get(split(ftype, '\.'), 0, '')
call s:LogDebugMessage("Vim filetype: '" . ftype . "', " .
\ "sanitized filetype: '" . sftype . "'")
" Don't do anything if the file isn't supported
if !s:IsValidFile(a:fname, ftype)
if !s:IsValidFile(a:fname, sftype)
call s:LogDebugMessage('Not a valid file, stopping processing')
return
endif
@@ -2769,12 +2754,12 @@ function! s:AutoUpdate(fname)
" if there was an error during the ctags execution
if s:known_files.has(a:fname) && !empty(s:known_files.get(a:fname))
if s:known_files.get(a:fname).mtime != getftime(a:fname)
call s:LogDebugMessage('Filedata outdated, updating ' . a:fname)
call s:ProcessFile(a:fname, ftype)
call s:LogDebugMessage('File data outdated, updating ' . a:fname)
call s:ProcessFile(a:fname, sftype)
endif
elseif !s:known_files.has(a:fname)
call s:LogDebugMessage('Unknown file, processing ' . a:fname)
call s:ProcessFile(a:fname, ftype)
call s:LogDebugMessage('New file, processing ' . a:fname)
call s:ProcessFile(a:fname, sftype)
endif
let fileinfo = s:known_files.get(a:fname)
@@ -2782,7 +2767,7 @@ function! s:AutoUpdate(fname)
" If we don't have an entry for the file by now something must have gone
" wrong, so don't change the tagbar content
if empty(fileinfo)
call s:LogDebugMessage('fileinfo empty after processing: ' . a:fname)
call s:LogDebugMessage('fileinfo empty after processing ' . a:fname)
return
endif
@@ -2965,7 +2950,7 @@ function! s:CheckMouseClick()
endif
endfunction
" s:DetermineFiletype() {{{2
" s:DetectFiletype() {{{2
function! s:DetectFiletype(bufnr)
" Filetype has already been detected for loaded buffers, but not
" necessarily for unloaded ones
@@ -2996,6 +2981,15 @@ function! s:DetectFiletype(bufnr)
return ftype
endfunction
" s:winexec() {{{2
function! s:winexec(cmd)
let eventignore_save = &eventignore
set eventignore=BufEnter
execute a:cmd
let &eventignore = eventignore_save
endfunction
" TagbarBalloonExpr() {{{2
function! TagbarBalloonExpr()