Add convenience function for automatically opening Tagbar

This commit is contained in:
Jan Larres 2011-12-21 19:28:34 +13:00
parent a93bd87715
commit 9a2cf9756f
2 changed files with 74 additions and 13 deletions

View File

@ -1390,7 +1390,7 @@ function! s:ToggleWindow()
return
endif
call s:OpenWindow(0)
call s:OpenWindow('')
endfunction
" s:OpenWindow() {{{2
@ -2935,6 +2935,38 @@ function! s:CheckMouseClick()
endif
endfunction
" s:DetermineFiletype() {{{2
function! s:DetectFiletype(bufnr)
" Filetype has already been detected for loaded buffers, but not
" necessarily for unloaded ones
let ftype = getbufvar(a:bufnr, '&filetype')
if bufloaded(a:bufnr)
return ftype
endif
if ftype != ''
return ftype
endif
" Unloaded buffer with non-detected filetype, need to detect filetype
" manually
let bufname = bufname(a:bufnr)
let eventignore_save = &eventignore
set eventignore=FileType
let filetype_save = &filetype
exe 'doautocmd filetypedetect BufRead ' . bufname
let ftype = &filetype
let &filetype = filetype_save
let &eventignore = eventignore_save
return ftype
endfunction
" TagbarBalloonExpr() {{{2
function! TagbarBalloonExpr()
let taginfo = s:GetTagInfo(v:beval_lnum, 1)
@ -3035,5 +3067,21 @@ function! tagbar#RestoreSession()
call s:RestoreSession()
endfunction
" Automatically open Tagbar if one of the open buffers contains a supported
" file
function! tagbar#autoopen()
call s:Init()
for bufnr in range(1, bufnr('$'))
if buflisted(bufnr)
let ftype = s:DetectFiletype(bufnr)
if s:IsValidFile(bufname(bufnr), ftype)
call s:OpenWindow('')
return
endif
endif
endfor
endfunction
" Modeline {{{1
" vim: ts=8 sw=4 sts=4 et foldenable foldmethod=marker foldcolumn=1

View File

@ -536,19 +536,32 @@ See |:highlight| for more information.
------------------------------------------------------------------------------
AUTOMATICALLY OPENING TAGBAR *tagbar-autoopen*
If you want Tagbar to open automatically, for example on Vim startup or for
specific filetypes, there are various ways to do it. For example, to always
open Tagbar on Vim startup you can put this into your vimrc file:
>
autocmd VimEnter * nested TagbarOpen
<
If you want to have it start for specific filetypes put
>
TagbarOpen
<
into a corresponding filetype plugin (see |filetype-plugin|).
Since there are several different situations in which you might want to open
Tagbar automatically there is no single option to enable automatic opening.
Instead, autocommands can be used together with a convenience function that
opens Tagbar only if a supported file is open(ed).
Check out |autocmd.txt| if you want it to automatically open in more
If you want to open Tagbar automatically on Vim startup no matter what put
this into your vimrc:
>
autocmd VimEnter * nested :TagbarOpen
<
If you want to open it only if you're opening Vim with a supported file/files
use this instead:
>
autocmd VimEnter * nested :call tagbar#autoopen()
<
For opening Tagbar also if you open a supported file in an already running
Vim:
>
autocmd FileType * nested :call tagbar#autoopen()
<
And if you only want to open Tagbar only for specific filetypes, not for all
of the supported ones:
>
autocmd FileType c,cpp nested :TagbarOpen
<
Check out |autocmd.txt| if you want it to open automatically in more
complicated cases.
==============================================================================