diff --git a/README.asciidoc b/README.asciidoc index d4c32374..4be677ec 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -59,6 +59,8 @@ Basic Movement * _x_: select line on which selection end lies (or next line when end lies on an end-of-line) * _alt-x_: expand selections to contain full lines (including end-of-lines) + * _alt-X_: trim selections to only contain full lines (not including last + end-of-line) * _%_: select whole buffer diff --git a/src/normal.cc b/src/normal.cc index 3897c0fc..b41561e3 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -759,6 +759,7 @@ KeyMap keymap = { { Key::Modifiers::None, 'x' }, repeated(select(select_line)) }, { { Key::Modifiers::None, 'X' }, repeated(select(select_line)) }, { { Key::Modifiers::Alt, 'x' }, select(select_whole_lines) }, + { { Key::Modifiers::Alt, 'X' }, select(trim_partial_lines) }, { { Key::Modifiers::None, 'm' }, select(select_matching) }, { { Key::Modifiers::None, 'M' }, select(select_matching) }, diff --git a/src/selectors.cc b/src/selectors.cc index f62e5a78..5a8fcc07 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -379,6 +379,22 @@ Selection select_whole_lines(const Selection& selection) return Selection(first, last); } +Selection trim_partial_lines(const Selection& selection) +{ + // same as select_whole_lines + BufferIterator first = selection.first(); + BufferIterator last = selection.last(); + BufferIterator& to_line_start = first <= last ? first : last; + BufferIterator& to_line_end = first <= last ? last : first; + + while (not is_begin(to_line_start) and *(to_line_start-1) != '\n') + ++to_line_start; + while (*(to_line_end+1) != '\n' and to_line_end != to_line_start) + --to_line_end; + + return Selection(first, last); +} + Selection select_whole_buffer(const Selection& selection) { const Buffer& buffer = selection.first().buffer(); diff --git a/src/selectors.hh b/src/selectors.hh index 9c87c73c..6bc85627 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -29,6 +29,7 @@ template Selection select_whole_word(const Selection& selection, bool inner); Selection select_whole_lines(const Selection& selection); Selection select_whole_buffer(const Selection& selection); +Selection trim_partial_lines(const Selection& selection); template Selection select_next_match(const Selection& selection, const Regex& regex);