2014-01-20 04:44:44 +00:00
|
|
|
" MIT License. Copyright (c) 2013-2014 Bailey Ling.
|
2013-08-28 13:51:54 +00:00
|
|
|
" vim: et ts=2 sts=2 sw=2
|
|
|
|
|
2013-09-07 00:43:51 +00:00
|
|
|
let s:formatter = get(g:, 'airline#extensions#tabline#formatter', 'default')
|
2013-09-02 22:01:52 +00:00
|
|
|
let s:excludes = get(g:, 'airline#extensions#tabline#excludes', [])
|
2013-09-04 18:06:57 +00:00
|
|
|
let s:tab_nr_type = get(g:, 'airline#extensions#tabline#tab_nr_type', 0)
|
2013-09-04 20:17:02 +00:00
|
|
|
let s:show_buffers = get(g:, 'airline#extensions#tabline#show_buffers', 1)
|
2013-12-22 00:27:42 +00:00
|
|
|
let s:show_tab_nr = get(g:, 'airline#extensions#tabline#show_tab_nr', 1)
|
2014-03-19 20:39:07 +00:00
|
|
|
let s:show_tab_type = get(g:, 'airline#extensions#tabline#show_tab_type', 1)
|
|
|
|
let s:close_symbol = get(g:, 'airline#extensions#tabline#close_symbol', 'X')
|
2013-09-02 00:06:41 +00:00
|
|
|
|
2013-09-04 19:20:06 +00:00
|
|
|
let s: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),
|
|
|
|
\ }
|
2013-09-23 18:52:37 +00:00
|
|
|
if get(g:, 'airline_powerline_fonts', 0)
|
2013-09-27 02:15:49 +00:00
|
|
|
let s:builder_context.left_sep = get(g:, 'airline#extensions#tabline#left_sep' , "\ue0b0")
|
|
|
|
let s:builder_context.left_alt_sep = get(g:, 'airline#extensions#tabline#left_alt_sep' , "\ue0b1")
|
2013-09-23 18:52:37 +00:00
|
|
|
else
|
|
|
|
let s:builder_context.left_sep = get(g:, 'airline#extensions#tabline#left_sep' , ' ')
|
|
|
|
let s:builder_context.left_alt_sep = get(g:, 'airline#extensions#tabline#left_alt_sep' , '|')
|
|
|
|
endif
|
2013-09-04 19:20:06 +00:00
|
|
|
|
2013-09-04 19:53:04 +00:00
|
|
|
let s:buf_min_count = get(g:, 'airline#extensions#tabline#buffer_min_count', 0)
|
2013-09-15 12:47:41 +00:00
|
|
|
let s:tab_min_count = get(g:, 'airline#extensions#tabline#tab_min_count', 0)
|
2013-11-02 21:46:36 +00:00
|
|
|
let s:spc = g:airline_symbols.space
|
2013-09-04 19:53:04 +00:00
|
|
|
|
2013-08-28 13:51:54 +00:00
|
|
|
function! airline#extensions#tabline#init(ext)
|
2013-09-03 18:48:08 +00:00
|
|
|
if has('gui_running')
|
|
|
|
set guioptions-=e
|
|
|
|
endif
|
2013-09-04 19:53:04 +00:00
|
|
|
|
2013-10-13 22:01:38 +00:00
|
|
|
autocmd User AirlineToggledOn call s:toggle_on()
|
|
|
|
autocmd User AirlineToggledOff call s:toggle_off()
|
2014-02-26 01:34:52 +00:00
|
|
|
autocmd BufDelete * let s:current_bufnr = -1
|
2013-10-13 22:01:38 +00:00
|
|
|
|
|
|
|
call s:toggle_on()
|
|
|
|
call a:ext.add_theme_func('airline#extensions#tabline#load_theme')
|
|
|
|
endfunction
|
2013-09-01 23:39:06 +00:00
|
|
|
|
2013-10-13 22:01:38 +00:00
|
|
|
function! s:toggle_off()
|
|
|
|
if exists('s:original_tabline')
|
|
|
|
let &tabline = s:original_tabline
|
|
|
|
let &showtabline = s:original_showtabline
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:toggle_on()
|
|
|
|
let [ s:original_tabline, s:original_showtabline ] = [ &tabline, &showtabline ]
|
|
|
|
|
|
|
|
set tabline=%!airline#extensions#tabline#get()
|
2013-10-08 17:05:43 +00:00
|
|
|
if s:buf_min_count <= 0 && s:tab_min_count <= 1
|
2013-09-04 19:53:04 +00:00
|
|
|
set showtabline=2
|
|
|
|
else
|
2013-10-13 22:01:38 +00:00
|
|
|
augroup airline_tabline
|
|
|
|
autocmd!
|
|
|
|
if s:show_buffers == 1
|
|
|
|
autocmd CursorMoved * call <sid>on_cursormove(s:buf_min_count, len(s:get_buffer_list()))
|
|
|
|
else
|
|
|
|
autocmd TabEnter * call <sid>on_cursormove(s:tab_min_count, tabpagenr('$'))
|
|
|
|
endif
|
|
|
|
augroup END
|
2013-09-04 19:53:04 +00:00
|
|
|
endif
|
2013-09-01 23:39:06 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#extensions#tabline#load_theme(palette)
|
2013-09-04 17:16:23 +00: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-04 19:38:12 +00:00
|
|
|
let l:tabmodu = get(colors, 'airline_tabmod_unsel', a:palette.normal_modified.airline_c)
|
2013-09-05 22:34:15 +00:00
|
|
|
let l:tabhid = get(colors, 'airline_tabhid', a:palette.normal.airline_c)
|
2013-09-04 15:54:48 +00: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-04 19:38:12 +00:00
|
|
|
call airline#highlighter#exec('airline_tabmod_unsel', l:tabmodu)
|
2013-09-05 22:34:15 +00:00
|
|
|
call airline#highlighter#exec('airline_tabhid', l:tabhid)
|
2013-08-28 13:51:54 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-09-24 22:50:49 +00:00
|
|
|
function! s:on_cursormove(min_count, total_count)
|
2013-09-15 12:47:41 +00:00
|
|
|
if a:total_count >= a:min_count
|
2013-09-04 19:53:04 +00:00
|
|
|
if &showtabline != 2
|
|
|
|
set showtabline=2
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
if &showtabline != 0
|
|
|
|
set showtabline=0
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2013-08-28 13:51:54 +00:00
|
|
|
function! airline#extensions#tabline#get()
|
2014-08-13 00:26:52 +00:00
|
|
|
let curtabcnt = tabpagenr('$')
|
|
|
|
if curtabcnt != s:current_tabcnt
|
|
|
|
let s:current_tabcnt = curtabcnt
|
|
|
|
let s:current_bufnr = -1 " force a refresh...
|
|
|
|
endif
|
|
|
|
if s:show_buffers && curtabcnt == 1
|
2013-09-02 22:01:52 +00:00
|
|
|
return s:get_buffers()
|
2013-08-28 13:51:54 +00:00
|
|
|
else
|
2013-09-02 22:01:52 +00:00
|
|
|
return s:get_tabs()
|
2013-08-28 13:51:54 +00:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#extensions#tabline#title(n)
|
|
|
|
let buflist = tabpagebuflist(a:n)
|
|
|
|
let winnr = tabpagewinnr(a:n)
|
2013-09-02 03:03:36 +00:00
|
|
|
return airline#extensions#tabline#get_buffer_name(buflist[winnr - 1])
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#extensions#tabline#get_buffer_name(nr)
|
2013-12-23 05:41:24 +00:00
|
|
|
return airline#extensions#tabline#{s:formatter}#format(a:nr, get(s:, 'current_buffer_list', s:get_buffer_list()))
|
2013-08-28 13:51:54 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-09-04 19:53:04 +00:00
|
|
|
function! s:get_buffer_list()
|
|
|
|
let buffers = []
|
2013-09-02 22:01:52 +00: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-10 19:23:29 +00:00
|
|
|
if getbufvar(nr, 'current_syntax') == 'qf'
|
|
|
|
continue
|
|
|
|
endif
|
2013-09-04 19:53:04 +00:00
|
|
|
call add(buffers, nr)
|
|
|
|
endif
|
|
|
|
endfor
|
2013-09-05 17:07:31 +00:00
|
|
|
|
2013-09-21 18:13:19 +00:00
|
|
|
let s:current_buffer_list = buffers
|
|
|
|
return buffers
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:get_visible_buffers()
|
|
|
|
let buffers = s:get_buffer_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
|
2013-09-22 15:01:12 +00:00
|
|
|
let position = index(buffers, cur)
|
2013-09-25 19:26:25 +00:00
|
|
|
let vimwidth = &columns
|
|
|
|
if total_width > vimwidth && position > -1
|
2013-09-21 18:13:19 +00: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-25 19:26:25 +00:00
|
|
|
let buf_max = vimwidth / max_width
|
2013-09-21 18:13:19 +00: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-05 17:07:31 +00:00
|
|
|
endif
|
|
|
|
|
2013-09-04 19:53:04 +00:00
|
|
|
return buffers
|
|
|
|
endfunction
|
|
|
|
|
2013-12-02 03:00:31 +00:00
|
|
|
let s:current_bufnr = -1
|
2013-12-16 05:27:18 +00:00
|
|
|
let s:current_tabnr = -1
|
2014-08-13 00:26:52 +00:00
|
|
|
let s:current_tabcnt = -1
|
2013-12-02 03:00:31 +00:00
|
|
|
let s:current_tabline = ''
|
2014-01-01 01:38:16 +00:00
|
|
|
let s:current_modified = 0
|
2013-09-04 19:53:04 +00:00
|
|
|
function! s:get_buffers()
|
|
|
|
let cur = bufnr('%')
|
2013-12-02 03:00:31 +00:00
|
|
|
if cur == s:current_bufnr
|
2014-01-01 01:38:16 +00:00
|
|
|
if !g:airline_detect_modified || getbufvar(cur, '&modified') == s:current_modified
|
|
|
|
return s:current_tabline
|
|
|
|
endif
|
2013-12-02 03:00:31 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
let b = airline#builder#new(s:builder_context)
|
2013-09-05 22:34:15 +00:00
|
|
|
let tab_bufs = tabpagebuflist(tabpagenr())
|
2013-09-21 18:13:19 +00:00
|
|
|
for nr in s:get_visible_buffers()
|
2013-09-11 03:49:28 +00:00
|
|
|
if nr < 0
|
|
|
|
call b.add_raw('%#airline_tabhid#...')
|
|
|
|
continue
|
|
|
|
endif
|
2013-09-04 19:53:04 +00:00
|
|
|
if cur == nr
|
|
|
|
if g:airline_detect_modified && getbufvar(nr, '&modified')
|
|
|
|
let group = 'airline_tabmod'
|
2013-09-03 02:36:28 +00:00
|
|
|
else
|
2013-09-04 19:53:04 +00:00
|
|
|
let group = 'airline_tabsel'
|
2013-09-03 02:36:28 +00:00
|
|
|
endif
|
2014-01-01 01:38:16 +00:00
|
|
|
let s:current_modified = (group == 'airline_tabmod') ? 1 : 0
|
2013-09-04 19:53:04 +00:00
|
|
|
else
|
2014-09-04 19:38:12 +00:00
|
|
|
if g:airline_detect_modified && getbufvar(nr, '&modified')
|
|
|
|
let group = 'airline_tabmod_unsel'
|
|
|
|
elseif index(tab_bufs, nr) > -1
|
2013-09-05 22:34:15 +00:00
|
|
|
let group = 'airline_tab'
|
|
|
|
else
|
|
|
|
let group = 'airline_tabhid'
|
|
|
|
endif
|
2013-09-02 22:01:52 +00:00
|
|
|
endif
|
2013-11-02 21:46:36 +00:00
|
|
|
call b.add_section(group, s:spc.'%(%{airline#extensions#tabline#get_buffer_name('.nr.')}%)'.s:spc)
|
2013-09-02 22:01:52 +00:00
|
|
|
endfor
|
2013-09-04 19:53:04 +00:00
|
|
|
|
2013-09-03 02:36:28 +00:00
|
|
|
call b.add_section('airline_tabfill', '')
|
2013-09-02 22:01:52 +00:00
|
|
|
call b.split()
|
2013-09-03 02:36:28 +00:00
|
|
|
call b.add_section('airline_tabtype', ' buffers ')
|
2013-12-02 03:00:31 +00:00
|
|
|
|
|
|
|
let s:current_bufnr = cur
|
|
|
|
let s:current_tabline = b.build()
|
|
|
|
return s:current_tabline
|
2013-09-02 22:01:52 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:get_tabs()
|
2013-12-16 05:27:18 +00:00
|
|
|
let curbuf = bufnr('%')
|
|
|
|
let curtab = tabpagenr()
|
|
|
|
if curbuf == s:current_bufnr && curtab == s:current_tabnr
|
2014-01-01 01:38:16 +00:00
|
|
|
if !g:airline_detect_modified || getbufvar(curbuf, '&modified') == s:current_modified
|
|
|
|
return s:current_tabline
|
|
|
|
endif
|
2013-12-16 05:27:18 +00:00
|
|
|
endif
|
|
|
|
|
2013-09-04 19:20:06 +00:00
|
|
|
let b = airline#builder#new(s:builder_context)
|
2013-09-02 22:01:52 +00:00
|
|
|
for i in range(1, tabpagenr('$'))
|
2013-12-16 05:27:18 +00:00
|
|
|
if i == curtab
|
2013-09-05 05:52:29 +00:00
|
|
|
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
|
2014-01-01 01:38:16 +00:00
|
|
|
let s:current_modified = (group == 'airline_tabmod') ? 1 : 0
|
2013-09-05 05:52:29 +00:00
|
|
|
else
|
|
|
|
let group = 'airline_tab'
|
|
|
|
endif
|
2013-09-04 18:06:57 +00:00
|
|
|
let val = '%('
|
2013-12-22 03:32:06 +00:00
|
|
|
if s:show_tab_nr
|
|
|
|
if s:tab_nr_type == 0
|
|
|
|
let val .= ' %{len(tabpagebuflist('.i.'))}'
|
|
|
|
else
|
|
|
|
let val .= (g:airline_symbols.space).i
|
|
|
|
endif
|
2013-12-22 00:27:42 +00:00
|
|
|
endif
|
2013-09-04 18:06:57 +00:00
|
|
|
call b.add_section(group, val.'%'.i.'T %{airline#extensions#tabline#title('.i.')} %)')
|
2013-09-02 22:01:52 +00:00
|
|
|
endfor
|
2013-12-16 05:27:18 +00:00
|
|
|
|
2013-09-02 22:01:52 +00:00
|
|
|
call b.add_raw('%T')
|
2013-09-03 02:36:28 +00:00
|
|
|
call b.add_section('airline_tabfill', '')
|
2013-09-02 22:01:52 +00:00
|
|
|
call b.split()
|
2014-03-19 20:39:07 +00:00
|
|
|
call b.add_section('airline_tab', ' %999X'.s:close_symbol.' ')
|
|
|
|
if s:show_tab_type
|
|
|
|
call b.add_section('airline_tabtype', ' tabs ')
|
|
|
|
endif
|
2013-12-16 05:27:18 +00:00
|
|
|
|
|
|
|
let s:current_bufnr = curbuf
|
|
|
|
let s:current_tabnr = curtab
|
|
|
|
let s:current_tabline = b.build()
|
|
|
|
return s:current_tabline
|
2013-09-02 22:01:52 +00:00
|
|
|
endfunction
|