Skip to content

Commit c10c09c

Browse files
committed
Use matchadd() instead of :match
Frees up :match for other uses, as seen in #98, also less :exe. A single matchadd() id should be used because matchadd() goes over from one buffer to the next, so creating a new id causes matchadd/matchdelete calls to not work as expected. It seems that using the same match id across buffers does not seem to interfere with other open buffers, neither in tabs nor splits.
1 parent 904c931 commit c10c09c

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

plugin/better-whitespace.vim

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,26 +161,25 @@ if g:current_line_whitespace_disabled_soft == 1
161161
else
162162
" Match Whitespace on all lines
163163
function! s:HighlightEOLWhitespace()
164+
call <SID>ClearHighlighting()
164165
if <SID>ShouldHighlight()
165-
exe 'match ExtraWhitespace "' . s:eol_whitespace_pattern . '"'
166-
else
167-
call <SID>ClearHighlighting()
166+
let s:match_id = matchadd('ExtraWhitespace', s:eol_whitespace_pattern, 10, get(s:, 'match_id', -1))
168167
endif
169168
endfunction
170169

171170
" Match Whitespace on all lines except the current one
172171
function! s:HighlightEOLWhitespaceExceptCurrentLine()
172+
call <SID>ClearHighlighting()
173173
if <SID>ShouldHighlight()
174-
exe 'match ExtraWhitespace "\%<' . line('.') . 'l' . s:eol_whitespace_pattern .
175-
\ '\|\%>' . line('.') . 'l' . s:eol_whitespace_pattern . '"'
176-
else
177-
call <SID>ClearHighlighting()
174+
let s:match_id = matchadd('ExtraWhitespace',
175+
\ '\%<' . line('.') . 'l' . s:eol_whitespace_pattern .
176+
\ '\|\%>' . line('.') . 'l' . s:eol_whitespace_pattern, 10, get(s:, 'match_id', -1))
178177
endif
179178
endfunction
180179

181180
" Remove Whitespace matching
182181
function! s:ClearHighlighting()
183-
match ExtraWhitespace ''
182+
silent! call matchdelete(get(s:, 'match_id', -1))
184183
endfunction
185184
endif
186185

0 commit comments

Comments
 (0)