2018-03-15 01:23:43 +00: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-15 19:53:08 +00:00
|
|
|
function! s:prototype.insert_tabs(curtab, first_tab, last_tab) dict
|
|
|
|
let self._first_tab = a:first_tab
|
|
|
|
let self._last_tab = a:last_tab
|
2018-03-15 17:46:33 +00: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-15 17:10:49 +00:00
|
|
|
endfunction
|
2018-03-15 17:05:58 +00:00
|
|
|
|
2018-03-18 00:15:29 +00:00
|
|
|
function! s:prototype.try_insert_tab(tab, group, pos, sep_size, force) dict
|
2018-03-15 18:14:50 +00:00
|
|
|
let tab_title = self.get_title(a:tab)
|
2018-03-15 18:09:18 +00:00
|
|
|
let self._remaining_space -= s:strchars(s:evaluate_tabline(tab_title)) + a:sep_size
|
|
|
|
if a:force || self._remaining_space >= 0
|
2018-03-15 19:41:58 +00:00
|
|
|
let pos = a:pos
|
|
|
|
if has_key(self, "get_pretitle")
|
|
|
|
call self.insert_raw(self.get_pretitle(a:tab), pos)
|
|
|
|
let self._right_position += 1
|
|
|
|
let pos += 1
|
|
|
|
endif
|
|
|
|
|
2018-03-18 00:15:29 +00:00
|
|
|
call self.insert_section(a:group, tab_title, pos)
|
2018-03-15 18:09:18 +00:00
|
|
|
let self._right_position += 1
|
2018-03-15 19:41:58 +00:00
|
|
|
let pos += 1
|
|
|
|
|
|
|
|
if has_key(self, "get_posttitle")
|
|
|
|
call self.insert_raw(self.get_posttitle(a:tab), pos)
|
|
|
|
let self._right_position += 1
|
|
|
|
let pos += 1
|
|
|
|
endif
|
|
|
|
|
2018-03-15 18:09:18 +00:00
|
|
|
return 1
|
2018-03-17 19:59:47 +00:00
|
|
|
else
|
|
|
|
let self._remaining_space += s:strchars(s:evaluate_tabline(tab_title)) + a:sep_size
|
2018-03-15 18:09:18 +00:00
|
|
|
endif
|
|
|
|
return 0
|
|
|
|
endfunction
|
|
|
|
|
2018-03-18 00:15:29 +00:00
|
|
|
function! s:get_separator_change(new_group, old_group, end_group, sep_size, alt_sep_size)
|
|
|
|
let sep_change = 0
|
|
|
|
if !empty(a:end_group)
|
|
|
|
let sep_change += airline#builder#should_change_group(a:new_group, a:end_group) ? a:sep_size : a:alt_sep_size
|
|
|
|
endif
|
|
|
|
if !empty(a:old_group)
|
|
|
|
let sep_change += airline#builder#should_change_group(a:new_group, a:old_group) ? a:sep_size : a:alt_sep_size
|
|
|
|
if !empty(a:end_group)
|
|
|
|
let sep_change -= airline#builder#should_change_group(a:old_group, a:end_group) ? a:sep_size : a:alt_sep_size
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
return sep_change
|
|
|
|
endfunction
|
|
|
|
|
2018-03-15 17:10:49 +00:00
|
|
|
function! s:prototype.build() dict
|
2018-03-15 17:46:33 +00:00
|
|
|
if has_key(self, '_left_position')
|
2018-03-15 18:01:39 +00:00
|
|
|
let self._remaining_space = &columns - s:strchars(s:evaluate_tabline(self._build()))
|
2018-03-15 17:05:58 +00:00
|
|
|
|
2018-03-15 20:07:44 +00:00
|
|
|
let center_active = get(g:, 'airline#extensions#tabline#center_active', 0)
|
|
|
|
|
2018-03-15 17:10:49 +00: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))
|
2018-03-18 00:15:29 +00:00
|
|
|
" Allow space for the markers before we begin filling in titles.
|
|
|
|
let self._remaining_space -= 2 * skipped_tabs_marker_size
|
|
|
|
|
|
|
|
let outer_left_group = airline#builder#get_prev_group(self._sections, self._left_position)
|
|
|
|
let outer_right_group = airline#builder#get_next_group(self._sections, self._right_position)
|
2018-03-15 18:09:18 +00:00
|
|
|
|
|
|
|
" Add the current tab
|
2018-03-18 00:15:29 +00:00
|
|
|
let group = self.get_group(self._left_tab)
|
|
|
|
let sep_change =
|
|
|
|
\ s:get_separator_change(group, "", outer_left_group, left_sep_size, left_alt_sep_size) +
|
|
|
|
\ s:get_separator_change(group, "", outer_right_group, left_sep_size, left_alt_sep_size)
|
|
|
|
let last_left_group = group
|
|
|
|
let last_right_group = group
|
2018-03-15 18:09:18 +00:00
|
|
|
let self._left_tab -=
|
2018-03-18 00:15:29 +00:00
|
|
|
\ self.try_insert_tab(self._left_tab, group, self._left_position, sep_change, 1)
|
2018-03-15 17:10:49 +00:00
|
|
|
|
|
|
|
if get(g:, 'airline#extensions#tabline#current_first', 0)
|
|
|
|
" always have current tabpage first
|
2018-03-15 17:41:08 +00:00
|
|
|
let self._left_position += 1
|
2018-03-15 17:10:49 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
" Add the tab to the right
|
2018-03-15 20:07:44 +00:00
|
|
|
if !center_active && self._right_tab <= self._last_tab
|
2018-03-18 00:15:29 +00:00
|
|
|
let group = self.get_group(self._right_tab)
|
|
|
|
let sep_change =
|
|
|
|
\ s:get_separator_change(group, last_right_group, outer_right_group, left_sep_size, left_alt_sep_size)
|
|
|
|
let last_right_group = group
|
2018-03-15 18:09:18 +00:00
|
|
|
let self._right_tab +=
|
2018-03-18 00:15:29 +00:00
|
|
|
\ self.try_insert_tab(self._right_tab, group, self._right_position, sep_change, 1)
|
2018-03-15 17:10:49 +00:00
|
|
|
endif
|
|
|
|
|
2018-03-15 18:01:39 +00:00
|
|
|
while self._remaining_space > 0
|
2018-03-15 20:07:44 +00:00
|
|
|
let done = 0
|
2018-03-15 19:53:08 +00:00
|
|
|
if self._left_tab >= self._first_tab
|
2018-03-18 00:15:29 +00:00
|
|
|
let group = self.get_group(self._left_tab)
|
|
|
|
let sep_change =
|
|
|
|
\ s:get_separator_change(group, last_left_group, outer_left_group, left_sep_size, left_alt_sep_size)
|
|
|
|
let last_left_group = group
|
|
|
|
let done = self.try_insert_tab(self._left_tab, group, self._left_position, sep_change, 0)
|
2018-03-17 19:59:47 +00:00
|
|
|
let self._left_tab -= done
|
2018-03-15 20:07:44 +00:00
|
|
|
endif
|
|
|
|
if self._right_tab <= self._last_tab && (center_active || !done)
|
2018-03-18 00:15:29 +00:00
|
|
|
let group = self.get_group(self._right_tab)
|
|
|
|
let sep_change =
|
|
|
|
\ s:get_separator_change(group, last_right_group, outer_right_group, left_sep_size, left_alt_sep_size)
|
|
|
|
let last_right_group = group
|
|
|
|
let done = self.try_insert_tab(self._right_tab, group, self._right_position, sep_change, 0)
|
2018-03-17 19:59:47 +00:00
|
|
|
let self._right_tab += done
|
2018-03-15 20:07:44 +00:00
|
|
|
endif
|
|
|
|
if !done
|
2018-03-15 17:10:49 +00:00
|
|
|
break
|
2018-03-15 17:05:58 +00:00
|
|
|
endif
|
2018-03-15 17:10:49 +00:00
|
|
|
endwhile
|
|
|
|
|
2018-03-15 19:53:08 +00:00
|
|
|
if self._left_tab >= self._first_tab
|
2018-03-15 17:10:49 +00:00
|
|
|
if get(g:, 'airline#extensions#tabline#current_first', 0)
|
2018-03-15 17:41:08 +00:00
|
|
|
let self._left_position -= 1
|
2018-03-15 17:10:49 +00:00
|
|
|
endif
|
2018-03-15 19:36:30 +00:00
|
|
|
call self.insert_raw('%#'.self.overflow_group.'#'.skipped_tabs_marker, self._left_position)
|
2018-03-15 17:41:08 +00:00
|
|
|
let self._right_position += 1
|
2018-03-15 17:05:58 +00:00
|
|
|
endif
|
|
|
|
|
2018-03-15 19:53:08 +00:00
|
|
|
if self._right_tab <= self._last_tab
|
2018-03-15 19:36:30 +00:00
|
|
|
call self.insert_raw('%#'.self.overflow_group.'#'.skipped_tabs_marker, self._right_position)
|
2018-03-15 17:05:58 +00:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2018-03-15 17:10:49 +00:00
|
|
|
return self._build()
|
2018-03-15 17:05:58 +00:00
|
|
|
endfunction
|
|
|
|
|
2018-03-15 19:36:30 +00:00
|
|
|
let s:prototype.overflow_group = 'airline_tab'
|
|
|
|
|
2018-03-15 17:05:58 +00:00
|
|
|
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')
|
2018-03-15 19:21:15 +00:00
|
|
|
if has('tablineat')
|
|
|
|
let tabline = substitute(tabline, '%@[^@]\+@', '', 'g')
|
|
|
|
endif
|
2018-03-15 17:05:58 +00:00
|
|
|
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 01:23:43 +00:00
|
|
|
function! airline#extensions#tabline#builder#new(context)
|
|
|
|
let builder = airline#builder#new(a:context)
|
2018-03-15 17:10:49 +00:00
|
|
|
let builder._build = builder.build
|
2018-03-15 01:23:43 +00:00
|
|
|
call extend(builder, s:prototype, 'force')
|
|
|
|
return builder
|
|
|
|
endfunction
|