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 Oct 3, 2021
1 parent 09f4ef0 commit 9342991
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 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
11 changes: 9 additions & 2 deletions src/normal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -645,17 +645,24 @@ void insert_output(Context& context, NormalParams params)
auto& selections = context.selections();
auto& buffer = context.buffer();
const size_t old_main = selections.main_index();
Vector<BufferRange> ins_range;

selections.for_each([&](size_t index, Selection& sel) {
selections.set_main_index(index);
auto [out, status] = ShellManager::instance().eval(
cmdline, context, content(context.buffer(), sel),
ShellManager::Flags::WaitForStdout);

insert(buffer, sel, out, mode);
auto range = insert(buffer, sel, out, mode);
ins_range.push_back(range);
});

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

Expand Down
3 changes: 2 additions & 1 deletion src/selection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,12 @@ void replace(Buffer& buffer, Selection& sel, StringView content)
max = range.end > range.begin ? buffer.char_prev(range.end) : range.begin;
}

void insert(Buffer& buffer, Selection& sel, StringView content, InsertMode mode)
BufferRange insert(Buffer& buffer, Selection& sel, StringView content, InsertMode mode)
{
auto range = buffer.insert(get_insert_pos(buffer, sel, mode), content);
sel.anchor() = buffer.clamp(update_insert(sel.anchor(), range.begin, range.end));
sel.cursor() = buffer.clamp(update_insert(sel.cursor(), range.begin, range.end));
return range;
}

void SelectionList::replace(ConstArrayView<String> strings)
Expand Down
2 changes: 1 addition & 1 deletion src/selection.hh
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ enum class InsertMode : unsigned
BufferCoord get_insert_pos(const Buffer& buffer, const Selection& sel, InsertMode mode);

void replace(Buffer& buffer, Selection& sel, StringView content);
void insert(Buffer& buffer, Selection& sel, StringView content, InsertMode mode);
BufferRange insert(Buffer& buffer, Selection& sel, StringView content, InsertMode mode);


struct SelectionList
Expand Down

0 comments on commit 9342991

Please sign in to comment.