2013-08-28 21:51:54 +08:00
|
|
|
" MIT License. Copyright (c) 2013 Bailey Ling.
|
|
|
|
" vim: et ts=2 sts=2 sw=2
|
|
|
|
|
2013-09-07 05:50:06 +08:00
|
|
|
let s:fnamefunc = get(g:, 'airline#extensions#tabline#fnamefunc', 'airline#extensions#tabline#fname_func')
|
2013-09-03 06:01:52 +08:00
|
|
|
let s:excludes = get(g:, 'airline#extensions#tabline#excludes', [])
|
2013-09-05 02:06:57 +08:00
|
|
|
let s:tab_nr_type = get(g:, 'airline#extensions#tabline#tab_nr_type', 0)
|
2013-09-05 04:17:02 +08:00
|
|
|
let s:show_buffers = get(g:, 'airline#extensions#tabline#show_buffers', 1)
|
2013-09-04 23:47:05 +08:00
|
|
|
let s:buf_nr_show = get(g:, 'airline#extensions#tabline#buffer_nr_show', 0)
|
|
|
|
let s:buf_nr_format = get(g:, 'airline#extensions#tabline#buffer_nr_format', '%s: ')
|
|
|
|
let s:buf_modified_symbol = g:airline_symbols.modified
|
2013-09-02 08:06:41 +08:00
|
|
|
|
2013-09-05 03:20:06 +08:00
|
|
|
let s:builder_context = {
|
|
|
|
\ 'active' : 1,
|
|
|
|
\ 'left_sep' : get(g:, 'airline#extensions#tabline#left_sep' , g:airline_left_sep),
|
|
|
|
\ 'left_alt_sep' : get(g:, 'airline#extensions#tabline#left_alt_sep' , g:airline_left_alt_sep),
|
|
|
|
\ '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),
|
|
|
|
\ }
|
|
|
|
|
2013-09-05 03:53:04 +08:00
|
|
|
let s:buf_min_count = get(g:, 'airline#extensions#tabline#buffer_min_count', 0)
|
|
|
|
let s:buf_len = 0
|
|
|
|
|
2013-09-06 01:07:31 +08:00
|
|
|
" TODO: temporary
|
|
|
|
let s:buf_max = get(g:, 'airline#extensions#tabline#buffer_max', 6)
|
|
|
|
|
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-08-28 21:51:54 +08:00
|
|
|
set tabline=%!airline#extensions#tabline#get()
|
2013-09-02 07:39:06 +08:00
|
|
|
|
2013-09-05 03:53:04 +08:00
|
|
|
if s:buf_min_count <= 0
|
|
|
|
set showtabline=2
|
|
|
|
else
|
|
|
|
autocmd CursorMoved * call <sid>cursormove()
|
|
|
|
endif
|
|
|
|
|
2013-09-02 07:39:06 +08:00
|
|
|
call a:ext.add_theme_func('airline#extensions#tabline#load_theme')
|
|
|
|
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)
|
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)
|
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
|
|
|
|
|
2013-09-05 03:53:04 +08:00
|
|
|
function! s:cursormove()
|
|
|
|
let c = len(s:get_buffer_list())
|
|
|
|
if c > s:buf_min_count
|
|
|
|
if &showtabline != 2
|
|
|
|
set showtabline=2
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
if &showtabline != 0
|
|
|
|
set showtabline=0
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2013-08-28 21:51:54 +08:00
|
|
|
function! airline#extensions#tabline#get()
|
2013-09-05 04:17:02 +08:00
|
|
|
if s:show_buffers && tabpagenr('$') == 1
|
2013-09-03 06:01:52 +08:00
|
|
|
return s:get_buffers()
|
2013-08-28 21:51:54 +08:00
|
|
|
else
|
2013-09-03 06:01:52 +08:00
|
|
|
return s:get_tabs()
|
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
|
|
|
|
|
2013-09-07 05:50:06 +08:00
|
|
|
function! airline#extensions#tabline#fname_func(name)
|
|
|
|
let fmod = get(g:, 'airline#extensions#tabline#fnamemod', ':p:.')
|
|
|
|
return substitute(fnamemodify(a:name, fmod), '\w\zs.\{-}\ze\/', '', 'g')
|
|
|
|
endfunction
|
|
|
|
|
2013-09-02 11:03:36 +08:00
|
|
|
function! airline#extensions#tabline#get_buffer_name(nr)
|
2013-09-04 23:47:05 +08:00
|
|
|
let _ = ''
|
2013-09-02 11:03:36 +08:00
|
|
|
let name = bufname(a:nr)
|
2013-09-04 23:47:05 +08:00
|
|
|
|
|
|
|
if s:buf_nr_show
|
|
|
|
let _ .= printf(s:buf_nr_format, a:nr)
|
|
|
|
endif
|
|
|
|
|
2013-09-02 11:03:36 +08:00
|
|
|
if empty(name)
|
2013-09-04 23:47:05 +08:00
|
|
|
let _ .= '[No Name]'
|
|
|
|
else
|
2013-09-07 05:50:06 +08:00
|
|
|
let _ .= call(s:fnamefunc, [name])
|
2013-09-02 11:03:36 +08:00
|
|
|
endif
|
2013-09-04 23:47:05 +08:00
|
|
|
|
|
|
|
if getbufvar(a:nr, '&modified') == 1
|
|
|
|
let _ .= s:buf_modified_symbol
|
|
|
|
endif
|
|
|
|
|
|
|
|
return _
|
2013-08-28 21:51:54 +08:00
|
|
|
endfunction
|
|
|
|
|
2013-09-05 03:53:04 +08:00
|
|
|
function! s:get_buffer_list()
|
|
|
|
let buffers = []
|
2013-09-03 06:01:52 +08:00
|
|
|
let cur = bufnr('%')
|
|
|
|
for nr in range(1, bufnr('$'))
|
|
|
|
if buflisted(nr) && bufexists(nr)
|
|
|
|
for ex in s:excludes
|
|
|
|
if match(bufname(nr), ex)
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
endfor
|
2013-09-05 03:53:04 +08:00
|
|
|
call add(buffers, nr)
|
|
|
|
endif
|
|
|
|
endfor
|
2013-09-06 01:07:31 +08:00
|
|
|
|
|
|
|
" TODO: temporary fix; force the active buffer to be first when there are many buffers open
|
2013-09-06 01:17:04 +08:00
|
|
|
if len(buffers) > s:buf_max && index(buffers, cur) > -1
|
2013-09-06 01:07:31 +08:00
|
|
|
while buffers[0] != cur
|
|
|
|
let first = remove(buffers, 0)
|
|
|
|
call add(buffers, first)
|
|
|
|
endwhile
|
|
|
|
let buffers = buffers[:s:buf_max]
|
|
|
|
endif
|
|
|
|
|
2013-09-05 03:53:04 +08:00
|
|
|
return buffers
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:get_buffers()
|
|
|
|
let b = airline#builder#new(s:builder_context)
|
|
|
|
let cur = bufnr('%')
|
2013-09-06 06:34:15 +08:00
|
|
|
let tab_bufs = tabpagebuflist(tabpagenr())
|
2013-09-05 03:53:04 +08:00
|
|
|
for nr in s:get_buffer_list()
|
|
|
|
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
|
2013-09-05 03:53:04 +08:00
|
|
|
else
|
2013-09-06 06:34:15 +08:00
|
|
|
if index(tab_bufs, nr) > -1
|
|
|
|
let group = 'airline_tab'
|
|
|
|
else
|
|
|
|
let group = 'airline_tabhid'
|
|
|
|
endif
|
2013-09-03 06:01:52 +08:00
|
|
|
endif
|
2013-09-05 03:53:04 +08:00
|
|
|
call b.add_section(group, '%( %{airline#extensions#tabline#get_buffer_name('.nr.')} %)')
|
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()
|
2013-09-03 10:36:28 +08:00
|
|
|
call b.add_section('airline_tabtype', ' buffers ')
|
2013-09-03 06:01:52 +08:00
|
|
|
return b.build()
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:get_tabs()
|
2013-09-05 03:20:06 +08:00
|
|
|
let b = airline#builder#new(s:builder_context)
|
2013-09-03 06:01:52 +08:00
|
|
|
for i in range(1, tabpagenr('$'))
|
2013-09-05 13:52:29 +08:00
|
|
|
if i == tabpagenr()
|
|
|
|
let group = 'airline_tabsel'
|
|
|
|
if g:airline_detect_modified
|
|
|
|
for bi in tabpagebuflist(i)
|
|
|
|
if getbufvar(bi, '&modified')
|
|
|
|
let group = 'airline_tabmod'
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
let group = 'airline_tab'
|
|
|
|
endif
|
2013-09-05 02:06:57 +08:00
|
|
|
let val = '%('
|
|
|
|
if s:tab_nr_type == 0
|
|
|
|
let val .= ' %{len(tabpagebuflist('.i.'))}'
|
|
|
|
else
|
|
|
|
let val .= ' '.i
|
|
|
|
endif
|
|
|
|
call b.add_section(group, val.'%'.i.'T %{airline#extensions#tabline#title('.i.')} %)')
|
2013-09-03 06:01:52 +08:00
|
|
|
endfor
|
|
|
|
call b.add_raw('%T')
|
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()
|
2013-09-03 10:36:28 +08:00
|
|
|
call b.add_section('airline_tab', ' %999XX ')
|
|
|
|
call b.add_section('airline_tabtype', ' tabs ')
|
2013-09-03 06:01:52 +08:00
|
|
|
return b.build()
|
|
|
|
endfunction
|
|
|
|
|