From ddff35e5ab6a9bed29c49547eaee91637854f029 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 17 Oct 2017 11:29:52 +0800 Subject: [PATCH] Move keymap as an implementation detail of the normal mode keys Only expose a free function that tries to get the NormalCmd from a key. --- src/input_handler.cc | 7 +++---- src/normal.cc | 10 +++++++++- src/normal.hh | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/input_handler.cc b/src/input_handler.cc index d4214ed1..25ceacc3 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -281,19 +281,18 @@ public: context().client().info_hide(); do_restore_hooks = true; - auto it = keymap.find(key); - if (it != keymap.end() and it->key == key) + if (auto command = get_normal_command(key)) { auto autoinfo = context().options()["autoinfo"].get(); if (autoinfo & AutoInfo::Normal and context().has_client()) - context().client().info_show(key_to_str(key), it->value.docstring.str(), + context().client().info_show(key_to_str(key), command->docstring.str(), {}, InfoStyle::Prompt); // reset m_params now to be reentrant NormalParams params = m_params; m_params = { 0, 0 }; - it->value.func(context(), params); + command->func(context(), params); } } diff --git a/src/normal.cc b/src/normal.cc index 454c8fdc..213598d3 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1943,7 +1943,7 @@ void force_redraw(Context& context, NormalParams) } } -const HashMap keymap{ +static const HashMap keymap{ { {'h'}, {"move left", move} }, { {'j'}, {"move down", move} }, { {'k'}, {"move up", move} }, @@ -2131,4 +2131,12 @@ const HashMap keymap{ { {ctrl('l')}, {"force redraw", force_redraw} }, }; +Optional get_normal_command(Key key) +{ + auto it = keymap.find(key); + if (it != keymap.end()) + return it->value; + return {}; +} + } diff --git a/src/normal.hh b/src/normal.hh index bb2d3ecc..061ab029 100644 --- a/src/normal.hh +++ b/src/normal.hh @@ -1,7 +1,7 @@ #ifndef normal_hh_INCLUDED #define normal_hh_INCLUDED -#include "hash_map.hh" +#include "optional.hh" #include "keys.hh" #include "string.hh" @@ -22,7 +22,7 @@ struct NormalCmd void (*func)(Context& context, NormalParams params); }; -extern const HashMap keymap; +Optional get_normal_command(Key key); }