diff --git a/src/selectors.cc b/src/selectors.cc index 7239e082..e8a3000b 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -516,9 +516,10 @@ void select_all_matches(SelectionList& selections, const Regex& regex) for (auto& match : *re_it) captures.emplace_back(match.first, match.second); - result.push_back({ begin.coord(), - (begin == end ? end : utf8::previous(end, begin)).coord(), - std::move(captures) }); + result.push_back( + keep_direction({ begin.coord(), + (begin == end ? end : utf8::previous(end, begin)).coord(), + std::move(captures) }, sel)); } } if (result.empty()) @@ -542,11 +543,11 @@ void split_selections(SelectionList& selections, const Regex& regex) { BufferIterator end = (*re_it)[0].first; - result.push_back({ begin.coord(), (begin == end) ? end.coord() : utf8::previous(end, begin).coord() }); + result.push_back(keep_direction({ begin.coord(), (begin == end) ? end.coord() : utf8::previous(end, begin).coord() }, sel)); begin = (*re_it)[0].second; } if (begin.coord() <= sel.max()) - result.push_back({ begin.coord(), sel.max() }); + result.push_back(keep_direction({ begin.coord(), sel.max() }, sel)); } selections = std::move(result); } diff --git a/src/selectors.hh b/src/selectors.hh index 940ec222..6382b0da 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -9,6 +9,13 @@ namespace Kakoune { +inline Selection keep_direction(Selection res, const Selection& ref) +{ + if ((res.cursor() < res.anchor()) != (ref.cursor() < ref.anchor())) + std::swap(res.cursor(), res.anchor()); + return res; +} + inline void clear_selections(SelectionList& selections) { for (auto& sel : selections) @@ -274,7 +281,7 @@ Selection find_next_match(const Buffer& buffer, const Selection& sel, const Rege if (direction == Backward) std::swap(begin, end); - return {begin.coord(), end.coord(), std::move(captures)}; + return keep_direction({begin.coord(), end.coord(), std::move(captures)}, sel); } void select_all_matches(SelectionList& selections,