diff --git a/autoload/color_helper.vim b/autoload/color_helper.vim new file mode 100644 index 0000000..f0938b1 --- /dev/null +++ b/autoload/color_helper.vim @@ -0,0 +1,92 @@ +" +" Regex pattern for a hex color. +" +" Examples: +" - '#123ABC' +" - '#FFFFFF' +" - '#000000' +" +let s:hex_color_pattern = '#[0-9A-Fa-f]\{6\}' + +" +" Return hex string equivalent to given decimal string or number. +" +" Example: color_helper#dec_to_hex(255, 2) +" Returns: 'FF' +" +" Example: color_helper#dec_to_hex(255, 5) +" Returns: '000FF' +" +function! color_helper#dec_to_hex(arg, padding) + return toupper(printf('%0' . a:padding . 'x', a:arg + 0)) +endfunction + +" +" Return number equivalent to given hex string ('0x' is optional). +" +" Example: color_helper#hex_to_dec('FF') +" Returns: 255 +" +" Example: color_helper#hex_to_dec('88') +" Returns: 136 +" +" Example: color_helper#hex_to_dec('00') +" Returns: 0 +" +function! color_helper#hex_to_dec(arg) + return (a:arg =~? '^0x') ? a:arg + 0 : ('0x'.a:arg) + 0 +endfunction + +" +" Example: color_helper#hex_color_to_rgb('#0088FF') +" Returns: [0, 136, 255] +" +function! color_helper#hex_color_to_rgb(hex_color) + let l:rgb = [] + if matchstr(a:hex_color, s:hex_color_pattern) == a:hex_color + 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)) + let l:rgb = [l:red, l:green, l:blue] + end + return l:rgb +endfunction + +" +" Example: color_helper#rgb_color_to_hex([0, 136, 255]) +" Returns: '#0088FF' +" +function! color_helper#rgb_color_to_hex(rgb_color) + let l:hex_color = '#' + let l:hex_color .= color_helper#dec_to_hex(a:rgb_color[0], 2) " red + let l:hex_color .= color_helper#dec_to_hex(a:rgb_color[1], 2) " green + let l:hex_color .= color_helper#dec_to_hex(a:rgb_color[2], 2) " blue + return l:hex_color +endfunction + +" +" Example: color_helper#hex_color_brighten('#000000', 0.10) +" Returns: '#191919' +" +function! color_helper#hex_color_brighten(color, percent) + let l:rgb = color_helper#hex_color_to_rgb(a:color) + let l:rgb_brightened = [] + for decimal in l:rgb + call add(l:rgb_brightened, float2nr((255 - decimal) * a:percent)) + endfor + return color_helper#rgb_color_to_hex(l:rgb_brightened) +endfunction + +" +" Example: color_helper#hex_color_darken('#FFFFFF', 0.10) +" Returns: '#E5E5E5' +" +function! color_helper#hex_color_darken(color, percent) + let l:rgb = color_helper#hex_color_to_rgb(a:color) + let l:rgb_darkened = [] + for decimal in l:rgb + call add(l:rgb_darkened, float2nr(decimal * (1 - a:percent))) + endfor + return color_helper#rgb_color_to_hex(l:rgb_darkened) +endfunction + diff --git a/autoload/indent_guides.vim b/autoload/indent_guides.vim new file mode 100644 index 0000000..a27529f --- /dev/null +++ b/autoload/indent_guides.vim @@ -0,0 +1,46 @@ +function! indent_guides#toggle() + let g:indent_guides_matches = + \ exists('g:indent_guides_matches') ? g:indent_guides_matches : [] + + if empty(g:indent_guides_matches) + call indent_guides#enable() + else + call indent_guides#disable() + endif +endfunction + +function! indent_guides#enable() + call indent_guides#disable() + call indent_guides#highlight_colors() + + for level in range(1, g:indent_guides_indent_levels) + let group = 'IndentLevel' . ((level % 2 == 0) ? 'Even' : 'Odd') + let multiplier = (&l:expandtab == 1) ? &l:shiftwidth : 1 + let pattern = '^\s\{' . (level * multiplier - multiplier) . '\}\zs' + let pattern .= '\s\{' . multiplier . '\}' + let pattern .= '\ze' + + call add(g:indent_guides_matches, matchadd(group, pattern)) + + if g:indent_guides_debug + echo "matchadd ('" . group . "', '" . pattern . "')" + end + endfor +endfunction + +function! indent_guides#disable() + if !empty(g:indent_guides_matches) + let index = 0 + for item in g:indent_guides_matches + call matchdelete(item) + call remove(g:indent_guides_matches, index) + let index += index + endfor + endif +endfunction + +function! indent_guides#highlight_colors() + hi IndentLevelOdd guibg=#FFFFFF + hi IndentLevelEven guibg=#FBFBFB +endfunction + diff --git a/doc/indent-guides.txt b/doc/indent_guides.txt similarity index 97% rename from doc/indent-guides.txt rename to doc/indent_guides.txt index 28524a8..23926d6 100644 --- a/doc/indent-guides.txt +++ b/doc/indent_guides.txt @@ -38,10 +38,10 @@ CONTENTS *indent-guides-contents* ============================================================================== 2. OPTIONS *indent-guides-options* - *'IndentGuides_indent_levels'* + *'indent_guides_indent_levels'* Use this option to control how many indent levels to display guides for: > - let g:IndentGuides_indent_levels=50 + let g:indent_guides_indent_levels=50 < ============================================================================== diff --git a/plugin/indent-guides.vim b/plugin/indent-guides.vim deleted file mode 100644 index b7bd444..0000000 --- a/plugin/indent-guides.vim +++ /dev/null @@ -1,145 +0,0 @@ -" Author: Nate Kane -" Homepage: http://github.com/nathanaelkane/vim-indent-guides - -let s:hex_color_pattern = '#[0-9A-Fa-f]\{6\}' - -function! s:IndentGuidesToggle() - let g:IndentGuides_matches = - \ exists('g:IndentGuides_matches') ? g:IndentGuides_matches : [] - - if empty(g:IndentGuides_matches) - IndentGuidesEnable - else - IndentGuidesDisable - endif -endfunction - -function! s:IndentGuidesEnable() - IndentGuidesDisable - call s:IndentGuidesHighlightColors() - - for level in range(1, g:IndentGuides_indent_levels) - let group = 'IndentLevel' . ((level % 2 == 0) ? 'Even' : 'Odd') - let multiplier = (&l:expandtab == 1) ? &l:shiftwidth : 1 - let pattern = '^\s\{' . (level * multiplier - multiplier) . '\}\zs' - let pattern .= '\s\{' . multiplier . '\}' - let pattern .= '\ze' - - call add(g:IndentGuides_matches, matchadd(group, pattern)) - - if g:IndentGuides_debug - echo "matchadd ('" . group . "', '" . pattern . "')" - end - endfor -endfunction - -function! s:IndentGuidesDisable() - if !empty(g:IndentGuides_matches) - let index = 0 - for item in g:IndentGuides_matches - call matchdelete(item) - call remove(g:IndentGuides_matches, index) - let index += index - endfor - endif -endfunction - -function! s:IndentGuidesHighlightColors() - hi IndentLevelOdd guibg=#FFFFFF - hi IndentLevelEven guibg=#FBFBFB -endfunction - -" Commands -command! IndentGuidesToggle call s:IndentGuidesToggle() -command! IndentGuidesEnable call s:IndentGuidesEnable() -command! IndentGuidesDisable call s:IndentGuidesDisable() - -" Default options -let g:IndentGuides_indent_levels = 50 -let g:IndentGuides_debug = 0 - -" Default mapping -nmap ig :IndentGuidesToggle - -" -" Return hex string equivalent to given decimal string or number. -" -" Example: DecToHex(255, 2) -" Returns: 'FF' -" -" Example: DecToHex(255, 5) -" Returns: '000FF' -" -function! DecToHex(arg, padding) - return toupper(printf('%0' . a:padding . 'x', a:arg + 0)) -endfunction - -" -" Return number equivalent to given hex string ('0x' is optional). -" -" Example: HexToDec('FF') -" Returns: 255 -" -" Example: HexToDec('88') -" Returns: 136 -" -" Example: HexToDec('00') -" Returns: 0 -" -function! HexToDec(arg) - return (a:arg =~? '^0x') ? a:arg + 0 : ('0x'.a:arg) + 0 -endfunction - -" -" Example: HexColorToRGB('#0088FF') -" Returns: [0, 136, 255] -" -function! HexColorToRGB(hex_color) - let l:rgb = [] - if matchstr(a:hex_color, s:hex_color_pattern) == a:hex_color - let l:red = HexToDec(strpart(a:hex_color, 1, 2)) - let l:green = HexToDec(strpart(a:hex_color, 3, 2)) - let l:blue = HexToDec(strpart(a:hex_color, 5, 2)) - let l:rgb = [l:red, l:green, l:blue] - end - return l:rgb -endfunction - -" -" Example: RGBColorToHex([0, 136, 255]) -" Returns: '#0088FF' -" -function! RGBColorToHex(rgb_color) - let l:hex_color = '#' - let l:hex_color .= DecToHex(a:rgb_color[0], 2) " red - let l:hex_color .= DecToHex(a:rgb_color[1], 2) " green - let l:hex_color .= DecToHex(a:rgb_color[2], 2) " blue - return l:hex_color -endfunction - -" -" Example: HexColorBrighten('#000000', 0.10) -" Returns: '#191919' -" -function! HexColorBrighten(color, percent) - let l:rgb = HexColorToRGB(a:color) - let l:rgb_brightened = [] - for decimal in l:rgb - call add(l:rgb_brightened, float2nr((255 - decimal) * a:percent)) - endfor - return RGBColorToHex(l:rgb_brightened) -endfunction - -" -" Example: HexColorDarken('#FFFFFF', 0.10) -" Returns: '#E5E5E5' -" -function! HexColorDarken(color, percent) - let l:rgb = HexColorToRGB(a:color) - let l:rgb_darkened = [] - for decimal in l:rgb - call add(l:rgb_darkened, float2nr(decimal * (1 - a:percent))) - endfor - return RGBColorToHex(l:rgb_darkened) -endfunction - diff --git a/plugin/indent_guides.vim b/plugin/indent_guides.vim new file mode 100644 index 0000000..f375d4b --- /dev/null +++ b/plugin/indent_guides.vim @@ -0,0 +1,27 @@ +" Author: Nate Kane +" Homepage: http://github.com/nathanaelkane/vim-indent-guides + +function! s:IndentGuidesToggle() + call indent_guides#toggle() +endfunction + +function! s:IndentGuidesEnable() + call indent_guides#enable() +endfunction + +function! s:IndentGuidesDisable() + call indent_guides#disable() +endfunction + +" Commands +command! IndentGuidesToggle call s:IndentGuidesToggle() +command! IndentGuidesEnable call s:IndentGuidesEnable() +command! IndentGuidesDisable call s:IndentGuidesDisable() + +" Default options +let g:indent_guides_indent_levels = 50 +let g:indent_guides_debug = 0 + +" Default mapping +nmap ig :IndentGuidesToggle +