1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-18 00:42:13 +03:00

Support explicit register for :, |, ! and $ commands

Giving an explicit register uses its content for the default value
to use if the user does not enter anything. This enables:

`set-register a %{commands}; execute-keys '"a:<ret>'`
`set-register a %{shell script}; execute-keys '"a|<ret>'`
...

This provides a nice way to avoid the need to escape keys to use
those normal mode commands.

Fixes #3825
This commit is contained in:
Maxime Coste 2020-10-20 21:45:29 +11:00
parent 354cfd3b8f
commit 9a5cf2fc9f
13 changed files with 45 additions and 24 deletions

View File

@ -452,22 +452,24 @@ void for_each_codepoint(Context& context, NormalParams)
selections.insert(strings, InsertMode::Replace);
}
void command(const Context& context, EnvVarMap env_vars)
void command(const Context& context, EnvVarMap env_vars, char reg = 0)
{
if (not CommandManager::has_instance())
throw runtime_error{"commands are not supported"};
CommandManager::instance().clear_last_complete_command();
String default_command = context.main_sel_register_value(reg ? reg : ':').str();
context.input_handler().prompt(
":", {}, context.main_sel_register_value(':').str(),
":", {}, default_command,
context.faces()["Prompt"], PromptFlags::DropHistoryEntriesWithBlankPrefix,
':',
[](const Context& context, CompletionFlags flags,
StringView cmd_line, ByteCount pos) {
return CommandManager::instance().complete(context, flags, cmd_line, pos);
},
[env_vars = std::move(env_vars)](StringView cmdline, PromptEvent event, Context& context) {
[env_vars = std::move(env_vars), default_command](StringView cmdline, PromptEvent event, Context& context) {
if (context.has_client())
{
context.client().info_hide();
@ -491,12 +493,9 @@ void command(const Context& context, EnvVarMap env_vars)
if (event == PromptEvent::Validate)
{
if (cmdline.empty())
cmdline = context.main_sel_register_value(':');
else if (not is_blank(cmdline[0]))
RegisterManager::instance()[':'].set(context, cmdline.str());
cmdline = default_command;
CommandManager::instance().execute(
cmdline, context, { {}, env_vars });
CommandManager::instance().execute(cmdline, context, { {}, env_vars });
}
});
}
@ -507,7 +506,7 @@ void command(Context& context, NormalParams params)
{ "count", to_string(params.count) },
{ "register", String{&params.reg, 1} }
};
command(context, std::move(env_vars));
command(context, std::move(env_vars), params.reg);
}
BufferCoord apply_diff(Buffer& buffer, BufferCoord pos, StringView before, StringView after)
@ -545,22 +544,22 @@ BufferCoord apply_diff(Buffer& buffer, BufferCoord pos, StringView before, Strin
}
template<bool replace>
void pipe(Context& context, NormalParams)
void pipe(Context& context, NormalParams params)
{
const char* prompt = replace ? "pipe:" : "pipe-to:";
String default_command = context.main_sel_register_value(params.reg ? params.reg : '|').str();
context.input_handler().prompt(
prompt, {}, context.main_sel_register_value("|").str(), context.faces()["Prompt"],
prompt, {}, default_command, context.faces()["Prompt"],
PromptFlags::DropHistoryEntriesWithBlankPrefix, '|',
shell_complete,
[](StringView cmdline, PromptEvent event, Context& context)
[default_command](StringView cmdline, PromptEvent event, Context& context)
{
if (event != PromptEvent::Validate)
return;
if (cmdline.empty())
cmdline = context.main_sel_register_value("|");
else
RegisterManager::instance()['|'].set(context, cmdline.str());
cmdline = default_command;
if (cmdline.empty())
return;
@ -626,22 +625,22 @@ void pipe(Context& context, NormalParams)
}
template<InsertMode mode>
void insert_output(Context& context, NormalParams)
void insert_output(Context& context, NormalParams params)
{
const char* prompt = mode == InsertMode::Insert ? "insert-output:" : "append-output:";
String default_command = context.main_sel_register_value(params.reg ? params.reg : '|').str();
context.input_handler().prompt(
prompt, {}, context.main_sel_register_value("|").str(), context.faces()["Prompt"],
prompt, {}, default_command, context.faces()["Prompt"],
PromptFlags::DropHistoryEntriesWithBlankPrefix, '|',
shell_complete,
[](StringView cmdline, PromptEvent event, Context& context)
[default_command](StringView cmdline, PromptEvent event, Context& context)
{
if (event != PromptEvent::Validate)
return;
if (cmdline.empty())
cmdline = context.main_sel_register_value("|");
else
RegisterManager::instance()['|'].set(context, cmdline.str());
cmdline = default_command;
if (cmdline.empty())
return;
@ -1133,14 +1132,22 @@ void keep(Context& context, NormalParams params)
});
}
void keep_pipe(Context& context, NormalParams)
void keep_pipe(Context& context, NormalParams params)
{
String default_command = context.main_sel_register_value(params.reg ? params.reg : '|').str();
context.input_handler().prompt(
"keep pipe:", {}, {}, context.faces()["Prompt"],
"keep pipe:", {}, default_command, context.faces()["Prompt"],
PromptFlags::DropHistoryEntriesWithBlankPrefix, '|', shell_complete,
[](StringView cmdline, PromptEvent event, Context& context) {
[default_command](StringView cmdline, PromptEvent event, Context& context) {
if (event != PromptEvent::Validate)
return;
if (cmdline.empty())
cmdline = default_command;
if (cmdline.empty())
return;
const Buffer& buffer = context.buffer();
auto& shell_manager = ShellManager::instance();
Vector<Selection> keep;

View File

@ -0,0 +1 @@
%<a-s>H"a$<ret>

View File

@ -0,0 +1,3 @@
foo
rha
bar

View File

@ -0,0 +1 @@
'foo' 'bar'

View File

@ -0,0 +1 @@
set-register a %{grep 'foo\|bar'}

1
test/normal/pipe-reg/cmd Normal file
View File

@ -0,0 +1 @@
"a|<ret>

1
test/normal/pipe-reg/in Normal file
View File

@ -0,0 +1 @@
%(foo)

1
test/normal/pipe-reg/out Normal file
View File

@ -0,0 +1 @@
bar

1
test/normal/pipe-reg/rc Normal file
View File

@ -0,0 +1 @@
set-register a 'sed s/foo/bar/'

View File

@ -0,0 +1 @@
"a<a-|><ret>:e! out<ret>

View File

@ -0,0 +1 @@
%(foobar)

View File

@ -0,0 +1 @@
barbar

View File

@ -0,0 +1 @@
set-register a %{sed -n "s/foo/bar/; P" > out}