summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2014-09-18 00:34:23 +0100
committerMaxime Coste <frrrwww@gmail.com>2014-09-18 00:34:23 +0100
commitdd2bdea8ddc3b975bbd6ed967adcd9ecb7267380 (patch)
tree88f71a4525c5fb3ea912ed5dce342f98dd848512
parentbe85eb5d0b79391ce48340a2425dafa218027369 (diff)
Keep selection direction on split/select/search
-rw-r--r--src/selectors.cc11
-rw-r--r--src/selectors.hh9
2 files changed, 14 insertions, 6 deletions
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<ByteCoord>(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,