From df097993727bcd3c72cf679020561483105184b0 Mon Sep 17 00:00:00 2001 From: Jan Larres Date: Sun, 9 Nov 2014 17:33:42 +1300 Subject: [PATCH] Add mappings for moving through folds, closes #224 --- autoload/tagbar.vim | 59 +++++++++++++++++++++++++++++++++++++++++++++ doc/tagbar.txt | 4 +++ plugin/tagbar.vim | 2 ++ 3 files changed, 65 insertions(+) diff --git a/autoload/tagbar.vim b/autoload/tagbar.vim index 15f18e4..2ac0732 100644 --- a/autoload/tagbar.vim +++ b/autoload/tagbar.vim @@ -955,6 +955,8 @@ function! s:MapKeys() abort \ ['togglefold', 'ToggleFold()'], \ ['openallfolds', 'SetFoldLevel(99, 1)'], \ ['closeallfolds', 'SetFoldLevel(0, 1)'], + \ ['nextfold', 'GotoNextFold()'], + \ ['prevfold', 'GotoPrevFold()'], \ \ ['togglesort', 'ToggleSort()'], \ ['toggleautoclose', 'ToggleAutoclose()'], @@ -2854,6 +2856,8 @@ function! s:PrintHelp() abort silent put ='\" ' . s:get_map_str('togglefold') . ': Toggle fold' silent put ='\" ' . s:get_map_str('openallfolds') . ': Open all folds' silent put ='\" ' . s:get_map_str('closeallfolds') . ': Close all folds' + silent put ='\" ' . s:get_map_str('nextfold') . ': Go to next fold' + silent put ='\" ' . s:get_map_str('prevfold') . ': Go to previous fold' silent put ='\"' silent put ='\" ---------- Misc -----------' silent put ='\" ' . s:get_map_str('togglesort') . ': Toggle sort' @@ -3302,6 +3306,61 @@ function! s:OpenParents(...) abort endif endfunction +" s:GotoNextFold() {{{2 +function! s:GotoNextFold() abort + let curlinenr = line('.') + let newlinenr = line('.') + + let range = range(line('.') + 1, line('$')) + + for linenr in range + let taginfo = s:GetTagInfo(linenr, 0) + + if empty(taginfo) + continue + elseif !empty(get(taginfo, 'children', [])) || taginfo.isKindheader() + let newlinenr = linenr + break + endif + endfor + + if curlinenr != newlinenr + execute linenr + call winline() + endif + + redraw +endfunction + +" s:GotoPrevFold() {{{2 +function! s:GotoPrevFold() abort + let curlinenr = line('.') + let newlinenr = line('.') + let curtag = s:GetTagInfo(curlinenr, 0) + let curparent = get(curtag, 'parent', {}) + + let range = range(line('.') - 1, 1, -1) + + for linenr in range + let taginfo = s:GetTagInfo(linenr, 0) + + if empty(taginfo) + continue + elseif !empty(taginfo.parent) && taginfo.parent != curparent && + \ empty(get(taginfo, 'children', [])) + let newlinenr = linenr + break + endif + endfor + + if curlinenr != newlinenr + execute linenr + call winline() + endif + + redraw +endfunction + " Helper functions {{{1 " s:AutoUpdate() {{{2 function! s:AutoUpdate(fname, force) abort diff --git a/doc/tagbar.txt b/doc/tagbar.txt index d933392..214df1e 100644 --- a/doc/tagbar.txt +++ b/doc/tagbar.txt @@ -347,6 +347,10 @@ o/za Toggle the fold under the cursor or the current one if there is Map option: tagbar_map_openallfolds =/zM Close all folds by setting foldlevel to 0. Map option: tagbar_map_closeallfolds +zj Go to the start of the next fold, like the standard Vim |zj|. + Map option: tagbar_map_nextfold +zk Go to the end of the previous fold, like the standard Vim |zk|. + Map option: tagbar_map_prevfold s Toggle sort order between name and file order. Map option: tagbar_map_togglesort c Toggle the |g:tagbar_autoclose| option. diff --git a/plugin/tagbar.vim b/plugin/tagbar.vim index d9507fe..873abb0 100644 --- a/plugin/tagbar.vim +++ b/plugin/tagbar.vim @@ -104,6 +104,8 @@ let s:keymaps = [ \ ['togglefold', ['o', 'za']], \ ['openallfolds', ['*', '', 'zR']], \ ['closeallfolds', ['=', 'zM']], + \ ['nextfold', 'zj'], + \ ['prevfold', 'zk'], \ \ ['togglesort', 's'], \ ['toggleautoclose', 'c'],