The plugin now automatically calculates indent highlight colors

This commit is contained in:
Nate Kane 2010-12-07 22:37:50 +10:00
parent e17ed074a0
commit 8d0c589f5c

View File

@ -1,3 +1,9 @@
" Author: Nate Kane <nathanaelkane AT gmail DOT com>
" Homepage: http://github.com/nathanaelkane/vim-indent-guides
"
"
"
function! indent_guides#toggle() function! indent_guides#toggle()
let g:indent_guides_matches = let g:indent_guides_matches =
\ exists('g:indent_guides_matches') ? g:indent_guides_matches : [] \ exists('g:indent_guides_matches') ? g:indent_guides_matches : []
@ -9,17 +15,26 @@ function! indent_guides#toggle()
endif endif
endfunction endfunction
"
"
"
function! indent_guides#enable() function! indent_guides#enable()
call indent_guides#disable() call indent_guides#disable()
call indent_guides#highlight_colors()
if g:indent_guides_auto_colors
call indent_guides#highlight_colors()
endif
" loop through each indent level and define a highlight pattern
" will automagically figure out whether to use tabs or spaces
for level in range(1, g:indent_guides_indent_levels) for level in range(1, g:indent_guides_indent_levels)
let group = 'IndentLevel' . ((level % 2 == 0) ? 'Even' : 'Odd') let group = 'IndentGuides' . ((level % 2 == 0) ? 'Even' : 'Odd')
let multiplier = (&l:expandtab == 1) ? &l:shiftwidth : 1 let multiplier = (&l:expandtab == 1) ? &l:shiftwidth : 1
let pattern = '^\s\{' . (level * multiplier - multiplier) . '\}\zs' let pattern = '^\s\{' . (level * multiplier - multiplier) . '\}\zs'
let pattern .= '\s\{' . multiplier . '\}' let pattern .= '\s\{' . multiplier . '\}'
let pattern .= '\ze' let pattern .= '\ze'
" define the higlight pattern and add to list
call add(g:indent_guides_matches, matchadd(group, pattern)) call add(g:indent_guides_matches, matchadd(group, pattern))
if g:indent_guides_debug if g:indent_guides_debug
@ -28,6 +43,9 @@ function! indent_guides#enable()
endfor endfor
endfunction endfunction
"
"
"
function! indent_guides#disable() function! indent_guides#disable()
if !empty(g:indent_guides_matches) if !empty(g:indent_guides_matches)
let index = 0 let index = 0
@ -39,8 +57,50 @@ function! indent_guides#disable()
endif endif
endfunction endfunction
"
"
"
function! indent_guides#highlight_colors() function! indent_guides#highlight_colors()
hi IndentLevelOdd guibg=#FFFFFF if g:indent_guides_auto_colors
hi IndentLevelEven guibg=#FBFBFB " capture the backgroud color from the normal highlight
let hi_normal = indent_guides#capture_highlight('normal')
let hi_normal_guibg = matchstr(hi_normal, 'guibg=\zs#[0-9A-Fa-f]\{6\}\ze')
" calculate the highlight background colors
let hi_odd_bg = indent_guides#brighten_or_darken_color(hi_normal_guibg)
let hi_even_bg = indent_guides#brighten_or_darken_color(hi_odd_bg)
" define the new highlights
exe "hi IndentGuidesOdd guibg=" . hi_odd_bg
exe "hi IndentGuidesEven guibg=" . hi_even_bg
endif
endfunction
"
" Takes a color and darkens or brightens it depending on whether a dark or
" light colorscheme is being used
"
function! indent_guides#brighten_or_darken_color(color)
let percent = g:indent_guides_auto_colors_change_percent
let new_color = (&g:background == 'dark') ?
\ color_helper#hex_color_brighten(a:color, percent) :
\ color_helper#hex_color_darken (a:color, percent)
return new_color
endfunction
"
" Captures and returns the output of highlight group definitions
"
" Example: indent_guides#capture_highlight('normal')
" Returns: 'Normal xxx guifg=#323232 guibg=#ffffff font=DejaVu Sans Mono 9'
"
function! indent_guides#capture_highlight(group_name)
redir => output
exe "silent hi " . a:group_name
redir END
return output
endfunction endfunction