Skip to content

Commit f84d46c

Browse files
Cimbalintpeters
authored andcommitted
Delay initialization of buffer-local settings (#81)
Only set b:better_whitespace_enabled and b:strip_whitespace_on_save to values {0, 1} whenever FileType is called or if Enable/Disable/Toggle functions are called. Leave them at -1 until then, and use global (g:) variables instead. Requires encapsulating all accesses to buffer-local settings in the functions listed above (Enable/Disable/Toggle) and ShouldXXX. This allows buffers that are opened not files and whose filetype is set later to behave correctly with respect to the blacklist (fixes #75 #80).
1 parent ca47857 commit f84d46c

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

plugin/better-whitespace.vim

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ endfunction
104104

105105
" Enable the whitespace highlighting
106106
function! s:EnableWhitespace()
107-
if b:better_whitespace_enabled == 0
107+
if b:better_whitespace_enabled != 1
108108
let b:better_whitespace_enabled = 1
109109
call <SID>WhitespaceInit()
110110
call <SID>SetupAutoCommands()
@@ -114,7 +114,7 @@ endfunction
114114

115115
" Disable the whitespace highlighting
116116
function! s:DisableWhitespace()
117-
if b:better_whitespace_enabled == 1
117+
if b:better_whitespace_enabled != 0
118118
let b:better_whitespace_enabled = 0
119119
call <SID>SetupAutoCommands()
120120
call <SID>Echo("Whitespace Highlighting: Disabled")
@@ -123,7 +123,8 @@ endfunction
123123

124124
" Toggle whitespace highlighting on/off
125125
function! s:ToggleWhitespace()
126-
if b:better_whitespace_enabled == 1
126+
call <SID>Echo("Whitespace Highlighting: Toggling...")
127+
if <SID>:ShouldHighlight()
127128
call <SID>DisableWhitespace()
128129
else
129130
call <SID>EnableWhitespace()
@@ -250,22 +251,40 @@ endfunction
250251

251252
" Strips whitespace on file save
252253
function! s:ToggleStripWhitespaceOnSave()
253-
call <SID>ShouldSkipHighlight()
254-
if b:strip_whitespace_on_save == 1
254+
call <SID>Echo("Strip Whitespace On Save: Toggling...")
255+
if <SID>ShouldStripWhitespace()
255256
call <SID>DisableStripWhitespaceOnSave()
256257
else
257258
call <SID>EnableStripWhitespaceOnSave()
258259
endif
259260
endfunction
260261

261262
" Determines if whitespace highlighting should currently be skipped
262-
function! s:ShouldSkipHighlight()
263-
if !exists('b:better_whitespace_enabled')
264-
let b:better_whitespace_enabled = &buftype != 'nofile' && index(g:better_whitespace_filetypes_blacklist, &ft) == -1
263+
function! s:ShouldHighlight()
264+
call s:InitVariable('b:better_whitespace_enabled', -1)
265+
if b:better_whitespace_enabled < 0
266+
if empty(&buftype) && empty(&filetype)
267+
" We can't initialize buffer value properly yet, fall back to global one
268+
return g:better_whitespace_enabled
269+
else
270+
let b:better_whitespace_enabled = &buftype != 'nofile' &&
271+
\ index(g:better_whitespace_filetypes_blacklist, &ft) == -1
272+
endif
265273
endif
266-
if !exists('b:strip_whitespace_on_save')
267-
let b:strip_whitespace_on_save = b:better_whitespace_enabled && g:strip_whitespace_on_save
274+
return b:better_whitespace_enabled
275+
endfunction
276+
277+
function! s:ShouldStripWhitespace()
278+
call s:InitVariable('b:strip_whitespace_on_save', -1)
279+
if b:strip_whitespace_on_save < 0
280+
if b:better_whitespace_enabled < 0
281+
" We can't initialize buffer value properly yet, fall back to global one
282+
return g:strip_whitespace_on_save
283+
else
284+
let b:strip_whitespace_on_save = b:better_whitespace_enabled && g:strip_whitespace_on_save
285+
endif
268286
endif
287+
return b:strip_whitespace_on_save
269288
endfunction
270289

271290
" Run :StripWhitespace to remove end of line whitespace
@@ -316,13 +335,12 @@ if !empty(g:better_whitespace_operator)
316335
endif
317336

318337
" Process auto commands upon load, update local enabled on filetype change
319-
autocmd FileType * call <SID>ShouldSkipHighlight() | call <SID>SetupAutoCommands()
338+
autocmd FileType * call <SID>ShouldHighlight() | call <SID>SetupAutoCommands()
320339
autocmd WinEnter,BufWinEnter * call <SID>SetupAutoCommands()
321340
autocmd ColorScheme * call <SID>WhitespaceInit()
322341

323342
function! s:PerformMatchHighlight(pattern)
324-
call <SID>ShouldSkipHighlight()
325-
if b:better_whitespace_enabled == 1
343+
if <SID>ShouldHighlight()
326344
exe 'match ExtraWhitespace "' . a:pattern . '"'
327345
else
328346
match ExtraWhitespace ''
@@ -331,8 +349,7 @@ endfunction
331349

332350
function! s:PerformSyntaxHighlight(pattern)
333351
syn clear ExtraWhitespace
334-
call <SID>ShouldSkipHighlight()
335-
if b:better_whitespace_enabled == 1
352+
if <SID>ShouldHighlight()
336353
exe 'syn match ExtraWhitespace excludenl "' . a:pattern . '"'
337354
endif
338355
endfunction
@@ -392,7 +409,7 @@ function! <SID>SetupAutoCommands()
392409
endif
393410

394411
" Strip whitespace on save if enabled.
395-
if (exists('b:strip_whitespace_on_save') ? b:strip_whitespace_on_save : g:strip_whitespace_on_save) == 1
412+
if <SID>ShouldStripWhitespace()
396413
autocmd BufWritePre * call <SID>StripWhitespace( 0, line("$") )
397414
endif
398415

0 commit comments

Comments
 (0)