|
| 1 | +function! s:create_tab(bufnr) |
| 2 | + let tab = #{ |
| 3 | + \ bufnr: -1, |
| 4 | + \ } |
| 5 | + let tab.bufnr = a:bufnr |
| 6 | + return tab |
| 7 | +endfunction |
| 8 | + |
| 9 | +function! s:clamp(value, min, max) |
| 10 | + if a:value < a:min |
| 11 | + return a:min |
| 12 | + elseif a:value > a:max |
| 13 | + return a:max |
| 14 | + else |
| 15 | + return a:value |
| 16 | + endif |
| 17 | +endfunction |
| 18 | + |
| 19 | +function! s:winbar_width(tabs) |
| 20 | + return strdisplaywidth(join(a:tabs, ' ')) + 4 " + left\right padding |
| 21 | +endfunction |
| 22 | + |
| 23 | +function! s:underscored(str) |
| 24 | + return a:str->str2list()->map({_, val -> list2str([val, 818])})->join('') |
| 25 | +endfunction |
| 26 | + |
| 27 | +" tab indexing is zero based |
| 28 | +function! s:init_once() |
| 29 | + if !exists('w:taberian') |
| 30 | + let w:taberian = #{ |
| 31 | + \ tabs: [], |
| 32 | + \ curr_nr: -1, |
| 33 | + \ prev_nr: -1, |
| 34 | + \ } |
| 35 | + endif |
| 36 | +endfunction |
| 37 | +call s:init_once() |
| 38 | + |
| 39 | +function! taberian#update_current_window() |
| 40 | + call s:init_once() |
| 41 | + if len(w:taberian.tabs) < 2 || w:taberian.curr_nr == -1 |
| 42 | + return |
| 43 | + endif |
| 44 | + let w:taberian.tabs[w:taberian.curr_nr].bufnr = bufnr('%') |
| 45 | + |
| 46 | + call taberian#render_current_window() |
| 47 | +endfunction |
| 48 | + |
| 49 | +function! taberian#create_tab() |
| 50 | + if len(w:taberian.tabs) < 2 |
| 51 | + let w:taberian.curr_nr = 0 |
| 52 | + let w:taberian.tabs = [s:create_tab(bufnr('%'))] |
| 53 | + endif |
| 54 | + |
| 55 | + let w:taberian.curr_nr += 1 |
| 56 | + call insert(w:taberian.tabs, s:create_tab(bufnr('%')), w:taberian.curr_nr) |
| 57 | + |
| 58 | + doautocmd User TaberianChanged |
| 59 | +endfunction |
| 60 | + |
| 61 | +function! taberian#goto_tab_nr(nr) |
| 62 | + if a:nr == -1 || a:nr >= len(w:taberian.tabs) || w:taberian.curr_nr == a:nr |
| 63 | + return |
| 64 | + endif |
| 65 | + |
| 66 | + let w:taberian.prev_nr = w:taberian.curr_nr |
| 67 | + let w:taberian.curr_nr = a:nr |
| 68 | + execute 'silent buffer ' . w:taberian.tabs[w:taberian.curr_nr].bufnr |
| 69 | + |
| 70 | + doautocmd User TaberianChanged |
| 71 | +endfunction |
| 72 | + |
| 73 | +function! taberian#goto_tab_offset(offset) |
| 74 | + let nr = s:clamp(w:taberian.curr_nr + a:offset, 0, len(w:taberian.tabs) - 1) |
| 75 | + call taberian#goto_tab_nr(nr) |
| 76 | +endfunction |
| 77 | + |
| 78 | +function! taberian#goto_previous_tab() |
| 79 | + call taberian#goto_tab_nr(w:taberian.prev_nr) |
| 80 | +endfunction |
| 81 | + |
| 82 | +function! taberian#move_current_tab_offset(offset) |
| 83 | + let nr = w:taberian.curr_nr + a:offset |
| 84 | + if nr < 0 || nr >= len(w:taberian.tabs) |
| 85 | + return |
| 86 | + endif |
| 87 | + let tab = w:taberian.tabs[w:taberian.curr_nr]->deepcopy() |
| 88 | + call remove(w:taberian.tabs, w:taberian.curr_nr) |
| 89 | + call insert(w:taberian.tabs, tab, nr) |
| 90 | + if w:taberian.prev_nr == nr |
| 91 | + let w:taberian.prev_nr = w:taberian.curr_nr |
| 92 | + endif |
| 93 | + let w:taberian.curr_nr = nr |
| 94 | + doautocmd User TaberianChanged |
| 95 | +endfunction |
| 96 | + |
| 97 | +function! taberian#close_current_tab() |
| 98 | + if len(w:taberian.tabs) < 2 || w:taberian.curr_nr == -1 |
| 99 | + return |
| 100 | + endif |
| 101 | + |
| 102 | + let old_curr = w:taberian.curr_nr |
| 103 | + |
| 104 | + if w:taberian.curr_nr > 0 |
| 105 | + call taberian#goto_tab_offset(-1) |
| 106 | + else |
| 107 | + call taberian#goto_tab_offset(+1) |
| 108 | + endif |
| 109 | + |
| 110 | + call remove(w:taberian.tabs, old_curr) |
| 111 | + if len(w:taberian.tabs) < 2 |
| 112 | + let w:taberian.tabs = [] |
| 113 | + let w:taberian.curr_nr = -1 |
| 114 | + endif |
| 115 | + doautocmd User TaberianChanged |
| 116 | +endfunction |
| 117 | + |
| 118 | +function! taberian#render_current_window() |
| 119 | + function! s:tab_prototyperim(str, max, remainder) |
| 120 | + let max = a:max |
| 121 | + let elipsis = '' |
| 122 | + let len = strdisplaywidth(a:str) |
| 123 | + if len > max |
| 124 | + if a:remainder.value > 0 |
| 125 | + let max += 1 |
| 126 | + let a:remainder.value -= 1 |
| 127 | + endif |
| 128 | + elseif len < max |
| 129 | + let a:remainder.value += max - len |
| 130 | + endif |
| 131 | + if len > max |
| 132 | + let elipsis = '…' |
| 133 | + let max -= 1 |
| 134 | + endif |
| 135 | + return strcharpart(a:str, 0, max) . elipsis |
| 136 | + endfunction |
| 137 | + |
| 138 | + call s:init_once() |
| 139 | + aunmenu WinBar |
| 140 | + let tabs = gettabwinvar(tabpagenr(), winnr(), 'taberian').tabs->deepcopy() |
| 141 | + let ts_count = len(tabs) |
| 142 | + if ts_count < 2 " render only if more than 1 tab |
| 143 | + return |
| 144 | + endif |
| 145 | + |
| 146 | + " convert bufnr to tab name: |
| 147 | + call map(tabs, {key, tab -> printf('%d %s ᴮ%d', key + 1, fnamemodify(bufname(tab.bufnr), ':t'), tab.bufnr)}) |
| 148 | + |
| 149 | + " make sure there is enough room: |
| 150 | + let win_width = winwidth(0) |
| 151 | + if win_width < 2 " there is a maximized window |
| 152 | + " create empty WinBar: |
| 153 | + execute 'amenu WinBar.\ \ ' |
| 154 | + return |
| 155 | + endif |
| 156 | + let min_win_width = tabs->len() * (7 + 2 + 4) + 4 " 7: tab nr (2) + space + tab name (3 chars + '…') |
| 157 | + if win_width < min_win_width |
| 158 | + execute 'vertical resize ' . min_win_width |
| 159 | + endif |
| 160 | + |
| 161 | + if s:winbar_width(tabs) > win_width |
| 162 | + " drop bufnr |
| 163 | + call map(tabs, {_, tab_name -> substitute(tab_name, '\(.*\) ᴮ.*', '\1', '')}) |
| 164 | + endif |
| 165 | + |
| 166 | + let max_bonus = 10 |
| 167 | + while s:winbar_width(tabs) > win_width |
| 168 | + " trim the end |
| 169 | + let max_len = win_width / ts_count |
| 170 | + let remainder = #{value: win_width - max_len * ts_count} |
| 171 | + call map(tabs, {_, tab_name -> s:tab_prototyperim(tab_name, max_len - 4 + max_bonus, remainder)}) " - padding |
| 172 | + let max_bonus -= 1 |
| 173 | + endwhile |
| 174 | + |
| 175 | + " mark current tab: |
| 176 | + let tabs[w:taberian.curr_nr] = s:underscored(tabs[w:taberian.curr_nr]) |
| 177 | + " escape whitespace and dots (including Unicode underscored): |
| 178 | + call map(tabs, {_, tab_name -> substitute(tab_name, '[ ̲.̲]', '\\&', 'g')}) |
| 179 | + |
| 180 | + let nr = 0 |
| 181 | + for tab in tabs |
| 182 | + execute 'amenu <silent> WinBar.' . tab . ' <Cmd>call taberian#goto_tab_nr(' . nr . ')<CR>' |
| 183 | + let nr += 1 |
| 184 | + endfor |
| 185 | +endfunction |
| 186 | + |
| 187 | +function! taberian#render_all_windows() |
| 188 | + let winids = gettabinfo(tabpagenr())[0].windows |
| 189 | + for winid in winids |
| 190 | + call win_execute(winid, 'call taberian#update_current_window()') |
| 191 | + endfor |
| 192 | +endfunction |
0 commit comments