generalize do_select_surrounding in do_select_object and add a whole word selector
This commit is contained in:
parent
df0f7b4689
commit
cd615b35a2
43
src/main.cc
43
src/main.cc
|
@ -691,29 +691,30 @@ void do_join(Editor& editor, int count)
|
||||||
editor.move_selections({0, -1});
|
editor.move_selections({0, -1});
|
||||||
}
|
}
|
||||||
|
|
||||||
template<bool inside>
|
template<bool inner>
|
||||||
void do_select_surrounding(Editor& editor, int count)
|
void do_select_object(Editor& editor, int count)
|
||||||
{
|
{
|
||||||
Key key = get_key();
|
typedef std::function<SelectionAndCaptures (const Selection&)> Selector;
|
||||||
const char id = key.key;
|
static const std::unordered_map<Key, Selector> key_to_selector =
|
||||||
|
|
||||||
static const std::unordered_map<char, std::pair<char, char>> id_to_matching =
|
|
||||||
{
|
{
|
||||||
{ '(', { '(', ')' } },
|
{ { Key::Modifiers::None, '(' }, std::bind(select_surrounding, _1, std::pair<char, char>{ '(', ')' }, inner) },
|
||||||
{ ')', { '(', ')' } },
|
{ { Key::Modifiers::None, ')' }, std::bind(select_surrounding, _1, std::pair<char, char>{ '(', ')' }, inner) },
|
||||||
{ 'b', { '(', ')' } },
|
{ { Key::Modifiers::None, 'b' }, std::bind(select_surrounding, _1, std::pair<char, char>{ '(', ')' }, inner) },
|
||||||
{ '{', { '{', '}' } },
|
{ { Key::Modifiers::None, '{' }, std::bind(select_surrounding, _1, std::pair<char, char>{ '{', '}' }, inner) },
|
||||||
{ '}', { '{', '}' } },
|
{ { Key::Modifiers::None, '}' }, std::bind(select_surrounding, _1, std::pair<char, char>{ '{', '}' }, inner) },
|
||||||
{ 'B', { '{', '}' } },
|
{ { Key::Modifiers::None, 'B' }, std::bind(select_surrounding, _1, std::pair<char, char>{ '{', '}' }, inner) },
|
||||||
{ '[', { '[', ']' } },
|
{ { Key::Modifiers::None, '[' }, std::bind(select_surrounding, _1, std::pair<char, char>{ '[', ']' }, inner) },
|
||||||
{ ']', { '[', ']' } },
|
{ { Key::Modifiers::None, ']' }, std::bind(select_surrounding, _1, std::pair<char, char>{ '[', ']' }, inner) },
|
||||||
{ '<', { '<', '>' } },
|
{ { Key::Modifiers::None, '<' }, std::bind(select_surrounding, _1, std::pair<char, char>{ '<', '>' }, inner) },
|
||||||
{ '>', { '<', '>' } }
|
{ { Key::Modifiers::None, '>' }, std::bind(select_surrounding, _1, std::pair<char, char>{ '<', '>' }, inner) },
|
||||||
|
{ { Key::Modifiers::None, 'w' }, std::bind(select_whole_word<false>, _1, inner) },
|
||||||
|
{ { Key::Modifiers::None, 'W' }, std::bind(select_whole_word<true>, _1, inner) },
|
||||||
};
|
};
|
||||||
|
|
||||||
auto matching = id_to_matching.find(id);
|
Key key = get_key();
|
||||||
if (matching != id_to_matching.end())
|
auto it = key_to_selector.find(key);
|
||||||
editor.select(std::bind(select_surrounding, _1, matching->second, inside));
|
if (it != key_to_selector.end())
|
||||||
|
editor.select(it->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unordered_map<Key, std::function<void (Editor& editor, int count)>> keymap =
|
std::unordered_map<Key, std::function<void (Editor& editor, int count)>> keymap =
|
||||||
|
@ -779,8 +780,8 @@ std::unordered_map<Key, std::function<void (Editor& editor, int count)>> keymap
|
||||||
{ { Key::Modifiers::None, 'u' }, [](Editor& editor, int count) { do { if (not editor.undo()) { NCurses::print_status("nothing left to undo"); break; } } while(--count > 0); } },
|
{ { Key::Modifiers::None, 'u' }, [](Editor& editor, int count) { do { if (not editor.undo()) { NCurses::print_status("nothing left to undo"); break; } } while(--count > 0); } },
|
||||||
{ { Key::Modifiers::None, 'U' }, [](Editor& editor, int count) { do { if (not editor.redo()) { NCurses::print_status("nothing left to redo"); break; } } while(--count > 0); } },
|
{ { Key::Modifiers::None, 'U' }, [](Editor& editor, int count) { do { if (not editor.redo()) { NCurses::print_status("nothing left to redo"); break; } } while(--count > 0); } },
|
||||||
|
|
||||||
{ { Key::Modifiers::Alt, 'i' }, do_select_surrounding<true> },
|
{ { Key::Modifiers::Alt, 'i' }, do_select_object<true> },
|
||||||
{ { Key::Modifiers::Alt, 'a' }, do_select_surrounding<false> },
|
{ { Key::Modifiers::Alt, 'a' }, do_select_object<false> },
|
||||||
|
|
||||||
{ { Key::Modifiers::Alt, 't' }, [](Editor& editor, int count) { editor.select(std::bind(select_to_reverse, _1, get_key().key, count, false)); } },
|
{ { Key::Modifiers::Alt, 't' }, [](Editor& editor, int count) { editor.select(std::bind(select_to_reverse, _1, get_key().key, count, false)); } },
|
||||||
{ { Key::Modifiers::Alt, 'f' }, [](Editor& editor, int count) { editor.select(std::bind(select_to_reverse, _1, get_key().key, count, true)); } },
|
{ { Key::Modifiers::Alt, 'f' }, [](Editor& editor, int count) { editor.select(std::bind(select_to_reverse, _1, get_key().key, count, true)); } },
|
||||||
|
|
|
@ -319,6 +319,34 @@ SelectionAndCaptures select_to_eol_reverse(const Selection& selection)
|
||||||
return Selection(begin, end.is_begin() ? end : end+1);
|
return Selection(begin, end.is_begin() ? end : end+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<bool punctuation_is_word>
|
||||||
|
SelectionAndCaptures select_whole_word(const Selection& selection, bool inner)
|
||||||
|
{
|
||||||
|
BufferIterator first = selection.last();
|
||||||
|
BufferIterator last = first;
|
||||||
|
if (is_word(*first))
|
||||||
|
{
|
||||||
|
if (not skip_while_reverse(first, is_word<punctuation_is_word>))
|
||||||
|
++first;
|
||||||
|
skip_while(last, is_word<punctuation_is_word>);
|
||||||
|
if (not inner)
|
||||||
|
skip_while(last, is_blank);
|
||||||
|
}
|
||||||
|
else if (not inner)
|
||||||
|
{
|
||||||
|
if (not skip_while_reverse(first, is_blank))
|
||||||
|
++first;
|
||||||
|
skip_while(last, is_blank);
|
||||||
|
if (not is_word<punctuation_is_word>(*last))
|
||||||
|
return selection;
|
||||||
|
skip_while(last, is_word<punctuation_is_word>);
|
||||||
|
}
|
||||||
|
--last;
|
||||||
|
return Selection(first, last);
|
||||||
|
}
|
||||||
|
template SelectionAndCaptures select_whole_word<false>(const Selection&, bool);
|
||||||
|
template SelectionAndCaptures select_whole_word<true>(const Selection&, bool);
|
||||||
|
|
||||||
SelectionAndCaptures select_whole_lines(const Selection& selection)
|
SelectionAndCaptures select_whole_lines(const Selection& selection)
|
||||||
{
|
{
|
||||||
BufferIterator first = selection.first();
|
BufferIterator first = selection.first();
|
||||||
|
|
|
@ -27,6 +27,8 @@ SelectionAndCaptures select_to_reverse(const Selection& selection,
|
||||||
SelectionAndCaptures select_to_eol(const Selection& selection);
|
SelectionAndCaptures select_to_eol(const Selection& selection);
|
||||||
SelectionAndCaptures select_to_eol_reverse(const Selection& selection);
|
SelectionAndCaptures select_to_eol_reverse(const Selection& selection);
|
||||||
|
|
||||||
|
template<bool punctuation_is_word>
|
||||||
|
SelectionAndCaptures select_whole_word(const Selection& selection, bool inner);
|
||||||
SelectionAndCaptures select_whole_lines(const Selection& selection);
|
SelectionAndCaptures select_whole_lines(const Selection& selection);
|
||||||
SelectionAndCaptures select_whole_buffer(const Selection& selection);
|
SelectionAndCaptures select_whole_buffer(const Selection& selection);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user