Skip to content

Instantly share code, notes, and snippets.

@kien
Created October 11, 2011 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kien/1279455 to your computer and use it in GitHub Desktop.
Save kien/1279455 to your computer and use it in GitHub Desktop.
Block color or Shaded indenting (Vim)
" <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 "}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment