2018-03-15 09:23:43 +08:00
|
|
|
" MIT License. Copyright (c) 2013-2018 Bailey Ling et al.
|
|
|
|
" vim: et ts=2 sts=2 sw=2
|
|
|
|
|
|
|
|
scriptencoding utf-8
|
|
|
|
|
|
|
|
let s:prototype = {}
|
|
|
|
|
2018-03-16 01:10:49 +08:00
|
|
|
function! s:prototype.insert_tabs(curtab) dict
|
2018-03-16 01:46:33 +08:00
|
|
|
let self._left_tab = a:curtab
|
|
|
|
let self._right_tab = a:curtab + 1
|
|
|
|
let self._left_position = self.get_position()
|
|
|
|
let self._right_position = self._left_position
|
2018-03-16 01:10:49 +08:00
|
|
|
endfunction
|
2018-03-16 01:05:58 +08:00
|
|
|
|
2018-03-16 01:10:49 +08:00
|
|
|
function! s:prototype.build() dict
|
2018-03-16 01:46:33 +08:00
|
|
|
if has_key(self, '_left_position')
|
2018-03-16 01:10:49 +08:00
|
|
|
let tab_nr_type = get(g:, 'airline#extensions#tabline#tab_nr_type', 0)
|
|
|
|
let num_tabs = tabpagenr('$')
|
|
|
|
let remaining_space = &columns - s:strchars(s:evaluate_tabline(self._build()))
|
2018-03-16 01:05:58 +08:00
|
|
|
|
2018-03-16 01:10:49 +08:00
|
|
|
let left_sep_size = s:strchars(s:evaluate_tabline(self._context.left_sep))
|
|
|
|
let left_alt_sep_size = s:strchars(s:evaluate_tabline(self._context.left_alt_sep))
|
|
|
|
|
|
|
|
let skipped_tabs_marker = get(g:, 'airline#extensions#tabline#overflow_marker', g:airline_symbols.ellipsis)
|
|
|
|
let skipped_tabs_marker_size = s:strchars(s:evaluate_tabline(skipped_tabs_marker))
|
|
|
|
" The left marker will have left_alt_sep, and the right will have left_sep.
|
|
|
|
let remaining_space -= 2 * skipped_tabs_marker_size + left_sep_size + left_alt_sep_size
|
|
|
|
|
|
|
|
" Add the current tab
|
2018-03-16 01:46:33 +08:00
|
|
|
let tab_title = self.get_title(tab_nr_type, self._left_tab)
|
2018-03-16 01:10:49 +08:00
|
|
|
let remaining_space -= s:strchars(s:evaluate_tabline(tab_title))
|
|
|
|
" There are always two left_seps (either side of the selected tab) and all
|
|
|
|
" other seperators are left_alt_seps.
|
|
|
|
let remaining_space -= 2 * left_sep_size - left_alt_sep_size
|
2018-03-16 01:46:33 +08:00
|
|
|
call self.insert_section(self.get_group(self._left_tab), tab_title, self._left_position)
|
|
|
|
let self._right_position += 1
|
|
|
|
let self._left_tab -= 1
|
2018-03-16 01:10:49 +08:00
|
|
|
|
|
|
|
if get(g:, 'airline#extensions#tabline#current_first', 0)
|
|
|
|
" always have current tabpage first
|
2018-03-16 01:41:08 +08:00
|
|
|
let self._left_position += 1
|
2018-03-16 01:10:49 +08:00
|
|
|
endif
|
|
|
|
|
|
|
|
" Add the tab to the right
|
2018-03-16 01:41:08 +08:00
|
|
|
if self._right_tab <= num_tabs
|
|
|
|
let tab_title = self.get_title(tab_nr_type, self._right_tab)
|
2018-03-16 01:05:58 +08:00
|
|
|
let remaining_space -= s:strchars(s:evaluate_tabline(tab_title)) + left_alt_sep_size
|
2018-03-16 01:41:08 +08:00
|
|
|
call self.insert_section(self.get_group(self._right_tab), tab_title, self._right_position)
|
|
|
|
let self._right_position += 1
|
|
|
|
let self._right_tab += 1
|
2018-03-16 01:10:49 +08:00
|
|
|
endif
|
|
|
|
|
|
|
|
while remaining_space > 0
|
2018-03-16 01:41:08 +08:00
|
|
|
if self._left_tab > 0
|
|
|
|
let tab_title = self.get_title(tab_nr_type, self._left_tab)
|
2018-03-16 01:10:49 +08:00
|
|
|
let remaining_space -= s:strchars(s:evaluate_tabline(tab_title)) + left_alt_sep_size
|
|
|
|
if remaining_space >= 0
|
2018-03-16 01:41:08 +08:00
|
|
|
call self.insert_section(self.get_group(self._left_tab), tab_title, self._left_position)
|
|
|
|
let self._right_position += 1
|
|
|
|
let self._left_tab -= 1
|
2018-03-16 01:10:49 +08:00
|
|
|
endif
|
2018-03-16 01:41:08 +08:00
|
|
|
elseif self._right_tab <= num_tabs
|
|
|
|
let tab_title = self.get_title(tab_nr_type, self._right_tab)
|
2018-03-16 01:10:49 +08:00
|
|
|
let remaining_space -= s:strchars(s:evaluate_tabline(tab_title)) + left_alt_sep_size
|
|
|
|
if remaining_space >= 0
|
2018-03-16 01:41:08 +08:00
|
|
|
call self.insert_section(self.get_group(self._right_tab), tab_title, self._right_position)
|
|
|
|
let self._right_position += 1
|
|
|
|
let self._right_tab += 1
|
2018-03-16 01:10:49 +08:00
|
|
|
endif
|
|
|
|
else
|
|
|
|
break
|
2018-03-16 01:05:58 +08:00
|
|
|
endif
|
2018-03-16 01:10:49 +08:00
|
|
|
endwhile
|
|
|
|
|
2018-03-16 01:41:08 +08:00
|
|
|
if self._left_tab > 0
|
2018-03-16 01:10:49 +08:00
|
|
|
if get(g:, 'airline#extensions#tabline#current_first', 0)
|
2018-03-16 01:41:08 +08:00
|
|
|
let self._left_position -= 1
|
2018-03-16 01:10:49 +08:00
|
|
|
endif
|
2018-03-16 01:41:08 +08:00
|
|
|
call self.insert_raw('%#airline_tab#'.skipped_tabs_marker, self._left_position)
|
|
|
|
let self._right_position += 1
|
2018-03-16 01:05:58 +08:00
|
|
|
endif
|
|
|
|
|
2018-03-16 01:41:08 +08:00
|
|
|
if self._right_tab <= num_tabs
|
|
|
|
call self.insert_raw('%#airline_tab#'.skipped_tabs_marker, self._right_position)
|
2018-03-16 01:05:58 +08:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2018-03-16 01:10:49 +08:00
|
|
|
return self._build()
|
2018-03-16 01:05:58 +08:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:evaluate_tabline(tabline)
|
|
|
|
let tabline = a:tabline
|
|
|
|
let tabline = substitute(tabline, '%{\([^}]\+\)}', '\=eval(submatch(1))', 'g')
|
|
|
|
let tabline = substitute(tabline, '%#[^#]\+#', '', 'g')
|
|
|
|
let tabline = substitute(tabline, '%(\([^)]\+\)%)', '\1', 'g')
|
|
|
|
let tabline = substitute(tabline, '%\d\+[TX]', '', 'g')
|
|
|
|
let tabline = substitute(tabline, '%=', '', 'g')
|
|
|
|
let tabline = substitute(tabline, '%\d*\*', '', 'g')
|
|
|
|
return tabline
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Compatibility wrapper for strchars, in case this vim version does not
|
|
|
|
" have it natively
|
|
|
|
function! s:strchars(str)
|
|
|
|
if exists('*strchars')
|
|
|
|
return strchars(a:str)
|
|
|
|
else
|
|
|
|
return strlen(substitute(a:str, '.', 'a', 'g'))
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2018-03-15 09:23:43 +08:00
|
|
|
function! airline#extensions#tabline#builder#new(context)
|
|
|
|
let builder = airline#builder#new(a:context)
|
2018-03-16 01:10:49 +08:00
|
|
|
let builder._build = builder.build
|
2018-03-15 09:23:43 +08:00
|
|
|
call extend(builder, s:prototype, 'force')
|
|
|
|
return builder
|
|
|
|
endfunction
|