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
This commit is contained in:
Maxime Coste 2014-07-27 20:18:09 +01:00
parent 7c10660f9a
commit 8ed16bb2e9
3 changed files with 20 additions and 2 deletions

View File

@ -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" } }, { "buffer", { true, "run in a disposable context for each given buffer in the comma separated list argument" } },
{ "draft", { false, "run in a disposable context" } }, { "draft", { false, "run in a disposable context" } },
{ "no-hooks", { false, "disable hooks" } }, { "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" } } }, { "itersel", { false, "run once for each selection with that selection as the only one" } } },
ParameterDesc::Flags::SwitchesOnlyAtStart, 1 ParameterDesc::Flags::SwitchesOnlyAtStart, 1
}; };
@ -1009,6 +1010,7 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
DisableOption<bool> disable_incsearch(context, "incsearch"); DisableOption<bool> disable_incsearch(context, "incsearch");
const bool disable_hooks = parser.has_option("no-hooks") or context.are_user_hooks_disabled(); 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(); ClientManager& cm = ClientManager::instance();
if (parser.has_option("buffer")) 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 // Propagate user hooks disabled status to the temporary context
if (disable_hooks) if (disable_hooks)
input_handler.context().disable_user_hooks(); input_handler.context().disable_user_hooks();
if (disable_keymaps)
input_handler.context().disable_keymaps();
func(parser, input_handler.context()); func(parser, input_handler.context());
} }
return; return;
@ -1048,6 +1052,8 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
// Propagate user hooks disabled status to the temporary context // Propagate user hooks disabled status to the temporary context
if (disable_hooks) if (disable_hooks)
input_handler.context().disable_user_hooks(); input_handler.context().disable_user_hooks();
if (disable_keymaps)
input_handler.context().disable_keymaps();
if (parser.has_option("itersel")) if (parser.has_option("itersel"))
{ {
@ -1074,9 +1080,14 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
if (disable_hooks) if (disable_hooks)
real_context->disable_user_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) if (disable_hooks)
real_context->enable_user_hooks(); real_context->enable_user_hooks();
if (disable_keymaps)
real_context->enable_keymaps();
}); });
func(parser, *real_context); func(parser, *real_context);

View File

@ -81,6 +81,11 @@ public:
void disable_user_hooks() { ++m_user_hooks_disabled; } void disable_user_hooks() { ++m_user_hooks_disabled; }
void enable_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: private:
void begin_edition(); void begin_edition();
void end_edition(); void end_edition();
@ -102,6 +107,7 @@ private:
JumpList::iterator m_current_jump = m_jump_list.begin(); JumpList::iterator m_current_jump = m_jump_list.begin();
int m_user_hooks_disabled = 0; int m_user_hooks_disabled = 0;
int m_keymaps_disabled = 0;
}; };
struct ScopedEdition struct ScopedEdition

View File

@ -991,7 +991,8 @@ void InputHandler::handle_key(Key key)
auto keymap_mode = m_mode->keymap_mode(); auto keymap_mode = m_mode->keymap_mode();
KeymapManager& keymaps = m_context.keymaps(); 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)) for (auto& k : keymaps.get_mapping(key, keymap_mode))
m_mode->on_key(k); m_mode->on_key(k);