Skip to content

Commit

Permalink
Add normal commands to extend selections downwards/upwards.
Browse files Browse the repository at this point in the history
Since we dropped the we changed the `x` keys behaviors, the `X` and `<a-X>` have been free.

I think it makes sense to make them work mostly like the `/` command, where shift extends the selection and alt changes the direction.

So the implemented behavior is be:
<X>   : select the current and next line (extend downwards)
<a-X> : select the current and previous line (extend upwards)
  • Loading branch information
arrufat committed Jun 20, 2023
1 parent d43268f commit d9bd153
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/pages/changelog.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ released versions.

== Development version

* `<X>` and `<a-X>` to expand line selections downwards and upwards,
respectively.

* History is now stored linearly instead of in a tree

* `<a-u>` and `<a-U>` now undo selection history
Expand Down
6 changes: 6 additions & 0 deletions doc/pages/keys.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ the Shift modifier and moving will extend each selection instead.
trim selections to only contain full lines (not including last
end-of-line)

*X*::
expand selections to contain full lines (including end-of-lines) and select the next line

*<a-X>*::
expand selections to contain full lines (including end-of-lines) and select the previous line

*%*, *<percent>*::
select whole buffer

Expand Down
2 changes: 2 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ struct {
"strings (as double quoted strings)\n"
"» {+u}show-matching -previous{} switch\n"
"» {+b}<c-g>{} to cancel current operation\n"
"» {+b}<X>{} and {+b}<a-X>{} to expand line selections downwards and "
"upwards, respectively\n"
}, {
20221031,
"» {+b}<esc>{} does not end macro recording anymore, use {+b}Q{}\n"
Expand Down
3 changes: 3 additions & 0 deletions src/normal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,9 @@ static constexpr HashMap<Key, NormalCmd, MemoryDomain::Undefined, KeymapBackend>
{ {'x'}, {"extend selections to whole lines", select<SelectMode::Replace, select_lines>} },
{ {alt('x')}, {"crop selections to whole lines", select<SelectMode::Replace, trim_partial_lines>} },

{ {'X'}, {"extend selections to whole lines downwards", repeated<select<SelectMode::Replace, extend_lines_downwards>>} },
{ {alt('X')}, {"extend selections to whole lines upwards", repeated<select<SelectMode::Replace, extend_lines_upwards>>} },

{ {'m'}, {"select to matching character", select<SelectMode::Replace, select_matching<true>>} },
{ {alt('m')}, {"backward select to matching character", select<SelectMode::Replace, select_matching<false>>} },
{ {'M'}, {"extend to matching character", select<SelectMode::Extend, select_matching<true>>} },
Expand Down
32 changes: 32 additions & 0 deletions src/selectors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,38 @@ trim_partial_lines(const Context& context, const Selection& selection)
return Selection{anchor, {cursor, max_column}};
}

Optional<Selection>
extend_lines_downwards(const Context& context, const Selection& selection)
{
auto& buffer = context.buffer();
BufferCoord anchor = selection.anchor();
BufferCoord cursor = selection.cursor();
BufferCoord& to_line_start = anchor <= cursor ? anchor : cursor;
BufferCoord& to_line_end = anchor <= cursor ? cursor : anchor;

to_line_start.column = 0;
to_line_end.column = buffer[to_line_end.line].length()-1;
to_line_end.line = std::min(to_line_end.line + 1, buffer.line_count() - 1);

return Selection{anchor, {cursor, max_column}};
}

Optional<Selection>
extend_lines_upwards(const Context& context, const Selection& selection)
{
auto& buffer = context.buffer();
BufferCoord anchor = selection.anchor();
BufferCoord cursor = selection.cursor();
BufferCoord& to_line_start = anchor <= cursor ? anchor : cursor;
BufferCoord& to_line_end = anchor <= cursor ? cursor : anchor;

to_line_start.column = 0;
to_line_start.line = std::max(to_line_start.line - 1, LineCount(0));
to_line_end.column = buffer[to_line_end.line].length()-1;

return Selection{anchor, {cursor, max_column}};
}

static RegexExecFlags
match_flags(const Buffer& buf, const BufferIterator& begin, const BufferIterator& end)
{
Expand Down
6 changes: 6 additions & 0 deletions src/selectors.hh
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ select_lines(const Context& context, const Selection& selection);
Optional<Selection>
trim_partial_lines(const Context& context, const Selection& selection);

Optional<Selection>
extend_lines_downwards(const Context& context, const Selection& selection);

Optional<Selection>
extend_lines_upwards(const Context& context, const Selection& selection);

enum class RegexMode;

template<RegexMode mode>
Expand Down

0 comments on commit d9bd153

Please sign in to comment.