diff --git a/src/selectors.cc b/src/selectors.cc index f5fdb088..781ea0e0 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -510,8 +510,8 @@ void select_all_matches(SelectionList& selections, const Regex& regex) for (; re_it != re_end; ++re_it) { - auto& begin = (*re_it)[0].first; - auto& end = (*re_it)[0].second; + auto begin = ensure_char_start(buffer, (*re_it)[0].first); + auto end = ensure_char_start(buffer, (*re_it)[0].second); if (begin == sel_end) continue; @@ -549,8 +549,9 @@ void split_selections(SelectionList& selections, const Regex& regex) if (end == buf_end) continue; + end = ensure_char_start(buffer, end); result.push_back(keep_direction({ begin.coord(), (begin == end) ? end.coord() : utf8::previous(end, begin).coord() }, sel)); - begin = (*re_it)[0].second; + begin = ensure_char_start(buffer, (*re_it)[0].second); } if (begin.coord() <= sel.max()) result.push_back(keep_direction({ begin.coord(), sel.max() }, sel)); diff --git a/src/selectors.hh b/src/selectors.hh index 3dc4e5ba..09ba45e1 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -256,6 +256,12 @@ bool find_match_in_buffer(const Buffer& buffer, const BufferIterator pos, find_last_match(buffer.begin(), buffer.end(), matches, ex)); } +inline BufferIterator ensure_char_start(const Buffer& buffer, const BufferIterator& it) +{ + return it != buffer.end() ? + utf8::character_start(it, buffer.iterator_at(it.coord().line)) : it; +} + template Selection find_next_match(const Buffer& buffer, const Selection& sel, const Regex& regex) { @@ -269,8 +275,8 @@ Selection find_next_match(const Buffer& buffer, const Selection& sel, const Rege : utf8::previous(begin, buffer.begin()); if ((found = find_match_in_buffer(buffer, pos, matches, regex))) { - begin = matches[0].first; - end = matches[0].second; + begin = ensure_char_start(buffer, matches[0].first); + end = ensure_char_start(buffer, matches[0].second); for (auto& match : matches) captures.emplace_back(match.first, match.second); }