From 9a2cf9756f7a2055d891e13d289e1ac2b1c82a98 Mon Sep 17 00:00:00 2001 From: Jan Larres Date: Wed, 21 Dec 2011 19:28:34 +1300 Subject: [PATCH] Add convenience function for automatically opening Tagbar --- autoload/tagbar.vim | 50 ++++++++++++++++++++++++++++++++++++++++++++- doc/tagbar.txt | 37 ++++++++++++++++++++++----------- 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/autoload/tagbar.vim b/autoload/tagbar.vim index ddc959a..49b7565 100644 --- a/autoload/tagbar.vim +++ b/autoload/tagbar.vim @@ -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 diff --git a/doc/tagbar.txt b/doc/tagbar.txt index 1e7f699..bd641ac 100644 --- a/doc/tagbar.txt +++ b/doc/tagbar.txt @@ -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. ==============================================================================