From 06d2b348747274d02022a508e391361bc3cb23b2 Mon Sep 17 00:00:00 2001 From: Nate Kane Date: Wed, 5 Jan 2011 14:07:53 +1000 Subject: [PATCH] Refactored some core logic and added some simple debugging --- autoload/indent_guides.vim | 59 ++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/autoload/indent_guides.vim b/autoload/indent_guides.vim index e34aafd..c00dd9a 100644 --- a/autoload/indent_guides.vim +++ b/autoload/indent_guides.vim @@ -33,16 +33,16 @@ endfunction function! indent_guides#enable() let g:indent_guides_autocmds_enabled = 1 - call indent_guides#init_buffer_vars() + call indent_guides#init_script_vars() call indent_guides#highlight_colors() call indent_guides#clear_matches() " loop through each indent level and define a highlight pattern " will automagically figure out whether to use tabs or spaces - for l:level in range(1, g:indent_guides_indent_levels) + for l:level in range(1, s:indent_levels) let l:group = 'IndentGuides' . ((l:level % 2 == 0) ? 'Even' : 'Odd') - let l:pattern = '^\s\{' . (l:level * b:indent_size - b:indent_size) . '\}\zs' - let l:pattern .= '\s\{' . b:guide_size . '\}' + let l:pattern = '^\s\{' . (l:level * s:indent_size - s:indent_size) . '\}\zs' + let l:pattern .= '\s\{' . s:guide_size . '\}' let l:pattern .= '\ze' " define the higlight pattern and add to list @@ -78,7 +78,7 @@ endfunction " Automagically calculates and defines the indent highlight colors. " function! indent_guides#highlight_colors() - if g:indent_guides_auto_colors + if s:auto_colors if has('gui_running') call indent_guides#gui_highlight_colors() else @@ -108,17 +108,17 @@ function! indent_guides#gui_highlight_colors() let l:hi_normal_guibg = '' " capture the backgroud color from the normal highlight - if b:hi_normal =~ g:indent_guides_color_hex_guibg_pattern + if s:hi_normal =~ s:color_hex_bg_pat " hex color code is being used, eg. '#FFFFFF' - let l:hi_normal_guibg = matchstr(b:hi_normal, g:indent_guides_color_hex_guibg_pattern) + let l:hi_normal_guibg = matchstr(s:hi_normal, s:color_hex_bg_pat) - elseif b:hi_normal =~ g:indent_guides_color_name_guibg_pattern + elseif s:hi_normal =~ s:color_name_bg_pat " color name is being used, eg. 'white' - let l:color_name = matchstr(b:hi_normal, g:indent_guides_color_name_guibg_pattern) + let l:color_name = matchstr(s:hi_normal, s:color_name_bg_pat) let l:hi_normal_guibg = color_helper#color_name_to_hex(l:color_name) endif - if l:hi_normal_guibg =~ g:indent_guides_color_hex_pattern + if l:hi_normal_guibg =~ s:color_hex_pat " calculate the highlight background colors let l:hi_odd_bg = indent_guides#lighten_or_darken_color(l:hi_normal_guibg) let l:hi_even_bg = indent_guides#lighten_or_darken_color(l:hi_odd_bg) @@ -135,12 +135,11 @@ endfunction " function! indent_guides#lighten_or_darken_color(color) let l:new_color = '' - let l:percent = g:indent_guides_color_change_percent / 100.0 if (&g:background == 'dark') - let l:new_color = color_helper#hex_color_lighten(a:color, l:percent) + let l:new_color = color_helper#hex_color_lighten(a:color, s:change_percent) else - let l:new_color = color_helper#hex_color_darken (a:color, l:percent) + let l:new_color = color_helper#hex_color_darken (a:color, s:change_percent) endif return l:new_color @@ -165,10 +164,31 @@ endfunction " We need to initialize these vars every time a buffer is entered while the " plugin is enabled. " -function! indent_guides#init_buffer_vars() - let b:indent_size = indent_guides#get_indent_size() - let b:guide_size = indent_guides#calculate_guide_size() - let b:hi_normal = indent_guides#capture_highlight('Normal') +function! indent_guides#init_script_vars() + let s:indent_size = indent_guides#get_indent_size() + let s:guide_size = indent_guides#calculate_guide_size() + let s:hi_normal = indent_guides#capture_highlight('Normal') + + " shortcuts to the global variables - this makes the code easier to read + let s:debug = g:indent_guides_debug + let s:indent_levels = g:indent_guides_indent_levels + let s:auto_colors = g:indent_guides_auto_colors + let s:change_percent = g:indent_guides_color_change_percent / 100.0 + let s:color_hex_pat = g:indent_guides_color_hex_pattern + let s:color_hex_bg_pat = g:indent_guides_color_hex_guibg_pattern + let s:color_name_bg_pat = g:indent_guides_color_name_guibg_pattern + + if s:debug + echo 's:indent_size = ' . s:indent_size + echo 's:guide_size = ' . s:guide_size + echo 's:hi_normal = ' . s:hi_normal + echo 's:indent_levels = ' . s:indent_levels + echo 's:auto_colors = ' . s:auto_colors + echo 's:change_percent = ' . string(s:change_percent) + echo 's:color_hex_pat = ' . s:color_hex_pat + echo 's:color_hex_bg_pat = ' . s:color_hex_bg_pat + echo 's:color_name_bg_pat = ' . s:color_name_bg_pat + endif endfunction " @@ -182,9 +202,9 @@ function! indent_guides#calculate_guide_size() let l:indent_size = indent_guides#get_indent_size() if l:indent_size > 1 && l:guide_size >= 1 - let l:guide_size = (l:guide_size > b:indent_size) ? b:indent_size : l:guide_size + let l:guide_size = (l:guide_size > s:indent_size) ? s:indent_size : l:guide_size else - let l:guide_size = b:indent_size + let l:guide_size = s:indent_size endif return l:guide_size @@ -209,6 +229,7 @@ function! indent_guides#capture_highlight(group_name) exe "silent hi " . a:group_name redir END + let l:output = substitute(l:output, "\n", "", "") return l:output endfunction