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
|
2011-01-04 08:53:17 -05:00
|
|
|
|
|
|
|
call indent_guides#init_buffer_vars()
|
2010-12-20 06:51:02 -05:00
|
|
|
call indent_guides#highlight_colors()
|
2010-12-09 07:45:09 -05:00
|
|
|
call indent_guides#clear_matches()
|
2010-12-07 04:41:05 -05:00
|
|
|
|
2010-12-07 07:37:50 -05:00
|
|
|
" loop through each indent level and define a highlight pattern
|
|
|
|
" will automagically figure out whether to use tabs or spaces
|
2010-12-29 05:31:49 -05:00
|
|
|
for l:level in range(1, g:indent_guides_indent_levels)
|
|
|
|
let l:group = 'IndentGuides' . ((l:level % 2 == 0) ? 'Even' : 'Odd')
|
2011-01-04 08:53:17 -05:00
|
|
|
let l:pattern = '^\s\{' . (l:level * b:indent_size - b:indent_size) . '\}\zs'
|
|
|
|
let l:pattern .= '\s\{' . b:guide_size . '\}'
|
2010-12-29 05:31:49 -05:00
|
|
|
let l:pattern .= '\ze'
|
2010-12-07 04:41:05 -05:00
|
|
|
|
2010-12-07 07:37:50 -05:00
|
|
|
" define the higlight pattern and add to list
|
2010-12-29 05:31:49 -05:00
|
|
|
call add(w:indent_guides_matches, matchadd(l:group, l: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-29 05:31:49 -05:00
|
|
|
let l:index = 0
|
|
|
|
for l:match_id in w:indent_guides_matches
|
|
|
|
call matchdelete(l:match_id)
|
|
|
|
call remove(w:indent_guides_matches, l:index)
|
|
|
|
let l:index += l:index
|
2010-12-07 04:41:05 -05:00
|
|
|
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-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
|
2010-12-20 06:51:02 -05:00
|
|
|
if has('gui_running')
|
|
|
|
call indent_guides#gui_highlight_colors()
|
|
|
|
else
|
|
|
|
call indent_guides#cterm_highlight_colors()
|
2010-12-11 02:48:01 -05:00
|
|
|
endif
|
2010-12-20 06:51:02 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
2010-12-07 07:37:50 -05:00
|
|
|
|
2010-12-20 06:51:02 -05:00
|
|
|
"
|
2010-12-29 05:17:34 -05:00
|
|
|
" Defines the indent highlight colors for terminal vim.
|
|
|
|
"
|
|
|
|
" NOTE: This function contains no magic at the moment, it will simply use some
|
|
|
|
" light or dark preset colors depending on the `set background=` value.
|
2010-12-20 06:51:02 -05:00
|
|
|
"
|
|
|
|
function! indent_guides#cterm_highlight_colors()
|
2011-01-04 08:53:17 -05:00
|
|
|
let l:colors = (&g:background == 'dark') ? ['darkgrey', 'black'] : ['lightgrey', 'white']
|
2010-12-29 05:17:34 -05:00
|
|
|
|
|
|
|
exe 'hi IndentGuidesEven ctermbg=' . l:colors[0]
|
|
|
|
exe 'hi IndentGuidesOdd ctermbg=' . l:colors[1]
|
2010-12-20 06:51:02 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
"
|
|
|
|
" Automagically calculates and defines the indent highlight colors for gui
|
|
|
|
" vim.
|
|
|
|
"
|
|
|
|
function! indent_guides#gui_highlight_colors()
|
2010-12-29 05:31:49 -05:00
|
|
|
let l:hi_normal_guibg = ''
|
2010-12-20 06:51:02 -05:00
|
|
|
|
|
|
|
" capture the backgroud color from the normal highlight
|
2011-01-04 08:53:17 -05:00
|
|
|
if b:hi_normal =~ g:indent_guides_color_hex_guibg_pattern
|
2010-12-20 06:51:02 -05:00
|
|
|
" hex color code is being used, eg. '#FFFFFF'
|
2011-01-04 08:53:17 -05:00
|
|
|
let l:hi_normal_guibg = matchstr(b:hi_normal, g:indent_guides_color_hex_guibg_pattern)
|
|
|
|
|
|
|
|
elseif b:hi_normal =~ g:indent_guides_color_name_guibg_pattern
|
2010-12-20 06:51:02 -05:00
|
|
|
" color name is being used, eg. 'white'
|
2011-01-04 08:53:17 -05:00
|
|
|
let l:color_name = matchstr(b:hi_normal, g:indent_guides_color_name_guibg_pattern)
|
2010-12-29 05:31:49 -05:00
|
|
|
let l:hi_normal_guibg = color_helper#color_name_to_hex(l:color_name)
|
2010-12-20 06:51:02 -05:00
|
|
|
endif
|
|
|
|
|
2011-01-04 08:53:17 -05:00
|
|
|
if l:hi_normal_guibg =~ g:indent_guides_color_hex_pattern
|
2010-12-20 06:51:02 -05:00
|
|
|
" calculate the highlight background colors
|
2010-12-29 05:31:49 -05:00
|
|
|
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)
|
2010-12-20 06:51:02 -05:00
|
|
|
|
|
|
|
" define the new highlights
|
2010-12-29 05:31:49 -05:00
|
|
|
exe 'hi IndentGuidesOdd guibg=' . l:hi_odd_bg
|
|
|
|
exe 'hi IndentGuidesEven guibg=' . l:hi_even_bg
|
2010-12-20 06:51:02 -05:00
|
|
|
end
|
2010-12-07 07:37:50 -05:00
|
|
|
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)
|
2011-01-04 08:53:17 -05:00
|
|
|
let l:new_color = ''
|
|
|
|
let l:percent = g:indent_guides_color_change_percent / 100.0
|
2010-12-07 07:37:50 -05:00
|
|
|
|
2011-01-04 08:53:17 -05:00
|
|
|
if (&g:background == 'dark')
|
|
|
|
let l:new_color = color_helper#hex_color_lighten(a:color, l:percent)
|
|
|
|
else
|
|
|
|
let l:new_color = color_helper#hex_color_darken (a:color, l:percent)
|
|
|
|
endif
|
2010-12-07 07:37:50 -05:00
|
|
|
|
2010-12-29 05:31:49 -05:00
|
|
|
return l:new_color
|
2010-12-07 07:37:50 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
"
|
2011-01-04 08:53:17 -05:00
|
|
|
" Define default highlights.
|
2010-12-07 07:37:50 -05:00
|
|
|
"
|
2011-01-04 08:53:17 -05:00
|
|
|
function! indent_guides#define_default_highlights()
|
|
|
|
exe 'hi IndentGuidesOdd guibg=NONE ctermbg=NONE'
|
|
|
|
exe 'hi IndentGuidesEven guibg=NONE ctermbg=NONE'
|
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()
|
2011-01-04 08:53:17 -05:00
|
|
|
let w:indent_guides_matches = exists('w:indent_guides_matches') ? w:indent_guides_matches : []
|
2010-12-08 06:48:00 -05:00
|
|
|
endfunction
|
|
|
|
|
2010-12-20 07:26:45 -05:00
|
|
|
"
|
2011-01-04 08:53:17 -05:00
|
|
|
" We need to initialize these vars every time a buffer is entered while the
|
|
|
|
" plugin is enabled.
|
2010-12-20 07:26:45 -05:00
|
|
|
"
|
2011-01-04 08:53:17 -05:00
|
|
|
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')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
"
|
|
|
|
" Calculate the indent guide size. Ensures the guide size is less than or
|
|
|
|
" equal to the actual indent size, otherwise some weird things can occur.
|
|
|
|
"
|
|
|
|
" NOTE: Currently, this only works when soft-tabs are being used.
|
|
|
|
"
|
|
|
|
function! indent_guides#calculate_guide_size()
|
|
|
|
let l:guide_size = g:indent_guides_indent_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
|
|
|
|
else
|
|
|
|
let l:guide_size = b:indent_size
|
|
|
|
endif
|
|
|
|
|
|
|
|
return l:guide_size
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
"
|
|
|
|
" Gets the indent size, which depends on whether soft-tabs or hard-tabs are
|
|
|
|
" being used.
|
|
|
|
"
|
|
|
|
function! indent_guides#get_indent_size()
|
|
|
|
return (&l:expandtab == 1) ? &l:shiftwidth : 1
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
"
|
|
|
|
" Captures and returns the output of highlight group definitions.
|
|
|
|
"
|
|
|
|
" Example: indent_guides#capture_highlight('normal')
|
|
|
|
" Returns: 'Normal xxx guifg=#323232 guibg=#ffffff
|
|
|
|
"
|
|
|
|
function! indent_guides#capture_highlight(group_name)
|
|
|
|
redir => l:output
|
|
|
|
exe "silent hi " . a:group_name
|
|
|
|
redir END
|
|
|
|
|
|
|
|
return l:output
|
2010-12-20 07:26:45 -05:00
|
|
|
endfunction
|
|
|
|
|