Skip to content

Commit f2608db

Browse files
committed
Refactored some core functionality and implemented an initial version of the custom guide size feature [issue2]
1 parent 538554b commit f2608db

File tree

1 file changed

+70
-33
lines changed

1 file changed

+70
-33
lines changed

autoload/indent_guides.vim

Lines changed: 70 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,17 @@ endfunction
3232
"
3333
function! indent_guides#enable()
3434
let g:indent_guides_autocmds_enabled = 1
35+
36+
call indent_guides#init_buffer_vars()
3537
call indent_guides#highlight_colors()
3638
call indent_guides#clear_matches()
3739

3840
" loop through each indent level and define a highlight pattern
3941
" will automagically figure out whether to use tabs or spaces
4042
for l:level in range(1, g:indent_guides_indent_levels)
4143
let l:group = 'IndentGuides' . ((l:level % 2 == 0) ? 'Even' : 'Odd')
42-
let l:multiplier = (&l:expandtab == 1) ? &l:shiftwidth : 1
43-
let l:pattern = '^\s\{' . (l:level * l:multiplier - l:multiplier) . '\}\zs'
44-
let l:pattern .= '\s\{' . l:multiplier . '\}'
44+
let l:pattern = '^\s\{' . (l:level * b:indent_size - b:indent_size) . '\}\zs'
45+
let l:pattern .= '\s\{' . b:guide_size . '\}'
4546
let l:pattern .= '\ze'
4647

4748
" define the higlight pattern and add to list
@@ -93,8 +94,7 @@ endfunction
9394
" light or dark preset colors depending on the `set background=` value.
9495
"
9596
function! indent_guides#cterm_highlight_colors()
96-
let l:colors = (&g:background == 'dark') ?
97-
\ ['darkgrey', 'black'] : ['lightgrey', 'white']
97+
let l:colors = (&g:background == 'dark') ? ['darkgrey', 'black'] : ['lightgrey', 'white']
9898

9999
exe 'hi IndentGuidesEven ctermbg=' . l:colors[0]
100100
exe 'hi IndentGuidesOdd ctermbg=' . l:colors[1]
@@ -105,22 +105,20 @@ endfunction
105105
" vim.
106106
"
107107
function! indent_guides#gui_highlight_colors()
108-
let l:hi_normal = indent_guides#capture_highlight('Normal')
109-
let l:hex_pattern = 'guibg=\zs'. g:indent_guides_hex_color_pattern . '\ze'
110-
let l:name_pattern = "guibg='\\?\\zs[0-9A-Za-z ]\\+\\ze'\\?"
111108
let l:hi_normal_guibg = ''
112109

113110
" capture the backgroud color from the normal highlight
114-
if l:hi_normal =~ l:hex_pattern
111+
if b:hi_normal =~ g:indent_guides_color_hex_guibg_pattern
115112
" hex color code is being used, eg. '#FFFFFF'
116-
let l:hi_normal_guibg = matchstr(l:hi_normal, l:hex_pattern)
117-
elseif l:hi_normal =~ l:name_pattern
113+
let l:hi_normal_guibg = matchstr(b:hi_normal, g:indent_guides_color_hex_guibg_pattern)
114+
115+
elseif b:hi_normal =~ g:indent_guides_color_name_guibg_pattern
118116
" color name is being used, eg. 'white'
119-
let l:color_name = matchstr(l:hi_normal, l:name_pattern)
117+
let l:color_name = matchstr(b:hi_normal, g:indent_guides_color_name_guibg_pattern)
120118
let l:hi_normal_guibg = color_helper#color_name_to_hex(l:color_name)
121119
endif
122120

123-
if l:hi_normal_guibg =~ g:indent_guides_hex_color_pattern
121+
if l:hi_normal_guibg =~ g:indent_guides_color_hex_pattern
124122
" calculate the highlight background colors
125123
let l:hi_odd_bg = indent_guides#lighten_or_darken_color(l:hi_normal_guibg)
126124
let l:hi_even_bg = indent_guides#lighten_or_darken_color(l:hi_odd_bg)
@@ -136,42 +134,81 @@ endfunction
136134
" colorscheme is being used.
137135
"
138136
function! indent_guides#lighten_or_darken_color(color)
139-
let l:percent = g:indent_guides_color_change_percent
137+
let l:new_color = ''
138+
let l:percent = g:indent_guides_color_change_percent / 100.0
140139

