From ca1c9ee2e2fee725e83b63208bda05b746a94d73 Mon Sep 17 00:00:00 2001 From: Jan Larres Date: Wed, 25 Jun 2014 17:52:43 +1200 Subject: [PATCH] Add mapping to toggle autoclose option The current tagbar_autoclose state will be displayed in the statusline. Also change the "hide nonpublic" flag to "v" to match the mapping and properly document the statusline flags. --- autoload/tagbar.vim | 33 ++++++++++++++++++++++++--------- doc/tagbar.txt | 24 ++++++++++++++++++------ plugin/tagbar.vim | 9 +++++---- 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/autoload/tagbar.vim b/autoload/tagbar.vim index 054b53f..dd427a3 100644 --- a/autoload/tagbar.vim +++ b/autoload/tagbar.vim @@ -938,10 +938,11 @@ function! s:MapKeys() abort \ ['openallfolds', 'SetFoldLevel(99, 1)'], \ ['closeallfolds', 'SetFoldLevel(0, 1)'], \ - \ ['togglesort', 'ToggleSort()'], - \ ['zoomwin', 'ZoomWindow()'], - \ ['close', 'CloseWindow()'], - \ ['help', 'ToggleHelp()'], + \ ['togglesort', 'ToggleSort()'], + \ ['toggleautoclose', 'ToggleAutoclose()'], + \ ['zoomwin', 'ZoomWindow()'], + \ ['close', 'CloseWindow()'], + \ ['help', 'ToggleHelp()'], \ ] for [map, func] in maps @@ -1808,12 +1809,12 @@ function! s:InitWindow(autoclose) abort setlocal foldmethod& setlocal foldexpr& + let w:autoclose = a:autoclose + call s:SetStatusLine('current') let s:new_window = 1 - let w:autoclose = a:autoclose - if has('balloon_eval') setlocal balloonexpr=TagbarBalloonExpr() set ballooneval @@ -3666,6 +3667,12 @@ function! s:ToggleHideNonPublicTags() abort call s:SetStatusLine('current') endfunction +" s:ToggleAutoclose() {{{2 +function! s:ToggleAutoclose() abort + let g:tagbar_autoclose = !g:tagbar_autoclose + call s:SetStatusLine('current') +endfunction + " s:IsValidFile() {{{2 function! s:IsValidFile(fname, ftype) abort call s:debug('Checking if file is valid [' . a:fname . ']') @@ -3727,13 +3734,21 @@ function! s:SetStatusLine(current) let fname = '' endif + let flags = [] + let flags += exists('w:autoclose') && w:autoclose ? ['c'] : [] + let flags += g:tagbar_autoclose ? ['C'] : [] + let flags += g:tagbar_hide_nonpublic ? ['v'] : [] + if exists('g:tagbar_status_func') - let args = [current, sort, fname] + let args = [current, sort, fname, flags] let &l:statusline = call(g:tagbar_status_func, args) else let colour = current ? '%#StatusLine#' : '%#StatusLineNC#' - let hide = g:tagbar_hide_nonpublic ? '[h] ' : '' - let text = colour . '[' . sort . '] ' . hide . fname + let flagstr = join(flags, '') + if flagstr != '' + let flagstr = '[' . flagstr . '] ' + endif + let text = colour . '[' . sort . '] ' . flagstr . fname let &l:statusline = text endif diff --git a/doc/tagbar.txt b/doc/tagbar.txt index f0dfe8e..9ba6d1f 100644 --- a/doc/tagbar.txt +++ b/doc/tagbar.txt @@ -251,7 +251,8 @@ COMMANDS *tagbar-commands* 'j' Jump to Tagbar window if already open 'c' Close Tagbar on tag selection (just as if |g:tagbar_autoclose| were set to 1, but doesn't imply 'f'), but only if the Tagbar window was - opened using this command. + opened using this command. If this is used the "c" flag will be + shown in the statusline of the Tagbar window. For example, the following command would always jump to the Tagbar window, opening it first if necessary, but keep it open after selecting a tag @@ -348,6 +349,8 @@ o/za Toggle the fold under the cursor or the current one if there is Map option: tagbar_map_closeallfolds s Toggle sort order between name and file order. Map option: tagbar_map_togglesort +c Toggle the |g:tagbar_autoclose| option. + Map option: tagbar_map_toggleautoclose x Toggle zooming the window. Map option: tagbar_map_zoomwin q Close the Tagbar window. @@ -422,7 +425,9 @@ g:tagbar_autoclose~ Default: 0 If you set this option the Tagbar window will automatically close when you -jump to a tag. This implies |g:tagbar_autofocus|. +jump to a tag. This implies |g:tagbar_autofocus|. If enabled the "C" flag will +be shown in the statusline of the Tagbar window. This can also be toggled with +a key, see |tagbar-keys|. Example: > @@ -514,7 +519,8 @@ g:tagbar_hide_nonpublic~ Default: 0 Hide tags that are declared non-public. Tags without any visibility -information will still be shown. This can also be toggled with a key, see +information will still be shown. If enabled the "v" flag will be shown in the +statusline of the Tagbar window. This can also be toggled with a key, see |tagbar-keys|. Example: @@ -656,11 +662,13 @@ Default: undefined This is the name of a function whose return value will be used to draw the statusline of the Tagbar window. -The function has to take three arguments: +The function has to take four arguments: 1. current: Whether Tagbar is the current window; 0 or 1. 2. sort: The sort order of the tags; 'Name' if they are sorted by name and 'Order' if they are sorted by their order of appearance in the file. 3. fname: The name of the file that the tags belong to. + 4. flags: A list of characters that represent various state in the Tagbar + window. In order to avoid possible future additions to the arguments resulting in an error it is recommended to add an additional vararg to the signature (see @@ -669,9 +677,13 @@ error it is recommended to add an additional vararg to the signature (see Here is an example that, when put into your vimrc, will emulate Tagbar's default statusline: > - function! TagbarStatusFunc(current, sort, fname, ...) abort + function! TagbarStatusFunc(current, sort, fname, flags, ...) abort let colour = a:current ? '%#StatusLine#' : '%#StatusLineNC#' - return colour . '[' . a:sort . '] ' . a:fname + let flagstr = join(flags, '') + if flagstr != '' + let flagstr = '[' . flagstr . '] ' + endif + return colour . '[' . sort . '] ' . flagstr . fname endfunction let g:tagbar_status_func = 'TagbarStatusFunc' < diff --git a/plugin/tagbar.vim b/plugin/tagbar.vim index 2887b9a..fe2a815 100644 --- a/plugin/tagbar.vim +++ b/plugin/tagbar.vim @@ -98,10 +98,11 @@ let s:keymaps = [ \ ['openallfolds', ['*', '', 'zR']], \ ['closeallfolds', ['=', 'zM']], \ - \ ['togglesort', 's'], - \ ['zoomwin', 'x'], - \ ['close', 'q'], - \ ['help', ['', '?']], + \ ['togglesort', 's'], + \ ['toggleautoclose', 'c'], + \ ['zoomwin', 'x'], + \ ['close', 'q'], + \ ['help', ['', '?']], \ ] for [map, key] in s:keymaps