Selectors: add selectors for WORDs

WORDs are the same as vim's ones, i.e. a group of contiguous non blank
characters. They are bound as alt + word selector key.
This commit is contained in:
Maxime Coste 2011-10-25 18:46:14 +00:00
parent 97a279e229
commit 2c8f3229c0
3 changed files with 60 additions and 2 deletions

View File

@ -548,6 +548,12 @@ std::unordered_map<char, std::function<void (Window& window, int count)>> alt_ke
{ 'f', [](Window& window, int count) { window.select(std::bind(select_to_reverse, _1, getch(), count, true)); } }, { 'f', [](Window& window, int count) { window.select(std::bind(select_to_reverse, _1, getch(), count, true)); } },
{ 'T', [](Window& window, int count) { window.select(std::bind(select_to_reverse, _1, getch(), count, false), true); } }, { 'T', [](Window& window, int count) { window.select(std::bind(select_to_reverse, _1, getch(), count, false), true); } },
{ 'F', [](Window& window, int count) { window.select(std::bind(select_to_reverse, _1, getch(), count, true), true); } }, { 'F', [](Window& window, int count) { window.select(std::bind(select_to_reverse, _1, getch(), count, true), true); } },
{ 'w', [](Window& window, int count) { do { window.select(select_to_next_WORD); } while(--count > 0); } },
{ 'e', [](Window& window, int count) { do { window.select(select_to_next_WORD_end); } while(--count > 0); } },
{ 'b', [](Window& window, int count) { do { window.select(select_to_previous_WORD); } while(--count > 0); } },
{ 'W', [](Window& window, int count) { do { window.select(select_to_next_WORD, true); } while(--count > 0); } },
{ 'E', [](Window& window, int count) { do { window.select(select_to_next_WORD_end, true); } while(--count > 0); } },
{ 'B', [](Window& window, int count) { do { window.select(select_to_previous_WORD, true); } while(--count > 0); } },
}; };
int main(int argc, char* argv[]) int main(int argc, char* argv[])

View File

@ -41,6 +41,7 @@ enum class CharCategories
Punctuation, Punctuation,
}; };
template<bool punctuation_is_not_word = true>
static CharCategories categorize(char c) static CharCategories categorize(char c)
{ {
if (is_word(c)) if (is_word(c))
@ -49,7 +50,8 @@ static CharCategories categorize(char c)
return CharCategories::EndOfLine; return CharCategories::EndOfLine;
if (is_blank(c)) if (is_blank(c))
return CharCategories::Blank; return CharCategories::Blank;
return CharCategories::Punctuation; return punctuation_is_not_word ? CharCategories::Punctuation
: CharCategories::Word;
} }
template<typename T> template<typename T>
@ -66,7 +68,6 @@ void skip_while_reverse(BufferIterator& it, T condition)
--it; --it;
} }
Selection select_to_next_word(const BufferIterator& cursor) Selection select_to_next_word(const BufferIterator& cursor)
{ {
BufferIterator begin = cursor; BufferIterator begin = cursor;
@ -124,6 +125,54 @@ Selection select_to_previous_word(const BufferIterator& cursor)
return Selection(begin, end+1); return Selection(begin, end+1);
} }
Selection select_to_next_WORD(const BufferIterator& cursor)
{
BufferIterator begin = cursor;
if (categorize<false>(*begin) != categorize<false>(*(begin+1)))
++begin;
skip_while(begin, is_eol);
BufferIterator end = begin+1;
skip_while(end, [] (char c) { return !is_blank(c) and !is_eol(c); });
skip_while(end, is_blank);
return Selection(begin, end-1);
}
Selection select_to_next_WORD_end(const BufferIterator& cursor)
{
BufferIterator begin = cursor;
if (categorize<false>(*begin) != categorize<false>(*(begin+1)))
++begin;
skip_while(begin, is_eol);
BufferIterator end = begin+1;
skip_while(end, is_blank);
skip_while(end, [] (char c) { return !is_blank(c) and !is_eol(c); });
return Selection(begin, end-1);
}
Selection select_to_previous_WORD(const BufferIterator& cursor)
{
BufferIterator begin = cursor;
if (categorize<false>(*begin) != categorize<false>(*(begin+1)))
++begin;
skip_while_reverse(begin, is_eol);
BufferIterator end = begin+1;
skip_while_reverse(end, is_blank);
skip_while_reverse(end, [] (char c) { return !is_blank(c) and !is_eol(c); });
return Selection(begin, end+1);
}
Selection select_line(const BufferIterator& cursor) Selection select_line(const BufferIterator& cursor)
{ {
BufferIterator first = cursor; BufferIterator first = cursor;

View File

@ -9,6 +9,9 @@ namespace Kakoune
Selection select_to_next_word(const BufferIterator& cursor); Selection select_to_next_word(const BufferIterator& cursor);
Selection select_to_next_word_end(const BufferIterator& cursor); Selection select_to_next_word_end(const BufferIterator& cursor);
Selection select_to_previous_word(const BufferIterator& cursor); Selection select_to_previous_word(const BufferIterator& cursor);
Selection select_to_next_WORD(const BufferIterator& cursor);
Selection select_to_next_WORD_end(const BufferIterator& cursor);
Selection select_to_previous_WORD(const BufferIterator& cursor);
Selection select_line(const BufferIterator& cursor); Selection select_line(const BufferIterator& cursor);
Selection select_matching(const BufferIterator& cursor); Selection select_matching(const BufferIterator& cursor);