Skip to content

Commit 7729bad

Browse files
committed
Add support for unicode whitespace characters
1 parent 966863e commit 7729bad

File tree

3 files changed

+95
-14
lines changed

3 files changed

+95
-14
lines changed

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Vim Better Whitespace Plugin
22

3-
This plugin causes all trailing whitespace characters (spaces and tabs) to be
3+
This plugin causes all trailing whitespace characters (see [Supported Whitespace Characters](#supported-whitespace-characters) below) to be
44
highlighted. Whitespace for the current line will not be highlighted
55
while in insert mode. It is possible to disable current line highlighting while in other
66
modes as well (see options below). A helper function `:StripWhitespace` is also provided
@@ -100,7 +100,7 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
100100
This replaces the filetypes from the default list of blacklisted filetypes. The
101101
default types that are blacklisted are:
102102
```
103-
['diff', 'gitcommit', 'unite', 'qf', 'help']
103+
['diff', 'gitcommit', 'unite', 'qf', 'help', 'markdown']
104104
```
105105
If you do not want any of these filetypes unignored, simply include them in the
106106
blacklist:
@@ -114,6 +114,43 @@ Whitespace highlighting is enabled by default, with a highlight color of red.
114114
let g:better_whitespace_verbosity=1
115115
```
116116
117+
##Supported Whitespace Characters
118+
Due to the fact that the built-in whitespace character class for patterns (`\s`)
119+
only matches against tabs and spaces, this plugin defines its own list of
120+
horizontal whitepsace characters to match for both highlighting and stripping.
121+
122+
This is list should match against all ASCII and Unicode horizontal whitespace
123+
characters:
124+
```
125+
U+0009 TAB
126+
U+0020 SPACE
127+
U+00A0 NO-BREAK SPACE
128+
U+1680 OGHAM SPACE MARK
129+
U+180E MONGOLIAN VOWEL SEPARATOR
130+
U+2000 EN QUAD
131+
U+2001 EM QUAD
132+
U+2002 EN SPACE
133+
U+2003 EM SPACE
134+
U+2004 THREE-PER-EM SPACE
135+
U+2005 FOUR-PER-EM SPACE
136+
U+2006 SIX-PER-EM SPACE
137+
U+2007 FIGURE SPACE
138+
U+2008 PUNCTUATION SPACE
139+
U+2009 THIN SPACE
140+
U+200A HAIR SPACE
141+
U+200B ZERO WIDTH SPACE
142+
U+202F NARROW NO-BREAK SPACE
143+
U+205F MEDIUM MATHEMATICAL SPACE
144+
U+3000 IDEOGRAPHIC SPACE
145+
U+FEFF ZERO WIDTH NO-BREAK SPACE
146+
```
147+
148+
A file is provided with samples of each of these characters to check the plugin
149+
working with them: whitespace_examples.txt
150+
151+
If you encounter any additional whitespace characters I have missed here,
152+
please submit a pull request.
153+
117154
##Screenshots
118155
Here are a couple more screenshots of the plugin at work.
119156

plugin/better-whitespace.vim

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ call s:InitVariable('g:better_whitespace_filetypes_blacklist', default_blacklist
3939
" Disable verbosity by default
4040
call 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
4348
let 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
142147
endfunction
@@ -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()
212217
function! 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
227249
endfunction
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

whitespace_examples.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
U+0009 TAB:
2+
U+0020 SPACE:
3+
U+00A0 NO-BREAK SPACE: 
4+
U+1680 OGHAM SPACE MARK: 
5+
U+180E MONGOLIAN VOWEL SEPARATOR:᠎
6+
U+2000 EN QUAD: 
7+
U+2001 EM QUAD: 
8+
U+2002 EN SPACE: 
9+
U+2003 EM SPACE: 
10+
U+2004 THREE-PER-EM SPACE: 
11+
U+2005 FOUR-PER-EM SPACE: 
12+
U+2006 SIX-PER-EM SPACE: 
13+
U+2007 FIGURE SPACE: 
14+
U+2008 PUNCTUATION SPACE: 
15+
U+2009 THIN SPACE: 
16+
U+200A HAIR SPACE: 
17+
U+200B ZERO WIDTH SPACE:​
18+
U+202F NARROW NO-BREAK SPACE: 
19+
U+205F MEDIUM MATHEMATICAL SPACE: 
20+
U+3000 IDEOGRAPHIC SPACE: 
21+
U+FEFF ZERO WIDTH NO-BREAK SPACE:

0 commit comments

Comments
 (0)