From b60613259cf0ffa868bf8099550b96b9fecff9a8 Mon Sep 17 00:00:00 2001 From: Delapouite Date: Sun, 23 Sep 2018 19:40:38 +0200 Subject: [PATCH] Add a way to unmap all keys of a given mode at once --- src/commands.cc | 35 ++++++++++++----------------------- src/keymap_manager.cc | 12 ++++++++++++ src/keymap_manager.hh | 1 + 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 7aad5549..d5290734 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1544,16 +1544,7 @@ static Completions map_key_completer(const Context& context, CompletionFlags fla const CommandDesc map_key_cmd = { "map", nullptr, - "map [] : map to in given mode in .\n" - " can be:\n" - " normal\n" - " insert\n" - " menu\n" - " prompt\n" - " goto\n" - " view\n" - " user\n" - " object\n", + "map [] : map to in given in ", ParameterDesc{ { { "docstring", { true, "specify mapping description" } } }, ParameterDesc::Flags::None, 4, 4 @@ -1579,18 +1570,10 @@ const CommandDesc map_key_cmd = { const CommandDesc unmap_key_cmd = { "unmap", nullptr, - "unmap []: unmap from given mode in .\n" - "If is specified, remove the mapping only if its value is \n" - " can be:\n" - " normal\n" - " insert\n" - " menu\n" - " prompt\n" - " goto\n" - " view\n" - " user\n" - " object\n", - ParameterDesc{{}, ParameterDesc::Flags::None, 3, 4}, + "unmap [ []]: unmap from given in .\n" + "If is specified, remove the mapping only if its value is .\n" + "If only and are specified remove all mappings", + ParameterDesc{{}, ParameterDesc::Flags::None, 2, 4}, CommandFlags::None, CommandHelper{}, map_key_completer, @@ -1599,9 +1582,15 @@ const CommandDesc unmap_key_cmd = { KeymapManager& keymaps = get_scope(parser[0], context).keymaps(); KeymapMode keymap_mode = parse_keymap_mode(parser[1], keymaps.user_modes()); + if (parser.positional_count() == 2) + { + keymaps.unmap_keys(keymap_mode); + return; + } + KeyList key = parse_keys(parser[2]); if (key.size() != 1) - throw runtime_error("only a single key can be mapped"); + throw runtime_error("only a single key can be unmapped"); if (keymaps.is_mapped(key[0], keymap_mode) and (parser.positional_count() < 4 or diff --git a/src/keymap_manager.cc b/src/keymap_manager.cc index 844f150f..582a271d 100644 --- a/src/keymap_manager.cc +++ b/src/keymap_manager.cc @@ -21,6 +21,18 @@ void KeymapManager::unmap_key(Key key, KeymapMode mode) m_mapping.remove(KeyAndMode{key, mode}); } +void KeymapManager::unmap_keys(KeymapMode mode) +{ + auto it = m_mapping.begin(); + while (it != m_mapping.end()) + { + auto& map = *it; + if (map.key.second == mode) + unmap_key(map.key.first, map.key.second); + else + ++it; + } +} bool KeymapManager::is_mapped(Key key, KeymapMode mode) const { diff --git a/src/keymap_manager.hh b/src/keymap_manager.hh index 21a291e3..64e39dd7 100644 --- a/src/keymap_manager.hh +++ b/src/keymap_manager.hh @@ -33,6 +33,7 @@ public: using KeyList = Vector; void map_key(Key key, KeymapMode mode, KeyList mapping, String docstring); void unmap_key(Key key, KeymapMode mode); + void unmap_keys(KeymapMode mode); bool is_mapped(Key key, KeymapMode mode) const; KeyList get_mapped_keys(KeymapMode mode) const;