diff --git a/src/main.cc b/src/main.cc index 5461dcd2..15e31317 100644 --- a/src/main.cc +++ b/src/main.cc @@ -377,24 +377,6 @@ void do_rotate_selections(Context& context) context.editor().select(std::move(sels)); }; -template -class Repeated -{ -public: - Repeated(T t) : m_func(t) {} - - void operator() (Context& context) - { - int count = context.numeric_param(); - do { m_func(context); } while(--count > 0); - } -private: - T m_func; -}; - -template -Repeated repeated(T func) { return Repeated(func); } - enum class SelectFlags { None = 0, @@ -462,6 +444,41 @@ String runtime_directory() return String(buffer, ptr); } +template +class Repeated +{ +public: + constexpr Repeated(T t) : m_func(t) {} + + void operator() (Context& context) + { + int count = context.numeric_param(); + do { m_func(context); } while(--count > 0); + } +private: + T m_func; +}; + +template +constexpr Repeated repeated(T func) { return Repeated(func); } + +template +class Select +{ +public: + constexpr Select(T t) : m_func(t) {} + + void operator() (Context& context) + { + context.editor().select(m_func, mode); + } +private: + T m_func; +}; + +template +constexpr Select select(T func) { return Select(func); } + std::unordered_map> keymap = { { { Key::Modifiers::None, 'h' }, [](Context& context) { context.editor().move_selections(-CharCount(std::max(context.numeric_param(),1))); } }, @@ -515,16 +532,16 @@ std::unordered_map> 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([](Context& context) { context.editor().select(select_to_next_word); }) }, - { { Key::Modifiers::None, 'e' }, repeated([](Context& context) { context.editor().select(select_to_next_word_end); }) }, - { { Key::Modifiers::None, 'b' }, repeated([](Context& context) { context.editor().select(select_to_previous_word); }) }, - { { Key::Modifiers::None, 'W' }, repeated([](Context& context) { context.editor().select(select_to_next_word, SelectMode::Extend); }) }, - { { Key::Modifiers::None, 'E' }, repeated([](Context& context) { context.editor().select(select_to_next_word_end, SelectMode::Extend); }) }, - { { Key::Modifiers::None, 'B' }, repeated([](Context& context) { context.editor().select(select_to_previous_word, SelectMode::Extend); }) }, - { { Key::Modifiers::None, 'x' }, repeated([](Context& context) { context.editor().select(select_line, SelectMode::Replace); }) }, - { { Key::Modifiers::None, 'X' }, repeated([](Context& context) { context.editor().select(select_line, SelectMode::Extend); }) }, - { { Key::Modifiers::None, 'm' }, [](Context& context) { context.editor().select(select_matching); } }, - { { Key::Modifiers::None, 'M' }, [](Context& context) { context.editor().select(select_matching, SelectMode::Extend); } }, + { { 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, 'x' }, repeated(select(select_line)) }, + { { Key::Modifiers::None, 'X' }, repeated(select(select_line)) }, + { { Key::Modifiers::None, 'm' }, select(select_matching) }, + { { Key::Modifiers::None, 'M' }, select(select_matching) }, { { Key::Modifiers::None, '/' }, do_search }, { { Key::Modifiers::None, '?' }, do_search }, @@ -540,20 +557,20 @@ std::unordered_map> keymap = { { Key::Modifiers::Alt, 'i' }, do_select_object }, { { Key::Modifiers::Alt, 'a' }, do_select_object }, - { { Key::Modifiers::None, ']' }, do_select_object }, - { { Key::Modifiers::None, '[' }, do_select_object }, + { { Key::Modifiers::None, ']' }, do_select_object }, + { { Key::Modifiers::None, '[' }, do_select_object }, - { { Key::Modifiers::Alt, 'w' }, repeated([](Context& context) { context.editor().select(select_to_next_word); }) }, - { { Key::Modifiers::Alt, 'e' }, repeated([](Context& context) { context.editor().select(select_to_next_word_end); }) }, - { { Key::Modifiers::Alt, 'b' }, repeated([](Context& context) { context.editor().select(select_to_previous_word); }) }, - { { Key::Modifiers::Alt, 'W' }, repeated([](Context& context) { context.editor().select(select_to_next_word, SelectMode::Extend); }) }, - { { Key::Modifiers::Alt, 'E' }, repeated([](Context& context) { context.editor().select(select_to_next_word_end, SelectMode::Extend); }) }, - { { Key::Modifiers::Alt, 'B' }, repeated([](Context& context) { context.editor().select(select_to_previous_word, SelectMode::Extend); }) }, + { { 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([](Context& context) { context.editor().select(select_to_eol, SelectMode::Replace); }) }, - { { Key::Modifiers::Alt, 'L' }, repeated([](Context& context) { context.editor().select(select_to_eol, SelectMode::Extend); }) }, - { { Key::Modifiers::Alt, 'h' }, repeated([](Context& context) { context.editor().select(select_to_eol_reverse, SelectMode::Replace); }) }, - { { Key::Modifiers::Alt, 'H' }, repeated([](Context& context) { context.editor().select(select_to_eol_reverse, SelectMode::Extend); }) }, + { { Key::Modifiers::Alt, 'l' }, repeated(select(select_to_eol)) }, + { { Key::Modifiers::Alt, 'L' }, repeated(select(select_to_eol)) }, + { { Key::Modifiers::Alt, 'h' }, repeated(select(select_to_eol_reverse)) }, + { { Key::Modifiers::Alt, 'H' }, repeated(select(select_to_eol_reverse)) }, { { Key::Modifiers::Alt, 's' }, do_split_regex }, @@ -562,7 +579,7 @@ std::unordered_map> keymap = { { Key::Modifiers::None, '<' }, do_deindent }, { { Key::Modifiers::None, '>' }, do_indent }, - { { Key::Modifiers::Alt, 'x' }, [](Context& context) { context.editor().select(select_whole_lines); } }, + { { Key::Modifiers::Alt, 'x' }, select(select_whole_lines) }, { { Key::Modifiers::Alt, 'c' }, [](Context& context) { if (context.has_window()) context.window().center_selection(); } },