From d9857cf204aa555f3c57bf00eaa856bc570a381a Mon Sep 17 00:00:00 2001 From: Jan Larres Date: Wed, 29 Feb 2012 00:21:50 +1300 Subject: [PATCH] Add function that returns the current tag, closes #59 --- autoload/tagbar.vim | 72 +++++++++++++++++++++++++++++++++------------ doc/tagbar.txt | 25 ++++++++++++++++ 2 files changed, 79 insertions(+), 18 deletions(-) diff --git a/autoload/tagbar.vim b/autoload/tagbar.vim index 3adfd80..a4aa67f 100644 --- a/autoload/tagbar.vim +++ b/autoload/tagbar.vim @@ -111,6 +111,10 @@ function! s:Init() call s:InitTypes() endif + if !s:autocommands_done + call s:CreateAutocommands() + endif + return 1 endfunction @@ -956,7 +960,6 @@ function! s:CreateAutocommands() augroup TagbarAutoCmds autocmd! autocmd BufEnter __Tagbar__ nested call s:QuitIfOnlyWindow() - autocmd BufUnload __Tagbar__ call s:CleanUp() autocmd CursorHold __Tagbar__ call s:ShowPrototype() autocmd BufWritePost * @@ -1259,6 +1262,26 @@ function! s:NormalTag.str() dict return self._getPrefix() . self.name . suffix . "\n" endfunction +" s:NormalTag.strshort() {{{3 +function! s:NormalTag.strshort(longsig) dict + let str = '' + if has_key(self.fields, 'access') + let str .= get(s:access_symbols, self.fields.access, '') + endif + + let str .= self.name + + if has_key(self.fields, 'signature') + if a:longsig + let str .= self.fields.signature + else + let str .= '()' + endif + endif + + return str +endfunction + " s:NormalTag.getPrototype() {{{3 function! s:NormalTag.getPrototype() dict return self.prototype @@ -1585,6 +1608,7 @@ function! s:InitWindow(autoclose) let s:is_maximized = 0 let s:short_help = 1 + let s:new_window = 1 let w:autoclose = a:autoclose @@ -1600,10 +1624,6 @@ function! s:InitWindow(autoclose) call s:MapKeys() endif - if !s:autocommands_done - call s:CreateAutocommands() - endif - let &cpoptions = cpoptions_save call s:LogDebugMessage('InitWindow finished') @@ -2212,6 +2232,7 @@ endfunction " s:RenderContent() {{{2 function! s:RenderContent(...) call s:LogDebugMessage('RenderContent called') + let s:new_window = 0 if a:0 == 1 let fileinfo = a:1 @@ -2521,6 +2542,9 @@ function! s:HighlightTag() endif let tagbarwinnr = bufwinnr('__Tagbar__') + if tagbarwinnr == -1 + return + endif let prevwinnr = winnr() call s:winexec(tagbarwinnr . 'wincmd w') @@ -2842,9 +2866,8 @@ function! s:AutoUpdate(fname) 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 || ftype == 'tagbar' + " Don't do anything if we're in the tagbar window + if ftype == 'tagbar' call s:LogDebugMessage('Tagbar window not open or in Tagbar window') return endif @@ -2888,7 +2911,8 @@ function! s:AutoUpdate(fname) " Display the tagbar content if the tags have been updated or a different " file is being displayed - if updated || a:fname != s:known_files.getCurrent().fpath + if bufwinnr('__Tagbar__') != -1 && + \ (s:new_window || updated || a:fname != s:known_files.getCurrent().fpath) call s:RenderContent(fileinfo) endif @@ -2917,15 +2941,6 @@ function! s:CheckMouseClick() endif endfunction -" s:CleanUp() {{{2 -function! s:CleanUp() - silent autocmd! TagbarAutoCmds - - unlet s:is_maximized - unlet s:compare_typeinfo - unlet s:short_help -endfunction - " s:DetectFiletype() {{{2 function! s:DetectFiletype(bufnr) " Filetype has already been detected for loaded buffers, but not @@ -3037,6 +3052,9 @@ endfunction " Get the tag info for a file near the cursor in the current file function! s:GetNearbyTag() let fileinfo = s:known_files.getCurrent() + if empty(fileinfo) + return + endif let curline = line('.') let tag = {} @@ -3268,5 +3286,23 @@ function! tagbar#autoopen(...) \ 'without finding valid file') endfunction +function! tagbar#currenttag(fmt, default, ...) + let longsig = a:0 > 0 ? a:1 : 0 + + if !s:Init() + return '' + endif + + let tag = s:GetNearbyTag() + + if !empty(tag) + return printf(a:fmt, tag.strshort(longsig)) + else + return a:default + endif +endfunction + +TagbarDebug + " 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 f9d047c..d77acb4 100644 --- a/doc/tagbar.txt +++ b/doc/tagbar.txt @@ -20,6 +20,7 @@ Contents *tagbar* *tagbar-contents* 5. Configuration ................... |tagbar-configuration| Highlight colours ............. |tagbar-highlight| Automatically opening Tagbar .. |tagbar-autoopen| + Show current tag in statusline |tagbar-statusline| 6. Extending Tagbar ................ |tagbar-extend| 7. Troubleshooting & Known issues .. |tagbar-issues| 8. History ......................... |tagbar-history| @@ -577,6 +578,30 @@ supported ones: Check out |autocmd.txt| if you want it to open automatically in more complicated cases. +------------------------------------------------------------------------------ +SHOWING THE CURRENT TAG IN THE STATUSLINE *tagbar-statusline* + +You can show the current tag in the 'statusline', or in any other place that +you want to, by calling the tagbar#currenttag() function. The current tag is +exactly the same as would be highlighted in the Tagbar window if it is open. +It is defined as the nearest tag upwards in the file starting from the cursor +position. This means that for example in a function it should usually be the +name of the function. + +The function has the following signature: + +tagbar#currenttag({format}, {default} [, {longsig}]) + {format} is a |printf()|-compatible format string where "%s" will be + replaced by the name of the tag. {default} will be displayed instead of + the format string if no tag can be found. If the optional argument + {longsig} is given and is non-zero then functions will have their complete + signature shown, otherwise only "()" will be appended so they can be more + easily distinguished from other tags. + + For example, if you put the following into your statusline: > + %{tagbar#currenttag('[%s] ', '')} +< then the function "myfunc" will be show as "[myfunc()] ". + ============================================================================== 6. Extending Tagbar *tagbar-extend*