aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md14
-rw-r--r--VERSION1
-rw-r--r--autoload/squeeze_repeat_blanks.vim44
-rw-r--r--doc/squeeze_repeat_blanks.txt37
-rw-r--r--plugin/squeeze_repeat_blanks.vim19
5 files changed, 115 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..6796209
--- /dev/null
+++ b/README.md
@@ -0,0 +1,14 @@
+squeeze\_repeat\_blanks.vim
+===============================
+
+This plugin provides a user command to reduce multiple blank lines to the last
+blank line in that group, for the given range or the whole buffer by default.
+What constitutes a "blank" line is configurable per-buffer.
+
+License
+-------
+
+Copyright (c) [Tom Ryder][1]. Distributed under the same terms as Vim itself.
+See `:help license`.
+
+[1]: https://sanctum.geek.nz/
diff --git a/VERSION b/VERSION
new file mode 100644
index 0000000..6e8bf73
--- /dev/null
+++ b/VERSION
@@ -0,0 +1 @@
+0.1.0
diff --git a/autoload/squeeze_repeat_blanks.vim b/autoload/squeeze_repeat_blanks.vim
new file mode 100644
index 0000000..5221093
--- /dev/null
+++ b/autoload/squeeze_repeat_blanks.vim
@@ -0,0 +1,44 @@
+" Reduce all groups of blank lines within range to the last line in that
+" group, deleting the others.
+function! squeeze_repeat_blanks#Squeeze(start, end) abort
+
+ " List of line numbers to delete
+ let l:deletions = []
+
+ " Flag for whether we've encountered a blank line group
+ let l:group = 0
+
+ " Pattern for what constitutes a 'blank'; configurable per-buffer
+ let l:pattern = get(b:, 'squeeze_repeat_blanks_blank')
+ \ ? b:squeeze_repeat_blanks_blank
+ \ : '^$'
+
+ " Iterate through the lines, collecting numbers to delete
+ for l:num in range(a:start, a:end)
+
+ " End a blank group if the current line doesn't match the pattern
+ if getline(l:num) !~# l:pattern
+ let l:group = 0
+
+ " If we've found a repeated blank line, flag the one before it for
+ " deletion; this way we end up with the last line of the group
+ elseif l:group
+ let l:deletions += [l:num - 1]
+
+ " If this is the first blank line, start a group
+ else
+ let l:group = 1
+ endif
+
+ endfor
+
+ " Delete each flagged line, in reverse order so that renumbering doesn't
+ " bite us
+ for l:num in reverse(l:deletions)
+ silent execute l:num . 'delete'
+ endfor
+
+ " Report how many lines were deleted
+ echomsg len(l:deletions) . ' deleted'
+
+endfunction
diff --git a/doc/squeeze_repeat_blanks.txt b/doc/squeeze_repeat_blanks.txt
new file mode 100644
index 0000000..ab2cca5
--- /dev/null
+++ b/doc/squeeze_repeat_blanks.txt
@@ -0,0 +1,37 @@
+*squeeze_repeat_blanks.txt* For Vim version 7.0 Last change: 2018 Aug 30
+
+DESCRIPTION *squeeze_repeat_blanks*
+
+This plugin provides a user command to reduce multiple blank lines to the last
+blank line in that group, for the given range or the whole buffer by default.
+What constitutes a "blank" line is configurable per-buffer.
+
+REQUIREMENTS *squeeze_repeat_blanks-requirements*
+
+This plugin is only available if 'compatible' is not set. It also requires the
+|+user_commands| feature.
+
+COMMANDS *squeeze_repeat_blanks-commands*
+
+ *:SqueezeRepeatBlanks*
+Find repeated blank lines matching the |b:squeeze_repeat_blanks_blank| pattern
+and delete all but the last line of every such group, reporting the number of
+lines removed. Accepts a range and defaults to the whole buffer.
+
+OPTIONS *squeeze_repeat_blanks-options*
+
+ *b:sequeeze_repeat_blanks_blank*
+The |buffer-variable| `b:squeeze_repeat_blanks_blank` can be set to a pattern
+that constitutes a blank line, for example including whitespace, or a comment
+or quoting leader. This might be desirable for some filetypes. The
+`:SqueezeRepeatBlanks` command defaults to using `^$`, for a true blank line.
+
+AUTHOR *squeeze_repeat_blanks-author*
+
+Written and maintained by Tom Ryder <tom@sanctum.geek.nz>.
+
+LICENSE *squeeze_repeat_blanks-license*
+
+Licensed for distribution under the same terms as Vim itself (see |license|).
+
+ vim:tw=78:ts=8:ft=help:norl:
diff --git a/plugin/squeeze_repeat_blanks.vim b/plugin/squeeze_repeat_blanks.vim
new file mode 100644
index 0000000..dcf8b0a
--- /dev/null
+++ b/plugin/squeeze_repeat_blanks.vim
@@ -0,0 +1,19 @@
+"
+" squeeze_repeat_blanks.vim: User command to reduce all groups of blank lines
+" to the last line in that group, deleting the others. Good for filtering
+" plaintext mail that's been extracted from HTML.
+"
+" Author: Tom Ryder <tom@sanctum.geek.nz>
+" License: Same as Vim itself
+"
+if exists('g:loaded_squeeze_repeat_blanks') || &compatible
+ finish
+endif
+if !has('user_commands') || v:version < 700
+ finish
+endif
+let g:loaded_squeeze_repeat_blanks = 1
+
+" User command for the above
+command! -range=% SqueezeRepeatBlanks
+ \ call squeeze_repeat_blanks#Squeeze(<line1>, <line2>)