Skip to content

Commit

Permalink
Change x-and-alikes to work consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
dpc committed Oct 22, 2021
1 parent bc11f97 commit 172a9fb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
15 changes: 15 additions & 0 deletions src/context.hh
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ public:
m_jump_list.push(selections());
}

void enter_or_keep_line_editing() {
m_keep_line_mode = true;
}

bool is_line_editing() {
return m_line_mode;
}

void post_key_logic() {
m_line_mode = m_keep_line_mode;
m_keep_line_mode = false;
}

template<typename Func>
void set_last_select(Func&& last_select) { m_last_select = std::forward<Func>(last_select); }

Expand All @@ -141,6 +154,8 @@ private:
void end_edition();
int m_edition_level = 0;
size_t m_edition_timestamp = 0;
bool m_line_mode = false;
bool m_keep_line_mode = false;

friend struct ScopedEdition;

Expand Down
2 changes: 2 additions & 0 deletions src/input_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ class Normal : public InputMode
context().hooks().run_hook(Hook::NormalKey, key_to_str(key), context());
if (enabled() and not transient) // The hook might have changed mode
m_idle_timer.set_next_date(Clock::now() + get_idle_timeout(context()));

context().post_key_logic();
}

DisplayLine mode_line() const override
Expand Down
19 changes: 13 additions & 6 deletions src/selectors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,17 @@ template Optional<Selection> select_word<WordType::Word>(const Context&, const S
template Optional<Selection> select_word<WordType::WORD>(const Context&, const Selection&, int, ObjectFlags);

Optional<Selection>
select_line(const Context& context, const Selection& selection)
select_line(Context& context, const Selection& selection)
{
auto& buffer = context.buffer();
auto line = selection.cursor().line;
// Next line if line fully selected
if (selection.anchor() <= BufferCoord{line, 0_byte} and
selection.cursor() == BufferCoord{line, buffer[line].length() - 1} and
line != buffer.line_count() - 1)
if (context.is_line_editing()) {
++line;
}

context.enter_or_keep_line_editing();

return Selection{{line, 0_byte}, {line, buffer[line].length() - 1, max_column}};
}

Expand Down Expand Up @@ -822,8 +824,10 @@ select_argument(const Context& context, const Selection& selection,
}

Optional<Selection>
select_lines(const Context& context, const Selection& selection)
select_lines(Context& context, const Selection& selection)
{
context.enter_or_keep_line_editing();

auto& buffer = context.buffer();
BufferCoord anchor = selection.anchor();
BufferCoord cursor = selection.cursor();
Expand All @@ -837,14 +841,17 @@ select_lines(const Context& context, const Selection& selection)
}

Optional<Selection>
trim_partial_lines(const Context& context, const Selection& selection)
trim_partial_lines(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;


context.enter_or_keep_line_editing();

if (to_line_start.column != 0)
to_line_start = to_line_start.line+1;
if (to_line_end.column != buffer[to_line_end.line].length()-1)
Expand Down
6 changes: 3 additions & 3 deletions src/selectors.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Optional<Selection>
select_to_previous_word(const Context& context, const Selection& selection);

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

template<bool forward>
Optional<Selection>
Expand Down Expand Up @@ -102,10 +102,10 @@ select_argument(const Context& context, const Selection& selection,
int level, ObjectFlags flags);

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

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

enum class RegexMode;

Expand Down

0 comments on commit 172a9fb

Please sign in to comment.