diff --git a/autoload/color_helper.vim b/autoload/color_helper.vim index 7648eff..7537253 100644 --- a/autoload/color_helper.vim +++ b/autoload/color_helper.vim @@ -1,16 +1,6 @@ " Author: Nate Kane " Homepage: http://github.com/nathanaelkane/vim-indent-guides -" -" Regex pattern for a hex color. -" -" Example matches: -" - '#123ABC' -" - '#ffffff' -" - '#000000' -" -let s:hex_color_pattern = '#[0-9A-Fa-f]\{6\}' - " " Return hex string equivalent to given decimal string or number. " @@ -49,7 +39,7 @@ endfunction function! color_helper#hex_color_to_rgb(hex_color) let l:rgb = [] - if matchstr(a:hex_color, s:hex_color_pattern) == a:hex_color + if a:hex_color =~ g:indent_guides_hex_color_pattern let l:red = color_helper#hex_to_dec(strpart(a:hex_color, 1, 2)) let l:green = color_helper#hex_to_dec(strpart(a:hex_color, 3, 2)) let l:blue = color_helper#hex_to_dec(strpart(a:hex_color, 5, 2)) diff --git a/autoload/indent_guides.vim b/autoload/indent_guides.vim index 636df4e..366e3f9 100644 --- a/autoload/indent_guides.vim +++ b/autoload/indent_guides.vim @@ -2,29 +2,37 @@ " Homepage: http://github.com/nathanaelkane/vim-indent-guides " -" Toggles the indent guides on and off for all buffers. +" Toggles the indent guides on and off. " function! indent_guides#toggle() call indent_guides#init_matches() + if empty(w:indent_guides_matches) call indent_guides#enable() - - " DEBUG - "echo 'enable' else call indent_guides#disable() - - " DEBUG - "echo 'disable' endif endfunction " -" Enables the indent guides for all buffers. +" 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. " function! indent_guides#enable() - call indent_guides#init_matches() - call indent_guides#disable() + let g:indent_guides_autocmds_enabled = 1 + call indent_guides#clear_matches() if g:indent_guides_auto_colors call indent_guides#highlight_colors() @@ -41,59 +49,61 @@ function! indent_guides#enable() " define the higlight pattern and add to list call add(w:indent_guides_matches, matchadd(group, pattern)) - - if g:indent_guides_debug - echo "matchadd ('" . group . "', '" . pattern . "')" - end endfor - - " DEBUG - "echo w:indent_guides_matches endfunction " -" Disables the indent guides for all buffers. +" Disables the indent guides for the current buffer and any other buffer upon +" entering it. " function! indent_guides#disable() + let g:indent_guides_autocmds_enabled = 0 + call indent_guides#clear_matches() +endfunction + +" +" Clear all highlight matches for the current window. +" +function! indent_guides#clear_matches() call indent_guides#init_matches() - if !empty(w:indent_guides_matches) - - " DEBUG - "echo w:indent_guides_matches - let index = 0 for match_id in w:indent_guides_matches call matchdelete(match_id) call remove(w:indent_guides_matches, index) let index += index endfor - "call filter(w:indent_guides_matches, 0) endif endfunction " " Automagically calculates and defines the indent highlight colors. " +" NOTE: Does not work when color names are used instead of hex colors. +" Eg. 'guibg=#FFFFFF' works, but 'guibg=green' doesn't. +" function! indent_guides#highlight_colors() if g:indent_guides_auto_colors " 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') + let pattern = 'guibg=\zs'. g:indent_guides_hex_color_pattern . '\ze' + let hi_normal_guibg = matchstr(hi_normal, 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) + 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) - " define the new highlights - exe "hi IndentGuidesOdd guibg=" . hi_odd_bg - exe "hi IndentGuidesEven guibg=" . hi_even_bg + " define the new highlights + exe "hi IndentGuidesOdd guibg=" . hi_odd_bg + exe "hi IndentGuidesEven guibg=" . hi_even_bg + end endif endfunction " -" Takes a color and darkens or lightens it depending on whether a dark or -" light colorscheme is being used. +" Takes a color and darkens or lightens it depending on whether a dark or light +" colorscheme is being used. " function! indent_guides#lighten_or_darken_color(color) let percent = g:indent_guides_auto_colors_change_percent @@ -106,10 +116,10 @@ function! indent_guides#lighten_or_darken_color(color) endfunction " -" Captures and returns the output of highlight group definitions +" 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' +" Returns: 'Normal xxx guifg=#323232 guibg=#ffffff " function! indent_guides#capture_highlight(group_name) redir => output @@ -119,9 +129,11 @@ function! indent_guides#capture_highlight(group_name) return output endfunction +" +" Init the w:indent_guides_matches variable. +" function! indent_guides#init_matches() let w:indent_guides_matches = - \ exists('w:indent_guides_matches') ? - \ w:indent_guides_matches : [] + \ exists('w:indent_guides_matches') ? w:indent_guides_matches : [] endfunction diff --git a/plugin/indent_guides.vim b/plugin/indent_guides.vim index 1092f1e..f8f454d 100644 --- a/plugin/indent_guides.vim +++ b/plugin/indent_guides.vim @@ -35,6 +35,24 @@ let g:indent_guides_debug = \ exists('g:indent_guides_debug') ? \ g:indent_guides_debug : 0 +let g:indent_guides_autocmds_enabled = 0 + +" +" Regex pattern for a hex color. +" +" Example matches: +" - '#123ABC' +" - '#ffffff' +" - '#000000' +" +let g:indent_guides_hex_color_pattern = '#[0-9A-Fa-f]\{6\}' + " Default mapping nmap ig :IndentGuidesToggle +" Auto commands +augroup indent_guides + autocmd! + autocmd BufEnter,WinEnter * call indent_guides#process_autocmds() +augroup END +