From fccb9546112523f19bbd6a7f775177b0e915c711 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 7 Oct 2013 18:44:22 +0100 Subject: [PATCH] Use WordType enum instead of a bool punctuation_is_word for word selector --- src/normal.cc | 28 +++++++++--------- src/selectors.cc | 77 +++++++++++++++++++++++------------------------- src/selectors.hh | 10 ++++--- 3 files changed, 57 insertions(+), 58 deletions(-) diff --git a/src/normal.cc b/src/normal.cc index 1390108e..abe46943 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -597,8 +597,8 @@ void select_object(Context& context) { { Key::Modifiers::None, '>' }, std::bind(select_surrounding, _1, _2, CodepointPair{ '<', '>' }, flags) }, { { Key::Modifiers::None, '"' }, std::bind(select_surrounding, _1, _2, CodepointPair{ '"', '"' }, flags) }, { { Key::Modifiers::None, '\'' }, std::bind(select_surrounding, _1, _2, CodepointPair{ '\'', '\'' }, flags) }, - { { Key::Modifiers::None, 'w' }, std::bind(select_whole_word, _1, _2, flags) }, - { { Key::Modifiers::None, 'W' }, std::bind(select_whole_word, _1, _2, flags) }, + { { Key::Modifiers::None, 'w' }, std::bind(select_whole_word, _1, _2, flags) }, + { { Key::Modifiers::None, 'W' }, std::bind(select_whole_word, _1, _2, flags) }, { { Key::Modifiers::None, 's' }, std::bind(select_whole_sentence, _1, _2, flags) }, { { Key::Modifiers::None, 'p' }, std::bind(select_whole_paragraph, _1, _2, flags) }, { { Key::Modifiers::None, 'i' }, std::bind(select_whole_indent, _1, _2, flags) }, @@ -905,19 +905,19 @@ KeyMap keymap = { { Key::Modifiers::Alt, ' ' }, [](Context& context) { int count = context.numeric_param(); if (count == 0) context.editor().flip_selections(); else context.editor().remove_selection(count-1); } }, - { { Key::Modifiers::None, 'w' }, repeated(select(select_to_next_word)) }, - { { Key::Modifiers::None, 'e' }, repeated(select(select_to_next_word_end)) }, - { { Key::Modifiers::None, 'b' }, repeated(select(select_to_previous_word)) }, - { { Key::Modifiers::None, 'W' }, repeated(select(select_to_next_word)) }, - { { Key::Modifiers::None, 'E' }, repeated(select(select_to_next_word_end)) }, - { { Key::Modifiers::None, 'B' }, repeated(select(select_to_previous_word)) }, + { { Key::Modifiers::None, 'w' }, repeated(select(select_to_next_word)) }, + { { Key::Modifiers::None, 'e' }, repeated(select(select_to_next_word_end)) }, + { { Key::Modifiers::None, 'b' }, repeated(select(select_to_previous_word)) }, + { { Key::Modifiers::None, 'W' }, repeated(select(select_to_next_word)) }, + { { Key::Modifiers::None, 'E' }, repeated(select(select_to_next_word_end)) }, + { { Key::Modifiers::None, 'B' }, repeated(select(select_to_previous_word)) }, - { { Key::Modifiers::Alt, 'w' }, repeated(select(select_to_next_word)) }, - { { Key::Modifiers::Alt, 'e' }, repeated(select(select_to_next_word_end)) }, - { { Key::Modifiers::Alt, 'b' }, repeated(select(select_to_previous_word)) }, - { { Key::Modifiers::Alt, 'W' }, repeated(select(select_to_next_word)) }, - { { Key::Modifiers::Alt, 'E' }, repeated(select(select_to_next_word_end)) }, - { { Key::Modifiers::Alt, 'B' }, repeated(select(select_to_previous_word)) }, + { { Key::Modifiers::Alt, 'w' }, repeated(select(select_to_next_word)) }, + { { Key::Modifiers::Alt, 'e' }, repeated(select(select_to_next_word_end)) }, + { { Key::Modifiers::Alt, 'b' }, repeated(select(select_to_previous_word)) }, + { { Key::Modifiers::Alt, 'W' }, repeated(select(select_to_next_word)) }, + { { Key::Modifiers::Alt, 'E' }, repeated(select(select_to_next_word_end)) }, + { { Key::Modifiers::Alt, 'B' }, repeated(select(select_to_previous_word)) }, { { Key::Modifiers::Alt, 'l' }, repeated(select(select_to_eol)) }, { { Key::Modifiers::Alt, 'L' }, repeated(select(select_to_eol)) }, diff --git a/src/selectors.cc b/src/selectors.cc index fb478e50..fb344486 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -15,14 +15,14 @@ using Utf8Iterator = utf8::utf8_iterator +template bool is_word(Codepoint c) { return Kakoune::is_word(c); } template<> -bool is_word(Codepoint c) +bool is_word(Codepoint c) { return !is_blank(c) and !is_eol(c); } @@ -40,7 +40,7 @@ enum class CharCategories Punctuation, }; -template +template CharCategories categorize(Codepoint c) { if (is_word(c)) @@ -49,8 +49,8 @@ CharCategories categorize(Codepoint c) return CharCategories::EndOfLine; if (is_blank(c)) return CharCategories::Blank; - return punctuation_is_word ? CharCategories::Word - : CharCategories::Punctuation; + return word_type == WORD ? CharCategories::Word + : CharCategories::Punctuation; } template @@ -76,14 +76,13 @@ Range utf8_range(const Utf8Iterator& first, const Utf8Iterator& last) typedef boost::regex_iterator RegexIterator; -template +template Selection select_to_next_word(const Buffer& buffer, const Selection& selection) { Utf8Iterator begin = buffer.iterator_at(selection.last()); if (begin+1 == buffer.end()) return selection; - if (categorize(*begin) != - categorize(*(begin+1))) + if (categorize(*begin) != categorize(*(begin+1))) ++begin; skip_while(begin, buffer.end(), is_eol); @@ -91,26 +90,25 @@ Selection select_to_next_word(const Buffer& buffer, const Selection& selection) return selection; Utf8Iterator end = begin+1; - if (not punctuation_is_word and is_punctuation(*begin)) + if (word_type == Word and is_punctuation(*begin)) skip_while(end, buffer.end(), is_punctuation); - else if (is_word(*begin)) - skip_while(end, buffer.end(), is_word); + else if (is_word(*begin)) + skip_while(end, buffer.end(), is_word); skip_while(end, buffer.end(), is_blank); return utf8_range(begin, end-1); } -template Selection select_to_next_word(const Buffer&, const Selection&); -template Selection select_to_next_word(const Buffer&, const Selection&); +template Selection select_to_next_word(const Buffer&, const Selection&); +template Selection select_to_next_word(const Buffer&, const Selection&); -template +template Selection select_to_next_word_end(const Buffer& buffer, const Selection& selection) { Utf8Iterator begin = buffer.iterator_at(selection.last()); if (begin+1 == buffer.end()) return selection; - if (categorize(*begin) != - categorize(*(begin+1))) + if (categorize(*begin) != categorize(*(begin+1))) ++begin; skip_while(begin, buffer.end(), is_eol); @@ -119,24 +117,23 @@ Selection select_to_next_word_end(const Buffer& buffer, const Selection& selecti Utf8Iterator end = begin; skip_while(end, buffer.end(), is_blank); - if (not punctuation_is_word and is_punctuation(*end)) + if (word_type == Word and is_punctuation(*end)) skip_while(end, buffer.end(), is_punctuation); - else if (is_word(*end)) - skip_while(end, buffer.end(), is_word); + else if (is_word(*end)) + skip_while(end, buffer.end(), is_word); return utf8_range(begin, end-1); } -template Selection select_to_next_word_end(const Buffer&, const Selection&); -template Selection select_to_next_word_end(const Buffer&, const Selection&); +template Selection select_to_next_word_end(const Buffer&, const Selection&); +template Selection select_to_next_word_end(const Buffer&, const Selection&); -template +template Selection select_to_previous_word(const Buffer& buffer, const Selection& selection) { Utf8Iterator begin = buffer.iterator_at(selection.last()); if (begin+1 == buffer.end()) return selection; - if (categorize(*begin) != - categorize(*(begin-1))) + if (categorize(*begin) != categorize(*(begin-1))) --begin; skip_while_reverse(begin, buffer.begin(), is_eol); @@ -144,21 +141,21 @@ Selection select_to_previous_word(const Buffer& buffer, const Selection& selecti skip_while_reverse(end, buffer.begin(), is_blank); bool with_end = false; - if (not punctuation_is_word and is_punctuation(*end)) + if (word_type == Word and is_punctuation(*end)) { skip_while_reverse(end, buffer.begin(), is_punctuation); with_end = is_punctuation(*end); } - else if (is_word(*end)) + else if (is_word(*end)) { - skip_while_reverse(end, buffer.begin(), is_word); - with_end = is_word(*end); + skip_while_reverse(end, buffer.begin(), is_word); + with_end = is_word(*end); } return utf8_range(begin, with_end ? end : end+1); } -template Selection select_to_previous_word(const Buffer&, const Selection&); -template Selection select_to_previous_word(const Buffer&, const Selection&); +template Selection select_to_previous_word(const Buffer&, const Selection&); +template Selection select_to_previous_word(const Buffer&, const Selection&); Selection select_line(const Buffer& buffer, const Selection& selection) { @@ -357,22 +354,22 @@ Selection select_to_eol_reverse(const Buffer& buffer, const Selection& selection return utf8_range(begin, end == buffer.begin() ? end : end+1); } -template +template Selection select_whole_word(const Buffer& buffer, const Selection& selection, ObjectFlags flags) { Utf8Iterator first = buffer.iterator_at(selection.last()); Utf8Iterator last = first; - if (is_word(*first)) + if (is_word(*first)) { if (flags & ObjectFlags::ToBegin) { - skip_while_reverse(first, buffer.begin(), is_word); - if (not is_word(*first)) + skip_while_reverse(first, buffer.begin(), is_word); + if (not is_word(*first)) ++first; } if (flags & ObjectFlags::ToEnd) { - skip_while(last, buffer.end(), is_word); + skip_while(last, buffer.end(), is_word); if (not (flags & ObjectFlags::Inner)) skip_while(last, buffer.end(), is_blank); --last; @@ -383,10 +380,10 @@ Selection select_whole_word(const Buffer& buffer, const Selection& selection, Ob if (flags & ObjectFlags::ToBegin) { skip_while_reverse(first, buffer.begin(), is_blank); - if (not is_word(*first)) + if (not is_word(*first)) return selection; - skip_while_reverse(first, buffer.begin(), is_word); - if (not is_word(*first)) + skip_while_reverse(first, buffer.begin(), is_word); + if (not is_word(*first)) ++first; } if (flags & ObjectFlags::ToEnd) @@ -398,8 +395,8 @@ Selection select_whole_word(const Buffer& buffer, const Selection& selection, Ob return (flags & ObjectFlags::ToEnd) ? utf8_range(first, last) : utf8_range(last, first); } -template Selection select_whole_word(const Buffer&, const Selection&, ObjectFlags); -template Selection select_whole_word(const Buffer&, const Selection&, ObjectFlags); +template Selection select_whole_word(const Buffer&, const Selection&, ObjectFlags); +template Selection select_whole_word(const Buffer&, const Selection&, ObjectFlags); Selection select_whole_sentence(const Buffer& buffer, const Selection& selection, ObjectFlags flags) { diff --git a/src/selectors.hh b/src/selectors.hh index 79c62ebc..997f144d 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -7,13 +7,15 @@ namespace Kakoune { -template +enum WordType { Word, WORD }; + +template Selection select_to_next_word(const Buffer& buffer, const Selection& selection); -template +template Selection select_to_next_word_end(const Buffer& buffer, const Selection& selection); -template +template Selection select_to_previous_word(const Buffer& buffer, const Selection& selection); @@ -41,7 +43,7 @@ constexpr bool operator&(ObjectFlags lhs, ObjectFlags rhs) constexpr ObjectFlags operator|(ObjectFlags lhs, ObjectFlags rhs) { return (ObjectFlags)((int)lhs | (int) rhs); } -template +template Selection select_whole_word(const Buffer& buffer, const Selection& selection, ObjectFlags flags); Selection select_whole_sentence(const Buffer& buffer, const Selection& selection,