Use WordType enum instead of a bool punctuation_is_word for word selector

This commit is contained in:
Maxime Coste 2013-10-07 18:44:22 +01:00
parent 6e5ff644f2
commit fccb954611
3 changed files with 57 additions and 58 deletions

View File

@ -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, '"' }, 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<false>, _1, _2, flags) }, { { Key::Modifiers::None, 'w' }, std::bind(select_whole_word<Word>, _1, _2, flags) },
{ { Key::Modifiers::None, 'W' }, std::bind(select_whole_word<true>, _1, _2, flags) }, { { Key::Modifiers::None, 'W' }, std::bind(select_whole_word<WORD>, _1, _2, flags) },
{ { Key::Modifiers::None, 's' }, std::bind(select_whole_sentence, _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, 'p' }, std::bind(select_whole_paragraph, _1, _2, flags) },
{ { Key::Modifiers::None, 'i' }, std::bind(select_whole_indent, _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(); { { Key::Modifiers::Alt, ' ' }, [](Context& context) { int count = context.numeric_param();
if (count == 0) context.editor().flip_selections(); if (count == 0) context.editor().flip_selections();
else context.editor().remove_selection(count-1); } }, else context.editor().remove_selection(count-1); } },
{ { Key::Modifiers::None, 'w' }, repeated(select<SelectMode::Replace>(select_to_next_word<false>)) }, { { Key::Modifiers::None, 'w' }, repeated(select<SelectMode::Replace>(select_to_next_word<Word>)) },
{ { Key::Modifiers::None, 'e' }, repeated(select<SelectMode::Replace>(select_to_next_word_end<false>)) }, { { Key::Modifiers::None, 'e' }, repeated(select<SelectMode::Replace>(select_to_next_word_end<Word>)) },
{ { Key::Modifiers::None, 'b' }, repeated(select<SelectMode::Replace>(select_to_previous_word<false>)) }, { { Key::Modifiers::None, 'b' }, repeated(select<SelectMode::Replace>(select_to_previous_word<Word>)) },
{ { Key::Modifiers::None, 'W' }, repeated(select<SelectMode::Extend>(select_to_next_word<false>)) }, { { Key::Modifiers::None, 'W' }, repeated(select<SelectMode::Extend>(select_to_next_word<Word>)) },
{ { Key::Modifiers::None, 'E' }, repeated(select<SelectMode::Extend>(select_to_next_word_end<false>)) }, { { Key::Modifiers::None, 'E' }, repeated(select<SelectMode::Extend>(select_to_next_word_end<Word>)) },
{ { Key::Modifiers::None, 'B' }, repeated(select<SelectMode::Extend>(select_to_previous_word<false>)) }, { { Key::Modifiers::None, 'B' }, repeated(select<SelectMode::Extend>(select_to_previous_word<Word>)) },
{ { Key::Modifiers::Alt, 'w' }, repeated(select<SelectMode::Replace>(select_to_next_word<true>)) }, { { Key::Modifiers::Alt, 'w' }, repeated(select<SelectMode::Replace>(select_to_next_word<WORD>)) },
{ { Key::Modifiers::Alt, 'e' }, repeated(select<SelectMode::Replace>(select_to_next_word_end<true>)) }, { { Key::Modifiers::Alt, 'e' }, repeated(select<SelectMode::Replace>(select_to_next_word_end<WORD>)) },
{ { Key::Modifiers::Alt, 'b' }, repeated(select<SelectMode::Replace>(select_to_previous_word<true>)) }, { { Key::Modifiers::Alt, 'b' }, repeated(select<SelectMode::Replace>(select_to_previous_word<WORD>)) },
{ { Key::Modifiers::Alt, 'W' }, repeated(select<SelectMode::Extend>(select_to_next_word<true>)) }, { { Key::Modifiers::Alt, 'W' }, repeated(select<SelectMode::Extend>(select_to_next_word<WORD>)) },
{ { Key::Modifiers::Alt, 'E' }, repeated(select<SelectMode::Extend>(select_to_next_word_end<true>)) }, { { Key::Modifiers::Alt, 'E' }, repeated(select<SelectMode::Extend>(select_to_next_word_end<WORD>)) },
{ { Key::Modifiers::Alt, 'B' }, repeated(select<SelectMode::Extend>(select_to_previous_word<true>)) }, { { Key::Modifiers::Alt, 'B' }, repeated(select<SelectMode::Extend>(select_to_previous_word<WORD>)) },
{ { Key::Modifiers::Alt, 'l' }, repeated(select<SelectMode::Replace>(select_to_eol)) }, { { Key::Modifiers::Alt, 'l' }, repeated(select<SelectMode::Replace>(select_to_eol)) },
{ { Key::Modifiers::Alt, 'L' }, repeated(select<SelectMode::Extend>(select_to_eol)) }, { { Key::Modifiers::Alt, 'L' }, repeated(select<SelectMode::Extend>(select_to_eol)) },

