vim-dfm.vim (4187B)
1 function! s:LoadOffsets() 2 let l:left_offset = ((winwidth('%')-80)/2) 3 if l:left_offset > 22 4 let l:left_offset = 22 5 endif 6 if l:left_offset <= 10 7 let s:numberwidth_offset = l:left_offset 8 let s:foldcolumn_offset = 0 9 else 10 let s:numberwidth_offset = 10 11 let s:foldcolumn_offset = l:left_offset - 10 12 endif 13 let &columns = &columns - l:left_offset 14 let &colorcolumn = 0 15 endfunction 16 17 18 " Retrieves the color for a provided scope and swatch in the current context 19 function! s:LoadColor(scope, swatch) 20 let l:scopeColor = synIDattr(hlID(a:scope), a:swatch, 'cterm') 21 return l:scopeColor < 0 ? 'none' : l:scopeColor 22 endfunction 23 24 25 " Generates a highlight command for the provided scope, foreground, and 26 " background 27 function! s:Highlight(scope, fg, bg) 28 return 'highlight ' . a:scope . ' ctermfg=' . a:fg . ' ctermbg=' . a:bg 29 endfunction 30 31 32 " Generate a highlight string that hides the given scope by setting its 33 " foreground and background to match the normal background 34 function! s:Hide(scope) 35 return s:Highlight(a:scope, s:NormalBG, s:NormalBG) 36 endfunction 37 38 39 " Generate a highlight string that restores the given scope to its original 40 " foreground and background values 41 function! s:Restore(scope) 42 return s:Highlight(a:scope, s:[a:scope . 'FG'], s:[a:scope . 'BG']) 43 endfunction 44 45 46 " Execute the given command within each window that is not ignored 47 function! s:ForEachWindow(cmd) 48 let l:initialWindow = winnr() 49 execute 'windo ' . a:cmd 50 execute l:initialWindow . 'wincmd w' 51 endfunction 52 53 54 " Load all necessary colors and assign them to script-wide variables 55 function! s:LoadDFMColors() 56 let s:LineNrFG = s:LoadColor('LineNr', 'fg') 57 let s:LineNrBG = s:LoadColor('LineNr', 'bg') 58 let s:CursorLineNrFG = s:LoadColor('CursorLineNr', 'fg') 59 let s:CursorLineNrBG = s:LoadColor('CursorLineNr', 'bg') 60 let s:NonTextFG = s:LoadColor('NonText', 'fg') 61 let s:NonTextBG = s:LoadColor('NonText', 'bg') 62 let s:FoldColumnFG = s:LoadColor('FoldColumn', 'fg') 63 let s:FoldColumnBG = s:LoadColor('FoldColumn', 'bg') 64 let s:TabLineFillFG = s:LoadColor('TabLineFill', 'fg') 65 let s:TabLineFillBG = s:LoadColor('TabLineFill', 'bg') 66 67 " Allow users to manually specify the color used to hide UI elements 68 let s:NormalBG = get(g:, 'lite_dfm_normal_bg_cterm', '0') 69 endfunction 70 71 72 " Function to enter DFM 73 function! LiteDFM() 74 75 " Remember user's default values. 76 let s:number_default = &number 77 let s:laststatus_default = &laststatus 78 let s:tabline_default = &tabline 79 let s:showtabline_default = &showtabline 80 let s:colorcolumn_default = &colorcolumn 81 let s:foldcolumn_default = &foldcolumn 82 let s:numberwidth_default = &numberwidth 83 84 if !get(s:, 'lite_dfm_on', 0) 85 call s:LoadDFMColors() 86 endif 87 call s:LoadOffsets() 88 let s:lite_dfm_on = 1 89 90 set number 91 set laststatus=0 92 set showtabline=2 93 set tabline=\ 94 call s:ForEachWindow('set numberwidth=' . s:numberwidth_offset . ' foldcolumn=' . s:foldcolumn_offset) 95 96 execute s:Hide('LineNr') 97 execute s:Hide('CursorLineNr') 98 execute s:Hide('NonText') 99 execute s:Hide('FoldColumn') 100 execute s:Hide('TabLineFill') 101 102 endfunction 103 104 105 " Function to close DFM 106 function! LiteDFMClose() 107 let s:lite_dfm_on = 0 108 109 let &number = s:number_default 110 let &laststatus = s:laststatus_default 111 let &tabline = s:tabline_default 112 let &showtabline = s:showtabline_default 113 let &colorcolumn = s:colorcolumn_default 114 let &columns = system('tput cols') 115 116 call s:ForEachWindow('set numberwidth=' . s:numberwidth_default . ' foldcolumn=' . s:foldcolumn_default) 117 118 try 119 execute s:Restore('LineNr') 120 execute s:Restore('CursorLineNr') 121 execute s:Restore('NonText') 122 execute s:Restore('FoldColumn') 123 execute s:Restore('TabLineFill') 124 catch /.*/ 125 " swallow the exception 126 endtry 127 128 endfunction 129 130 131 " Function to toggle DFM 132 function! LiteDFMToggle() 133 if get(s:, 'lite_dfm_on', 0) 134 call LiteDFMClose() 135 else 136 call LiteDFM() 137 endif 138 endfunction 139 140 141 " Map function calls to commands 142 command! LiteDFM call LiteDFM() 143 command! LiteDFMClose call LiteDFMClose() 144 command! LiteDFMToggle call LiteDFMToggle()