Skip to content

Commit 8bfe2ad

Browse files
committed
Global variables override by buffer variables
1 parent 9a106c7 commit 8bfe2ad

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

autoload/indent_guides.vim

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,18 @@ endfunction
1919
" other buffers and windows.
2020
"
2121
function! indent_guides#process_autocmds() abort
22-
if g:indent_guides_autocmds_enabled
22+
if indent_guides#getvar('indent_guides_autocmds_enabled')
2323
call indent_guides#enable()
2424
else
2525
call indent_guides#disable()
2626
end
2727
endfunction
2828

2929
"
30-
" Enables the indent guides for the current buffer and any other buffer upon
31-
" entering it.
30+
" Enables the indent guides for the current buffer
3231
"
3332
function! indent_guides#enable() abort
34-
let g:indent_guides_autocmds_enabled = 1
33+
let b:indent_guides_autocmds_enabled = 1
3534

3635
if &diff || indent_guides#exclude_filetype()
3736
call indent_guides#clear_matches()
@@ -49,11 +48,11 @@ function! indent_guides#enable() abort
4948
let l:column_start = (l:level - 1) * s:indent_size + 1
5049

5150
" define the higlight patterns and add to matches list
52-
if g:indent_guides_space_guides
53-
let l:soft_pattern = indent_guides#indent_highlight_pattern(g:indent_guides_soft_pattern, l:column_start, s:guide_size)
51+
if indent_guides#getvar('indent_guides_space_guides')
52+
let l:soft_pattern = indent_guides#indent_highlight_pattern(indent_guides#getvar('indent_guides_soft_pattern'), l:column_start, s:guide_size)
5453
call add(w:indent_guides_matches, matchadd(l:group, l:soft_pattern))
5554
end
56-
if g:indent_guides_tab_guides
55+
if indent_guides#getvar('indent_guides_tab_guides')
5756
let l:hard_pattern = indent_guides#indent_highlight_pattern('\t', l:column_start, s:indent_size)
5857
call add(w:indent_guides_matches, matchadd(l:group, l:hard_pattern))
5958
end
@@ -65,7 +64,7 @@ endfunction
6564
" entering it.
6665
"
6766
function! indent_guides#disable() abort
68-
let g:indent_guides_autocmds_enabled = 0
67+
let b:indent_guides_autocmds_enabled = 0
6968
call indent_guides#clear_matches()
7069
endfunction
7170

@@ -201,19 +200,19 @@ function! indent_guides#init_script_vars() abort
201200
let s:hi_normal = substitute(s:hi_normal, ' font=[A-Za-z0-9:]\+', '', '')
202201

203202
" shortcuts to the global variables - this makes the code easier to read
204-
let s:debug = g:indent_guides_debug
205-
let s:indent_levels = g:indent_guides_indent_levels
206-
let s:auto_colors = g:indent_guides_auto_colors
207-
let s:color_hex_pat = g:indent_guides_color_hex_pattern
208-
let s:color_hex_bg_pat = g:indent_guides_color_hex_guibg_pattern
209-
let s:color_name_bg_pat = g:indent_guides_color_name_guibg_pattern
210-
let s:start_level = g:indent_guides_start_level
203+
let s:debug = indent_guides#getvar('indent_guides_debug')
204+
let s:indent_levels = indent_guides#getvar('indent_guides_indent_levels')
205+
let s:auto_colors = indent_guides#getvar('indent_guides_auto_colors')
206+
let s:color_hex_pat = indent_guides#getvar('indent_guides_color_hex_pattern')
207+
let s:color_hex_bg_pat = indent_guides#getvar('indent_guides_color_hex_guibg_pattern')
208+
let s:color_name_bg_pat = indent_guides#getvar('indent_guides_color_name_guibg_pattern')
209+
let s:start_level = indent_guides#getvar('indent_guides_start_level')
211210

212211
" str2float not available in vim versions <= 7.1
213212
if has('float')
214-
let s:change_percent = g:indent_guides_color_change_percent / str2float('100.0')
213+
let s:change_percent = indent_guides#getvar('indent_guides_color_change_percent') / str2float('100.0')
215214
else
216-
let s:change_percent = g:indent_guides_color_change_percent / 100.0
215+
let s:change_percent = indent_guides#getvar('indent_guides_color_change_percent') / 100.0
217216
endif
218217

219218
if s:debug
@@ -237,7 +236,7 @@ endfunction
237236
" NOTE: Currently, this only works when soft-tabs are being used.
238237
"
239238
function! indent_guides#calculate_guide_size() abort
240-
let l:guide_size = g:indent_guides_guide_size
239+
let l:guide_size = indent_guides#getvar('indent_guides_guide_size')
241240

242241
if l:guide_size == 0 || l:guide_size > s:indent_size
243242
let l:guide_size = s:indent_size
@@ -281,15 +280,30 @@ endfunction
281280
" Detect if any of the buffer filetypes should be excluded.
282281
"
283282
function! indent_guides#exclude_filetype() abort
284-
if exists('g:indent_guides_exclude_buftype')
285-
if g:indent_guides_exclude_buftype && &buftype !=# ''
286-
return 1
287-
endif
283+
if indent_guides#getvar('indent_guides_exclude_buftype') && &buftype !=# ''
284+
return 1
288285
endif
289286
for ft in split(&ft, '\.', 1)
290-
if index(g:indent_guides_exclude_filetypes, ft) > -1
287+
if index(indent_guides#getvar('indent_guides_exclude_filetypes'), ft) > -1
291288
return 1
292289
end
293290
endfor
294291
return 0
295292
endfunction
293+
294+
"
295+
" Return variables value
296+
" Choose local buffer variable first if exist or global variable if not
297+
" return -1 if none of local buffer / global variable exists
298+
"
299+
function! indent_guides#getvar(var)
300+
let varName=a:var
301+
if (exists ("b:" . varName))
302+
exe "let retVal=b:" . varName
303+
elseif (exists ("g:" . varName))
304+
exe "let retVal=g:" . varName
305+
else
306+
exe "let retVal=-1"
307+
endif
308+
return retVal
309+
endfunction

doc/indent_guides.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ Features:~
6363
==============================================================================
6464
3. OPTIONS *indent-guides-options*
6565

66+
All options described bellow could be used at buffer level if needed without
67+
cluttering the other windows / buffers. The buffer variables could be used for
68+
example to change only settings for particular filetype with ftplugin files.
69+
70+
Example (in ~/vim/ftplugin/sh.vim):
71+
>
72+
let b:indent_guides_indent_levels = 50
73+
<
6674
------------------------------------------------------------------------------
6775
*'indent_guides_indent_levels'*
6876
Use this option to control how many indent levels to display guides for.
@@ -163,7 +171,8 @@ Default: '\s'. Values: Vim regexp.
163171

164172
------------------------------------------------------------------------------
165173
*'indent_guides_enable_on_vim_startup'*
166-
Use this option to control whether the plugin is enabled on Vim startup.
174+
Use this option to control whether the plugin is enabled on Vim startup and
175+
future buffers.
167176

168177
Default: 0. Values: 0 or 1.
169178
>

plugin/indent_guides.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ augroup indent_guides
8282
autocmd!
8383

8484
if g:indent_guides_enable_on_vim_startup
85+
let g:indent_guides_autocmds_enabled = 1
8586
autocmd VimEnter * :IndentGuidesEnable
8687
endif
8788

0 commit comments

Comments
 (0)