From 17cab9c7c4d820b762b5d8e6e703a6d4bd3e30e3 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 20 Dec 2011 14:27:01 +0000 Subject: [PATCH] Filters: add cleanup_whitespace filter this filter remove trailing whitespaces on the previous line when inserting an end-of-line. --- src/filters.cc | 18 ++++++++++++++++++ src/kakrc | 1 + 2 files changed, 19 insertions(+) diff --git a/src/filters.cc b/src/filters.cc index 42fa50c7..8290d6a7 100644 --- a/src/filters.cc +++ b/src/filters.cc @@ -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")); + registry.register_factory("cleanup_whitespaces", SimpleFilterFactory("cleanup_whitespaces")); registry.register_factory("expand_tabulations", SimpleFilterFactory("expand_tabulations")); } diff --git a/src/kakrc b/src/kakrc index cd65ca56..e175a484 100644 --- a/src/kakrc +++ b/src/kakrc @@ -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