" Autoload funtions for custom statusbar. " Last Changed: 2021-09-29 " Author: Federico Igne <> " License: This file is placed in the public domain. " Highlight statusbar if current buffer contains unsaved changes. function! dyamon#statusline#setcolor() abort if getbufvar(bufnr('%'), '&modified') highlight User3 ctermbg=3 guibg=#d79921 else highlight User3 ctermbg=248 guibg=#bdae93 endif " Force statusline update setlocal statusline= " Overrides default return value (0) that would be displayed in the " statusbar return '' endfunction function! dyamon#statusline#modeflag() abort " The preview window is an exception among exceptions if &previewwindow && has_key(g:specialFlag, 'preview') return g:specialFlag.preview endif " Compute the correct flag let l:flag=get( g:nameFlag, \ expand('%'), \ get( g:filetypeFlag, \ &filetype, \ get(g:modeFlag, mode(), 'Err') \ ) \ ) return l:flag endfunction function! dyamon#statusline#fileprefix() abort let l:basename=expand('%:h') if l:basename ==# '' || l:basename ==# '.' return '' elseif winwidth(0) > g:fullstatusline return substitute(l:basename . '/', '\C^' . $HOME, '~', '') else return '' endif endfunction " Return session flag, if an active (Ob)session is detected. function! dyamon#statusline#session() abort let l:session_flag = ['s', 'S'] let l:numeric = !empty(v:this_session) + exists('g:this_obsession') if l:numeric return ',' . l:session_flag[l:numeric-1] else return '' endif endfunction " Return filetype (if any). function! dyamon#statusline#filetype() abort if strlen(&ft) && !has_key(g:filetypeFlag,&ft) return ',' . &ft else return '' endif endfunction " Return file encoding if different from default (utf8). function! dyamon#statusline#fileenc() abort if strlen(&fenc) && &fenc !=# 'utf-8' && winwidth(0) > g:fullstatusline return ',' . &fenc else return '' endif endfunction " Return git related info (if GitGutter is active provide additional info " about changed hunks in the current buffer) function! dyamon#statusline#gitinfo() abort let l:git = ' ' let l:head = '' if exists('*FugitiveHead') && len(FugitiveHead(8)) let l:head = FugitiveHead(8) let l:git .= ' ' . l:head . ' ' endif if len(l:head) && \ exists('g:gitgutter_enabled') && g:gitgutter_enabled && \ winwidth(0) ># g:fullstatusline let l:s = GitGutterGetHunkSummary() let l:git .= len(l:s) ? printf('(+%d ~%d -%d) ', l:s[0], l:s[1], l:s[2]) : '' endif return l:git endfun " If a language server is registered and enabled (via `vim-lsp` plugin) " add a custom indicator. function! dyamon#statusline#lsp() abort " Manually detect the server name based on the filetype let l:servers = { \ 'scala' : 'metals', \ 'sbt' : 'metals' \} " Indicators let l:indicators = { \ 'unknown server' : ' ', \ 'exited' : ' ', \ 'starting' : ' ', \ 'failed' : ' ', \ 'running' : ' ', \ 'not running' : ' ', \ 'hint' : ' ', \ 'information' : ' ', \ 'warning' : ' ', \ 'error' : ' ' \} let l:server = get(l:servers, &filetype, '') if len(l:server) let l:status = lsp#get_server_status(l:server) let l:prefix = '▕ ' if l:status ==# 'running' " Get language server counts let l:counts = lsp#get_buffer_diagnostics_counts() let l:hint = get(l:counts, 'hint', 0) let l:info = get(l:counts, 'information', 0) let l:warn = get(l:counts, 'warning', 0) let l:erro = get(l:counts, 'error', 0) " Build output let l:output = '' if l:hint > 0 let l:output .= get(l:indicators,'hint') . l:hint . ' ' endif if l:info > 0 let l:output .= get(l:indicators,'information') . l:info . ' ' endif if l:warn > 0 let l:output .= get(l:indicators,'warning') . l:warn . ' ' endif if l:erro > 0 let l:output .= get(l:indicators,'error') . l:erro . ' ' endif if len(l:output) return l:prefix . l:output else return l:prefix . get(l:indicators, l:status) . ' ' endif else return l:prefix . get(l:indicators, l:status) . ' ' endif else return '' endif endfun " Blur statusline function! dyamon#statusline#blur() abort if ! exists("b:disable_statusline") let l:blurred='\ ' " space let l:blurred.='%<' " truncation point let l:blurred.='%f' " filename let l:blurred.='%=' " split left/right halves (makes background cover whole) execute 'setlocal statusline=' . l:blurred endif endfunction " Focus statusline function! dyamon#statusline#focus() abort if ! exists("b:disable_statusline") " Reset to global option setlocal statusline= endif endfunction " Custom function for the TagBar plugin function! dyamon#statusline#tagbar(current, sort, fname, flags, ...) abort " Initialise custom statusline let l:statusline = '' if a:current " Tagbar label is always set to 'unmodified' color highlight User3 cterm=bold ctermfg=18 ctermbg=20 " 'TagBar' label let l:statusline .= '%3* TagBar ' " Filename let l:statusline .= '%* '.a:fname.' ' " Left/right separator let l:statusline .= '%=' " Flags (sort method included) let l:statusline .= '[' . ((a:sort ==# 'Name') ? 'N' : 'O') . ',' . join(a:flags, ',') . '] ' else " 'TagBar' label let l:statusline .= ' TagBar > ' " Filename let l:statusline .= a:fname endif return l:statusline endfunction function! dyamon#statusline#tabline() abort let line = '' for i in range(tabpagenr('$')) " Select the relevant highlight group if i + 1 == tabpagenr() let line .= '%#TabLineSel#' else let line .= '%#TabLine#' endif " Start tabpage label (with number indicator) let line .= '%' . (i + 1) . 'T ' . (i + 1) " Modified buffer indicator let l:modified = '' let l:buflist = tabpagebuflist(i + 1) for l:buf in l:buflist if getbufvar(l:buf, '&modified') let l:modified = '+' endif endfor let line .= l:modified " Get tabpage label let line .= ' %{dyamon#statusline#tablabel(' . (i + 1) . ')} ' " Get number of windows "let line .= '[%{tabpagewinnr(' . (i + 1) . ', "$")}]' endfor " Fill with TabLineFill and reset tabpage label let line .= '%#TabLineFill#%T' return line endfunction function! dyamon#statusline#tablabel(tab) abort let buflist = tabpagebuflist(a:tab) let winnr = tabpagewinnr(a:tab) let cwd = getcwd(-1, a:tab) if exists('*FugitiveGitDir') && len(FugitiveGitDir(buflist[winnr-1])) let gitroot = fnamemodify(FugitiveGitDir(buflist[winnr-1]), ':h:h') let cwd = substitute(cwd, gitroot . '/', '', '') else let cwd = fnamemodify(cwd, ':t') endif return cwd endfunction