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

Change slightly how x/X works #2595

Closed
wants to merge 7 commits into from
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/context.hh
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ public:
m_jump_list.push(selections());
}

void enter_or_keep_line_editing() const {
m_keep_line_mode = true;
}

bool is_line_editing() const {
return m_line_mode;
}

void post_movement_logic() const {
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 @@ -135,6 +148,8 @@ private:
void end_edition();
int m_edition_level = 0;
size_t m_edition_timestamp = 0;
mutable bool m_line_mode = false;
mutable bool m_keep_line_mode = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is using mutable really necessary here?

Copy link
Contributor Author

@dpc dpc Nov 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's kind of a hack/workaround that mawww threw at me, when I didn't know what to do. You can revert Is it a hack, or is it a solution? :D to see the problem, or just see the error in this commit: dpc@b8d3f08

Basically: now some functions are taking Context& some consts Context& and this big hashmap is complaining, even though it it is storing Context& in intself, so I don't get why it can't promote.


friend struct ScopedEdition;

Expand Down
1 change: 1 addition & 0 deletions src/input_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ class Normal : public InputMode
m_params = { 0, 0 };

command->func(context(), params);
context().post_movement_logic();
}
}

Expand Down
29 changes: 27 additions & 2 deletions src/normal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ void for_each_codepoint(Context& context, NormalParams)

void command(Context& context, NormalParams params)
{
if (context.is_line_editing()) {
context.enter_or_keep_line_editing();
}
if (not CommandManager::has_instance())
throw runtime_error{"commands are not supported"};

Expand All @@ -449,9 +452,15 @@ void command(Context& context, NormalParams params)
context.faces()["Prompt"], PromptFlags::DropHistoryEntriesWithBlankPrefix,
[](const Context& context, CompletionFlags flags,
StringView cmd_line, ByteCount pos) {
if (context.is_line_editing()) {
context.enter_or_keep_line_editing();
}
return CommandManager::instance().complete(context, flags, cmd_line, pos);
},
[params](StringView cmdline, PromptEvent event, Context& context) {
if (context.is_line_editing()) {
context.enter_or_keep_line_editing();
}
if (context.has_client())
{
context.client().info_hide();
Expand Down Expand Up @@ -2001,7 +2010,11 @@ class Repeated
void operator() (Context& context, NormalParams params)
{
ScopedEdition edition(context);
do { m_func(context, {0, params.reg}); } while(--params.count > 0);
do {
m_func(context, {0, params.reg});
if (params.count > 1)
context.post_movement_logic();
} while (--params.count > 0);
}
private:
T m_func;
Expand All @@ -2011,7 +2024,11 @@ template<void (*func)(Context&, NormalParams)>
void repeated(Context& context, NormalParams params)
{
ScopedEdition edition(context);
do { func(context, {0, params.reg}); } while(--params.count > 0);
do {
func(context, {0, params.reg});
if (params.count > 1)
context.post_movement_logic();
} while (--params.count > 0);
}

template<typename Type, Direction direction, SelectMode mode = SelectMode::Replace>
Expand Down Expand Up @@ -2067,6 +2084,10 @@ void clear_selections(Context& context, NormalParams)

void flip_selections(Context& context, NormalParams)
{
if (context.is_line_editing()) {
context.enter_or_keep_line_editing();
}

for (auto& sel : context.selections())
{
const BufferCoord tmp = sel.anchor();
Expand All @@ -2078,6 +2099,10 @@ void flip_selections(Context& context, NormalParams)

void ensure_forward(Context& context, NormalParams)
{
if (context.is_line_editing()) {
context.enter_or_keep_line_editing();
}

for (auto& sel : context.selections())
{
const BufferCoord min = sel.min(), max = sel.max();
Expand Down
17 changes: 13 additions & 4 deletions src/selectors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,16 @@ select_line(const 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();

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

Expand Down Expand Up @@ -811,6 +816,8 @@ select_argument(const Context& context, const Selection& selection,
Optional<Selection>
select_lines(const 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 @@ -832,6 +839,8 @@ trim_partial_lines(const Context& context, const Selection& selection)
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