public
Created

Block color or Shaded indenting (Vim)

  • Download Gist
blockcolor.vim
VimL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
" <leader>B to toggle. The value of guibg must be hexadecimal.
" http://www.reddit.com/r/vim/comments/l8gua/code_block_background_color_is_there_a_way_to/
 
nn <silent> <leader>B :cal BlockColor(8, 0x101010)<cr>
 
func! BlockColor(max, step, ...) "{{{
let [max, bufnr] = [a:max, bufnr('%')]
" Colors
if !exists('g:colors_name') | let g:colors_name = 'default' | endif
if !exists('g:bc_highl') || ( exists('g:bc_highl') && g:bc_highl != g:colors_name )
if !exists('a:1')
redi => gnorm
sil! hi Normal
redi END
let bgc = matchstr(gnorm, 'guibg=#\zs[^# ]\+')
let bgc = empty(bgc) ? &bg == 'dark' ? '0x000000' : '0xffffff' : '0x'.bgc
else
let bgc = a:1
endif
for id in range(1, max)
let step = &bg == 'dark' ? bgc + (a:step * id) : bgc - (a:step * id)
exe 'hi BlockColor'.id.' guifg=bg guibg=#'.s:gethex(step)
endfor
let g:bc_highl = g:colors_name
endif
" Toggling
if exists('b:blockcolor') && b:blockcolor
let b:blockcolor = 0
for id in range(1, max)
sil! cal matchdelete(bufnr.id)
endfor
else
let b:blockcolor = 1
for id in range(1, max)
cal matchadd('BlockColor'.id, '^\s\{'.id.'}', id, bufnr.id)
endfor
endif
endfunc
 
func! s:gethex(step)
let newco = printf('%x', a:step)
let newco = strlen(newco) < 6 ? s:padzero(newco) : newco
let newco = '0x'.newco >= 0xffffff ? 'ffffff' : newco
let newco = '0x'.newco <= 0 ? '000000' : newco
retu newco
endfunc
 
func! s:padzero(nr)
let [nr, len] = [a:nr, 6 - strlen(a:nr)]
for each in range(1, len)
let nr = '0'.nr
endfor
retu nr
endfunc "}}}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.