@@ -39,6 +39,11 @@ call s:InitVariable('g:better_whitespace_filetypes_blacklist', default_blacklist
3939" Disable verbosity by default
4040call s: InitVariable (' g:better_whitespace_verbosity' , 0 )
4141
42+ " Define custom whitespace character group to include all horizontal unicode
43+ " whitespace characters. Vim's '\s' class only includes ASCII spaces and tabs.
44+ let s: whitespace_group= ' [\u0009\u0020\u00a0\u1680\u180e\u2000-\u200b\u202f\u205f\u3000\ufeff]'
45+ let s: eol_whitespace_pattern = s: whitespace_group . ' \+$'
46+
4247" Only init once
4348let s: better_whitespace_initialized = 0
4449
@@ -117,7 +122,7 @@ function! s:CurrentLineWhitespaceOff( level )
117122 if a: level == ' hard'
118123 let g: current_line_whitespace_disabled_hard = 1
119124 let g: current_line_whitespace_disabled_soft = 0
120- call s: InAllWindows (' syn clear ExtraWhitespace | match ExtraWhitespace /\s\+$/ ' )
125+ call s: InAllWindows (' syn clear ExtraWhitespace | match ExtraWhitespace " ' . s: eol_whitespace_pattern . ' " ' )
121126 call <SID> Echo (" Current Line Hightlight Off (hard)" )
122127 elseif a: level == ' soft'
123128 let g: current_line_whitespace_disabled_soft = 1
@@ -136,7 +141,7 @@ function! s:CurrentLineWhitespaceOn()
136141 let g: current_line_whitespace_disabled_hard = 0
137142 let g: current_line_whitespace_disabled_soft = 0
138143 call <SID> SetupAutoCommands ()
139- call s: InAllWindows (' syn clear ExtraWhitespace | match ExtraWhitespace /\s\+$/ ' )
144+ call s: InAllWindows (' syn clear ExtraWhitespace | match ExtraWhitespace " ' . s: eol_whitespace_pattern . ' " ' )
140145 call <SID> Echo (" Current Line Hightlight On" )
141146 endif
142147endfunction
@@ -149,7 +154,7 @@ function! s:StripWhitespace( line1, line2 )
149154 let c = col (" ." )
150155
151156 " Strip the whitespace
152- silent ! execute ' :' . a: line1 . ' ,' . a: line2 . ' s/\s\+$ //e'
157+ silent ! execute ' :' . a: line1 . ' ,' . a: line2 . ' s/' . s: eol_whitespace_pattern . ' //e'
153158
154159 " Restore the saved search and cursor position
155160 let @/ = _s
@@ -212,7 +217,7 @@ autocmd ColorScheme * call <SID>WhitespaceInit()
212217function ! s: PerformMatchHighlight (pattern)
213218 call s: InitVariable (' b:better_whitespace_enabled' , ! <SID> ShouldSkipHighlight ())
214219 if b: better_whitespace_enabled == 1
215- exe ' match ExtraWhitespace ' . a: pattern
220+ exe ' match ExtraWhitespace " ' . a: pattern . ' " '
216221 else
217222 match ExtraWhitespace ' '
218223 endif
@@ -222,7 +227,24 @@ function! s:PerformSyntaxHighlight(pattern)
222227 syn clear ExtraWhitespace
223228 call s: InitVariable (' b:better_whitespace_enabled' , ! <SID> ShouldSkipHighlight ())
224229 if b: better_whitespace_enabled == 1
225- exe ' syn match ExtraWhitespace excludenl ' . a: pattern
230+ exe ' syn match ExtraWhitespace excludenl "' . a: pattern . ' "'
231+ endif
232+ endfunction
233+
234+ function ! s: HighlightEOLWhitespace (type )
235+ if (a: type == ' match' )
236+ call s: PerformMatchHighlight (s: eol_whitespace_pattern )
237+ elseif (a: type == ' syntax' )
238+ call s: PerformSyntaxHighlight (s: eol_whitespace_pattern )
239+ endif
240+ endfunction
241+
242+ function ! s: HighlightEOLWhitespaceExceptCurrentLine (type )
243+ let a: exclude_current_line_eol_whitespace_pattern = ' \%<' . line (" ." ) . ' l' . s: eol_whitespace_pattern . ' \|\%>' . line (" ." ) . ' l' . s: eol_whitespace_pattern
244+ if (a: type == ' match' )
245+ call s: PerformMatchHighlight (a: exclude_current_line_eol_whitespace_pattern )
246+ elseif (a: type == ' syntax' )
247+ call s: PerformSyntaxHighlight (a: exclude_current_line_eol_whitespace_pattern )
226248 endif
227249endfunction
228250
@@ -237,28 +259,29 @@ function! <SID>SetupAutoCommands()
237259 call <SID> WhitespaceInit ()
238260 endif
239261
262+
240263 " Check if current line is disabled softly
241264 if g: current_line_whitespace_disabled_soft == 0
242265 " Highlight all whitespace upon entering buffer
243- call <SID> PerformMatchHighlight (' /\s\+$/ ' )
266+ call <SID> PerformMatchHighlight (s: eol_whitespace_pattern )
244267 " Check if current line highglighting is disabled
245268 if g: current_line_whitespace_disabled_hard == 1
246269 " Never highlight whitespace on current line
247- autocmd InsertEnter ,CursorMoved ,CursorMovedI * call <SID> PerformMatchHighlight ( ' /\%< ' . line ( " . " ) . ' l\s\+$\|\%> ' . line ( " . " ) . ' l\s\+$/ ' )
270+ autocmd InsertEnter ,CursorMoved ,CursorMovedI * call <SID> HighlightEOLWhitespaceExceptCurrentLine ( ' match ' )
248271 else
249272 " When in insert mode, do not highlight whitespace on the current line
250- autocmd InsertEnter ,CursorMovedI * call <SID> PerformMatchHighlight ( ' /\%< ' . line ( " . " ) . ' l\s\+$\|\%> ' . line ( " . " ) . ' l\s\+$/ ' )
273+ autocmd InsertEnter ,CursorMovedI * call <SID> HighlightEOLWhitespaceExceptCurrentLine ( ' match ' )
251274 endif
252275 " Highlight all whitespace when exiting insert mode
253- autocmd InsertLeave ,BufReadPost * call <SID> PerformMatchHighlight ( ' /\s\+$/ ' )
276+ autocmd InsertLeave ,BufReadPost * call <SID> HighlightEOLWhitespace ( ' match ' )
254277 " Clear whitespace highlighting when leaving buffer
255278 autocmd BufWinLeave * match ExtraWhitespace ' '
256279 else
257280 " Highlight extraneous whitespace at the end of lines, but not the
258281 " current line.
259- call <SID> PerformSyntaxHighlight ( ' /\s\+$/ ' )
260- autocmd InsertEnter * call <SID> PerformSyntaxHighlight ( ' /\s\+\%#\@!$/ ' )
261- autocmd InsertLeave ,BufReadPost * call <SID> PerformSyntaxHighlight ( ' /\s\+$/ ' )
282+ call <SID> HighlightEOLWhitespace ( ' syntax ' )
283+ autocmd InsertEnter * call <SID> HighlightEOLWhitespaceExceptCurrentLine ( ' syntax ' )
284+ autocmd InsertLeave ,BufReadPost * call <SID> HighlightEOLWhitespace ( ' syntax ' )
262285 endif
263286 endif
264287
0 commit comments