From ad7145dda445c26ad5b0d01a78f05700126dbd98 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 1 Jan 2014 18:45:28 +0000 Subject: [PATCH] Minor code cleanup in normal.cc --- src/normal.cc | 154 ++++++++++++++++++++++++++------------------------ 1 file changed, 80 insertions(+), 74 deletions(-) diff --git a/src/normal.cc b/src/normal.cc index ba821076..2426d9a8 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -113,59 +113,64 @@ enum class SelectMode ReplaceMain, }; +template +void select(Context& context, Func func) +{ + auto& buffer = context.buffer(); + auto& selections = context.selections(); + if (mode == SelectMode::Append) + { + auto& sel = selections.main(); + auto res = func(buffer, sel); + if (res.captures().empty()) + res.captures() = sel.captures(); + selections.push_back(res); + selections.set_main_index(selections.size() - 1); + } + else if (mode == SelectMode::ReplaceMain) + { + auto& sel = selections.main(); + auto res = func(buffer, sel); + sel.first() = res.first(); + sel.last() = res.last(); + if (not res.captures().empty()) + sel.captures() = std::move(res.captures()); + } + else + { + for (auto& sel : selections) + { + auto res = func(buffer, sel); + if (mode == SelectMode::Extend) + sel.merge_with(res); + else + { + sel.first() = res.first(); + sel.last() = res.last(); + } + if (not res.captures().empty()) + sel.captures() = std::move(res.captures()); + } + } + selections.sort_and_merge_overlapping(); + selections.check_invariant(); +} + template class Select { public: constexpr Select(T t) : m_func(t) {} - - void operator() (Context& context, int) - { - auto& buffer = context.buffer(); - auto& selections = context.selections(); - if (mode == SelectMode::Append) - { - auto& sel = selections.main(); - auto res = m_func(buffer, sel); - if (res.captures().empty()) - res.captures() = sel.captures(); - selections.push_back(res); - selections.set_main_index(selections.size() - 1); - } - else if (mode == SelectMode::ReplaceMain) - { - auto& sel = selections.main(); - auto res = m_func(buffer, sel); - sel.first() = res.first(); - sel.last() = res.last(); - if (not res.captures().empty()) - sel.captures() = std::move(res.captures()); - } - else - { - for (auto& sel : selections) - { - auto res = m_func(buffer, sel); - if (mode == SelectMode::Extend) - sel.merge_with(res); - else - { - sel.first() = res.first(); - sel.last() = res.last(); - } - if (not res.captures().empty()) - sel.captures() = std::move(res.captures()); - } - } - selections.sort_and_merge_overlapping(); - selections.check_invariant(); - } + void operator() (Context& context, int) { select(context, m_func); } private: T m_func; }; template -constexpr Select select(T func) { return Select(func); } +constexpr Select make_select(T func) +{ + return Select(func); +} template void select_coord(const Buffer& buffer, BufferCoord coord, SelectionList& selections) @@ -240,10 +245,10 @@ void goto_commands(Context& context, int line) select_coord(buffer, BufferCoord{0,0}, context.selections()); break; case 'l': - select(select_to_eol)(context, 0); + select(context, select_to_eol); break; case 'h': - select(select_to_eol_reverse)(context, 0); + select(context, select_to_eol_reverse); break; case 'j': { @@ -720,8 +725,8 @@ void split_lines(Context& context, int) void join_select_spaces(Context& context, int) { - select(select_whole_lines)(context, 0); - select(select_to_eol)(context, 0); + select(context, select_whole_lines); + select(context, select_to_eol); auto& buffer = context.buffer(); auto& selections = context.selections(); select_all_matches(buffer, selections, Regex{"(\n\\h*)+"}); @@ -847,7 +852,7 @@ void select_object(Context& context, int param) for (auto& sel : selectors) { if (c == sel.key) - return select(std::bind(sel.func, _1, _2, flags))(context, 0); + return select(context, std::bind(sel.func, _1, _2, flags)); } static constexpr struct @@ -862,12 +867,12 @@ void select_object(Context& context, int param) { { '"', '"' }, 'Q' }, { { '\'', '\'' }, 'q' }, }; - for (auto& sur : surrounding_pairs ) + for (auto& sur : surrounding_pairs) { if (sur.pair.first == c or sur.pair.second == c or (sur.name != 0 and sur.name == c)) - return select(std::bind(select_surrounding, _1, _2, - sur.pair, level, flags))(context, 0); + return select(context, std::bind(select_surrounding, _1, _2, + sur.pair, level, flags)); } }, "select object", "b,(,): parenthesis block\n" @@ -948,8 +953,9 @@ void select_to_next_char(Context& context, int param) { on_next_key_with_autoinfo(context, [param](Key key, Context& context) { select( + context, std::bind(flags & SelectFlags::Reverse ? select_to_reverse : select_to, - _1, _2, key.key, param, flags & SelectFlags::Inclusive))(context, 0); + _1, _2, key.key, param, flags & SelectFlags::Inclusive)); }, "select to next char","enter char to select to"); } @@ -1270,32 +1276,32 @@ KeyMap keymap = else keep_selection(context.selections(), count-1); } }, { alt(' '), [](Context& context, int count) { if (count == 0) flip_selections(context.selections()); else remove_selection(context.selections(), count-1); } }, - { 'w', repeated(select(select_to_next_word)) }, - { 'e', repeated(select(select_to_next_word_end)) }, - { 'b', repeated(select(select_to_previous_word)) }, - { 'W', repeated(select(select_to_next_word)) }, - { 'E', repeated(select(select_to_next_word_end)) }, - { 'B', repeated(select(select_to_previous_word)) }, + { 'w', repeated(make_select(select_to_next_word)) }, + { 'e', repeated(make_select(select_to_next_word_end)) }, + { 'b', repeated(make_select(select_to_previous_word)) }, + { 'W', repeated(make_select(select_to_next_word)) }, + { 'E', repeated(make_select(select_to_next_word_end)) }, + { 'B', repeated(make_select(select_to_previous_word)) }, - { alt('w'), repeated(select(select_to_next_word)) }, - { alt('e'), repeated(select(select_to_next_word_end)) }, - { alt('b'), repeated(select(select_to_previous_word)) }, - { alt('W'), repeated(select(select_to_next_word)) }, - { alt('E'), repeated(select(select_to_next_word_end)) }, - { alt('B'), repeated(select(select_to_previous_word)) }, + { alt('w'), repeated(make_select(select_to_next_word)) }, + { alt('e'), repeated(make_select(select_to_next_word_end)) }, + { alt('b'), repeated(make_select(select_to_previous_word)) }, + { alt('W'), repeated(make_select(select_to_next_word)) }, + { alt('E'), repeated(make_select(select_to_next_word_end)) }, + { alt('B'), repeated(make_select(select_to_previous_word)) }, - { alt('l'), repeated(select(select_to_eol)) }, - { alt('L'), repeated(select(select_to_eol)) }, - { alt('h'), repeated(select(select_to_eol_reverse)) }, - { alt('H'), repeated(select(select_to_eol_reverse)) }, + { alt('l'), repeated(make_select(select_to_eol)) }, + { alt('L'), repeated(make_select(select_to_eol)) }, + { alt('h'), repeated(make_select(select_to_eol_reverse)) }, + { alt('H'), repeated(make_select(select_to_eol_reverse)) }, - { 'x', repeated(select(select_line)) }, - { 'X', repeated(select(select_line)) }, - { alt('x'), select(select_whole_lines) }, - { alt('X'), select(trim_partial_lines) }, + { 'x', repeated(make_select(select_line)) }, + { 'X', repeated(make_select(select_line)) }, + { alt('x'), make_select(select_whole_lines) }, + { alt('X'), make_select(trim_partial_lines) }, - { 'm', select(select_matching) }, - { 'M', select(select_matching) }, + { 'm', make_select(select_matching) }, + { 'M', make_select(select_matching) }, { '/', search }, { '?', search },