diff --git a/src/selectors.cc b/src/selectors.cc index 583116896..a77a23799 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; - } - - BufferIterator last = first; - - if (flags & ObjectFlags::ToBegin) - { - bool saw_non_blank = false; - while (first != buffer.begin()) + if (not (flags & ObjectFlags::ToEnd) and first != buffer.begin()) { - 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()) + 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 (last == BufferIterator{}) + last = first; + + if (flags & ObjectFlags::ToBegin) + { + 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); } - skip_while(first, buffer.end(), is_horizontal_blank); - } - if (flags & ObjectFlags::ToEnd) - { - while (last != buffer.end()) + if (flags & ObjectFlags::ToEnd) { - 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; + 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()} diff --git a/test/normal/object/end-extending/sentence/count/cmd b/test/normal/object/end-extending/sentence/count/cmd new file mode 100644 index 000000000..072603cbd --- /dev/null +++ b/test/normal/object/end-extending/sentence/count/cmd @@ -0,0 +1 @@ +2}s diff --git a/test/normal/object/end-extending/sentence/count/in b/test/normal/object/end-extending/sentence/count/in new file mode 100644 index 000000000..847784aee --- /dev/null +++ b/test/normal/object/end-extending/sentence/count/in @@ -0,0 +1 @@ +%(a b) c. d e. f. diff --git a/test/normal/object/end-extending/sentence/count/kak_quoted_selections b/test/normal/object/end-extending/sentence/count/kak_quoted_selections new file mode 100644 index 000000000..794843b37 --- /dev/null +++ b/test/normal/object/end-extending/sentence/count/kak_quoted_selections @@ -0,0 +1 @@ +'a b c. d e. ' diff --git a/test/normal/object/end/sentence/count/cmd b/test/normal/object/end/sentence/count/cmd new file mode 100644 index 000000000..1fdb928e5 --- /dev/null +++ b/test/normal/object/end/sentence/count/cmd @@ -0,0 +1 @@ +2]s diff --git a/test/normal/object/end/sentence/count/in b/test/normal/object/end/sentence/count/in new file mode 100644 index 000000000..db224594c --- /dev/null +++ b/test/normal/object/end/sentence/count/in @@ -0,0 +1 @@ +%(a b) c. d e. f g. diff --git a/test/normal/object/end/sentence/count/kak_quoted_selections b/test/normal/object/end/sentence/count/kak_quoted_selections new file mode 100644 index 000000000..794843b37 --- /dev/null +++ b/test/normal/object/end/sentence/count/kak_quoted_selections @@ -0,0 +1 @@ +'a b c. d e. ' diff --git a/test/normal/object/end/sentence/to-buffer-end/cmd b/test/normal/object/end/sentence/to-buffer-end/cmd new file mode 100644 index 000000000..a2d3cd589 --- /dev/null +++ b/test/normal/object/end/sentence/to-buffer-end/cmd @@ -0,0 +1 @@ +9]s diff --git a/test/normal/object/end/sentence/to-buffer-end/in b/test/normal/object/end/sentence/to-buffer-end/in new file mode 100644 index 000000000..7b2bc302a --- /dev/null +++ b/test/normal/object/end/sentence/to-buffer-end/in @@ -0,0 +1,3 @@ +a b. + +c d. diff --git a/test/normal/object/end/sentence/to-buffer-end/kak_quoted_selections b/test/normal/object/end/sentence/to-buffer-end/kak_quoted_selections new file mode 100644 index 000000000..387268362 --- /dev/null +++ b/test/normal/object/end/sentence/to-buffer-end/kak_quoted_selections @@ -0,0 +1 @@ +'a b.' diff --git a/test/normal/object/start-extending/sentence/count/cmd b/test/normal/object/start-extending/sentence/count/cmd new file mode 100644 index 000000000..c732db031 --- /dev/null +++ b/test/normal/object/start-extending/sentence/count/cmd @@ -0,0 +1 @@ +2{s diff --git a/test/normal/object/start-extending/sentence/count/in b/test/normal/object/start-extending/sentence/count/in new file mode 100644 index 000000000..765e3c6f5 --- /dev/null +++ b/test/normal/object/start-extending/sentence/count/in @@ -0,0 +1 @@ +a b. c d. e %(f g) diff --git a/test/normal/object/start-extending/sentence/count/kak_quoted_selections b/test/normal/object/start-extending/sentence/count/kak_quoted_selections new file mode 100644 index 000000000..7142771ff --- /dev/null +++ b/test/normal/object/start-extending/sentence/count/kak_quoted_selections @@ -0,0 +1 @@ +'c d. e f g' diff --git a/test/normal/object/start/sentence/count/cmd b/test/normal/object/start/sentence/count/cmd new file mode 100644 index 000000000..0d60a64ba --- /dev/null +++ b/test/normal/object/start/sentence/count/cmd @@ -0,0 +1 @@ +2[s diff --git a/test/normal/object/start/sentence/count/in b/test/normal/object/start/sentence/count/in new file mode 100644 index 000000000..b8356c658 --- /dev/null +++ b/test/normal/object/start/sentence/count/in @@ -0,0 +1 @@ +a b. c d. e %(f) diff --git a/test/normal/object/start/sentence/count/kak_quoted_selections b/test/normal/object/start/sentence/count/kak_quoted_selections new file mode 100644 index 000000000..592a862e9 --- /dev/null +++ b/test/normal/object/start/sentence/count/kak_quoted_selections @@ -0,0 +1 @@ +'c d. e f' diff --git a/test/normal/object/start/sentence/to-buffer-begin/cmd b/test/normal/object/start/sentence/to-buffer-begin/cmd new file mode 100644 index 000000000..c5d20d0ad --- /dev/null +++ b/test/normal/object/start/sentence/to-buffer-begin/cmd @@ -0,0 +1 @@ +9[s diff --git a/test/normal/object/start/sentence/to-buffer-begin/in b/test/normal/object/start/sentence/to-buffer-begin/in new file mode 100644 index 000000000..7842b1207 --- /dev/null +++ b/test/normal/object/start/sentence/to-buffer-begin/in @@ -0,0 +1,3 @@ +a b. + +c d. %(e) f. diff --git a/test/normal/object/start/sentence/to-buffer-begin/kak_quoted_selections b/test/normal/object/start/sentence/to-buffer-begin/kak_quoted_selections new file mode 100644 index 000000000..f0beb97d7 --- /dev/null +++ b/test/normal/object/start/sentence/to-buffer-begin/kak_quoted_selections @@ -0,0 +1,3 @@ +'a b. + +c d.'