diff options
| author | Maxime Coste <mawww@kakoune.org> | 2020-08-03 18:58:06 +1000 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2020-08-03 18:58:06 +1000 |
| commit | 34baa568910d4d50d9efefae659c6b550bedadf5 (patch) | |
| tree | 62f0149759314fc5d9fc6af96b05e4d7bb8da2bf /src | |
| parent | ba7831035f77c4f3a6736a3580f91522dc363288 (diff) | |
| parent | 266fe6f659aed2921c9aa024f2e07eb0e480d61f (diff) | |
Merge remote-tracking branch 'krobelus/textobject-count'
Diffstat (limited to 'src')
| -rw-r--r-- | src/selectors.cc | 161 |
1 files changed, 85 insertions, 76 deletions
diff --git a/src/selectors.cc b/src/selectors.cc index c7e126b3..a77a2379 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -494,58 +494,63 @@ select_sentence(const Context& context, const Selection& selection, auto& buffer = context.buffer(); BufferIterator first = buffer.iterator_at(selection.cursor()); + BufferIterator last; - if (not (flags & ObjectFlags::ToEnd) and first != buffer.begin()) + for (++count; count > 0; --count) { - BufferIterator prev_non_blank = first-1; - skip_while_reverse(prev_non_blank, buffer.begin(), - [](char c) { return is_horizontal_blank(c) or is_eol(c); }); - if (is_end_of_sentence(*prev_non_blank)) - first = prev_non_blank; - } + if (not (flags & ObjectFlags::ToEnd) and first != buffer.begin()) + { + BufferIterator prev_non_blank = first-1; + skip_while_reverse(prev_non_blank, buffer.begin(), + [](char c) { return is_horizontal_blank(c) or is_eol(c); }); + if (is_end_of_sentence(*prev_non_blank)) + first = prev_non_blank; + } - BufferIterator last = first; + if (last == BufferIterator{}) + last = first; - if (flags & ObjectFlags::ToBegin) - { - bool saw_non_blank = false; - while (first != buffer.begin()) + if (flags & ObjectFlags::ToBegin) { - char cur = *first; - char prev = *(first-1); - if (not is_horizontal_blank(cur)) - saw_non_blank = true; - if (is_eol(prev) and is_eol(cur) and first + 1 != buffer.end()) + bool saw_non_blank = false; + while (first != buffer.begin()) { - ++first; - break; - } - else if (is_end_of_sentence(prev)) - { - if (saw_non_blank) + char cur = *first; + char prev = *(first-1); + if (not is_horizontal_blank(cur)) + saw_non_blank = true; + if (is_eol(prev) and is_eol(cur) and first + 1 != buffer.end()) + { + ++first; break; - else if (flags & ObjectFlags::ToEnd) - last = first-1; + } + else if (is_end_of_sentence(prev)) + { + if (saw_non_blank) + break; + else if (flags & ObjectFlags::ToEnd) + last = first-1; + } + --first; } - --first; - } - skip_while(first, buffer.end(), is_horizontal_blank); - } - if (flags & ObjectFlags::ToEnd) - { - while (last != buffer.end()) - { - char cur = *last; - if (is_end_of_sentence(cur) or - (is_eol(cur) and (last+1 == buffer.end() or is_eol(*(last+1))))) - break; - ++last; + skip_while(first, buffer.end(), is_horizontal_blank); } - if (not (flags & ObjectFlags::Inner) and last != buffer.end()) + if (flags & ObjectFlags::ToEnd) { - ++last; - skip_while(last, buffer.end(), is_horizontal_blank); - --last; + while (last != buffer.end()) + { + char cur = *last; + if (is_end_of_sentence(cur) or + (is_eol(cur) and (last+1 == buffer.end() or is_eol(*(last+1))))) + break; + ++last; + } + if (not (flags & ObjectFlags::Inner) and last != buffer.end()) + { + ++last; + skip_while(last, buffer.end(), is_horizontal_blank); + --last; + } } } return (flags & ObjectFlags::ToEnd) ? Selection{first.coord(), last.coord()} @@ -558,51 +563,55 @@ select_paragraph(const Context& context, const Selection& selection, { auto& buffer = context.buffer(); BufferIterator first = buffer.iterator_at(selection.cursor()); + BufferIterator last; - if (not (flags & ObjectFlags::ToEnd) and first.coord() > BufferCoord{0,1} and - is_eol(*(first-1)) and first-1 != buffer.begin() and is_eol(*(first-2))) - --first; - else if ((flags & ObjectFlags::ToEnd) and - first != buffer.begin() and (first+1) != buffer.end() and - is_eol(*(first-1)) and is_eol(*first)) - ++first; - - BufferIterator last = first; - - if ((flags & ObjectFlags::ToBegin) and first != buffer.begin()) + for (++count; count > 0; --count) { - skip_while_reverse(first, buffer.begin(), - [](Codepoint c){ return is_eol(c); }); - if (flags & ObjectFlags::ToEnd) + if (not (flags & ObjectFlags::ToEnd) and first.coord() > BufferCoord{0,1} and + is_eol(*(first-1)) and first-1 != buffer.begin() and is_eol(*(first-2))) + --first; + else if ((flags & ObjectFlags::ToEnd) and + first != buffer.begin() and (first+1) != buffer.end() and + is_eol(*(first-1)) and is_eol(*first)) + ++first; + if (last == BufferIterator{}) last = first; - while (first != buffer.begin()) + + if ((flags & ObjectFlags::ToBegin) and first != buffer.begin()) { - char cur = *first; - char prev = *(first-1); - if (is_eol(prev) and is_eol(cur)) + skip_while_reverse(first, buffer.begin(), + [](Codepoint c){ return is_eol(c); }); + if (flags & ObjectFlags::ToEnd) + last = first; + while (first != buffer.begin()) { - ++first; - break; + char cur = *first; + char prev = *(first-1); + if (is_eol(prev) and is_eol(cur)) + { + ++first; + break; + } + --first; } - --first; } - } - if (flags & ObjectFlags::ToEnd) - { - if (last != buffer.end() and is_eol(*last)) - ++last; - while (last != buffer.end()) + if (flags & ObjectFlags::ToEnd) { - if (last != buffer.begin() and is_eol(*last) and is_eol(*(last-1))) + if (last != buffer.end() and is_eol(*last)) + ++last; + while (last != buffer.end()) { - if (not (flags & ObjectFlags::Inner)) - skip_while(last, buffer.end(), - [](Codepoint c){ return is_eol(c); }); - break; + if (last != buffer.begin() and is_eol(*last) and is_eol(*(last-1))) + { + if (not (flags & ObjectFlags::Inner)) + skip_while(last, buffer.end(), + [](Codepoint c){ return is_eol(c); }); + break; + } + ++last; } - ++last; + --last; } - --last; } return (flags & ObjectFlags::ToEnd) ? Selection{first.coord(), last.coord()} : Selection{last.coord(), first.coord()}; |
