From 94796509a093e5b7d75ab3423b13a5ba1b4beb5c Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 11 Feb 2019 21:48:09 +1100 Subject: [PATCH] Fix bug in 'itersel' handling that could result in unsorted selections --- src/commands.cc | 4 ++-- src/selection.cc | 12 ++++++------ src/selection.hh | 1 + 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index b4d1b908..0a24742d 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1739,8 +1739,8 @@ void context_wrap(const ParametersParser& parser, Context& context, StringView d if (&sel == &sels.main()) main = new_sels.size() + c.selections().main_index(); - for (auto& sel : c.selections()) - new_sels.push_back(sel); + 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); } } diff --git a/src/selection.cc b/src/selection.cc index 5e3ff2e7..00e750a5 100644 --- a/src/selection.cc +++ b/src/selection.cc @@ -50,6 +50,12 @@ void SelectionList::set(Vector list, size_t main) check_invariant(); } +bool compare_selections(const Selection& lhs, const Selection& rhs) +{ + const auto lmin = lhs.min(), rmin = rhs.min(); + return lmin == rmin ? lhs.max() < rhs.max() : lmin < rmin; +} + namespace { @@ -78,12 +84,6 @@ BufferCoord update_erase(BufferCoord coord, BufferCoord begin, BufferCoord end) return coord; } */ -bool compare_selections(const Selection& lhs, const Selection& rhs) -{ - const auto lmin = lhs.min(), rmin = rhs.min(); - return lmin == rmin ? lhs.max() < rhs.max() : lmin < rmin; -} - template Iterator merge_overlapping(Iterator begin, Iterator end, size_t& main, OverlapsFunc overlaps) { diff --git a/src/selection.hh b/src/selection.hh index 02266528..bd7e35c2 100644 --- a/src/selection.hh +++ b/src/selection.hh @@ -67,6 +67,7 @@ inline bool overlaps(const Selection& lhs, const Selection& rhs) void update_selections(Vector& selections, size_t& main, Buffer& buffer, size_t timestamp); +bool compare_selections(const Selection& lhs, const Selection& rhs); void sort_selections(Vector& selections, size_t& main); void merge_overlapping_selections(Vector& selections, size_t& main); void clamp_selections(Vector& sel, const Buffer& buffer);