2010-12-07 07:37:50 -05:00
|
|
|
" Author: Nate Kane <nathanaelkane AT gmail DOT com>
|
|
|
|
" Homepage: http://github.com/nathanaelkane/vim-indent-guides
|
|
|
|
|
|
|
|
"
|
2010-12-09 07:45:09 -05:00
|
|
|
" Toggles the indent guides on and off.
|
2010-12-07 07:37:50 -05:00
|
|
|
"
|
2010-12-07 04:41:05 -05:00
|
|
|
function! indent_guides#toggle()
|
2010-12-08 06:48:00 -05:00
|
|
|
call indent_guides#init_matches()
|
2010-12-09 07:45:09 -05:00
|
|
|
|
2010-12-08 06:48:00 -05:00
|
|
|
if empty(w:indent_guides_matches)
|
2010-12-07 04:41:05 -05:00
|
|
|
call indent_guides#enable()
|
|
|
|
else
|
|
|
|
call indent_guides#disable()
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2010-12-07 07:37:50 -05:00
|
|
|
"
|
2010-12-09 07:45:09 -05:00
|
|
|
" Called from autocmds, keeps indent guides enabled or disabled when entering
|
|
|
|
" other buffers and windows.
|
|
|
|
"
|
|
|
|
function! indent_guides#process_autocmds()
|
|
|
|
if g:indent_guides_autocmds_enabled
|
|
|
|
call indent_guides#enable()
|
|
|
|
else
|
|
|
|
call indent_guides#disable()
|
|
|
|
end
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
"
|
|
|
|
" Enables the indent guides for the current buffer and any other buffer upon
|
|
|
|
" entering it.
|
2010-12-07 07:37:50 -05:00
|
|
|
"
|
2010-12-07 04:41:05 -05:00
|
|
|
function! indent_guides#enable()
|
2010-12-09 07:45:09 -05:00
|
|
|
let g:indent_guides_autocmds_enabled = 1
|
|
|
|
call indent_guides#clear_matches()
|
2010-12-07 04:41:05 -05:00
|
|
|
|
2010-12-07 07:37:50 -05:00
|
|
|
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
|
2010-12-07 04:41:05 -05:00
|
|
|
for level in range(1, g:indent_guides_indent_levels)
|
2010-12-07 07:37:50 -05:00
|
|
|
let group = 'IndentGuides' . ((level % 2 == 0) ? 'Even' : 'Odd')
|
2010-12-07 04:41:05 -05:00
|
|
|
let multiplier = (&l:expandtab == 1) ? &l:shiftwidth : 1
|
|
|
|
let pattern = '^\s\{' . (level * multiplier - multiplier) . '\}\zs'
|
|
|
|
let pattern .= '\s\{' . multiplier . '\}'
|
|
|
|
let pattern .= '\ze'
|
|
|
|
|
2010-12-07 07:37:50 -05:00
|
|
|
" define the higlight pattern and add to list
|
2010-12-08 06:48:00 -05:00
|
|
|
call add(w:indent_guides_matches, matchadd(group, pattern))
|
2010-12-07 04:41:05 -05:00
|
|
|
endfor
|
|
|
|
endfunction
|
|
|
|
|
2010-12-07 07:37:50 -05:00
|
|
|
"
|
2010-12-09 07:45:09 -05:00
|
|
|
" Disables the indent guides for the current buffer and any other buffer upon
|
|
|
|
" entering it.
|
2010-12-07 07:37:50 -05:00
|
|
|
"
|
2010-12-07 04:41:05 -05:00
|
|
|
function! indent_guides#disable()
|
2010-12-09 07:45:09 -05:00
|
|
|
let g:indent_guides_autocmds_enabled = 0
|
|
|
|
call indent_guides#clear_matches()
|
|
|
|
endfunction
|
2010-12-08 06:48:00 -05:00
|
|
|
|
2010-12-09 07:45:09 -05:00
|
|
|
"
|
|
|
|
" Clear all highlight matches for the current window.
|
|
|
|
"
|
|
|
|
function! indent_guides#clear_matches()
|
|
|
|
call indent_guides#init_matches()
|
2010-12-08 06:48:00 -05:00
|
|
|
if !empty(w:indent_guides_matches)
|
2010-12-07 04:41:05 -05:00
|
|
|
let index = 0
|
2010-12-08 06:48:00 -05:00
|
|
|
for match_id in w:indent_guides_matches
|
|
|
|
call matchdelete(match_id)
|
|
|
|
call remove(w:indent_guides_matches, index)
|
2010-12-07 04:41:05 -05:00
|
|
|
let index += index
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2010-12-07 07:37:50 -05:00
|
|
|
"
|
2010-12-07 07:51:31 -05:00
|
|
|
" Automagically calculates and defines the indent highlight colors.
|
2010-12-07 07:37:50 -05:00
|
|
|
"
|
2010-12-09 07:45:09 -05:00
|
|
|
" NOTE: Does not work when color names are used instead of hex colors.
|
|
|
|
" Eg. 'guibg=#FFFFFF' works, but 'guibg=green' doesn't.
|
|
|
|
"
|
2010-12-07 04:41:05 -05:00
|
|
|
function! indent_guides#highlight_colors()
|
2010-12-07 07:37:50 -05:00
|
|
|
if g:indent_guides_auto_colors
|
|
|
|
" capture the backgroud color from the normal highlight
|
|
|
|
let hi_normal = indent_guides#capture_highlight('normal')
|
2010-12-09 07:45:09 -05:00
|
|
|
let pattern = 'guibg=\zs'. g:indent_guides_hex_color_pattern . '\ze'
|
|
|
|
let hi_normal_guibg = matchstr(hi_normal, pattern)
|
2010-12-07 07:37:50 -05:00
|
|
|
|
2010-12-09 07:45:09 -05:00
|
|
|
if hi_normal_guibg =~ g:indent_guides_hex_color_pattern
|
|
|
|
" calculate the highlight background colors
|
|
|
|
let hi_odd_bg = indent_guides#lighten_or_darken_color(hi_normal_guibg)
|
|
|
|
let hi_even_bg = indent_guides#lighten_or_darken_color(hi_odd_bg)
|
2010-12-07 07:37:50 -05:00
|
|
|
|
2010-12-09 07:45:09 -05:00
|
|
|
" define the new highlights
|
|
|
|
exe "hi IndentGuidesOdd guibg=" . hi_odd_bg
|
|
|
|
exe "hi IndentGuidesEven guibg=" . hi_even_bg
|
|
|
|
end
|
2010-12-07 07:37:50 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
"
|
2010-12-09 07:45:09 -05:00
|
|
|
" Takes a color and darkens or lightens it depending on whether a dark or light
|
|
|
|
" colorscheme is being used.
|
2010-12-07 07:37:50 -05:00
|
|
|
"
|
2010-12-07 07:51:31 -05:00
|
|
|
function! indent_guides#lighten_or_darken_color(color)
|
2010-12-07 07:37:50 -05:00
|
|
|
let percent = g:indent_guides_auto_colors_change_percent
|
|
|
|
|
|
|
|
let new_color = (&g:background == 'dark') ?
|
2010-12-08 06:48:00 -05:00
|
|
|
\ color_helper#hex_color_lighten(a:color, percent) :
|
|
|
|
\ color_helper#hex_color_darken (a:color, percent)
|
2010-12-07 07:37:50 -05:00
|
|
|
|
|
|
|
return new_color
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
"
|
2010-12-09 07:45:09 -05:00
|
|
|
" Captures and returns the output of highlight group definitions.
|
2010-12-07 07:37:50 -05:00
|
|
|
"
|
|
|
|
" Example: indent_guides#capture_highlight('normal')
|
2010-12-09 07:45:09 -05:00
|
|
|
" Returns: 'Normal xxx guifg=#323232 guibg=#ffffff
|
2010-12-07 07:37:50 -05:00
|
|
|
"
|
|
|
|
function! indent_guides#capture_highlight(group_name)
|
|
|
|
redir => output
|
|
|
|
exe "silent hi " . a:group_name
|
|
|
|
redir END
|
|
|
|
|
|
|
|
return output
|
2010-12-07 04:41:05 -05:00
|
|
|
endfunction
|
|
|
|
|
2010-12-09 07:45:09 -05:00
|
|
|
"
|
|
|
|
" Init the w:indent_guides_matches variable.
|
|
|
|
"
|
2010-12-08 06:48:00 -05:00
|
|
|
function! indent_guides#init_matches()
|
|
|
|
let w:indent_guides_matches =
|
2010-12-09 07:45:09 -05:00
|
|
|
\ exists('w:indent_guides_matches') ? w:indent_guides_matches : []
|
2010-12-08 06:48:00 -05:00
|
|
|
endfunction
|
|
|
|
|