diff --git a/src/main.cc b/src/main.cc index c32ce8ed..15c2a867 100644 --- a/src/main.cc +++ b/src/main.cc @@ -145,29 +145,28 @@ void register_registers() register_manager.add_register(c, make_unique()); using StringList = Vector; - static const struct { - char name; - StringList (*func)(const Context&); - } dyn_regs[] = { - { '%', [](const Context& context) { return StringList{{context.buffer().display_name()}}; } }, - { '.', [](const Context& context) { + + register_manager.add_register('%', make_dyn_reg( + [](const Context& context) + { return StringList{{context.buffer().display_name()}}; })); + + register_manager.add_register('.', make_dyn_reg( + [](const Context& context) { auto content = context.selections_content(); return StringList{content.begin(), content.end()}; - } }, - { '#', [](const Context& context) { + })); + + register_manager.add_register('#', make_dyn_reg( + [](const Context& context) { StringList res; for (size_t i = 1; i < context.selections().size()+1; ++i) res.push_back(to_string((int)i)); return res; - } } - }; - - for (auto& dyn_reg : dyn_regs) - register_manager.add_register(dyn_reg.name, make_unique(dyn_reg.func)); + })); for (size_t i = 0; i < 10; ++i) { - register_manager.add_register('0'+i, make_unique( + register_manager.add_register('0'+i, make_dyn_reg( [i](const Context& context) { StringList result; for (auto& sel : context.selections()) diff --git a/src/register_manager.hh b/src/register_manager.hh index 355bec1f..84bf0a60 100644 --- a/src/register_manager.hh +++ b/src/register_manager.hh @@ -8,8 +8,6 @@ #include "string.hh" #include "vector.hh" -#include - namespace Kakoune { @@ -46,14 +44,13 @@ protected: Vector m_content; }; -using RegisterRetriever = std::function (const Context&)>; - // Dynamic value register, use it's RegisterRetriever // to get it's value when needed. +template class DynamicRegister : public StaticRegister { public: - DynamicRegister(RegisterRetriever function) + DynamicRegister(Func function) : m_function(std::move(function)) {} Register& operator=(ConstArrayView values) override @@ -68,9 +65,15 @@ public: } private: - RegisterRetriever m_function; + Func m_function; }; +template +std::unique_ptr make_dyn_reg(Func func) +{ + return make_unique>(std::move(func)); +} + class NullRegister : public Register { public: