2015-02-14 07:46:23 +08:00
|
|
|
" MIT License. Copyright (c) 2013-2015 Bailey Ling.
|
2013-08-28 21:51:54 +08:00
|
|
|
" vim: et ts=2 sts=2 sw=2
|
|
|
|
|
2015-01-04 08:38:17 +08:00
|
|
|
scriptencoding utf-8
|
|
|
|
|
2013-09-07 08:43:51 +08:00
|
|
|
let s:formatter = get(g:, 'airline#extensions#tabline#formatter', 'default')
|
2013-09-05 04:17:02 +08:00
|
|
|
let s:show_buffers = get(g:, 'airline#extensions#tabline#show_buffers', 1)
|
2014-12-27 05:48:06 +08:00
|
|
|
let s:show_tabs = get(g:, 'airline#extensions#tabline#show_tabs', 1)
|
2014-09-02 08:16:58 +08:00
|
|
|
let s:buffer_idx_mode = get(g:, 'airline#extensions#tabline#buffer_idx_mode', 0)
|
2013-11-03 05:46:36 +08:00
|
|
|
let s:spc = g:airline_symbols.space
|
2013-09-05 03:53:04 +08:00
|
|
|
|
2014-09-17 21:52:08 +08:00
|
|
|
let s:number_map = &encoding == 'utf-8'
|
|
|
|
\ ? {
|
|
|
|
\ '0': '⁰',
|
|
|
|
\ '1': '¹',
|
|
|
|
\ '2': '²',
|
|
|
|
\ '3': '³',
|
|
|
|
\ '4': '⁴',
|
|
|
|
\ '5': '⁵',
|
|
|
|
\ '6': '⁶',
|
|
|
|
\ '7': '⁷',
|
|
|
|
\ '8': '⁸',
|
|
|
|
\ '9': '⁹'
|
|
|
|
\ }
|
|
|
|
\ : {}
|
|
|
|
|
2013-08-28 21:51:54 +08:00
|
|
|
function! airline#extensions#tabline#init(ext)
|
2013-09-04 02:48:08 +08:00
|
|
|
if has('gui_running')
|
|
|
|
set guioptions-=e
|
|
|
|
endif
|
2013-09-05 03:53:04 +08:00
|
|
|
|
2013-10-14 06:01:38 +08:00
|
|
|
autocmd User AirlineToggledOn call s:toggle_on()
|
|
|
|
autocmd User AirlineToggledOff call s:toggle_off()
|
2014-02-26 09:34:52 +08:00
|
|
|
autocmd BufDelete * let s:current_bufnr = -1
|
2013-10-14 06:01:38 +08:00
|
|
|
|
|
|
|
call s:toggle_on()
|
|
|
|
call a:ext.add_theme_func('airline#extensions#tabline#load_theme')
|
2014-09-02 08:16:58 +08:00
|
|
|
if s:buffer_idx_mode
|
|
|
|
call s:define_buffer_idx_mode_mappings()
|
|
|
|
endif
|
2013-10-14 06:01:38 +08:00
|
|
|
endfunction
|
2013-09-02 07:39:06 +08:00
|
|
|
|
2013-10-14 06:01:38 +08:00
|
|
|
function! s:toggle_off()
|
2015-02-19 07:58:37 +08:00
|
|
|
call airline#extensions#tabline#autoshow#off()
|
2015-02-19 10:24:05 +08:00
|
|
|
call airline#extensions#tabline#tabs#off()
|
2013-10-14 06:01:38 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:toggle_on()
|
2015-02-19 07:58:37 +08:00
|
|
|
call airline#extensions#tabline#autoshow#on()
|
2015-02-19 10:24:05 +08:00
|
|
|
call airline#extensions#tabline#tabs#on()
|
|
|
|
|
|
|
|
set tabline=%!airline#extensions#tabline#get()
|
2013-09-02 07:39:06 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#extensions#tabline#load_theme(palette)
|
2013-09-05 01:16:23 +08:00
|
|
|
let colors = get(a:palette, 'tabline', {})
|
|
|
|
let l:tab = get(colors, 'airline_tab', a:palette.normal.airline_b)
|
|
|
|
let l:tabsel = get(colors, 'airline_tabsel', a:palette.normal.airline_a)
|
|
|
|
let l:tabtype = get(colors, 'airline_tabtype', a:palette.visual.airline_a)
|
|
|
|
let l:tabfill = get(colors, 'airline_tabfill', a:palette.normal.airline_c)
|
|
|
|
let l:tabmod = get(colors, 'airline_tabmod', a:palette.insert.airline_a)
|
2014-09-08 21:43:15 +08:00
|
|
|
if has_key(a:palette, 'normal_modified') && has_key(a:palette.normal_modified, 'airline_c')
|
|
|
|
let l:tabmodu = get(colors, 'airline_tabmod_unsel', a:palette.normal_modified.airline_c)
|
|
|
|
else
|
|
|
|
"Fall back to normal airline_c if modified airline_c isn't present
|
|
|
|
let l:tabmodu = get(colors, 'airline_tabmod_unsel', a:palette.normal.airline_c)
|
|
|
|
endif
|
|
|
|
|
2013-09-06 06:34:15 +08:00
|
|
|
let l:tabhid = get(colors, 'airline_tabhid', a:palette.normal.airline_c)
|
2013-09-04 23:54:48 +08:00
|
|
|
call airline#highlighter#exec('airline_tab', l:tab)
|
|
|
|
call airline#highlighter#exec('airline_tabsel', l:tabsel)
|
|
|
|
call airline#highlighter#exec('airline_tabtype', l:tabtype)
|
|
|
|
call airline#highlighter#exec('airline_tabfill', l:tabfill)
|
|
|
|
call airline#highlighter#exec('airline_tabmod', l:tabmod)
|
2014-09-05 03:38:12 +08:00
|
|
|
call airline#highlighter#exec('airline_tabmod_unsel', l:tabmodu)
|
2013-09-06 06:34:15 +08:00
|
|
|
call airline#highlighter#exec('airline_tabhid', l:tabhid)
|
2013-08-28 21:51:54 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#extensions#tabline#get()
|
2014-08-13 08:26:52 +08:00
|
|
|
let curtabcnt = tabpagenr('$')
|
|
|
|
if curtabcnt != s:current_tabcnt
|
|
|
|
let s:current_tabcnt = curtabcnt
|
|
|
|
let s:current_bufnr = -1 " force a refresh...
|
|
|
|
endif
|
2015-02-19 10:24:05 +08:00
|
|
|
|
2014-12-27 05:48:06 +08:00
|
|
|
if s:show_buffers && curtabcnt == 1 || !s:show_tabs
|
2013-09-03 06:01:52 +08:00
|
|
|
return s:get_buffers()
|
2013-08-28 21:51:54 +08:00
|
|
|
else
|
2015-02-19 10:24:05 +08:00
|
|
|
return airline#extensions#tabline#tabs#get()
|
2013-08-28 21:51:54 +08:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#extensions#tabline#title(n)
|
|
|
|
let buflist = tabpagebuflist(a:n)
|
|
|
|
let winnr = tabpagewinnr(a:n)
|
2013-09-02 11:03:36 +08:00
|
|
|
return airline#extensions#tabline#get_buffer_name(buflist[winnr - 1])
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#extensions#tabline#get_buffer_name(nr)
|
2015-02-19 07:48:47 +08:00
|
|
|
return airline#extensions#tabline#formatters#{s:formatter}#format(a:nr, airline#extensions#tabline#buflist#list())
|
2013-09-22 02:13:19 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:get_visible_buffers()
|
2015-02-19 07:48:47 +08:00
|
|
|
let buffers = airline#extensions#tabline#buflist#list()
|
2013-09-22 02:13:19 +08:00
|
|
|
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
|
2013-09-22 23:01:12 +08:00
|
|
|
let position = index(buffers, cur)
|
2013-09-26 03:26:25 +08:00
|
|
|
let vimwidth = &columns
|
|
|
|
if total_width > vimwidth && position > -1
|
2013-09-22 02:13:19 +08:00
|
|
|
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
|
2013-09-26 03:26:25 +08:00
|
|
|
let buf_max = vimwidth / max_width
|
2013-09-22 02:13:19 +08:00
|
|
|
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
|
2013-09-06 01:07:31 +08:00
|
|
|
endif
|
|
|
|
|
2014-09-17 21:52:08 +08:00
|
|
|
let g:current_visible_buffers = buffers
|
2013-09-05 03:53:04 +08:00
|
|
|
return buffers
|
|
|
|
endfunction
|
|
|
|
|
2013-12-02 11:00:31 +08:00
|
|
|
let s:current_bufnr = -1
|
2014-08-13 08:26:52 +08:00
|
|
|
let s:current_tabcnt = -1
|
2013-12-02 11:00:31 +08:00
|
|
|
let s:current_tabline = ''
|
2014-01-01 09:38:16 +08:00
|
|
|
let s:current_modified = 0
|
2013-09-05 03:53:04 +08:00
|
|
|
function! s:get_buffers()
|
|
|
|
let cur = bufnr('%')
|
2013-12-02 11:00:31 +08:00
|
|
|
if cur == s:current_bufnr
|
2014-01-01 09:38:16 +08:00
|
|
|
if !g:airline_detect_modified || getbufvar(cur, '&modified') == s:current_modified
|
|
|
|
return s:current_tabline
|
|
|
|
endif
|
2013-12-02 11:00:31 +08:00
|
|
|
endif
|
|
|
|
|
2014-09-17 21:52:08 +08:00
|
|
|
let l:index = 1
|
2015-02-19 10:24:05 +08:00
|
|
|
let b = airline#extensions#tabline#new_builder()
|
2013-09-06 06:34:15 +08:00
|
|
|
let tab_bufs = tabpagebuflist(tabpagenr())
|
2013-09-22 02:13:19 +08:00
|
|
|
for nr in s:get_visible_buffers()
|
2013-09-11 11:49:28 +08:00
|
|
|
if nr < 0
|
|
|
|
call b.add_raw('%#airline_tabhid#...')
|
|
|
|
continue
|
|
|
|
endif
|
2014-09-17 21:52:08 +08:00
|
|
|
|
2013-09-05 03:53:04 +08:00
|
|
|
if cur == nr
|
|
|
|
if g:airline_detect_modified && getbufvar(nr, '&modified')
|
|
|
|
let group = 'airline_tabmod'
|
2013-09-03 10:36:28 +08:00
|
|
|
else
|
2013-09-05 03:53:04 +08:00
|
|
|
let group = 'airline_tabsel'
|
2013-09-03 10:36:28 +08:00
|
|
|
endif
|
2014-01-01 09:38:16 +08:00
|
|
|
let s:current_modified = (group == 'airline_tabmod') ? 1 : 0
|
2013-09-05 03:53:04 +08:00
|
|
|
else
|
2014-09-05 03:38:12 +08:00
|
|
|
if g:airline_detect_modified && getbufvar(nr, '&modified')
|
|
|
|
let group = 'airline_tabmod_unsel'
|
|
|
|
elseif index(tab_bufs, nr) > -1
|
2013-09-06 06:34:15 +08:00
|
|
|
let group = 'airline_tab'
|
|
|
|
else
|
|
|
|
let group = 'airline_tabhid'
|
|
|
|
endif
|
2013-09-03 06:01:52 +08:00
|
|
|
endif
|
2014-09-17 21:52:08 +08:00
|
|
|
|
2014-09-02 08:16:58 +08:00
|
|
|
if s:buffer_idx_mode
|
2014-09-17 21:52:08 +08:00
|
|
|
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
|
2014-09-02 08:16:58 +08:00
|
|
|
let l:index = l:index + 1
|
|
|
|
else
|
|
|
|
call b.add_section(group, s:spc.'%(%{airline#extensions#tabline#get_buffer_name('.nr.')}%)'.s:spc)
|
|
|
|
endif
|
2013-09-03 06:01:52 +08:00
|
|
|
endfor
|
2013-09-05 03:53:04 +08:00
|
|
|
|
2013-09-03 10:36:28 +08:00
|
|
|
call b.add_section('airline_tabfill', '')
|
2013-09-03 06:01:52 +08:00
|
|
|
call b.split()
|
2014-10-19 01:59:24 +08:00
|
|
|
call b.add_section('airline_tabfill', '')
|
2013-09-03 10:36:28 +08:00
|
|
|
call b.add_section('airline_tabtype', ' buffers ')
|
2013-12-02 11:00:31 +08:00
|
|
|
|
|
|
|
let s:current_bufnr = cur
|
|
|
|
let s:current_tabline = b.build()
|
|
|
|
return s:current_tabline
|
2013-09-03 06:01:52 +08:00
|
|
|
endfunction
|
|
|
|
|
2014-09-02 08:16:58 +08:00
|
|
|
function! s:select_tab(buf_index)
|
|
|
|
" no-op when called in the NERDTree buffer
|
|
|
|
if exists('t:NERDTreeBufName') && bufname('%') == t:NERDTreeBufName
|
|
|
|
return
|
|
|
|
endif
|
2014-09-17 21:52:08 +08:00
|
|
|
|
|
|
|
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
|
2014-09-02 08:16:58 +08:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:define_buffer_idx_mode_mappings()
|
2014-09-17 21:52:08 +08:00
|
|
|
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>
|
2014-09-02 08:16:58 +08:00
|
|
|
endfunction
|
|
|
|
|
2015-02-19 10:24:05 +08:00
|
|
|
function! airline#extensions#tabline#new_builder()
|
2014-11-05 03:48:00 +08:00
|
|
|
let builder_context = {
|
|
|
|
\ 'active' : 1,
|
|
|
|
\ 'right_sep' : get(g:, 'airline#extensions#tabline#right_sep' , g:airline_right_sep),
|
|
|
|
\ 'right_alt_sep' : get(g:, 'airline#extensions#tabline#right_alt_sep', g:airline_right_alt_sep),
|
|
|
|
\ }
|
|
|
|
if get(g:, 'airline_powerline_fonts', 0)
|
|
|
|
let builder_context.left_sep = get(g:, 'airline#extensions#tabline#left_sep' , g:airline_left_sep)
|
|
|
|
let builder_context.left_alt_sep = get(g:, 'airline#extensions#tabline#left_alt_sep' , g:airline_left_alt_sep)
|
|
|
|
else
|
|
|
|
let builder_context.left_sep = get(g:, 'airline#extensions#tabline#left_sep' , ' ')
|
|
|
|
let builder_context.left_alt_sep = get(g:, 'airline#extensions#tabline#left_alt_sep' , '|')
|
|
|
|
endif
|
|
|
|
|
|
|
|
return airline#builder#new(builder_context)
|
|
|
|
endfunction
|