diff --git a/src/selectors.hh b/src/selectors.hh index 3d3d8576..84cc9ed9 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -240,17 +240,14 @@ bool find_match_in_buffer(const Buffer& buffer, const BufferIterator pos, find_last_match(pos, buffer.end(), matches, ex)); } -template -void select_next_match(const Buffer& buffer, SelectionList& selections, - const Regex& regex) +template +Selection find_next_match(const Buffer& buffer, const Selection& sel, const Regex& regex) { - auto& sel = selections.main(); auto begin = buffer.iterator_at(sel.last()); auto end = begin; + CaptureList captures; - MatchResults matches; - bool found = false; if ((found = find_match_in_buffer(buffer, utf8::next(begin), matches, regex))) { @@ -266,14 +263,28 @@ void select_next_match(const Buffer& buffer, SelectionList& selections, if (direction == Backward) std::swap(begin, end); - Selection res{begin.coord(), end.coord(), std::move(captures)}; + return {begin.coord(), end.coord(), std::move(captures)}; +} + +template +void select_next_match(const Buffer& buffer, SelectionList& selections, + const Regex& regex) +{ if (mode == SelectMode::Replace) - selections = SelectionList{ std::move(res) }; + { + for (auto& sel : selections) + sel = find_next_match(buffer, sel, regex); + } + if (mode == SelectMode::Extend) + { + for (auto& sel : selections) + sel.merge_with(find_next_match(buffer, sel, regex)); + } else if (mode == SelectMode::ReplaceMain) - sel = std::move(res); + selections.main() = find_next_match(buffer, selections.main(), regex); else if (mode == SelectMode::Append) { - selections.push_back(std::move(res)); + selections.push_back(find_next_match(buffer, selections.main(), regex)); selections.set_main_index(selections.size() - 1); } selections.sort_and_merge_overlapping();