Skip to content

Commit

Permalink
Preserve selection direction and improve entering line editing
Browse files Browse the repository at this point in the history
  • Loading branch information
dpc committed Nov 26, 2018
1 parent e5c43f3 commit 33c3dee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/normal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2221,7 +2221,7 @@ static const HashMap<Key, NormalCmd, MemoryDomain::Undefined, KeymapBackend> key
{ shift(Key::Home), {"extend to line begin", repeated<select<SelectMode::Extend, select_to_line_begin<false>>>} },

{ {'x'}, {"select line", repeated<select<SelectMode::Replace, select_line>>} },
{ {'X'}, {"extend line", repeated<select<SelectMode::Extend, select_line>>} },
{ {'X'}, {"extend line", repeated<select<SelectMode::Extend, select_line_extend>>} },
{ {alt('x')}, {"extend selections to whole lines", select<SelectMode::Replace, select_lines>} },
{ {alt('X')}, {"crop selections to whole lines", select<SelectMode::Replace, trim_partial_lines>} },

Expand Down
34 changes: 32 additions & 2 deletions src/selectors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ Optional<Selection>
select_line(const Context& context, const Selection& selection)
{
auto& buffer = context.buffer();
auto line = selection.cursor().line;
bool forward = selection.anchor() < selection.cursor();
auto line = forward ? std::max(selection.cursor().line, selection.anchor().line)
: std::min(selection.cursor().line, selection.anchor().line);

if (context.is_line_editing()) {
++line;
Expand All @@ -185,7 +187,35 @@ select_line(const Context& context, const Selection& selection)
if (line >= buffer.line_count()) {
return Optional<Selection>();
}
return target_eol({{line, 0_byte}, {line, buffer[line].length() - 1}});
if (forward) {
return target_eol({{line, 0_byte}, {line, buffer[line].length() - 1}});
} else {
return target_eol({{line, buffer[line].length() - 1}, {line, 0_byte}});
}
}

Optional<Selection>
select_line_extend(const Context& context, const Selection& selection)
{
auto& buffer = context.buffer();
bool forward = selection.anchor() < selection.cursor();
auto line = std::max(selection.cursor().line, selection.anchor().line);
auto starting_line = std::min(selection.cursor().line, selection.anchor().line);

if (context.is_line_editing()) {
++line;
}

context.enter_or_keep_line_editing();

if (line >= buffer.line_count()) {
return Optional<Selection>();
}
if (forward) {
return target_eol({{starting_line, 0_byte}, {line, buffer[line].length() - 1}});
} else {
return target_eol({{line, buffer[line].length() - 1}, {starting_line, 0_byte}});
}
}

template<bool only_move>
Expand Down
3 changes: 3 additions & 0 deletions src/selectors.hh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ select_to_previous_word(const Context& context, const Selection& selection);
Optional<Selection>
select_line(const Context& context, const Selection& selection);

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

template<bool forward>
Optional<Selection>
select_matching(const Context& context, const Selection& selection);
Expand Down

0 comments on commit 33c3dee

Please sign in to comment.