Skip to content

Commit eb3902e

Browse files
committed
Handle enabling/disabling across all windows
Fixes: #22
1 parent fb5b0b8 commit eb3902e

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

plugin/better-whitespace.vim

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ call s:InitVariable('g:better_whitespace_filetypes_blacklist', [])
3838
" Only init once
3939
let s:better_whitespace_initialized = 0
4040

41+
" Like windo but restore the current window.
42+
function! s:Windo(command)
43+
let currwin=winnr()
44+
execute 'windo ' . a:command
45+
execute currwin . 'wincmd w'
46+
endfunction
47+
48+
" Like tabdo but restore the current tab.
49+
function! s:Tabdo(command)
50+
let currTab=tabpagenr()
51+
execute 'tabdo ' . a:command
52+
execute 'tabn ' . currTab
53+
endfunction
54+
55+
" Execute command in all windows (across tabs).
56+
function! s:InAllWindows(command)
57+
call s:Tabdo("call s:Windo('".substitute(a:command, "'", "''", 'g')."')")
58+
endfunction
59+
4160
" Ensure the 'ExtraWhitespace' highlight group has been defined
4261
function! s:WhitespaceInit()
4362
" Check if the user has already defined highlighting for this group
@@ -53,7 +72,7 @@ function! s:EnableWhitespace()
5372
let g:better_whitespace_enabled = 1
5473
call <SID>WhitespaceInit()
5574
" Match default whitespace
56-
match ExtraWhitespace /\s\+$/
75+
call s:InAllWindows('match ExtraWhitespace /\s\+$/')
5776
call <SID>SetupAutoCommands()
5877
echo "Whitespace Highlighting: Enabled"
5978
endif
@@ -64,8 +83,7 @@ function! s:DisableWhitespace()
6483
if g:better_whitespace_enabled == 1
6584
let g:better_whitespace_enabled = 0
6685
" Clear current whitespace matches
67-
match ExtraWhitespace ''
68-
syn clear ExtraWhitespace
86+
call s:InAllWindows("match ExtraWhitespace '' | syn clear ExtraWhitespace")
6987
call <SID>SetupAutoCommands()
7088
echo "Whitespace Highlighting: Disabled"
7189
endif
@@ -92,13 +110,12 @@ function! s:CurrentLineWhitespaceOff( level )
92110
if a:level == 'hard'
93111
let g:current_line_whitespace_disabled_hard = 1
94112
let g:current_line_whitespace_disabled_soft = 0
95-
syn clear ExtraWhitespace
96-
match ExtraWhitespace /\s\+$/
113+
call s:InAllWindows('syn clear ExtraWhitespace | match ExtraWhitespace /\s\+$/')
97114
echo "Current Line Hightlight Off (hard)"
98115
elseif a:level == 'soft'
99116
let g:current_line_whitespace_disabled_soft = 1
100117
let g:current_line_whitespace_disabled_hard = 0
101-
match ExtraWhitespace ''
118+
call s:InAllWindows("match ExtraWhitespace ''")
102119
echo "Current Line Hightlight Off (soft)"
103120
endif
104121
" Re-run auto commands with the new settings
@@ -112,8 +129,7 @@ function! s:CurrentLineWhitespaceOn()
112129
let g:current_line_whitespace_disabled_hard = 0
113130
let g:current_line_whitespace_disabled_soft = 0
114131
call <SID>SetupAutoCommands()
115-
syn clear ExtraWhitespace
116-
match ExtraWhitespace /\s\+$/
132+
call s:InAllWindows('syn clear ExtraWhitespace | match ExtraWhitespace /\s\+$/')
117133
echo "Current Line Hightlight On"
118134
endif
119135
endfunction
@@ -198,8 +214,8 @@ function! <SID>SetupAutoCommands()
198214
autocmd BufWinLeave * call clearmatches()
199215
else
200216
" Highlight extraneous whitespace at the end of lines, but not the
201-
" current line
202-
syn clear ExtraWhitespace | syn match ExtraWhitespace excludenl /\s\+$/
217+
" current line.
218+
call s:InAllWindows('syn clear ExtraWhitespace | syn match ExtraWhitespace excludenl /\s\+$/')
203219
autocmd InsertEnter * syn clear ExtraWhitespace | syn match ExtraWhitespace excludenl /\s\+\%#\@!$/ containedin=ALL
204220
autocmd InsertLeave,BufReadPost * syn clear ExtraWhitespace | syn match ExtraWhitespace excludenl /\s\+$/ containedin=ALL
205221
endif

0 commit comments

Comments
 (0)