Add onkey command for executing commands after reading a key

This completes the various user interaction primitives, on_next_key
was the last not to be available through a command.
This commit is contained in:
Maxime Coste 2015-04-04 13:10:39 +01:00
parent ac70b3e6d7
commit dde16b00a9
2 changed files with 23 additions and 0 deletions

View File

@ -1155,6 +1155,8 @@ Some helper commands can be used to define composite commands:
* `:prompt <prompt> <register> <command>`: Prompt the user for a string, when
the user validates, store the result in given <register> and run <commmand>.
the -init <str> switch allows setting initial content.
* `:onkey <register> <command>`: Wait for next key from user, writes it into given
<register> and execute commands.
* `:menu <label1> <commands1> <label2> <commands2>...`: display a menu using
labels, the selected label's commands are executed.
`menu` can take a -auto-single argument, to automatically run commands

View File

@ -1300,6 +1300,26 @@ const CommandDesc menu_cmd = {
}
};
const CommandDesc onkey_cmd = {
"onkey",
nullptr,
"onkey <register> <command>: wait for next user key, store it in <register> and execute <command>",
ParameterDesc{ {}, ParameterDesc::Flags::None, 2, 2 },
CommandFlags::None,
CommandHelper{},
CommandCompleter{},
[](const ParametersParser& parser, Context& context)
{
String reg = parser[0];
String command = parser[1];
context.input_handler().on_next_key(KeymapMode::None,
[=](Key key, Context& context) {
RegisterManager::instance()[reg] = key_to_str(key);
CommandManager::instance().execute(command, context);
});
}
};
const CommandDesc info_cmd = {
"info",
nullptr,
@ -1517,6 +1537,7 @@ void register_commands()
register_command(eval_string_cmd);
register_command(prompt_cmd);
register_command(menu_cmd);
register_command(onkey_cmd);
register_command(info_cmd);
register_command(try_catch_cmd);
register_command(face_cmd);