:prompt and :onkey (renamed :on-key) now use values instead of registers
:prompt provides the entered text in the `text` value, :on-key provides the entered key in the `key` value. Closes #938
This commit is contained in:
parent
b337f99ca7
commit
a4738ace5f
|
@ -151,15 +151,19 @@ Helpers
|
||||||
Kakoune provides some helper commands that can be used to define composite
|
Kakoune provides some helper commands that can be used to define composite
|
||||||
commands:
|
commands:
|
||||||
|
|
||||||
*prompt* <prompt> <register> <command>::
|
*prompt* <prompt> <command>::
|
||||||
prompt the user for a string, when the user validates, store the
|
prompt the user for a string, when the user validates, executes the
|
||||||
result in given *register* and run *commmand*. the *-init <str>*
|
command. The entered text is available in the `text` value accessible
|
||||||
switch allows setting initial content, the *-password* switch hides
|
through `$kak_text` in shells or `%val{text}` in commands.
|
||||||
the entered text and clears the register after command execution.
|
|
||||||
|
|
||||||
*onkey* <register> <command>::
|
The *-init <str>* switch allows setting initial content, the
|
||||||
wait for next key from user, writes it into given <register> and
|
*-password* switch hides the entered text and clears the register
|
||||||
execute commands
|
after command execution.
|
||||||
|
|
||||||
|
*on-key* <command>::
|
||||||
|
wait for next key from user, then execute <command>, the key is
|
||||||
|
available through the `key` value, accessiable through `$kak_key`
|
||||||
|
in shells, or `%val{key}` in commands.
|
||||||
|
|
||||||
*menu* <label1> <commands1> <label2> <commands2> ...::
|
*menu* <label1> <commands1> <label2> <commands2> ...::
|
||||||
display a menu using labels, the selected label’s commands are
|
display a menu using labels, the selected label’s commands are
|
||||||
|
|
|
@ -1675,8 +1675,8 @@ const CommandDesc eval_string_cmd = {
|
||||||
const CommandDesc prompt_cmd = {
|
const CommandDesc prompt_cmd = {
|
||||||
"prompt",
|
"prompt",
|
||||||
nullptr,
|
nullptr,
|
||||||
"prompt <prompt> <register> <command>: prompt the use to enter a text string "
|
"prompt <prompt> <command>: prompt the use to enter a text string"
|
||||||
"stores it in <register> and then executes <command>",
|
"and then executes <command>, entered text is available in the 'text' value",
|
||||||
ParameterDesc{
|
ParameterDesc{
|
||||||
{ { "init", { true, "set initial prompt content" } },
|
{ { "init", { true, "set initial prompt content" } },
|
||||||
{ "password", { false, "Do not display entered text and clear reg after command" } },
|
{ "password", { false, "Do not display entered text and clear reg after command" } },
|
||||||
|
@ -1684,17 +1684,14 @@ const CommandDesc prompt_cmd = {
|
||||||
{ "client-completion", { false, "use client completion for prompt" } },
|
{ "client-completion", { false, "use client completion for prompt" } },
|
||||||
{ "buffer-completion", { false, "use buffer completion for prompt" } },
|
{ "buffer-completion", { false, "use buffer completion for prompt" } },
|
||||||
{ "command-completion", { false, "use command completion for prompt" } } },
|
{ "command-completion", { false, "use command completion for prompt" } } },
|
||||||
ParameterDesc::Flags::None, 3, 3
|
ParameterDesc::Flags::None, 2, 2
|
||||||
},
|
},
|
||||||
CommandFlags::None,
|
CommandFlags::None,
|
||||||
CommandHelper{},
|
CommandHelper{},
|
||||||
CommandCompleter{},
|
CommandCompleter{},
|
||||||
[](const ParametersParser& parser, Context& context, const ShellContext& shell_context)
|
[](const ParametersParser& parser, Context& context, const ShellContext& shell_context)
|
||||||
{
|
{
|
||||||
if (parser[1].length() != 1)
|
const String& command = parser[1];
|
||||||
throw runtime_error("register name should be a single character");
|
|
||||||
const char reg = parser[1][0_byte];
|
|
||||||
const String& command = parser[2];
|
|
||||||
auto initstr = parser.get_switch("init").value_or(StringView{});
|
auto initstr = parser.get_switch("init").value_or(StringView{});
|
||||||
|
|
||||||
Completer completer;
|
Completer completer;
|
||||||
|
@ -1724,24 +1721,26 @@ const CommandDesc prompt_cmd = {
|
||||||
const auto flags = parser.get_switch("password") ?
|
const auto flags = parser.get_switch("password") ?
|
||||||
PromptFlags::Password : PromptFlags::None;
|
PromptFlags::Password : PromptFlags::None;
|
||||||
|
|
||||||
|
// const cast so that lambda capute is mutable
|
||||||
|
ShellContext& sc = const_cast<ShellContext&>(shell_context);
|
||||||
context.input_handler().prompt(
|
context.input_handler().prompt(
|
||||||
parser[0], initstr.str(), get_face("Prompt"),
|
parser[0], initstr.str(), get_face("Prompt"),
|
||||||
flags, std::move(completer),
|
flags, std::move(completer),
|
||||||
[=](StringView str, PromptEvent event, Context& context)
|
[=](StringView str, PromptEvent event, Context& context) mutable
|
||||||
{
|
{
|
||||||
if (event != PromptEvent::Validate)
|
if (event != PromptEvent::Validate)
|
||||||
return;
|
return;
|
||||||
RegisterManager::instance()[reg] = ConstArrayView<String>(str.str());
|
sc.env_vars["text"] = str.str();
|
||||||
|
|
||||||
ScopedSetBool disable_history{context.history_disabled()};
|
ScopedSetBool disable_history{context.history_disabled()};
|
||||||
|
|
||||||
CommandManager::instance().execute(command, context, shell_context);
|
CommandManager::instance().execute(command, context, sc);
|
||||||
|
|
||||||
if (flags & PromptFlags::Password)
|
if (flags & PromptFlags::Password)
|
||||||
{
|
{
|
||||||
const String& str = RegisterManager::instance()[reg].values(context)[0];
|
String& str = sc.env_vars["text"];;
|
||||||
memset(const_cast<String&>(str).data(), 0, (int)str.length());
|
memset(str.data(), 0, (int)str.length());
|
||||||
RegisterManager::instance()[reg] = ConstArrayView<String>("");
|
str = "";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1802,24 +1801,27 @@ const CommandDesc menu_cmd = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const CommandDesc onkey_cmd = {
|
const CommandDesc on_key_cmd = {
|
||||||
"onkey",
|
"on-key",
|
||||||
nullptr,
|
nullptr,
|
||||||
"onkey <register> <command>: wait for next user key, store it in <register> and execute <command>",
|
"on-key <command>: wait for next user key then and execute <command>, "
|
||||||
ParameterDesc{ {}, ParameterDesc::Flags::None, 2, 2 },
|
"with key availabe in the `key` value",
|
||||||
|
ParameterDesc{ {}, ParameterDesc::Flags::None, 1, 1 },
|
||||||
CommandFlags::None,
|
CommandFlags::None,
|
||||||
CommandHelper{},
|
CommandHelper{},
|
||||||
CommandCompleter{},
|
CommandCompleter{},
|
||||||
[](const ParametersParser& parser, Context& context, const ShellContext& shell_context)
|
[](const ParametersParser& parser, Context& context, const ShellContext& shell_context)
|
||||||
{
|
{
|
||||||
String reg = parser[0];
|
String command = parser[0];
|
||||||
String command = parser[1];
|
|
||||||
context.input_handler().on_next_key(KeymapMode::None,
|
// const cast so that the lambda capute is mutable
|
||||||
[=](Key key, Context& context) {
|
ShellContext& sc = const_cast<ShellContext&>(shell_context);
|
||||||
RegisterManager::instance()[reg] = key_to_str(key);
|
context.input_handler().on_next_key(
|
||||||
|
KeymapMode::None, [=](Key key, Context& context) mutable {
|
||||||
|
sc.env_vars["key"] = key_to_str(key);
|
||||||
ScopedSetBool disable_history{context.history_disabled()};
|
ScopedSetBool disable_history{context.history_disabled()};
|
||||||
|
|
||||||
CommandManager::instance().execute(command, context, shell_context);
|
CommandManager::instance().execute(command, context, sc);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -2071,7 +2073,7 @@ void register_commands()
|
||||||
register_command(eval_string_cmd);
|
register_command(eval_string_cmd);
|
||||||
register_command(prompt_cmd);
|
register_command(prompt_cmd);
|
||||||
register_command(menu_cmd);
|
register_command(menu_cmd);
|
||||||
register_command(onkey_cmd);
|
register_command(on_key_cmd);
|
||||||
register_command(info_cmd);
|
register_command(info_cmd);
|
||||||
register_command(try_catch_cmd);
|
register_command(try_catch_cmd);
|
||||||
register_command(set_face_cmd);
|
register_command(set_face_cmd);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user