Filters: add cleanup_whitespace filter

this filter remove trailing whitespaces on the previous line
when inserting an end-of-line.
This commit is contained in:
Maxime Coste 2011-12-20 14:27:01 +00:00
parent e77f58c010
commit 17cab9c7c4
2 changed files with 19 additions and 0 deletions

View File

@ -20,6 +20,23 @@ void preserve_indent(Buffer& buffer, Modification& modification)
}
}
void cleanup_whitespaces(Buffer& buffer, Modification& modification)
{
if (modification.type == Modification::Insert and
modification.content[0] == '\n' and not modification.position.is_begin())
{
BufferIterator position = modification.position-1;
while (*position == ' ' or *position == '\t' and not position.is_begin())
--position;
++position;
if (position != modification.position)
{
buffer.modify(Modification::make_erase(position, modification.position));
modification.position = position;
}
}
}
void expand_tabulations(Buffer& buffer, Modification& modification)
{
const int tabstop = 8;
@ -63,6 +80,7 @@ void register_filters()
FilterRegistry& registry = FilterRegistry::instance();
registry.register_factory("preserve_indent", SimpleFilterFactory<preserve_indent>("preserve_indent"));
registry.register_factory("cleanup_whitespaces", SimpleFilterFactory<cleanup_whitespaces>("cleanup_whitespaces"));
registry.register_factory("expand_tabulations", SimpleFilterFactory<expand_tabulations>("expand_tabulations"));
}

View File

@ -1,3 +1,4 @@
hook WinCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) addhl hlcpp
hook WinCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) addfilter preserve_indent
hook WinCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) addfilter cleanup_whitespaces
hook WinCreate .* addhl regex \h+(?=\n) default red