From 8ed16bb2e9f29a7f31f38365b87e5a9ee72fb921 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 27 Jul 2014 20:18:09 +0100 Subject: [PATCH] In non interactive interactive mode, disable user key mappings exec and eval now accepts a -with-maps to use them. But by default they are disabled, so that all the indent scripts work even if you remap basic keys. Fixes #217 --- src/commands.cc | 13 ++++++++++++- src/context.hh | 6 ++++++ src/input_handler.cc | 3 ++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 6e3c63fb..2967ae1f 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -982,6 +982,7 @@ const ParameterDesc context_wrap_params = { { "buffer", { true, "run in a disposable context for each given buffer in the comma separated list argument" } }, { "draft", { false, "run in a disposable context" } }, { "no-hooks", { false, "disable hooks" } }, + { "with-maps", { false, "use user defined key mapping when executing keys" } }, { "itersel", { false, "run once for each selection with that selection as the only one" } } }, ParameterDesc::Flags::SwitchesOnlyAtStart, 1 }; @@ -1009,6 +1010,7 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func) DisableOption disable_incsearch(context, "incsearch"); const bool disable_hooks = parser.has_option("no-hooks") or context.are_user_hooks_disabled(); + const bool disable_keymaps = not parser.has_option("with-maps"); ClientManager& cm = ClientManager::instance(); if (parser.has_option("buffer")) @@ -1021,6 +1023,8 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func) // Propagate user hooks disabled status to the temporary context if (disable_hooks) input_handler.context().disable_user_hooks(); + if (disable_keymaps) + input_handler.context().disable_keymaps(); func(parser, input_handler.context()); } return; @@ -1048,6 +1052,8 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func) // Propagate user hooks disabled status to the temporary context if (disable_hooks) input_handler.context().disable_user_hooks(); + if (disable_keymaps) + input_handler.context().disable_keymaps(); if (parser.has_option("itersel")) { @@ -1074,9 +1080,14 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func) if (disable_hooks) real_context->disable_user_hooks(); - auto restore_hooks = on_scope_end([&](){ + if (disable_keymaps) + real_context->disable_keymaps(); + + auto restore = on_scope_end([&](){ if (disable_hooks) real_context->enable_user_hooks(); + if (disable_keymaps) + real_context->enable_keymaps(); }); func(parser, *real_context); diff --git a/src/context.hh b/src/context.hh index 60b49abb..9967cb70 100644 --- a/src/context.hh +++ b/src/context.hh @@ -81,6 +81,11 @@ public: void disable_user_hooks() { ++m_user_hooks_disabled; } void enable_user_hooks() { --m_user_hooks_disabled; } + bool are_keymaps_disabled() const { return m_keymaps_disabled; } + + void disable_keymaps() { ++m_keymaps_disabled; } + void enable_keymaps() { --m_keymaps_disabled; } + private: void begin_edition(); void end_edition(); @@ -102,6 +107,7 @@ private: JumpList::iterator m_current_jump = m_jump_list.begin(); int m_user_hooks_disabled = 0; + int m_keymaps_disabled = 0; }; struct ScopedEdition diff --git a/src/input_handler.cc b/src/input_handler.cc index 90e455a4..9459dd74 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -991,7 +991,8 @@ void InputHandler::handle_key(Key key) auto keymap_mode = m_mode->keymap_mode(); KeymapManager& keymaps = m_context.keymaps(); - if (keymaps.is_mapped(key, keymap_mode)) + if (keymaps.is_mapped(key, keymap_mode) and + not m_context.are_keymaps_disabled()) { for (auto& k : keymaps.get_mapping(key, keymap_mode)) m_mode->on_key(k);