View File

@ -15,14 +15,14 @@ using Utf8Iterator = utf8::utf8_iterator<BufferIterator, utf8::InvalidBytePolicy
namespace namespace
{ {
template<bool punctuation_is_word = false> template<WordType word_type = Word>
bool is_word(Codepoint c) bool is_word(Codepoint c)
{ {
return Kakoune::is_word(c); return Kakoune::is_word(c);
} }
template<> template<>
bool is_word<true>(Codepoint c) bool is_word<WORD>(Codepoint c)
{ {
return !is_blank(c) and !is_eol(c); return !is_blank(c) and !is_eol(c);
} }
@ -40,7 +40,7 @@ enum class CharCategories
Punctuation, Punctuation,
}; };
template<bool punctuation_is_word = false> template<WordType word_type = Word>
CharCategories categorize(Codepoint c) CharCategories categorize(Codepoint c)
{ {
if (is_word(c)) if (is_word(c))
@ -49,7 +49,7 @@ CharCategories categorize(Codepoint c)
return CharCategories::EndOfLine; return CharCategories::EndOfLine;
if (is_blank(c)) if (is_blank(c))
return CharCategories::Blank; return CharCategories::Blank;
return punctuation_is_word ? CharCategories::Word return word_type == WORD ? CharCategories::Word
: CharCategories::Punctuation; : CharCategories::Punctuation;
} }
@ -76,14 +76,13 @@ Range utf8_range(const Utf8Iterator& first, const Utf8Iterator& last)
typedef boost::regex_iterator<BufferIterator> RegexIterator; typedef boost::regex_iterator<BufferIterator> RegexIterator;
template<bool punctuation_is_word> template<WordType word_type>
Selection select_to_next_word(const Buffer& buffer, const Selection& selection) Selection select_to_next_word(const Buffer& buffer, const Selection& selection)
{ {
Utf8Iterator begin = buffer.iterator_at(selection.last()); Utf8Iterator begin = buffer.iterator_at(selection.last());
if (begin+1 == buffer.end()) if (begin+1 == buffer.end())
return selection; return selection;
if (categorize<punctuation_is_word>(*begin) != if (categorize<word_type>(*begin) != categorize<word_type>(*(begin+1)))
categorize<punctuation_is_word>(*(begin+1)))
++begin; ++begin;
skip_while(begin, buffer.end(), is_eol); skip_while(begin, buffer.end(), is_eol);
@ -91,26 +90,25 @@ Selection select_to_next_word(const Buffer& buffer, const Selection& selection)
return selection; return selection;
Utf8Iterator end = begin+1; 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); skip_while(end, buffer.end(), is_punctuation);
else if (is_word<punctuation_is_word>(*begin)) else if (is_word<word_type>(*begin))
skip_while(end, buffer.end(), is_word<punctuation_is_word>); skip_while(end, buffer.end(), is_word<word_type>);
skip_while(end, buffer.end(), is_blank); skip_while(end, buffer.end(), is_blank);
return utf8_range(begin, end-1); return utf8_range(begin, end-1);
} }
template Selection select_to_next_word<false>(const Buffer&, const Selection&); template Selection select_to_next_word<Word>(const Buffer&, const Selection&);
template Selection select_to_next_word<true>(const Buffer&, const Selection&); template Selection select_to_next_word<WORD>(const Buffer&, const Selection&);
template<bool punctuation_is_word> template<WordType word_type>
Selection select_to_next_word_end(const Buffer& buffer, const Selection& selection) Selection select_to_next_word_end(const Buffer& buffer, const Selection& selection)
{ {
Utf8Iterator begin = buffer.iterator_at(selection.last()); Utf8Iterator begin = buffer.iterator_at(selection.last());
if (begin+1 == buffer.end()) if (begin+1 == buffer.end())
return selection; return selection;
if (categorize<punctuation_is_word>(*begin) != if (categorize<word_type>(*begin) != categorize<word_type>(*(begin+1)))
categorize<punctuation_is_word>(*(begin+1)))
++begin; ++begin;
skip_while(begin, buffer.end(), is_eol); 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; Utf8Iterator end = begin;
skip_while(end, buffer.end(), is_blank); 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); skip_while(end, buffer.end(), is_punctuation);
else if (is_word<punctuation_is_word>(*end)) else if (is_word<word_type>(*end))
skip_while(end, buffer.end(), is_word<punctuation_is_word>); skip_while(end, buffer.end(), is_word<word_type>);
return utf8_range(begin, end-1); return utf8_range(begin, end-1);
} }
template Selection select_to_next_word_end<false>(const Buffer&, const Selection&); template Selection select_to_next_word_end<Word>(const Buffer&, const Selection&);
template Selection select_to_next_word_end<true>(const Buffer&, const Selection&); template Selection select_to_next_word_end<WORD>(const Buffer&, const Selection&);
template<bool punctuation_is_word> template<WordType word_type>
Selection select_to_previous_word(const Buffer& buffer, const Selection& selection) Selection select_to_previous_word(const Buffer& buffer, const Selection& selection)
{ {
Utf8Iterator begin = buffer.iterator_at(selection.last()); Utf8Iterator begin = buffer.iterator_at(selection.last());
if (begin+1 == buffer.end()) if (begin+1 == buffer.end())
return selection; return selection;
if (categorize<punctuation_is_word>(*begin) != if (categorize<word_type>(*begin) != categorize<word_type>(*(begin-1)))
categorize<punctuation_is_word>(*(begin-1)))
--begin; --begin;
skip_while_reverse(begin, buffer.begin(), is_eol); 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); skip_while_reverse(end, buffer.begin(), is_blank);
bool with_end = false; 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); skip_while_reverse(end, buffer.begin(), is_punctuation);
with_end = is_punctuation(*end); with_end = is_punctuation(*end);
} }
else if (is_word<punctuation_is_word>(*end)) else if (is_word<word_type>(*end))
{ {
skip_while_reverse(end, buffer.begin(), is_word<punctuation_is_word>); skip_while_reverse(end, buffer.begin(), is_word<word_type>);
with_end = is_word<punctuation_is_word>(*end); with_end = is_word<word_type>(*end);
} }
return utf8_range(begin, with_end ? end : end+1); return utf8_range(begin, with_end ? end : end+1);
} }
template Selection select_to_previous_word<false>(const Buffer&, const Selection&); template Selection select_to_previous_word<Word>(const Buffer&, const Selection&);
template Selection select_to_previous_word<true>(const Buffer&, const Selection&); template Selection select_to_previous_word<WORD>(const Buffer&, const Selection&);
Selection select_line(const Buffer& buffer, const Selection& 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); return utf8_range(begin, end == buffer.begin() ? end : end+1);
} }
template<bool punctuation_is_word> template<WordType word_type>
Selection select_whole_word(const Buffer& buffer, const Selection& selection, ObjectFlags flags) Selection select_whole_word(const Buffer& buffer, const Selection& selection, ObjectFlags flags)
{ {
Utf8Iterator first = buffer.iterator_at(selection.last()); Utf8Iterator first = buffer.iterator_at(selection.last());
Utf8Iterator last = first; Utf8Iterator last = first;
if (is_word<punctuation_is_word>(*first)) if (is_word<word_type>(*first))
{ {
if (flags & ObjectFlags::ToBegin) if (flags & ObjectFlags::ToBegin)
{ {
skip_while_reverse(first, buffer.begin(), is_word<punctuation_is_word>); skip_while_reverse(first, buffer.begin(), is_word<word_type>);
if (not is_word<punctuation_is_word>(*first)) if (not is_word<word_type>(*first))
++first; ++first;
} }
if (flags & ObjectFlags::ToEnd) if (flags & ObjectFlags::ToEnd)
{ {
skip_while(last, buffer.end(), is_word<punctuation_is_word>); skip_while(last, buffer.end(), is_word<word_type>);
if (not (flags & ObjectFlags::Inner)) if (not (flags & ObjectFlags::Inner))
skip_while(last, buffer.end(), is_blank); skip_while(last, buffer.end(), is_blank);
--last; --last;
@ -383,10 +380,10 @@ Selection select_whole_word(const Buffer& buffer, const Selection& selection, Ob
if (flags & ObjectFlags::ToBegin) if (flags & ObjectFlags::ToBegin)
{ {
skip_while_reverse(first, buffer.begin(), is_blank); skip_while_reverse(first, buffer.begin(), is_blank);
if (not is_word<punctuation_is_word>(*first)) if (not is_word<word_type>(*first))
return selection; return selection;
skip_while_reverse(first, buffer.begin(), is_word<punctuation_is_word>); skip_while_reverse(first, buffer.begin(), is_word<word_type>);
if (not is_word<punctuation_is_word>(*first)) if (not is_word<word_type>(*first))
++first; ++first;
} }
if (flags & ObjectFlags::ToEnd) 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) return (flags & ObjectFlags::ToEnd) ? utf8_range(first, last)
: utf8_range(last, first); : utf8_range(last, first);
} }
template Selection select_whole_word<false>(const Buffer&, const Selection&, ObjectFlags); template Selection select_whole_word<Word>(const Buffer&, const Selection&, ObjectFlags);
template Selection select_whole_word<true>(const Buffer&, const Selection&, ObjectFlags); template Selection select_whole_word<WORD>(const Buffer&, const Selection&, ObjectFlags);
Selection select_whole_sentence(const Buffer& buffer, const Selection& selection, ObjectFlags flags) Selection select_whole_sentence(const Buffer& buffer, const Selection& selection, ObjectFlags flags)
{ {

View File

@ -7,13 +7,15 @@
namespace Kakoune namespace Kakoune
{ {
template<bool punctuation_is_word> enum WordType { Word, WORD };
template<WordType word_type>
Selection select_to_next_word(const Buffer& buffer, Selection select_to_next_word(const Buffer& buffer,
const Selection& selection); const Selection& selection);
template<bool punctuation_is_word> template<WordType word_type>
Selection select_to_next_word_end(const Buffer& buffer, Selection select_to_next_word_end(const Buffer& buffer,
const Selection& selection); const Selection& selection);
template<bool punctuation_is_word> template<WordType word_type>
Selection select_to_previous_word(const Buffer& buffer, Selection select_to_previous_word(const Buffer& buffer,
const Selection& selection); const Selection& selection);
@ -41,7 +43,7 @@ constexpr bool operator&(ObjectFlags lhs, ObjectFlags rhs)
constexpr ObjectFlags operator|(ObjectFlags lhs, ObjectFlags rhs) constexpr ObjectFlags operator|(ObjectFlags lhs, ObjectFlags rhs)
{ return (ObjectFlags)((int)lhs | (int) rhs); } { return (ObjectFlags)((int)lhs | (int) rhs); }
template<bool punctuation_is_word> template<WordType word_type>
Selection select_whole_word(const Buffer& buffer, const Selection& selection, Selection select_whole_word(const Buffer& buffer, const Selection& selection,
ObjectFlags flags); ObjectFlags flags);
Selection select_whole_sentence(const Buffer& buffer, const Selection& selection, Selection select_whole_sentence(const Buffer& buffer, const Selection& selection,