Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add normal commands to extend selections downwards/upwards. #4929

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/pages/changelog.asciidoc
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
8 changes: 8 additions & 0 deletions doc/pages/keys.asciidoc
Expand Up @@ -168,6 +168,14 @@ 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 extend
the selected block one line downwards

*<a-X>*::
expand selections to contain full lines (including end-of-lines) and extend
the selected block one line upwards

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

Expand Down
2 changes: 2 additions & 0 deletions src/main.cc
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
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
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
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