diff --git a/src/commands.cc b/src/commands.cc index 51adb80c..b1fe3580 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1782,26 +1782,38 @@ void context_wrap(const ParametersParser& parser, Context& context, StringView d Vector new_sels; size_t main = 0; size_t timestamp = c.buffer().timestamp(); + bool one_selection_succeeded = false; for (auto& sel : sels) { c.selections_write_only() = SelectionList{sels.buffer(), sel, sels.timestamp()}; c.selections().update(); - func(parser, c); - - if (not draft) + try { + func(parser, c); + one_selection_succeeded = true; + if (&sels.buffer() != &c.buffer()) throw runtime_error("buffer has changed while iterating on selections"); - update_selections(new_sels, main, c.buffer(), timestamp); - timestamp = c.buffer().timestamp(); - if (&sel == &sels.main()) - main = new_sels.size() + c.selections().main_index(); + if (not draft) + { + update_selections(new_sels, main, c.buffer(), timestamp); + timestamp = c.buffer().timestamp(); + if (&sel == &sels.main()) + main = new_sels.size() + c.selections().main_index(); - const auto middle = new_sels.insert(new_sels.end(), c.selections().begin(), c.selections().end()); - std::inplace_merge(new_sels.begin(), middle, new_sels.end(), compare_selections); + const auto middle = new_sels.insert(new_sels.end(), c.selections().begin(), c.selections().end()); + std::inplace_merge(new_sels.begin(), middle, new_sels.end(), compare_selections); + } } + catch (no_selections_remaining&) {} + } + + if (not one_selection_succeeded) + { + c.selections_write_only() = std::move(sels); + throw no_selections_remaining{}; } if (not draft) diff --git a/src/normal.cc b/src/normal.cc index 1101302b..565f0472 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -113,7 +113,7 @@ void select(Context& context, T func) } if (to_remove.size() == selections.size()) - throw runtime_error{"no selections remaining"}; + throw no_selections_remaining{}; for (auto& i : to_remove | reverse()) selections.remove(i); } @@ -1135,7 +1135,7 @@ void keep(Context& context, NormalParams params) keep.push_back(sel); } if (keep.empty()) - throw runtime_error("no selections remaining"); + throw no_selections_remaining{}; context.selections_write_only() = std::move(keep); }); } @@ -1168,7 +1168,7 @@ void keep_pipe(Context& context, NormalParams) } } if (keep.empty()) - throw runtime_error("no selections remaining"); + throw no_selections_remaining{}; if (new_main == -1) new_main = keep.size() - 1; context.selections_write_only().set(std::move(keep), new_main); @@ -1775,7 +1775,7 @@ void trim_selections(Context& context, NormalParams) } if (to_remove.size() == selections.size()) - throw runtime_error{"no selections remaining"}; + throw no_selections_remaining{}; for (auto& i : to_remove | reverse()) selections.remove(i); } diff --git a/src/normal.hh b/src/normal.hh index 10ee5ffa..e0eb8477 100644 --- a/src/normal.hh +++ b/src/normal.hh @@ -5,12 +5,18 @@ #include "keys.hh" #include "keymap_manager.hh" #include "string.hh" +#include "exception.hh" namespace Kakoune { class Context; +struct no_selections_remaining : runtime_error +{ + no_selections_remaining() : runtime_error("no selections remaining") {} +}; + struct NormalParams { int count; diff --git a/test/compose/discard-selections-with-itersel/cmd b/test/compose/discard-selections-with-itersel/cmd new file mode 100644 index 00000000..1219eba1 --- /dev/null +++ b/test/compose/discard-selections-with-itersel/cmd @@ -0,0 +1 @@ +:exec -itersel a-k>baret>d diff --git a/test/compose/discard-selections-with-itersel/in b/test/compose/discard-selections-with-itersel/in new file mode 100644 index 00000000..3ad2ca0b --- /dev/null +++ b/test/compose/discard-selections-with-itersel/in @@ -0,0 +1,3 @@ +%(foo) +%(bar) +%(baz) diff --git a/test/compose/discard-selections-with-itersel/out b/test/compose/discard-selections-with-itersel/out new file mode 100644 index 00000000..333dc6b4 --- /dev/null +++ b/test/compose/discard-selections-with-itersel/out @@ -0,0 +1,3 @@ +foo + +