Remove <scope> from user-modes commands

This commit is contained in:
Delapouite 2018-03-02 07:45:04 +01:00
parent 7a54c0edfe
commit c4eb4438d2
9 changed files with 36 additions and 31 deletions

View File

@ -129,10 +129,10 @@ command *q!* has to be used). Aliases are mentionned below each commands.
*unmap* <scope> <mode> <key> [<expected>]:: *unmap* <scope> <mode> <key> [<expected>]::
unbind a key combination (See <<mapping#,`:doc mapping`>>) unbind a key combination (See <<mapping#,`:doc mapping`>>)
*declare-user-mode* <scope> <name>:: *declare-user-mode* <name>::
declare a new user keymap mode within the context of a scope declare a new user keymap mode
*enter-user-mode* <scope> <name>:: *enter-user-mode* <name>::
enable <name> keymap mode for next key enable <name> keymap mode for next key
*-lock*::: *-lock*:::

View File

@ -126,6 +126,8 @@ informations about Kakoune's state:
*kak_client_env_<name>*:: *kak_client_env_<name>*::
value of the *name* variable in the client environment value of the *name* variable in the client environment
(e.g. *$kak_client_env_SHELL* is the SHELL variable) (e.g. *$kak_client_env_SHELL* is the SHELL variable)
*kak_user_modes*::
the user modes list, each modes separated by a colon
Note that in order for Kakoune to pass a value in the environment, the Note that in order for Kakoune to pass a value in the environment, the
variable has to be spelled out within the body of the expansion. variable has to be spelled out within the body of the expansion.

View File

@ -102,7 +102,7 @@ mostly useful for plugin authors who want to bind their new commands in
extensible menus. extensible menus.
-------------------------------- --------------------------------
declare-user-mode <scope> <name> declare-user-mode <name>
-------------------------------- --------------------------------
Declare a new custom mode that can later be referred by the *map* command. Declare a new custom mode that can later be referred by the *map* command.
@ -110,7 +110,7 @@ For example a `grep` custom mode to attach keys like `n` or `p` to skim
through the output results. through the output results.
------------------------------- -------------------------------
enter-user-mode <scope> <name> enter-user-mode <name>
------------------------------- -------------------------------
Enable the designated mode for the next key. Docstrings are shown in the Enable the designated mode for the next key. Docstrings are shown in the

View File

@ -1192,10 +1192,11 @@ const CommandDesc debug_cmd = {
auto& keymaps = context.keymaps(); auto& keymaps = context.keymaps();
auto modes = {"normal", "insert", "prompt", "menu", auto modes = {"normal", "insert", "prompt", "menu",
"goto", "view", "user", "object"}; "goto", "view", "user", "object"};
auto user_modes = keymaps.user_modes();
write_to_debug_buffer("Mappings:"); write_to_debug_buffer("Mappings:");
for (auto& mode : modes) for (auto& mode : concatenated(modes, user_modes) | gather<Vector<String>>())
{ {
KeymapMode m = parse_keymap_mode(mode, keymaps.user_modes()); KeymapMode m = parse_keymap_mode(mode, user_modes);
for (auto& key : keymaps.get_mapped_keys(m)) for (auto& key : keymaps.get_mapped_keys(m))
write_to_debug_buffer(format(" * {} {}: {}", write_to_debug_buffer(format(" * {} {}: {}",
mode, key_to_str(key), mode, key_to_str(key),
@ -2123,15 +2124,14 @@ const CommandDesc fail_cmd = {
const CommandDesc declare_user_mode_cmd = { const CommandDesc declare_user_mode_cmd = {
"declare-user-mode", "declare-user-mode",
nullptr, nullptr,
"declare-user-mode <scope> <name>: add a new user keymap mode in given <scope>", "declare-user-mode <name>: add a new user keymap mode",
ParameterDesc{ {}, ParameterDesc::Flags::None, 2, 2 }, single_param,
CommandFlags::None, CommandFlags::None,
CommandHelper{}, CommandHelper{},
make_completer(complete_scope), CommandCompleter{},
[](const ParametersParser& parser, Context& context, const ShellContext&) [](const ParametersParser& parser, Context& context, const ShellContext&)
{ {
KeymapManager& keymaps = get_scope(parser[0], context).keymaps(); context.keymaps().add_user_mode(std::move(parser[0]));
keymaps.add_user_mode(std::move(parser[1]));
} }
}; };
@ -2162,10 +2162,10 @@ void enter_user_mode(Context& context, const String mode_name, KeymapMode mode,
const CommandDesc enter_user_mode_cmd = { const CommandDesc enter_user_mode_cmd = {
"enter-user-mode", "enter-user-mode",
nullptr, nullptr,
"enter-user-mode <switches> <scope> <name>: enable <name> keymap mode for next key", "enter-user-mode <switches> <name>: enable <name> keymap mode for next key",
ParameterDesc{ ParameterDesc{
{ { "lock", { false, "stay in mode until <esc> is pressed" } } }, { { "lock", { false, "stay in mode until <esc> is pressed" } } },
ParameterDesc::Flags::SwitchesOnlyAtStart, 2, 2 ParameterDesc::Flags::SwitchesOnlyAtStart, 1, 1
}, },
CommandFlags::None, CommandFlags::None,
CommandHelper{}, CommandHelper{},
@ -2174,23 +2174,17 @@ const CommandDesc enter_user_mode_cmd = {
ByteCount pos_in_token) -> Completions ByteCount pos_in_token) -> Completions
{ {
if (token_to_complete == 0) if (token_to_complete == 0)
return { 0_byte, params[0].length(),
complete(params[0], pos_in_token, scopes) };
if (token_to_complete == 1)
{ {
KeymapManager& keymaps = get_scope(params[0], context).keymaps(); return { 0_byte, params[0].length(),
return { 0_byte, params[1].length(), complete(params[0], pos_in_token, context.keymaps().user_modes()) };
complete(params[1], pos_in_token, keymaps.user_modes()) };
} }
return {}; return {};
}, },
[](const ParametersParser& parser, Context& context, const ShellContext&) [](const ParametersParser& parser, Context& context, const ShellContext&)
{ {
auto lock = (bool)parser.get_switch("lock"); auto lock = (bool)parser.get_switch("lock");
KeymapManager& keymaps = get_scope(parser[0], context).keymaps(); KeymapMode mode = parse_keymap_mode(parser[0], context.keymaps().user_modes());
KeymapMode mode = parse_keymap_mode(parser[1], keymaps.user_modes()); enter_user_mode(context, std::move(parser[0]), mode, lock);
enter_user_mode(context, std::move(parser[1]), mode, lock);
} }
}; };

View File

@ -54,16 +54,17 @@ KeymapManager::KeyList KeymapManager::get_mapped_keys(KeymapMode mode) const
void KeymapManager::add_user_mode(String user_mode_name) void KeymapManager::add_user_mode(String user_mode_name)
{ {
auto modes = {"normal", "insert", "prompt", "menu", "goto", "view", "user", "object"}; auto modes = {"normal", "insert", "prompt", "menu", "goto", "view", "user", "object"};
if (contains(modes, user_mode_name)) if (contains(modes, user_mode_name))
throw runtime_error(format("'{}' is already a regular mode", user_mode_name)); throw runtime_error(format("'{}' is already a regular mode", user_mode_name));
if (contains(m_user_modes, user_mode_name)) if (contains(user_modes(), user_mode_name))
throw runtime_error(format("user mode '{}' already defined", user_mode_name)); throw runtime_error(format("user mode '{}' already defined", user_mode_name));
if (contains_that(user_mode_name, [](char c){ return not isalnum(c); })) if (contains_that(user_mode_name, [](char c){ return not isalnum(c); }))
throw runtime_error(format("invalid mode name: '{}'", user_mode_name)); throw runtime_error(format("invalid mode name: '{}'", user_mode_name));
m_user_modes.push_back(std::move(user_mode_name)); user_modes().push_back(std::move(user_mode_name));
} }
} }

View File

@ -45,7 +45,11 @@ public:
const KeymapInfo& get_mapping(Key key, KeymapMode mode) const; const KeymapInfo& get_mapping(Key key, KeymapMode mode) const;
using UserModeList = Vector<String>; using UserModeList = Vector<String>;
const UserModeList& user_modes() const { return m_user_modes; } UserModeList& user_modes() {
if (m_parent)
return m_parent->user_modes();
return m_user_modes;
}
void add_user_mode(const String user_mode_name); void add_user_mode(const String user_mode_name);
private: private:

View File

@ -193,6 +193,10 @@ static const EnvVarDesc builtin_env_vars[] = { {
"window_height", false, "window_height", false,
[](StringView name, const Context& context) -> String [](StringView name, const Context& context) -> String
{ return to_string(context.window().dimensions().line); } { return to_string(context.window().dimensions().line); }
}, {
"user_modes", false,
[](StringView name, const Context& context) -> String
{ return join(context.keymaps().user_modes(), ':'); }
} }
}; };

View File

@ -1,3 +1,3 @@
declare-user-mode global foo declare-user-mode foo
map global foo f d map global foo f d
map global normal <a-,> ':enter-user-mode -lock global foo<ret>' map global normal <a-,> ':enter-user-mode -lock foo<ret>'

View File

@ -1,3 +1,3 @@
declare-user-mode global foo declare-user-mode foo
map global foo f 'wchello from foo<esc>' map global foo f 'wchello from foo<esc>'
map global normal <a-,> ':enter-user-mode global foo<ret>' map global normal <a-,> ':enter-user-mode foo<ret>'