From 4dab93470f385559c9a914525ff4c2bdd569d4ea Mon Sep 17 00:00:00 2001 From: Bailey Ling Date: Wed, 17 Sep 2014 09:52:08 -0400 Subject: [PATCH] improve buffer index mode and fix usages where there's overflow --- autoload/airline/extensions/tabline.vim | 59 ++++++++++++++++++------- doc/airline.txt | 24 ++++++---- 2 files changed, 58 insertions(+), 25 deletions(-) diff --git a/autoload/airline/extensions/tabline.vim b/autoload/airline/extensions/tabline.vim index 3f724ad6..955f3db1 100644 --- a/autoload/airline/extensions/tabline.vim +++ b/autoload/airline/extensions/tabline.vim @@ -28,6 +28,21 @@ let s:buf_min_count = get(g:, 'airline#extensions#tabline#buffer_min_count', 0) let s:tab_min_count = get(g:, 'airline#extensions#tabline#tab_min_count', 0) let s:spc = g:airline_symbols.space +let s:number_map = &encoding == 'utf-8' + \ ? { + \ '0': '⁰', + \ '1': '¹', + \ '2': '²', + \ '3': '³', + \ '4': '⁴', + \ '5': '⁵', + \ '6': '⁶', + \ '7': '⁷', + \ '8': '⁸', + \ '9': '⁹' + \ } + \ : {} + function! airline#extensions#tabline#init(ext) if has('gui_running') set guioptions-=e @@ -198,6 +213,7 @@ function! s:get_visible_buffers() endif endif + let g:current_visible_buffers = buffers return buffers endfunction @@ -214,10 +230,7 @@ function! s:get_buffers() endif endif - if s:buffer_idx_mode - let s:buffer_idx_mode_buffers = [] - let l:index = 1 - endif + let l:index = 1 let b = airline#builder#new(s:builder_context) let tab_bufs = tabpagebuflist(tabpagenr()) for nr in s:get_visible_buffers() @@ -225,6 +238,7 @@ function! s:get_buffers() call b.add_raw('%#airline_tabhid#...') continue endif + if cur == nr if g:airline_detect_modified && getbufvar(nr, '&modified') let group = 'airline_tabmod' @@ -241,10 +255,14 @@ function! s:get_buffers() let group = 'airline_tabhid' endif endif + if s:buffer_idx_mode - call b.add_section(group, '['.l:index.s:spc.'%(%{airline#extensions#tabline#get_buffer_name('.nr.')}%)'.']') + if len(s:number_map) > 0 + call b.add_section(group, s:spc . get(s:number_map, l:index, '') . '%(%{airline#extensions#tabline#get_buffer_name('.nr.')}%)' . s:spc) + else + call b.add_section(group, '['.l:index.s:spc.'%(%{airline#extensions#tabline#get_buffer_name('.nr.')}%)'.']') + endif let l:index = l:index + 1 - call add(s:buffer_idx_mode_buffers, nr) else call b.add_section(group, s:spc.'%(%{airline#extensions#tabline#get_buffer_name('.nr.')}%)'.s:spc) endif @@ -264,21 +282,28 @@ function! s:select_tab(buf_index) if exists('t:NERDTreeBufName') && bufname('%') == t:NERDTreeBufName return endif - if a:buf_index <= len(s:buffer_idx_mode_buffers) - exec "b!" . s:buffer_idx_mode_buffers[a:buf_index - 1] + + let idx = a:buf_index + if g:current_visible_buffers[0] == -1 + let idx = idx + 1 + endif + + let buf = get(g:current_visible_buffers, idx, 0) + if buf != 0 + exec 'b!' . buf endif endfunction function! s:define_buffer_idx_mode_mappings() - noremap AirlineSelectTab1 :call select_tab(1) - noremap AirlineSelectTab2 :call select_tab(2) - noremap AirlineSelectTab3 :call select_tab(3) - noremap AirlineSelectTab4 :call select_tab(4) - noremap AirlineSelectTab5 :call select_tab(5) - noremap AirlineSelectTab6 :call select_tab(6) - noremap AirlineSelectTab7 :call select_tab(7) - noremap AirlineSelectTab8 :call select_tab(8) - noremap AirlineSelectTab9 :call select_tab(9) + noremap AirlineSelectTab1 :call select_tab(0) + noremap AirlineSelectTab2 :call select_tab(1) + noremap AirlineSelectTab3 :call select_tab(2) + noremap AirlineSelectTab4 :call select_tab(3) + noremap AirlineSelectTab5 :call select_tab(4) + noremap AirlineSelectTab6 :call select_tab(5) + noremap AirlineSelectTab7 :call select_tab(6) + noremap AirlineSelectTab8 :call select_tab(7) + noremap AirlineSelectTab9 :call select_tab(8) endfunction function! s:get_tabs() diff --git a/doc/airline.txt b/doc/airline.txt index 526ffad6..ddc81ce0 100644 --- a/doc/airline.txt +++ b/doc/airline.txt @@ -405,15 +405,23 @@ eclim let g:airline#extensions#tabline#show_tab_type = 1 * enable/disable displaying index of the buffer. - " When enabled, a map between buffer index and buffer number - " will be maintained. Up to nine mappings will be defined to - " allow switching to a buffer using its index easily. - " The mappings have the form AirlineSelectTab{x} - " where x is a number from 1 to 9. One can defining mappings like this - " nmap 1 AirlineSelectTab1 - " Note that buffering switching won't happen if the current buffer - " is a NERDTree buffer. + +When enabled, numbers will be displayed in the tabline and mappings will be +exposed to allow you to select a buffer directly. Up to 9 mappings will be +exposed. + let g:airline#extensions#tabline#buffer_idx_mode = 1 + nmap 1 AirlineSelectTab1 + nmap 2 AirlineSelectTab2 + nmap 3 AirlineSelectTab3 + nmap 4 AirlineSelectTab4 + nmap 5 AirlineSelectTab5 + nmap 6 AirlineSelectTab6 + nmap 7 AirlineSelectTab7 + nmap 8 AirlineSelectTab8 + nmap 9 AirlineSelectTab9 + + Note: Mappings will be ignored within a NERDTree buffer. * defines the name of a formatter for how buffer names are displayed. > let g:airline#extensions#tabline#formatter = 'default'