Skip to content

Commit

Permalink
src: Select the data inserted by ! and <a-!>
Browse files Browse the repository at this point in the history
Closes mawww#1468
  • Loading branch information
lenormf committed Jul 22, 2021
1 parent 15aa4fe commit cb8502a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions doc/pages/keys.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,10 @@ The default command comes from the *|* register by default (See <<registers#,`:d
ignore its output.

*!*::
insert command output before each selection.
insert and select command output before each selection.

*<a-!>*::
append command output after each selection.
append and select command output after each selection.

== Searching

Expand Down
14 changes: 10 additions & 4 deletions src/normal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ void insert_output(Context& context, NormalParams params)
ScopedEdition edition(context);
auto& selections = context.selections();
const size_t old_main = selections.main_index();
Vector<BufferRange> ins_range;

auto ins = selections | transform([&, i=0](auto& sel) mutable {
selections.set_main_index(i++);
Expand All @@ -656,8 +657,13 @@ void insert_output(Context& context, NormalParams params)
ShellManager::Flags::WaitForStdout).first;
}) | gather<Vector>();

selections.set_main_index(old_main);
selections.insert(ins, mode);
selections.insert(ins, mode, &ins_range);
selections.set(ins_range | transform([&context](auto& range) {
if (range.empty())
return Selection{range.begin, range.end};
return Selection{range.begin,
context.buffer().char_prev(range.end)};
}) | gather<Vector>(), old_main);
});
}

Expand Down Expand Up @@ -737,7 +743,7 @@ void paste_all(Context& context, NormalParams params)
offsets.push_back(all.length());
}

Vector<BufferCoord> insert_pos;
Vector<BufferRange> insert_pos;
auto& selections = context.selections();
{
ScopedEdition edition(context);
Expand All @@ -749,7 +755,7 @@ void paste_all(Context& context, NormalParams params)
for (auto& ins_pos : insert_pos)
{
ByteCount pos_offset = 0;
BufferCoord pos = ins_pos;
BufferCoord pos = ins_pos.begin;
for (auto offset : offsets)
{
BufferCoord end = buffer.advance(pos, offset - pos_offset - 1);
Expand Down
11 changes: 6 additions & 5 deletions src/selection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ static void fix_overflowing_selections(Vector<Selection>& selections,
}

void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
Vector<BufferCoord>* out_insert_pos)
Vector<BufferRange>* out_insert_range)
{
if (strings.empty())
return;
Expand Down Expand Up @@ -415,9 +415,10 @@ void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
const auto pos = (mode == InsertMode::Replace) ?
sel.min() : changes_tracker.get_new_coord(insert_pos[index]);

BufferRange range;
if (mode == InsertMode::Replace)
{
auto range = replace(*m_buffer, sel, str);
range = replace(*m_buffer, sel, str);
// we want min and max from *before* we do any change
auto& min = sel.min();
auto& max = sel.max();
Expand All @@ -426,14 +427,14 @@ void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
}
else
{
auto range = m_buffer->insert(pos, str);
range = m_buffer->insert(pos, str);
sel.anchor() = m_buffer->clamp(update_insert(sel.anchor(), range.begin, range.end));
sel.cursor() = m_buffer->clamp(update_insert(sel.cursor(), range.begin, range.end));
}

changes_tracker.update(*m_buffer, m_timestamp);
if (out_insert_pos)
out_insert_pos->push_back(pos);
if (out_insert_range)
out_insert_range->push_back(range);
}

// We might just have been deleting text if strings were empty,
Expand Down
2 changes: 1 addition & 1 deletion src/selection.hh
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ struct SelectionList
void force_timestamp(size_t timestamp) { m_timestamp = timestamp; }

void insert(ConstArrayView<String> strings, InsertMode mode,
Vector<BufferCoord>* out_insert_pos = nullptr);
Vector<BufferRange>* out_insert_range = nullptr);
void erase();

private:
Expand Down

0 comments on commit cb8502a

Please sign in to comment.