diff --git a/README.asciidoc b/README.asciidoc index 6fac63d5..add67f68 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -339,6 +339,7 @@ object you want. * _W_: select the whole WORD * _s_: select the sentence * _p_: select the paragraph + * _␣_: select the whitespaces * _i_: select the current indentation block * _n_: select the number diff --git a/src/normal.cc b/src/normal.cc index 316f657f..be670397 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -839,6 +839,7 @@ void select_object(Context& context, int param) { 'W', select_word }, { 's', select_sentence }, { 'p', select_paragraph }, + { ' ', select_whitespaces }, { 'i', select_indent }, { 'n', select_number }, }; @@ -880,6 +881,7 @@ void select_object(Context& context, int param) "W: WORD \n" "s: sentence \n" "p: paragraph \n" + "␣: whitespaces \n" "i: indent \n"); } diff --git a/src/selectors.cc b/src/selectors.cc index ca85142a..bd1acc01 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -377,6 +377,35 @@ static CharCount get_indent(const String& str, int tabstop) return indent; } +Selection select_whitespaces(const Buffer& buffer, const Selection& selection, ObjectFlags flags) +{ + auto is_whitespace = [&](char c) { + return c == ' ' or c == '\t' or + (not (flags & ObjectFlags::Inner) and c == '\n'); + }; + BufferIterator first = buffer.iterator_at(selection.cursor()); + BufferIterator last = first; + if (flags & ObjectFlags::ToBegin) + { + if (is_whitespace(*first)) + { + skip_while_reverse(first, buffer.begin(), is_whitespace); + if (not is_whitespace(*first)) + ++first; + } + } + if (flags & ObjectFlags::ToEnd) + { + if (is_whitespace(*last)) + { + skip_while(last, buffer.end(), is_whitespace); + --last; + } + } + return (flags & ObjectFlags::ToEnd) ? utf8_range(first, last) + : utf8_range(last, first); +} + static bool is_only_whitespaces(const String& str) { auto it = str.begin(); diff --git a/src/selectors.hh b/src/selectors.hh index 7e86a738..d9e5f770 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -212,6 +212,10 @@ Selection select_paragraph(const Buffer& buffer, const Selection& selection, ObjectFlags flags); +Selection select_whitespaces(const Buffer& buffer, + const Selection& selection, + ObjectFlags flags); + Selection select_indent(const Buffer& buffer, const Selection& selection, ObjectFlags flags);