summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2018-05-01 22:44:42 +1000
committerMaxime Coste <mawww@kakoune.org>2018-05-01 22:48:14 +1000
commit286a244aa27a0f0c375ad3bd3d99dc754aaeb999 (patch)
tree044c21764f845aaf092a51e23f9912d7c5b66c48 /src
parent6777c1469727b27d7909cd1aafee23458ba19431 (diff)
Replace RegisterRestorer with a on_scope_end + lambda
Diffstat (limited to 'src')
-rw-r--r--src/commands.cc46
1 files changed, 8 insertions, 38 deletions
diff --git a/src/commands.cc b/src/commands.cc
index 6feb1b2c..978afbf4 100644
--- a/src/commands.cc
+++ b/src/commands.cc
@@ -1549,41 +1549,6 @@ const ParameterDesc context_wrap_params = {
ParameterDesc::Flags::SwitchesOnlyAtStart, 1
};
-class RegisterRestorer
-{
-public:
- RegisterRestorer(char name, Context& context)
- : m_context{context}, m_name{name}
- {
- ConstArrayView<String> save = RegisterManager::instance()[name].get(context);
- m_save = Vector<String>(save.begin(), save.end());
- }
-
- RegisterRestorer(RegisterRestorer&& other) noexcept
- : m_context{other.m_context}, m_save{std::move(other.m_save)}, m_name{other.m_name}
- {
- other.m_name = 0;
- }
-
- ~RegisterRestorer()
- {
- if (m_name != 0) try
- {
- RegisterManager::instance()[m_name].set(m_context, m_save);
- }
- catch (runtime_error& e)
- {
- write_to_debug_buffer(format("could not restore register '{}': {}",
- m_name, e.what()));
- }
- }
-
-private:
- Vector<String> m_save;
- Context& m_context;
- char m_name;
-};
-
template<typename Func>
void context_wrap(const ParametersParser& parser, Context& context, Func func)
{
@@ -1595,9 +1560,14 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
const bool no_hooks = parser.get_switch("no-hooks") or context.hooks_disabled();
const bool no_keymaps = not parser.get_switch("with-maps");
- Vector<RegisterRestorer> saved_registers;
- for (auto& r : parser.get_switch("save-regs").value_or("/\"|^@"))
- saved_registers.emplace_back(r, context);
+ auto& register_manager = RegisterManager::instance();
+ auto make_register_restorer = [&](char c) {
+ return on_scope_end([&, c, save=register_manager[c].get(context) | gather<Vector<String>>()] {
+ RegisterManager::instance()[c].set(context, save);
+ });
+ };
+ auto saved_registers = parser.get_switch("save-regs").value_or("/\"|^@") |
+ transform(make_register_restorer) | gather<Vector<decltype(make_register_restorer(0))>>();
if (auto bufnames = parser.get_switch("buffer"))
{