Add support for user mappings, bound to comma

:map have a 'user' mode that is accessible through the comma key.
the mapping will get executed in normal mode.
This commit is contained in:
Maxime Coste 2014-12-12 13:57:02 +00:00
parent 801bd5245d
commit a0b35bf590
4 changed files with 22 additions and 3 deletions

View File

@ -938,6 +938,7 @@ KeymapMode parse_keymap_mode(const String& str)
if (prefix_match("prompt", str)) return KeymapMode::Prompt;
if (prefix_match("goto", str)) return KeymapMode::Goto;
if (prefix_match("view", str)) return KeymapMode::View;
if (prefix_match("user", str)) return KeymapMode::User;
throw runtime_error("unknown keymap mode '" + str + "'");
}
@ -954,7 +955,8 @@ const CommandDesc map_key_cmd = {
" normal\n"
" insert\n"
" menu\n"
" prompt\n",
" prompt\n"
" user\n",
ParameterDesc{ SwitchMap{}, ParameterDesc::Flags::None, 4, 4 },
CommandFlags::None,
[](const Context& context, CompletionFlags flags,
@ -1380,7 +1382,7 @@ private:
}
void exec_keys(const KeyList& keys, Context& context)
void exec_keys(memoryview<Key> keys, Context& context)
{
RegisterRestorer quote('"', context);
RegisterRestorer slash('/', context);

View File

@ -2,6 +2,7 @@
#define commands_hh_INCLUDED
#include "keys.hh"
#include "memoryview.hh"
namespace Kakoune
{
@ -9,7 +10,7 @@ namespace Kakoune
class Context;
void register_commands();
void exec_keys(const KeyList& keys, Context& context);
void exec_keys(memoryview<Key> keys, Context& context);
}

View File

@ -19,6 +19,7 @@ enum class KeymapMode : int
Menu,
Goto,
View,
User,
};
template<typename T> class memoryview;

View File

@ -1262,6 +1262,19 @@ void redo(Context& context, NormalParams)
context.print_status({ "nothing left to redo", get_face("Information") });
}
void exec_user_mappings(Context& context, NormalParams params)
{
on_next_key_with_autoinfo(context, KeymapMode::None,
[params](Key key, Context& context) mutable {
if (not context.keymaps().is_mapped(key, KeymapMode::User))
return;
auto mapping = context.keymaps().get_mapping(key, KeymapMode::User);
ScopedEdition edition(context);
exec_keys(mapping, context);
}, "user mapping", "enter user key");
}
template<typename T>
class Repeated
{
@ -1448,6 +1461,8 @@ KeyMap keymap =
{ '@', { "convert tabs to spaces in selections", tabs_to_spaces } },
{ alt('@'), { "convert spaces to tabs in selections", spaces_to_tabs } },
{ ',', { "user mappings", exec_user_mappings } },
{ Key::Left, { "move left", move<CharCount, Backward> } },
{ Key::Down, { "move down", move<LineCount, Forward> } },
{ Key::Up, { "move up", move<LineCount, Backward> } },