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
|
|
|
|
2013-03-06 17:59:56 -05:00
|
|
|
if &diff || indent_guides#exclude_filetype()
|
2013-03-06 06:57:33 -05:00
|
|
|
call indent_guides#clear_matches()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2011-01-04 23:07:53 -05:00
|
|
|
call indent_guides#init_script_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
|
2011-01-09 05:53:48 -05:00
|
|
|
for l:level in range(s:start_level, s:indent_levels)
|
2011-03-06 08:31:54 -05:00
|
|
|
let l:group = 'IndentGuides' . ((l:level % 2 == 0) ? 'Even' : 'Odd')
|
2011-03-13 06:29:27 -04:00
|
|
|
let l:column_start = (l:level - 1) * s:indent_size + 1
|
2012-10-11 08:51:43 -04:00
|
|
|
let l:soft_pattern = indent_guides#indent_highlight_pattern(g:indent_guides_soft_pattern, l:column_start, s:guide_size)
|
2011-03-13 06:29:27 -04:00
|
|
|
let l:hard_pattern = indent_guides#indent_highlight_pattern('\t', l:column_start, s:indent_size)
|
2011-03-06 08:31:54 -05:00
|
|
|
|
2011-03-13 06:29:27 -04:00
|
|
|
" define the higlight patterns and add to matches list
|
2011-07-31 21:11:04 -04:00
|
|
|
if g:indent_guides_space_guides
|
|
|
|
call add(w:indent_guides_matches, matchadd(l:group, l:soft_pattern))
|
|
|
|
end
|
2011-03-06 08:31:54 -05:00
|
|
|
call add(w:indent_guides_matches, matchadd(l:group, l:hard_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
|
2011-09-28 20:19:41 -04:00
|
|
|
try
|
|
|
|
call matchdelete(l:match_id)
|
|
|
|
catch /E803:/
|
|
|
|
" Do nothing
|
|
|
|
endtry
|
2010-12-29 05:31:49 -05:00
|
|
|
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()
|
2011-01-04 23:07:53 -05:00
|
|
|
if s:auto_colors
|
2010-12-20 06:51:02 -05:00
|
|
|
if has('gui_running')
|
|
|
|
call indent_guides#gui_highlight_colors()
|
|
|
|
else
|
2011-01-16 22:13:50 -05:00
|
|
|
call indent_guides#basic_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
|
|
|
"
|
2011-01-16 22:13:50 -05:00
|
|
|
" Defines some basic indent highlight colors that work for Terminal Vim and
|
|
|
|
" gVim when colors can't be automatically calculated.
|
2010-12-29 05:17:34 -05:00
|
|
|
"
|
2011-01-16 22:13:50 -05:00
|
|
|
function! indent_guides#basic_highlight_colors()
|
|
|
|
let l:cterm_colors = (&g:background == 'dark') ? ['darkgrey', 'black'] : ['lightgrey', 'white']
|
|
|
|
let l:gui_colors = (&g:background == 'dark') ? ['grey15', 'grey30'] : ['grey70', 'grey85']
|
2010-12-29 05:17:34 -05:00
|
|
|
|
2012-07-10 08:14:26 -04:00
|
|
|
exe 'hi IndentGuidesEven guibg=' . l:gui_colors[0] . ' guifg=' . l:gui_colors[1] . ' ctermbg=' . l:cterm_colors[0] . ' ctermfg=' . l:cterm_colors[1]
|
|
|
|
exe 'hi IndentGuidesOdd guibg=' . l:gui_colors[1] . ' guifg=' . l:gui_colors[0] . ' ctermbg=' . l:cterm_colors[1] . ' ctermfg=' . l:cterm_colors[0]
|
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 23:07:53 -05:00
|
|
|
if s:hi_normal =~ s:color_hex_bg_pat
|
2010-12-20 06:51:02 -05:00
|
|
|
" hex color code is being used, eg. '#FFFFFF'
|
2011-01-04 23:07:53 -05:00
|
|
|
let l:hi_normal_guibg = matchstr(s:hi_normal, s:color_hex_bg_pat)
|
2011-01-04 08:53:17 -05:00
|
|
|
|
2011-01-04 23:07:53 -05:00
|
|
|
elseif s:hi_normal =~ s:color_name_bg_pat
|
2010-12-20 06:51:02 -05:00
|
|
|
" color name is being used, eg. 'white'
|
2011-01-04 23:07:53 -05:00
|
|
|
let l:color_name = matchstr(s:hi_normal, s:color_name_bg_pat)
|
2010-12-29 05:31:49 -05:00
|
|
|
let l:hi_normal_guibg = color_helper#color_name_to_hex(l:color_name)
|
2011-01-16 22:13:50 -05:00
|
|
|
|
|
|
|
else
|
2011-01-17 05:35:37 -05:00
|
|
|
" background color could not be detected, default to basic colors
|
2011-01-16 22:13:50 -05:00
|
|
|
call indent_guides#basic_highlight_colors()
|
2010-12-20 06:51:02 -05:00
|
|
|
endif
|
|
|
|
|
2011-01-04 23:07:53 -05:00
|
|
|
if l:hi_normal_guibg =~ s:color_hex_pat
|
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
|
2012-07-10 08:14:26 -04:00
|
|
|
exe 'hi IndentGuidesOdd guibg=' . l:hi_odd_bg . ' guifg=' . l:hi_even_bg
|
|
|
|
exe 'hi IndentGuidesEven guibg=' . l:hi_even_bg . ' guifg=' . l:hi_odd_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 = ''
|
2010-12-07 07:37:50 -05:00
|
|
|
|
2011-01-04 08:53:17 -05:00
|
|
|
if (&g:background == 'dark')
|
2011-01-04 23:07:53 -05:00
|
|
|
let l:new_color = color_helper#hex_color_lighten(a:color, s:change_percent)
|
2011-01-04 08:53:17 -05:00
|
|
|
else
|
2011-01-04 23:07:53 -05:00
|
|
|
let l:new_color = color_helper#hex_color_darken (a:color, s:change_percent)
|
2011-01-04 08:53:17 -05:00
|
|
|
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()
|
2011-02-21 08:29:30 -05:00
|
|
|
hi default clear IndentGuidesOdd
|
|
|
|
hi default clear IndentGuidesEven
|
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 23:07:53 -05:00
|
|
|
function! indent_guides#init_script_vars()
|
2014-02-16 21:07:37 -05:00
|
|
|
if &l:shiftwidth > 0
|
|
|
|
let s:indent_size = &l:shiftwidth
|
|
|
|
else
|
|
|
|
let s:indent_size = &l:tabstop
|
|
|
|
endif
|
2011-01-04 23:07:53 -05:00
|
|
|
let s:guide_size = indent_guides#calculate_guide_size()
|
|
|
|
let s:hi_normal = indent_guides#capture_highlight('Normal')
|
|
|
|
|
2011-01-24 07:23:32 -05:00
|
|
|
" remove 'font=<value>' from the s:hi_normal string (only seems to happen on Vim startup in Windows)
|
|
|
|
let s:hi_normal = substitute(s:hi_normal, ' font=[A-Za-z0-9:]\+', "", "")
|
|
|
|
|
2011-01-04 23:07:53 -05:00
|
|
|
" 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: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
|
2011-01-09 05:53:48 -05:00
|
|
|
let s:start_level = g:indent_guides_start_level
|
2011-01-04 23:07:53 -05:00
|
|
|
|
2011-10-19 22:36:39 -04:00
|
|
|
" str2float not available in vim versions <= 7.1
|
|
|
|
if has('float')
|
|
|
|
let s:change_percent = g:indent_guides_color_change_percent / str2float('100.0')
|
|
|
|
else
|
|
|
|
let s:change_percent = g:indent_guides_color_change_percent / 100.0
|
|
|
|
endif
|
|
|
|
|
2011-01-04 23:07:53 -05:00
|
|
|
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
|
2011-01-09 05:53:48 -05:00
|
|
|
echo 's:start_level = ' . s:start_level
|
2011-01-04 23:07:53 -05:00
|
|
|
endif
|
2011-01-04 08:53:17 -05:00
|
|
|
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()
|
2011-03-06 08:31:54 -05:00
|
|
|
let l:guide_size = g:indent_guides_guide_size
|
2011-01-04 08:53:17 -05:00
|
|
|
|
2011-03-06 08:48:08 -05:00
|
|
|
if l:guide_size == 0 || l:guide_size > s:indent_size
|
2011-01-04 23:07:53 -05:00
|
|
|
let l:guide_size = s:indent_size
|
2011-01-04 08:53:17 -05:00
|
|
|
endif
|
|
|
|
|
|
|
|
return l:guide_size
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
"
|
|
|
|
" Captures and returns the output of highlight group definitions.
|
|
|
|
"
|
|
|
|
" Example: indent_guides#capture_highlight('normal')
|
2013-03-06 07:03:16 -05:00
|
|
|
" Returns: 'Normal xxx guifg=#323232 guibg=#ffffff'
|
2011-01-04 08:53:17 -05:00
|
|
|
"
|
|
|
|
function! indent_guides#capture_highlight(group_name)
|
|
|
|
redir => l:output
|
|
|
|
exe "silent hi " . a:group_name
|
|
|
|
redir END
|
|
|
|
|
2011-01-04 23:07:53 -05:00
|
|
|
let l:output = substitute(l:output, "\n", "", "")
|
2011-01-04 08:53:17 -05:00
|
|
|
return l:output
|
2010-12-20 07:26:45 -05:00
|
|
|
endfunction
|
|
|
|
|
2011-03-13 06:29:27 -04:00
|
|
|
"
|
|
|
|
" Returns a regex pattern for highlighting an indent level.
|
|
|
|
"
|
|
|
|
" Example: indent_guides#indent_highlight_pattern(' ', 1, 4)
|
|
|
|
" Returns: /^ *\%1v\zs *\%5v\ze/
|
|
|
|
"
|
|
|
|
" Example: indent_guides#indent_highlight_pattern('\s', 5, 2)
|
|
|
|
" Returns: /^\s*\%5v\zs\s*\%7v\ze/
|
|
|
|
"
|
|
|
|
" Example: indent_guides#indent_highlight_pattern('\t', 9, 2)
|
|
|
|
" Returns: /^\t*\%9v\zs\t*\%11v\ze/
|
|
|
|
"
|
|
|
|
function! indent_guides#indent_highlight_pattern(indent_pattern, column_start, indent_size)
|
|
|
|
let l:pattern = '^' . a:indent_pattern . '*\%' . a:column_start . 'v\zs'
|
|
|
|
let l:pattern .= a:indent_pattern . '*\%' . (a:column_start + a:indent_size) . 'v'
|
|
|
|
let l:pattern .= '\ze'
|
|
|
|
return l:pattern
|
|
|
|
endfunction
|
2013-03-06 06:57:33 -05:00
|
|
|
|
|
|
|
"
|
|
|
|
" Detect if any of the buffer filetypes should be excluded.
|
|
|
|
"
|
|
|
|
function! indent_guides#exclude_filetype()
|
2013-03-13 19:55:58 -04:00
|
|
|
for ft in split(&ft, '\.')
|
2013-03-06 06:57:33 -05:00
|
|
|
if index(g:indent_guides_exclude_filetypes, ft) > -1
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
endfor
|
|
|
|
return 0
|
|
|
|
endfunction
|