aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2023-03-21 19:00:51 +1300
committerTom Ryder <tom@sanctum.geek.nz>2023-03-21 19:00:51 +1300
commit03782cdaf9db6329665316e1dc2d1b8270714a36 (patch)
treeac720467dd425f8e82636c73b582aa8cae035453
parentMerge branch 'release/v3.2.0' (diff)
parentMatch [:space:] and all Unicode spaces (diff)
downloadvim-strip-trailing-whitespace-master.tar.gz
vim-strip-trailing-whitespace-master.zip
Merge branch 'release/v3.3.0'HEADv3.3.0master
* release/v3.3.0: Match [:space:] and all Unicode spaces
-rw-r--r--VERSION2
-rw-r--r--autoload/strip_trailing_whitespace.vim49
-rw-r--r--doc/strip_trailing_whitespace.txt2
3 files changed, 49 insertions, 4 deletions
diff --git a/VERSION b/VERSION
index 944880f..15a2799 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-3.2.0
+3.3.0
diff --git a/autoload/strip_trailing_whitespace.vim b/autoload/strip_trailing_whitespace.vim
index f9e9a1e..17dec7a 100644
--- a/autoload/strip_trailing_whitespace.vim
+++ b/autoload/strip_trailing_whitespace.vim
@@ -1,3 +1,48 @@
+" Set the pattern for trailing horizontal whitespace to match and remove. The
+" `[:space:]` character class suffices for almost everything in practice, but
+" at the time of writing it still only includes ASCII characters. I'm writing
+" this because I had a document with lines with a trailing NO-BREAK SPACE
+" (U+00A0) which `[:space:]` doesn't catch, so we'll round out the collection
+" by adding all the Unicode space characters, since that's easy to do.
+"
+" <https://jkorpela.fi/chars/spaces.html>
+" archived: <http://web.archive.org/web/20230318105634/https://jkorpela.fi/chars/spaces.html>
+"
+" * U+0020: SPACE
+" * U+00A0: NO-BREAK SPACE
+" * U+1680: OGHAM SPACE MARK
+" * U+180E: MONGOLIAN VOWEL SEPARATOR
+" * U+2000: EN QUAD
+" * U+2001: EM QUAD
+" * U+2002: EN SPACE
+" * U+2003: EM SPACE
+" * U+2004: THREE-PER-EM SPACE
+" * U+2005: FOUR-PER-EM SPACE
+" * U+2006: SIX-PER-EM SPACE
+" * U+2007: FIGURE SPACE
+" * U+2008: PUNCTUATION SPACE
+" * U+2009: THIN SPACE
+" * U+200A: HAIR SPACE
+" * U+200B: ZERO WIDTH SPACE
+" * U+202F: NARROW NO-BREAK SPACE
+" * U+205F: MEDIUM MATHEMATICAL SPACE
+" * U+3000: IDEOGRAPHIC SPACE
+" * U+FEFF: ZERO WIDTH NO-BREAK SPACE
+"
+let s:pattern
+ \ = '['
+ \ . '[:space:]'
+ \ . '\u0020'
+ \ . '\u00A0'
+ \ . '\u1680'
+ \ . '\u180E'
+ \ . '\u2000-\u200B'
+ \ . '\u202F'
+ \ . '\u205F'
+ \ . '\u3000'
+ \ . '\uFEFF'
+ \ . ']\+$'
+
" Wrapper function to strip both horizontal and vertical trailing whitespace,
" return the cursor to its previous position, and report changes
function! strip_trailing_whitespace#(start, end) abort
@@ -46,8 +91,8 @@ function! s:StripHorizontal(start, end) abort
" If the line has trailing whitespace, strip it off and bump the count
let line = getline(num)
- if line =~# '\s\+$'
- call setline(num, substitute(line, '\s*$', '', ''))
+ if line =~# s:pattern
+ call setline(num, substitute(line, s:pattern, '', ''))
let stripped = stripped + 1
endif
diff --git a/doc/strip_trailing_whitespace.txt b/doc/strip_trailing_whitespace.txt
index dc2a324..46a5864 100644
--- a/doc/strip_trailing_whitespace.txt
+++ b/doc/strip_trailing_whitespace.txt
@@ -1,4 +1,4 @@
-*strip_trailing_whitespace.txt* For Vim version 7.0 Last change: 2019 May 25
+*strip_trailing_whitespace.txt* For Vim version 7.0 Last change: 2023 Mar 21
DESCRIPTION *strip_trailing_whitespace*