Created
October 19, 2020 10:59
-
-
Save Milly/01fc8b30d1d0ac04560f0819c29163a5 to your computer and use it in GitHub Desktop.
My autoload/my/lightline.vim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" autoload: my/lightline | |
function! my#lightline#setup(...) abort | |
let g:enable_powerline_font = get(g:, 'enable_powerline_font', | |
\ exists('$ENABLE_POWERLINE_FONT') && $ENABLE_POWERLINE_FONT !=# '') | |
if g:enable_powerline_font && exists('&ambiwidth') | |
set ambiwidth=double | |
endif | |
let c = g:enable_powerline_font ? | |
\ { 'Branch': "\ue0a0 ", 'LineNo': "\ue0a1", 'ReadOnly': "\ue0a2", | |
\ 'LeftSep': "\ue0b0", 'RightSep': "\ue0b2", | |
\ 'LeftSubSep': "\ue0b1", 'RightSubSep': "\ue0b3" } : | |
\ { 'Branch': 'B:', 'LineNo': '', 'ReadOnly': '[RO]', | |
\ 'LeftSep': '', 'RightSep': '', | |
\ 'LeftSubSep': '', 'RightSubSep': '' } | |
let s:lightline_chars = c | |
let g:lightline = { | |
\ 'colorscheme': 'jellybeans', | |
\ 'active': { | |
\ 'left': [ [ 'mode', 'paste' ], | |
\ [ 'truncate', 'vcs', 'relativepath' ] ], | |
\ 'right': [ [ 'bufnum', 'winnum' ], | |
\ [ 'errorcount', 'lineinfo' ], | |
\ [ 'vimsyntax', 'charcode', 'fileinfo' ] ], | |
\ }, | |
\ 'inactive': { | |
\ 'left': [ [ 'filename' ] ], | |
\ 'right': [ [ 'bufnum', 'winnum' ], | |
\ [ 'filetype' ] ], | |
\ }, | |
\ 'component': { | |
\ 'relativepath': | |
\ '%q%{my#lightline#buftype()}'. | |
\ '%#LightLineLeft_normal_1_Readonly#%(%{my#lightline#readonly()} %)'. | |
\ '%#LightLineLeft_normal_1_Filepath#%{my#lightline#filepath(0)}'. | |
\ '%#LightLineLeft_normal_1_Filename#%{my#lightline#filename()}'. | |
\ '%#LightLineLeft_normal_1_Modified#%( %{my#lightline#modified(1)}%)'. | |
\ '%#LightLineLeft_normal_1_NoModified#%( %{my#lightline#modified(0)}%)'. | |
\ '%#LightLineLeft_normal_1#', | |
\ 'filename': | |
\ '%q%{my#lightline#buftype()}'. | |
\ '%#LightLineLeft_inactive_0_Readonly#%(%{my#lightline#readonly()} %)'. | |
\ '%#LightLineLeft_inactive_0_Filepath#%{my#lightline#filepath(1)}'. | |
\ '%#LightLineLeft_inactive_0_Filename#%{my#lightline#filename()}'. | |
\ '%#LightLineLeft_inactive_0_Modified#%( %{my#lightline#modified(1)}%)'. | |
\ '%#LightLineLeft_inactive_0_NoModified#%( %{my#lightline#modified(0)}%)'. | |
\ '%#LightLineLeft_inactive_0#', | |
\ 'modified': | |
\ '%#LightLineLeft_normal_1_Modified#%(%{my#lightline#modified(1)}%)'. | |
\ '%#LightLineLeft_normal_1_NoModified#%(%{my#lightline#modified(0)}%)'. | |
\ '%#LightLineLeft_normal_1#', | |
\ 'readonly': '%#LightLineLeft_normal_1_Readonly#%{my#lightline#readonly()}%#LightLineLeft_normal_1#', | |
\ 'fileinfo': | |
\ '%#LightLineMiddle_normal_0_Filetype#%{my#lightline#filetype()} '. | |
\ '%#LightLineMiddle_normal_0_Fileinfo#%{my#lightline#fileinfo()}'. | |
\ '%#LightLineRight_normal_2#', | |
\ 'filetype': | |
\ '%#LightLineMiddle_normal_0_Filetype#%{my#lightline#filetype()} '. | |
\ '%#LightLineRight_normal_2#', | |
\ 'bufnum' : '#%n', | |
\ 'winnum' : '%{winnr()}', | |
\ 'vcs': | |
\ '%{my#lightline#vcs_branch()!=""?'.string(c.Branch).':""}'. | |
\ '%#LightLineLeft_normal_1_VCSBranch#%{my#lightline#vcs_branch("")}'. | |
\ '%#LightLineLeft_normal_1_VCSModified#%{my#lightline#vcs_branch("m")}'. | |
\ '%#LightLineLeft_normal_1_VCSUntracked#%{my#lightline#vcs_branch("u")}'. | |
\ '%#LightLineLeft_normal_1#', | |
\ 'vimsyntax': '%{(1==$SYN_DEBUG)?"SYN[".my#vimsyntax#get_syntax_name()."]":""}', | |
\ 'errorcount': | |
\ '%{my#lightline#loccount()||my#lightline#qfcount()?" ":""}'. | |
\ '%#LightLineRight_normal_1_Warning#%{my#lightline#loccount()}'. | |
\ '%{my#lightline#loccount()&&my#lightline#qfcount()?",":""}'. | |
\ '%#LightLineRight_normal_1_Error#%{my#lightline#qfcount()}'. | |
\ '%{my#lightline#loccount()||my#lightline#qfcount()?" ":""}'. | |
\ '%#LightLineRight_normal_1#', | |
\ 'truncate': '%<', | |
\ }, | |
\ 'component_visible_condition': { | |
\ 'vcs' : '""!=my#lightline#vcs_branch()', | |
\ 'vimsyntax': '1==$SYN_DEBUG', | |
\ 'errorcount': 'my#lightline#loccount() || my#lightline#qfcount()', | |
\ 'truncate': '0', | |
\ }, | |
\ 'component_function': { | |
\ 'mode': 'my#lightline#mode', | |
\ 'charcode': 'my#lightline#charcode' , | |
\ }, | |
\ 'component_expand': { | |
\ 'lineinfo': 'my#lightline#lineinfo_expand', | |
\ }, | |
\ 'component_type': { | |
\ }, | |
\ 'component_raw': { | |
\ 'errorcount': 1, | |
\ 'truncate': 1, | |
\ }, | |
\ 'separator': { 'left': c.LeftSep, 'right': c.RightSep }, | |
\ 'subseparator': { 'left': c.LeftSubSep, 'right': c.RightSubSep }, | |
\ } | |
augroup LightLineColorscheme | |
autocmd! | |
autocmd VimEnter,ColorScheme * call my#lightline#init() | |
autocmd BufWritePost,BufWinEnter,WinEnter * silent! unlet b:vcs_state | |
autocmd FileType vimshell call my#lightline#hook_vimshell() | |
augroup END | |
set noshowmode | |
let g:vimshell_force_overwrite_statusline = 0 | |
endfunction | |
function! my#lightline#hook_vimshell() abort | |
call vimshell#hook#add('chpwd', 'my_lightline_chpwd', 'my#lightline#chpwd') | |
call vimshell#hook#add('postexec', 'my_lightline_postexec', 'my#lightline#postexec') | |
endfunction | |
function! my#lightline#chpwd(args, context) abort | |
if exists('b:git_dir') | |
unlet b:git_dir | |
endif | |
call fugitive#detect(a:args) | |
endfunction | |
function! my#lightline#postexec(args, context) abort | |
if exists('b:vcs_state') | |
unlet b:vcs_state | |
endif | |
endfunction | |
function! my#lightline#mode() abort | |
return | |
\ &filetype ==# 'denite' ? trim(denite#get_status('mode'), '- ') : | |
\ &filetype ==# 'fern' ? '' : | |
\ lightline#mode() | |
endfunction | |
function! my#lightline#readonly() abort | |
return index(['vimshell', 'denite'], &filetype) >= 0 ? '' : | |
\ &readonly && &modifiable ? s:lightline_chars.ReadOnly : | |
\ '' | |
endfunction | |
function! my#lightline#modified(modified) abort | |
return index(['vimshell', 'denite'], &filetype) >= 0 ? '' : | |
\ &modified ? (a:modified ? '+' : '') : | |
\ &modifiable && !a:modified ? '-' : '' | |
endfunction | |
function! my#lightline#pathmodify(path) abort | |
let mods = exists('+shellslash') ? ':p:gs?\\?/?' : ':p' | |
let path = fnamemodify(a:path, mods) | |
let cdir = fnamemodify(getcwd(), mods) | |
if stridx(path, cdir) == 0 | |
return path[len(cdir):] | |
endif | |
let home = fnamemodify(expand('~'), mods) | |
if stridx(path, home) == 0 | |
return '~/' . path[len(home):] | |
endif | |
return path | |
endfunction | |
function! my#lightline#buftype() abort | |
let file = expand('%') | |
return | |
\ &buftype ==# 'terminal' ? '[TERM] ' : | |
\ &buftype ==# 'help' ? '[HELP] ' : | |
\ &previewwindow ==# 1 ? '[PREV] ' : | |
\ &filetype ==# 'vimshell' ? '[vimshell] ' : | |
\ &filetype ==# 'denite' ? | |
\ printf('[denite:%s]', denite#get_status('buffer_name')) : | |
\ file[0:10] ==# 'fugitive://' ? '[GIT] ' : | |
\ file ==# '' ? '[No Name]' : '' | |
endfunction | |
function! my#lightline#filepath(inactive) abort | |
if &buftype ==# 'terminal' | |
return term_gettitle(bufnr('%')) | |
elseif &filetype ==# 'vimshell' | |
return vimshell#get_status_string().'/ ' | |
elseif &filetype ==# 'denite' | |
let path = denite#get_status('path')[1:-2] | |
else | |
let path = expand('%') | |
endif | |
let prefix = '' | |
if path[:0] ==# '!' | |
let prefix = path[0] | |
let path = path[1:] | |
endif | |
if path =~# '^fugitive://.*//' | |
let path = substitute(path, '^.*//', '', '').'/' | |
else | |
if path =~# 'fern://' | |
let fri = fern#fri#parse(path) | |
let path = fern#fri#to#filepath(fern#fri#parse(fri.path)) | |
else | |
let path = fnamemodify(path, ':p:h') | |
endif | |
let path = my#lightline#pathmodify(path) | |
endif | |
if a:inactive | |
let path = pathshorten(path) | |
endif | |
return prefix . path | |
endfunction | |
function! my#lightline#filename() abort | |
if &buftype ==# 'terminal' || &filetype =~# '^\(vimshell\|denite\)$' | |
return '' | |
endif | |
let path = expand('%') | |
if path =~# 'fern://' | |
return '.' | |
endif | |
let path = fnamemodify(path, ':t') | |
return path | |
endfunction | |
function! my#lightline#filetype() abort | |
if &filetype ==# 'fern' | |
return '' | |
endif | |
let icon = '' | |
if exists('*nerdfont#find') | |
let icon = nerdfont#find() | |
endif | |
if winwidth(0) < 100 | |
return icon | |
endif | |
return empty(&filetype) ? 'no ft' : icon.&filetype | |
endfunction | |
function! my#lightline#fileinfo() abort | |
let icon = '' | |
if exists('*nerdfont#fileformat#find') | |
let icon = nerdfont#fileformat#find() | |
endif | |
if winwidth(0) < 100 | |
return icon | |
endif | |
return substitute(&fileencoding, '^utf-', 'u', '') | |
\ . (empty(icon) ? ','.toupper(&fileformat[0]) : icon) | |
endfunction | |
function! my#lightline#lineinfo_expand() abort | |
if &buftype ==# 'terminal' | |
return '' | |
elseif &filetype ==# 'denite' | |
return '%{my#lightline#lineinfo_denite()}' | |
endif | |
return '%3v:%-3l/%L %P' | |
endfunction | |
function! my#lightline#lineinfo_denite() abort | |
let line = denite#get_status('line_cursor') | |
let total = denite#get_status('line_total') | |
return printf('%-3d/%d %d%%', line, total, line * 100 / total) | |
endfunction | |
function! my#lightline#charcode() abort | |
if winwidth(0) < 110 || &buftype ==# 'terminal' || &filetype ==# 'denite' | |
return '' | |
endif | |
let c = matchstr(getline('.'), '.', col('.') - 1) | |
if empty(c) | |
return '' | |
endif | |
let fc = iconv(c, &enc, &fenc) | |
let binary = s:string2hex(fc) | |
let unicode = printf('%X', char2nr(c)) | |
let b_or_u = binary == unicode ? binary : (binary.':u'.unicode) | |
return printf('[%s:%s]', c, b_or_u) | |
endfunction | |
" Converts each character in a string to a two character Hex string. | |
function! s:string2hex(str) abort | |
let out = '' | |
let ix = 0 | |
while ix < strlen(a:str) | |
let out = printf('%s%X', out, char2nr(a:str[ix])) | |
let ix = ix + 1 | |
endwhile | |
return out | |
endfunction | |
function! my#lightline#vcs_branch(...) abort | |
if get(b:, 'git_dir', '') !=# '' && &buftype ==# '' | |
if !exists('b:vcs_state') | |
let s = fugitive#repo().git_chomp('status') | |
let b:vcs_state = | |
\ match(s, "\nChanges not staged") >= 0 ? 'm' : | |
\ match(s, "\nUntracked files:") >= 0 ? 'u' : | |
\ '' | |
endif | |
let branch = fugitive#head(7) | |
if !a:0 || a:1 == b:vcs_state | |
return branch | |
endif | |
endif | |
return '' | |
endfunction | |
function! my#lightline#loccount() abort | |
let bn = bufnr('%') | |
let loc = len(filter(getloclist(0), 'v:val["bufnr"] == bn')) | |
return (loc ? string(loc) : '') | |
endfunction | |
function! my#lightline#qfcount() abort | |
let bn = bufnr('%') | |
let qf = len(filter(getqflist(), 'v:val["bufnr"] == bn')) | |
return (qf ? string(qf) : '') | |
endfunction | |
function! my#lightline#highlight() abort | |
let palette = g:lightline#palette() | |
let types = { 'normal': 0, 'inactive': 1 } | |
" colors: name => [ normal, inactive ] | |
let colors = { | |
\ 'Modified' : [ '#ffa0a0', '#a08080' ], | |
\ 'NoModified' : [ '#a0a0a0', '#808080' ], | |
\ 'Readonly' : [ '#ffa080', '#a08060' ], | |
\ 'Filepath' : [ '#ffff60', '#a0a060' ], | |
\ 'Filename' : [ '#60ffff', '#60a0a0' ], | |
\ 'Filetype' : [ '#60c0c0', '#60a0a0' ], | |
\ 'Fileinfo' : [ '#c0c060', '#a0a060' ], | |
\ 'VCSBranch' : [ '#a0ffa0', '#a0ffa0' ], | |
\ 'VCSModified' : [ '#ffa0a0', '#ffa0a0' ], | |
\ 'VCSUntracked' : [ '#ffffa0', '#ffffa0' ], | |
\ 'Warning' : [ '#fe8019', '#fe8019' ], | |
\ 'Error' : [ '#fb4934', '#fb4934' ], | |
\ } | |
let colors = map(colors, 'map(v:val, "[v:val, colors#rgb(v:val)]")') | |
for side in [ 'Left', 'Right', 'Middle' ] | |
for [type, t] in items(types) | |
let typepal = get(get(palette, type, {}), tolower(side), []) | |
for level in range(len(typepal)) | |
let p = typepal[level] | |
for [name, c] in items(colors) | |
exec printf('hi LightLine%s_%s_%d_%s guifg=%s guibg=%s ctermfg=%s ctermbg=%s', | |
\ side, type, level, name, c[t][0], p[1], c[t][1], p[3]) | |
endfor | |
endfor | |
endfor | |
endfor | |
endfunction | |
function! my#lightline#init() abort | |
if !exists('g:loaded_lightline') | |
return | |
endif | |
try | |
call lightline#init() | |
call lightline#colorscheme() | |
call my#lightline#highlight() | |
call lightline#update() | |
catch | |
endtry | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment