aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md (renamed from README.markdown)3
-rw-r--r--VERSION1
-rw-r--r--doc/strip_trailing_whitespace.txt5
-rw-r--r--plugin/strip_trailing_whitespace.vim64
4 files changed, 48 insertions, 25 deletions
diff --git a/README.markdown b/README.md
index 0fc2117..0f7d95c 100644
--- a/README.markdown
+++ b/README.md
@@ -12,12 +12,11 @@ because I could not find a plugin that did this in exactly the way I wanted:
- Provide a `<Plug>` mapping
- Provide a user command to do the stripping as well if wanted
-- Strip trailing lines as well as trailing spaces
+- Strip trailing lines as well as trailing spaces, reporting both
- Work with even very old Vim (>=6.0)
- Work with a single `undo`
- Don't move the cursor
- Don't change the search pattern
-- Don't emit output
- Don't define an `autocmd`
- Don't force a key mapping
- Don't define a global function
diff --git a/VERSION b/VERSION
new file mode 100644
index 0000000..0ea3a94
--- /dev/null
+++ b/VERSION
@@ -0,0 +1 @@
+0.2.0
diff --git a/doc/strip_trailing_whitespace.txt b/doc/strip_trailing_whitespace.txt
index 3e1807b..96635ab 100644
--- a/doc/strip_trailing_whitespace.txt
+++ b/doc/strip_trailing_whitespace.txt
@@ -1,4 +1,4 @@
-*strip_trailing_whitespace.txt* For Vim version 6.0 Last change: 2018 May 31
+*strip_trailing_whitespace.txt* For Vim version 6.0 Last change: 2018 June 10
DESCRIPTION *strip_trailing_whitespace*
@@ -13,12 +13,11 @@ because I could not find a plugin that did this in exactly the way I wanted:
- Provide a |<Plug>| mapping
- Provide a user command to do the stripping as well if wanted
-- Strip trailing lines as well as trailing spaces
+- Strip trailing lines as well as trailing spaces, reporting both
- Work with even very old Vim (>=6.0)
- Work with a single |undo|
- Don't move the cursor
- Don't change the search pattern
-- Don't emit output
- Don't define an |autocmd|
- Don't force a key mapping
- Don't define a global function
diff --git a/plugin/strip_trailing_whitespace.vim b/plugin/strip_trailing_whitespace.vim
index 1b6d2f3..7055d26 100644
--- a/plugin/strip_trailing_whitespace.vim
+++ b/plugin/strip_trailing_whitespace.vim
@@ -13,29 +13,36 @@ let g:loaded_strip_trailing_whitespace = 1
" Define function for stripping whitespace
function! s:StripTrailingWhitespace()
- " Iterating line number
- let l:li = 1
-
" Line number of last line that had non-whitespace characters on it
- let l:lw = 0
+ let l:cutoff = 0
+
+ " Tracking lines trimmed between non-whitespace lines and then totalling
+ let l:trimmed_buffer = 0
+ let l:trimmed = 0
" Line number of the file's last line
- let l:ll = line('$')
+ let l:last = line('$')
" Iterate over the lines
- while l:li <= l:ll
+ let l:li = 1
+ while l:li <= l:last
" Get the line text
let l:line = getline(l:li)
- " Replace the line with a subsitution of its text stripping extraneous
- " whitespace
- call setline(l:li, substitute(l:line, '\m\C\s\+$', '', 'g'))
+ " If the current line contains trailing whitespace, substitute it out
+ if l:line =~# '\s\+$'
+ call setline(l:li, substitute(l:line, '\s\+$', '', ''))
+ let l:trimmed_buffer = l:trimmed_buffer + 1
+ endif
- " If this line has any non-whitespace characters on it, update l:lw with
- " its index
- if l:line =~# '\m\S'
- let l:lw = l:li
+ " If this line has any non-whitespace characters on it, update our cutoff
+ " point using its index, and push the trimmed lines we've counted since
+ " the last non-whitespace line onto the trimmed total
+ if l:line =~# '\S'
+ let l:cutoff = l:li
+ let l:trimmed = l:trimmed + l:trimmed_buffer
+ let l:trimmed_buffer = 0
endif
" Increment the line counter for the next iteration
@@ -45,21 +52,38 @@ function! s:StripTrailingWhitespace()
" If the last non-whitespace line was before the last line proper, we can
" delete all lines after it
- if l:lw < l:ll
+ let l:deleted = 0
+ if l:cutoff < l:last
" Get the current line and column so we can return to it
" (Yes I know about winsaveview() and winrestview(); I want this to work
" even on very old versions of Vim if possible)
- let l:lc = line('.')
- let l:cc = col('.')
+ let l:cursor_line = line('.')
+ let l:cursor_col = col('.')
+
+ " Delete the rest of the lines, which will move the cursor
+ silent execute l:cutoff + 1 . ',$ delete _'
+
+ " Return the cursor to the saved position (Vim 6.0 fallback)
+ if exists('*cursor')
+ call cursor(l:cursor_line, l:cursor_col)
+ else
+ execute 'normal! '
+ \ . l:cursor_line . 'G'
+ \ . l:cursor_col . '|'
+ endif
- " Delete the lines, which will move the cursor
- silent execute l:lw + 1 . ',$ delete'
+ " Record the number of lines deleted
+ let l:deleted = l:last - l:cutoff
- " Return the cursor to the saved position
- call cursor(l:lc, l:cc)
endif
+ " Print what we did
+ echomsg l:trimmed . ' trimmed, ' . l:deleted . ' deleted'
+
+ " Return the number of affected lines
+ return l:trimmed + l:deleted
+
endfunction
" Create mapping proxy to the function just defined