From ae472d6716f7bcf10ba80af9149d5b8b4a534021 Mon Sep 17 00:00:00 2001 From: Yu Huang Date: Mon, 1 Sep 2014 17:16:58 -0700 Subject: [PATCH] Add buffer_idx_mode to vim-airline. Update documentation. Keep buffer index-id map a script scope variable. --- autoload/airline/extensions/tabline.vim | 38 ++++++++++++++++++++++++- doc/airline.txt | 11 +++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/autoload/airline/extensions/tabline.vim b/autoload/airline/extensions/tabline.vim index 9019517f..3f724ad6 100644 --- a/autoload/airline/extensions/tabline.vim +++ b/autoload/airline/extensions/tabline.vim @@ -9,6 +9,7 @@ let s:show_tab_nr = get(g:, 'airline#extensions#tabline#show_tab_nr', 1) let s:show_tab_type = get(g:, 'airline#extensions#tabline#show_tab_type', 1) let s:show_close_button = get(g:, 'airline#extensions#tabline#show_close_button', 1) let s:close_symbol = get(g:, 'airline#extensions#tabline#close_symbol', 'X') +let s:buffer_idx_mode = get(g:, 'airline#extensions#tabline#buffer_idx_mode', 0) let s:builder_context = { \ 'active' : 1, @@ -38,6 +39,9 @@ function! airline#extensions#tabline#init(ext) call s:toggle_on() call a:ext.add_theme_func('airline#extensions#tabline#load_theme') + if s:buffer_idx_mode + call s:define_buffer_idx_mode_mappings() + endif endfunction function! s:toggle_off() @@ -210,6 +214,10 @@ function! s:get_buffers() endif endif + if s:buffer_idx_mode + let s:buffer_idx_mode_buffers = [] + let l:index = 1 + endif let b = airline#builder#new(s:builder_context) let tab_bufs = tabpagebuflist(tabpagenr()) for nr in s:get_visible_buffers() @@ -233,7 +241,13 @@ function! s:get_buffers() let group = 'airline_tabhid' endif endif - call b.add_section(group, s:spc.'%(%{airline#extensions#tabline#get_buffer_name('.nr.')}%)'.s:spc) + if s:buffer_idx_mode + call b.add_section(group, '['.l:index.s:spc.'%(%{airline#extensions#tabline#get_buffer_name('.nr.')}%)'.']') + 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 endfor call b.add_section('airline_tabfill', '') @@ -245,6 +259,28 @@ function! s:get_buffers() return s:current_tabline endfunction +function! s:select_tab(buf_index) + " no-op when called in the NERDTree buffer + 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] + 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) +endfunction + function! s:get_tabs() let curbuf = bufnr('%') let curtab = tabpagenr() diff --git a/doc/airline.txt b/doc/airline.txt index 0864218a..970ba8f1 100644 --- a/doc/airline.txt +++ b/doc/airline.txt @@ -404,6 +404,17 @@ eclim * enable/disable displaying tab type (far right) 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. + let g:airline#extensions#tabline#buffer_idx_mode = 1 + * defines the name of a formatter for how buffer names are displayed. > let g:airline#extensions#tabline#formatter = 'default'