extract buffers view mode into its own file

This commit is contained in:
Bailey Ling 2015-02-18 21:37:13 -05:00
parent 987306dcaf
commit 7394642293
3 changed files with 197 additions and 168 deletions

View File

@ -1,28 +1,9 @@
" MIT License. Copyright (c) 2013-2015 Bailey Ling.
" vim: et ts=2 sts=2 sw=2
scriptencoding utf-8
let s:formatter = get(g:, 'airline#extensions#tabline#formatter', 'default')
let s:show_buffers = get(g:, 'airline#extensions#tabline#show_buffers', 1)
let s:show_tabs = get(g:, 'airline#extensions#tabline#show_tabs', 1)
let s:buffer_idx_mode = get(g:, 'airline#extensions#tabline#buffer_idx_mode', 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')
@ -31,23 +12,21 @@ function! airline#extensions#tabline#init(ext)
autocmd User AirlineToggledOn call s:toggle_on()
autocmd User AirlineToggledOff call s:toggle_off()
autocmd BufDelete * let s:current_bufnr = -1
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()
call airline#extensions#tabline#autoshow#off()
call airline#extensions#tabline#tabs#off()
call airline#extensions#tabline#buffers#off()
endfunction
function! s:toggle_on()
call airline#extensions#tabline#autoshow#on()
call airline#extensions#tabline#tabs#on()
call airline#extensions#tabline#buffers#on()
set tabline=%!airline#extensions#tabline#get()
endfunction
@ -76,15 +55,17 @@ function! airline#extensions#tabline#load_theme(palette)
call airline#highlighter#exec('airline_tabhid', l:tabhid)
endfunction
let s:current_tabcnt = -1
function! airline#extensions#tabline#get()
let curtabcnt = tabpagenr('$')
if curtabcnt != s:current_tabcnt
let s:current_tabcnt = curtabcnt
let s:current_bufnr = -1 " force a refresh...
call airline#extensions#tabline#tabs#invalidate()
call airline#extensions#tabline#buffers#invalidate()
endif
if s:show_buffers && curtabcnt == 1 || !s:show_tabs
return s:get_buffers()
return airline#extensions#tabline#buffers#get()
else
return airline#extensions#tabline#tabs#get()
endif
@ -100,148 +81,6 @@ function! airline#extensions#tabline#get_buffer_name(nr)
return airline#extensions#tabline#formatters#{s:formatter}#format(a:nr, airline#extensions#tabline#buflist#list())
endfunction
function! s:get_visible_buffers()
let buffers = airline#extensions#tabline#buflist#list()
let cur = bufnr('%')
let total_width = 0
let max_width = 0
for nr in buffers
let width = len(airline#extensions#tabline#get_buffer_name(nr)) + 4
let total_width += width
let max_width = max([max_width, width])
endfor
" only show current and surrounding buffers if there are too many buffers
let position = index(buffers, cur)
let vimwidth = &columns
if total_width > vimwidth && position > -1
let buf_count = len(buffers)
" determine how many buffers to show based on the longest buffer width,
" use one on the right side and put the rest on the left
let buf_max = vimwidth / max_width
let buf_right = 1
let buf_left = max([0, buf_max - buf_right])
let start = max([0, position - buf_left])
let end = min([buf_count, position + buf_right])
" fill up available space on the right
if position < buf_left
let end += (buf_left - position)
endif
" fill up available space on the left
if end > buf_count - 1 - buf_right
let start -= max([0, buf_right - (buf_count - 1 - position)])
endif
let buffers = eval('buffers[' . start . ':' . end . ']')
if start > 0
call insert(buffers, -1, 0)
endif
if end < buf_count - 1
call add(buffers, -1)
endif
endif
let g:current_visible_buffers = buffers
return buffers
endfunction
let s:current_bufnr = -1
let s:current_tabcnt = -1
let s:current_tabline = ''
let s:current_modified = 0
function! s:get_buffers()
let cur = bufnr('%')
if cur == s:current_bufnr
if !g:airline_detect_modified || getbufvar(cur, '&modified') == s:current_modified
return s:current_tabline
endif
endif
let l:index = 1
let b = airline#extensions#tabline#new_builder()
let tab_bufs = tabpagebuflist(tabpagenr())
for nr in s:get_visible_buffers()
if nr < 0
call b.add_raw('%#airline_tabhid#...')
continue
endif
if cur == nr
if g:airline_detect_modified && getbufvar(nr, '&modified')
let group = 'airline_tabmod'
else
let group = 'airline_tabsel'
endif
let s:current_modified = (group == 'airline_tabmod') ? 1 : 0
else
if g:airline_detect_modified && getbufvar(nr, '&modified')
let group = 'airline_tabmod_unsel'
elseif index(tab_bufs, nr) > -1
let group = 'airline_tab'
else
let group = 'airline_tabhid'
endif
endif
if s:buffer_idx_mode
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
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', '')
call b.split()
call b.add_section('airline_tabfill', '')
call b.add_section('airline_tabtype', ' buffers ')
let s:current_bufnr = cur
let s:current_tabline = b.build()
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
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 <unique> <Plug>AirlineSelectTab1 :call <SID>select_tab(0)<CR>
noremap <unique> <Plug>AirlineSelectTab2 :call <SID>select_tab(1)<CR>
noremap <unique> <Plug>AirlineSelectTab3 :call <SID>select_tab(2)<CR>
noremap <unique> <Plug>AirlineSelectTab4 :call <SID>select_tab(3)<CR>
noremap <unique> <Plug>AirlineSelectTab5 :call <SID>select_tab(4)<CR>
noremap <unique> <Plug>AirlineSelectTab6 :call <SID>select_tab(5)<CR>
noremap <unique> <Plug>AirlineSelectTab7 :call <SID>select_tab(6)<CR>
noremap <unique> <Plug>AirlineSelectTab8 :call <SID>select_tab(7)<CR>
noremap <unique> <Plug>AirlineSelectTab9 :call <SID>select_tab(8)<CR>
endfunction
function! airline#extensions#tabline#new_builder()
let builder_context = {
\ 'active' : 1,

View File

@ -0,0 +1,186 @@
" MIT License. Copyright (c) 2013-2015 Bailey Ling.
" vim: et ts=2 sts=2 sw=2
scriptencoding utf-8
let s:buffer_idx_mode = get(g:, 'airline#extensions#tabline#buffer_idx_mode', 0)
let s:spc = g:airline_symbols.space
let s:current_bufnr = -1
let s:current_modified = 0
let s:current_tabline = ''
let s:current_visible_buffers = []
let s:number_map = &encoding == 'utf-8'
\ ? {
\ '0': '⁰',
\ '1': '¹',
\ '2': '²',
\ '3': '³',
\ '4': '⁴',
\ '5': '⁵',
\ '6': '⁶',
\ '7': '⁷',
\ '8': '⁸',
\ '9': '⁹'
\ }
\ : {}
function! airline#extensions#tabline#buffers#off()
augroup airline_tabline_buffers
autocmd!
augroup END
endfunction
function! airline#extensions#tabline#buffers#on()
augroup airline_tabline_buffers
autocmd!
autocmd BufDelete * call airline#extensions#tabline#buffers#invalidate()
augroup END
if s:buffer_idx_mode
call s:define_buffer_idx_mode_mappings()
endif
endfunction
function! airline#extensions#tabline#buffers#invalidate()
let s:current_bufnr = -1
endfunction
function! airline#extensions#tabline#buffers#get()
let cur = bufnr('%')
if cur == s:current_bufnr
if !g:airline_detect_modified || getbufvar(cur, '&modified') == s:current_modified
return s:current_tabline
endif
endif
let l:index = 1
let b = airline#extensions#tabline#new_builder()
let tab_bufs = tabpagebuflist(tabpagenr())
for nr in s:get_visible_buffers()
if nr < 0
call b.add_raw('%#airline_tabhid#...')
continue
endif
if cur == nr
if g:airline_detect_modified && getbufvar(nr, '&modified')
let group = 'airline_tabmod'
else
let group = 'airline_tabsel'
endif
let s:current_modified = (group == 'airline_tabmod') ? 1 : 0
else
if g:airline_detect_modified && getbufvar(nr, '&modified')
let group = 'airline_tabmod_unsel'
elseif index(tab_bufs, nr) > -1
let group = 'airline_tab'
else
let group = 'airline_tabhid'
endif
endif
if s:buffer_idx_mode
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
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', '')
call b.split()
call b.add_section('airline_tabfill', '')
call b.add_section('airline_tabtype', ' buffers ')
let s:current_bufnr = cur
let s:current_tabline = b.build()
return s:current_tabline
endfunction
function! s:get_visible_buffers()
let buffers = airline#extensions#tabline#buflist#list()
let cur = bufnr('%')
let total_width = 0
let max_width = 0
for nr in buffers
let width = len(airline#extensions#tabline#get_buffer_name(nr)) + 4
let total_width += width
let max_width = max([max_width, width])
endfor
" only show current and surrounding buffers if there are too many buffers
let position = index(buffers, cur)
let vimwidth = &columns
if total_width > vimwidth && position > -1
let buf_count = len(buffers)
" determine how many buffers to show based on the longest buffer width,
" use one on the right side and put the rest on the left
let buf_max = vimwidth / max_width
let buf_right = 1
let buf_left = max([0, buf_max - buf_right])
let start = max([0, position - buf_left])
let end = min([buf_count, position + buf_right])
" fill up available space on the right
if position < buf_left
let end += (buf_left - position)
endif
" fill up available space on the left
if end > buf_count - 1 - buf_right
let start -= max([0, buf_right - (buf_count - 1 - position)])
endif
let buffers = eval('buffers[' . start . ':' . end . ']')
if start > 0
call insert(buffers, -1, 0)
endif
if end < buf_count - 1
call add(buffers, -1)
endif
endif
let s:current_visible_buffers = buffers
return buffers
endfunction
function! s:select_tab(buf_index)
" no-op when called in the NERDTree buffer
if exists('t:NERDTreeBufName') && bufname('%') == t:NERDTreeBufName
return
endif
let idx = a:buf_index
if s:current_visible_buffers[0] == -1
let idx = idx + 1
endif
let buf = get(s:current_visible_buffers, idx, 0)
if buf != 0
exec 'b!' . buf
endif
endfunction
function! s:define_buffer_idx_mode_mappings()
noremap! <unique> <Plug>AirlineSelectTab1 :call <SID>select_tab(0)<CR>
noremap! <unique> <Plug>AirlineSelectTab2 :call <SID>select_tab(1)<CR>
noremap! <unique> <Plug>AirlineSelectTab3 :call <SID>select_tab(2)<CR>
noremap! <unique> <Plug>AirlineSelectTab4 :call <SID>select_tab(3)<CR>
noremap! <unique> <Plug>AirlineSelectTab5 :call <SID>select_tab(4)<CR>
noremap! <unique> <Plug>AirlineSelectTab6 :call <SID>select_tab(5)<CR>
noremap! <unique> <Plug>AirlineSelectTab7 :call <SID>select_tab(6)<CR>
noremap! <unique> <Plug>AirlineSelectTab8 :call <SID>select_tab(7)<CR>
noremap! <unique> <Plug>AirlineSelectTab9 :call <SID>select_tab(8)<CR>
endfunction

View File

@ -20,10 +20,14 @@ endfunction
function! airline#extensions#tabline#tabs#on()
augroup airline_tabline_tabs
autocmd!
autocmd BufDelete * let s:current_bufnr = -1
autocmd BufDelete * call airline#extensions#tabline#tabs#invalidate()
augroup END
endfunction
function! airline#extensions#tabline#tabs#invalidate()
let s:current_bufnr = -1
endfunction
function! airline#extensions#tabline#tabs#get()
let curbuf = bufnr('%')
let curtab = tabpagenr()