From 2d93933d81ea7e00b0541396e11087ef01c4ff7d Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 5 Mar 2015 19:57:46 +0000 Subject: [PATCH] Rework keymap to use function pointer rather than std::function --- src/normal.cc | 84 ++++++++++++++++++++++++--------------------------- src/normal.hh | 9 +++--- 2 files changed, 43 insertions(+), 50 deletions(-) diff --git a/src/normal.cc b/src/normal.cc index 06f71a13..02f5490c 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -31,15 +31,15 @@ enum class SelectMode Append, }; -template -void select(Context& context, Func func) +template +void select(Context& context, T func) { auto& buffer = context.buffer(); auto& selections = context.selections(); if (mode == SelectMode::Append) { auto& sel = selections.main(); - auto res = func(buffer, sel); + auto res = func(buffer, sel); if (res.captures().empty()) res.captures() = sel.captures(); selections.push_back(res); @@ -65,20 +65,10 @@ void select(Context& context, Func func) selections.check_invariant(); } -template -class Select +template +void select(Context& context, NormalParams) { -public: - constexpr Select(T t) : m_func(t) {} - void operator() (Context& context, NormalParams) { select(context, m_func); } -private: - T m_func; -}; - -template -constexpr Select make_select(T func) -{ - return Select(func); + select(context, func); } template @@ -154,10 +144,10 @@ void goto_commands(Context& context, NormalParams params) select_coord(buffer, ByteCoord{0,0}, context.selections()); break; case 'l': - select(context, select_to_eol); + select(context, {}); break; case 'h': - select(context, select_to_eol_reverse); + select(context, {}); break; case 'j': { @@ -1332,8 +1322,12 @@ private: T m_func; }; -template -constexpr Repeated repeated(T func) { return Repeated(func); } +template +void repeated(Context& context, NormalParams params) +{ + ScopedEdition edition(context); + do { func(context, {0, params.reg}); } while(--params.count > 0); +} template void move(Context& context, NormalParams params) @@ -1355,7 +1349,7 @@ void move(Context& context, NormalParams params) selections.sort_and_merge_overlapping(); } -KeyMap keymap = +const KeyMap keymap = { { 'h', { "move left", move } }, { 'j', { "move down", move } }, @@ -1392,8 +1386,8 @@ KeyMap keymap = { 'v', { "move view", view_commands } }, { 'y', { "yank selected text", yank } }, - { 'p', { "paste after selected text", repeated(paste) } }, - { 'P', { "paste before selected text", repeated(paste) } }, + { 'p', { "paste after selected text", repeated> } }, + { 'P', { "paste before selected text", repeated> } }, { alt('p'), { "paste every yanked selection after selected text", paste_all } }, { alt('P'), { "paste every yanked selection before selected text", paste_all } }, { 'R', { "replace selected text with yanked text", paste } }, @@ -1417,32 +1411,32 @@ KeyMap keymap = { ';', { "reduce selections to their cursor", [](Context& context, NormalParams) { clear_selections(context.selections()); } } }, { alt(';'), { "swap selections cursor and anchor", [](Context& context, NormalParams) { flip_selections(context.selections()); } } }, - { 'w', { "select to next word start", repeated(make_select(select_to_next_word)) } }, - { 'e', { "select to next word end", repeated(make_select(select_to_next_word_end)) } }, - { 'b', { "select to prevous word start", repeated(make_select(select_to_previous_word)) } }, - { 'W', { "extend to next word start", repeated(make_select(select_to_next_word)) } }, - { 'E', { "extend to next word end", repeated(make_select(select_to_next_word_end)) } }, - { 'B', { "extend to prevous word start", repeated(make_select(select_to_previous_word)) } }, + { 'w', { "select to next word start", repeated<&select>> } }, + { 'e', { "select to next word end", repeated>> } }, + { 'b', { "select to prevous word start", repeated>> } }, + { 'W', { "extend to next word start", repeated>> } }, + { 'E', { "extend to next word end", repeated>> } }, + { 'B', { "extend to prevous word start", repeated>> } }, - { alt('w'), { "select to next WORD start", repeated(make_select(select_to_next_word)) } }, - { alt('e'), { "select to next WORD end", repeated(make_select(select_to_next_word_end)) } }, - { alt('b'), { "select to prevous WORD start", repeated(make_select(select_to_previous_word)) } }, - { alt('W'), { "extend to next WORD start", repeated(make_select(select_to_next_word)) } }, - { alt('E'), { "extend to next WORD end", repeated(make_select(select_to_next_word_end)) } }, - { alt('B'), { "extend to prevous WORD start", repeated(make_select(select_to_previous_word)) } }, + { alt('w'), { "select to next WORD start", repeated>> } }, + { alt('e'), { "select to next WORD end", repeated>> } }, + { alt('b'), { "select to prevous WORD start", repeated>> } }, + { alt('W'), { "extend to next WORD start", repeated>> } }, + { alt('E'), { "extend to next WORD end", repeated>> } }, + { alt('B'), { "extend to prevous WORD start", repeated>> } }, - { alt('l'), { "select to line end", repeated(make_select(select_to_eol)) } }, - { alt('L'), { "extend to line end", repeated(make_select(select_to_eol)) } }, - { alt('h'), { "select to line begin", repeated(make_select(select_to_eol_reverse)) } }, - { alt('H'), { "extend to line begin", repeated(make_select(select_to_eol_reverse)) } }, + { alt('l'), { "select to line end", repeated> } }, + { alt('L'), { "extend to line end", repeated> } }, + { alt('h'), { "select to line begin", repeated> } }, + { alt('H'), { "extend to line begin", repeated> } }, - { 'x', { "select line", repeated(make_select(select_line)) } }, - { 'X', { "extend line", repeated(make_select(select_line)) } }, - { alt('x'), { "extend selections to whole lines", make_select(select_lines) } }, - { alt('X'), { "crop selections to whole lines", make_select(trim_partial_lines) } }, + { 'x', { "select line", repeated> } }, + { 'X', { "extend line", repeated> } }, + { alt('x'), { "extend selections to whole lines", select } }, + { alt('X'), { "crop selections to whole lines", select } }, - { 'm', { "select to matching character", make_select(select_matching) } }, - { 'M', { "extend to matching character", make_select(select_matching) } }, + { 'm', { "select to matching character", select } }, + { 'M', { "extend to matching character", select } }, { '/', { "select next given regex match", search } }, { '?', { "extend with next given regex match", search } }, diff --git a/src/normal.hh b/src/normal.hh index 4baa21df..7f90075d 100644 --- a/src/normal.hh +++ b/src/normal.hh @@ -3,8 +3,7 @@ #include "keys.hh" #include "unordered_map.hh" - -#include +#include "string.hh" namespace Kakoune { @@ -19,12 +18,12 @@ struct NormalParams struct NormalCmdDesc { - const char* docstring; - std::function func; + StringView docstring; + void (*func)(Context& context, NormalParams params); }; using KeyMap = UnorderedMap; -extern KeyMap keymap; +extern const KeyMap keymap; }