141-
let l:new_color = (&g:background == 'dark') ?
142-
\ color_helper#hex_color_lighten(a:color, l:percent) :
143-
\ color_helper#hex_color_darken (a:color, l:percent)
140+
if (&g:background == 'dark')
141+
let l:new_color = color_helper#hex_color_lighten(a:color, l:percent)
142+
else
143+
let l:new_color = color_helper#hex_color_darken (a:color, l:percent)
144+
endif
144145

145146
return l:new_color
146147
endfunction
147148

148149
"
149-
" Captures and returns the output of highlight group definitions.
150-
"
151-
" Example: indent_guides#capture_highlight('normal')
152-
" Returns: 'Normal xxx guifg=#323232 guibg=#ffffff
150+
" Define default highlights.
153151
"
154-
function! indent_guides#capture_highlight(group_name)
155-
redir => l:output
156-
exe "silent hi " . a:group_name
157-
redir END
158-
159-
return l:output
152+
function! indent_guides#define_default_highlights()
153+
exe 'hi IndentGuidesOdd guibg=NONE ctermbg=NONE'
154+
exe 'hi IndentGuidesEven guibg=NONE ctermbg=NONE'
160155
endfunction
161156

162157
"
163158
" Init the w:indent_guides_matches variable.
164159
"
165160
function! indent_guides#init_matches()
166-
let w:indent_guides_matches =
167-
\ exists('w:indent_guides_matches') ? w:indent_guides_matches : []
161+
let w:indent_guides_matches = exists('w:indent_guides_matches') ? w:indent_guides_matches : []
168162
endfunction
169163

170164
"
171-
" Define default highlights.
165+
" We need to initialize these vars every time a buffer is entered while the
166+
" plugin is enabled.
172167
"
173-
function! indent_guides#define_default_highlights()
174-
exe 'hi IndentGuidesOdd guibg=NONE ctermbg=NONE'
175-
exe 'hi IndentGuidesEven guibg=NONE ctermbg=NONE'
168+
function! indent_guides#init_buffer_vars()
169+
let b:indent_size = indent_guides#get_indent_size()
170+
let b:guide_size = indent_guides#calculate_guide_size()
171+
let b:hi_normal = indent_guides#capture_highlight('Normal')
172+
endfunction
173+
174+
"
175+
" Calculate the indent guide size. Ensures the guide size is less than or
176+
" equal to the actual indent size, otherwise some weird things can occur.
177+
"
178+
" NOTE: Currently, this only works when soft-tabs are being used.
179+
"
180+
function! indent_guides#calculate_guide_size()
181+
let l:guide_size = g:indent_guides_indent_guide_size
182+
let l:indent_size = indent_guides#get_indent_size()
183+
184+
if l:indent_size > 1 && l:guide_size >= 1
185+
let l:guide_size = (l:guide_size > b:indent_size) ? b:indent_size : l:guide_size
186+
else
187+
let l:guide_size = b:indent_size
188+
endif
189+
190+
return l:guide_size
191+
endfunction
192+
193+
"
194+
" Gets the indent size, which depends on whether soft-tabs or hard-tabs are
195+
" being used.
196+
"
197+
function! indent_guides#get_indent_size()
198+
return (&l:expandtab == 1) ? &l:shiftwidth : 1
199+
endfunction
200+
201+
"
202+
" Captures and returns the output of highlight group definitions.
203+
"
204+
" Example: indent_guides#capture_highlight('normal')
205+
" Returns: 'Normal xxx guifg=#323232 guibg=#ffffff
206+
"
207+
function! indent_guides#capture_highlight(group_name)
208+
redir => l:output
209+
exe "silent hi " . a:group_name
210+
redir END
211+
212+
return l:output
176213
endfunction
177214

0 commit comments

Comments
 (0)