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:
parent
354cfd3b8f
commit
9a5cf2fc9f
|
@ -452,22 +452,24 @@ void for_each_codepoint(Context& context, NormalParams)
|
||||||
selections.insert(strings, InsertMode::Replace);
|
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())
|
if (not CommandManager::has_instance())
|
||||||
throw runtime_error{"commands are not supported"};
|
throw runtime_error{"commands are not supported"};
|
||||||
|
|
||||||
CommandManager::instance().clear_last_complete_command();
|
CommandManager::instance().clear_last_complete_command();
|
||||||
|
|
||||||
|
String default_command = context.main_sel_register_value(reg ? reg : ':').str();
|
||||||
|
|
||||||
context.input_handler().prompt(
|
context.input_handler().prompt(
|
||||||
":", {}, context.main_sel_register_value(':').str(),
|
":", {}, default_command,
|
||||||
context.faces()["Prompt"], PromptFlags::DropHistoryEntriesWithBlankPrefix,
|
context.faces()["Prompt"], PromptFlags::DropHistoryEntriesWithBlankPrefix,
|
||||||
':',
|
':',
|
||||||
[](const Context& context, CompletionFlags flags,
|
[](const Context& context, CompletionFlags flags,
|
||||||
StringView cmd_line, ByteCount pos) {
|
StringView cmd_line, ByteCount pos) {
|
||||||
return CommandManager::instance().complete(context, flags, cmd_line, 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())
|
if (context.has_client())
|
||||||
{
|
{
|
||||||
context.client().info_hide();
|
context.client().info_hide();
|
||||||
|
@ -491,12 +493,9 @@ void command(const Context& context, EnvVarMap env_vars)
|
||||||
if (event == PromptEvent::Validate)
|
if (event == PromptEvent::Validate)
|
||||||
{
|
{
|
||||||
if (cmdline.empty())
|
if (cmdline.empty())
|
||||||
cmdline = context.main_sel_register_value(':');
|
cmdline = default_command;
|
||||||
else if (not is_blank(cmdline[0]))
|
|
||||||
RegisterManager::instance()[':'].set(context, cmdline.str());
|
|
||||||
|
|
||||||
CommandManager::instance().execute(
|
CommandManager::instance().execute(cmdline, context, { {}, env_vars });
|
||||||
cmdline, context, { {}, env_vars });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -507,7 +506,7 @@ void command(Context& context, NormalParams params)
|
||||||
{ "count", to_string(params.count) },
|
{ "count", to_string(params.count) },
|
||||||
{ "register", String{¶ms.reg, 1} }
|
{ "register", String{¶ms.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)
|
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>
|
template<bool replace>
|
||||||
void pipe(Context& context, NormalParams)
|
void pipe(Context& context, NormalParams params)
|
||||||
{
|
{
|
||||||
const char* prompt = replace ? "pipe:" : "pipe-to:";
|
const char* prompt = replace ? "pipe:" : "pipe-to:";
|
||||||
|
String default_command = context.main_sel_register_value(params.reg ? params.reg : '|').str();
|
||||||
|
|
||||||
context.input_handler().prompt(
|
context.input_handler().prompt(
|
||||||
prompt, {}, context.main_sel_register_value("|").str(), context.faces()["Prompt"],
|
prompt, {}, default_command, context.faces()["Prompt"],
|
||||||
PromptFlags::DropHistoryEntriesWithBlankPrefix, '|',
|
PromptFlags::DropHistoryEntriesWithBlankPrefix, '|',
|
||||||
shell_complete,
|
shell_complete,
|
||||||
[](StringView cmdline, PromptEvent event, Context& context)
|
[default_command](StringView cmdline, PromptEvent event, Context& context)
|
||||||
{
|
{
|
||||||
if (event != PromptEvent::Validate)
|
if (event != PromptEvent::Validate)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (cmdline.empty())
|
if (cmdline.empty())
|
||||||
cmdline = context.main_sel_register_value("|");
|
cmdline = default_command;
|
||||||
else
|
|
||||||
RegisterManager::instance()['|'].set(context, cmdline.str());
|
|
||||||
|
|
||||||
if (cmdline.empty())
|
if (cmdline.empty())
|
||||||
return;
|
return;
|
||||||
|
@ -626,22 +625,22 @@ void pipe(Context& context, NormalParams)
|
||||||
}
|
}
|
||||||
|
|
||||||
template<InsertMode mode>
|
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:";
|
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(
|
context.input_handler().prompt(
|
||||||
prompt, {}, context.main_sel_register_value("|").str(), context.faces()["Prompt"],
|
prompt, {}, default_command, context.faces()["Prompt"],
|
||||||
PromptFlags::DropHistoryEntriesWithBlankPrefix, '|',
|
PromptFlags::DropHistoryEntriesWithBlankPrefix, '|',
|
||||||
shell_complete,
|
shell_complete,
|
||||||
[](StringView cmdline, PromptEvent event, Context& context)
|
[default_command](StringView cmdline, PromptEvent event, Context& context)
|
||||||
{
|
{
|
||||||
if (event != PromptEvent::Validate)
|
if (event != PromptEvent::Validate)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (cmdline.empty())
|
if (cmdline.empty())
|
||||||
cmdline = context.main_sel_register_value("|");
|
cmdline = default_command;
|
||||||
else
|
|
||||||
RegisterManager::instance()['|'].set(context, cmdline.str());
|
|
||||||
|
|
||||||
if (cmdline.empty())
|
if (cmdline.empty())
|
||||||
return;
|
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(
|
context.input_handler().prompt(
|
||||||
"keep pipe:", {}, {}, context.faces()["Prompt"],
|
"keep pipe:", {}, default_command, context.faces()["Prompt"],
|
||||||
PromptFlags::DropHistoryEntriesWithBlankPrefix, '|', shell_complete,
|
PromptFlags::DropHistoryEntriesWithBlankPrefix, '|', shell_complete,
|
||||||
[](StringView cmdline, PromptEvent event, Context& context) {
|
[default_command](StringView cmdline, PromptEvent event, Context& context) {
|
||||||
if (event != PromptEvent::Validate)
|
if (event != PromptEvent::Validate)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (cmdline.empty())
|
||||||
|
cmdline = default_command;
|
||||||
|
|
||||||
|
if (cmdline.empty())
|
||||||
|
return;
|
||||||
|
|
||||||
const Buffer& buffer = context.buffer();
|
const Buffer& buffer = context.buffer();
|
||||||
auto& shell_manager = ShellManager::instance();
|
auto& shell_manager = ShellManager::instance();
|
||||||
Vector<Selection> keep;
|
Vector<Selection> keep;
|
||||||
|
|
1
test/normal/keep-cmd-reg/cmd
Normal file
1
test/normal/keep-cmd-reg/cmd
Normal file
|
@ -0,0 +1 @@
|
||||||
|
%<a-s>H"a$<ret>
|
3
test/normal/keep-cmd-reg/in
Normal file
3
test/normal/keep-cmd-reg/in
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
foo
|
||||||
|
rha
|
||||||
|
bar
|
1
test/normal/keep-cmd-reg/kak_quoted_selections
Normal file
1
test/normal/keep-cmd-reg/kak_quoted_selections
Normal file
|
@ -0,0 +1 @@
|
||||||
|
'foo' 'bar'
|
1
test/normal/keep-cmd-reg/rc
Normal file
1
test/normal/keep-cmd-reg/rc
Normal file
|
@ -0,0 +1 @@
|
||||||
|
set-register a %{grep 'foo\|bar'}
|
1
test/normal/pipe-reg/cmd
Normal file
1
test/normal/pipe-reg/cmd
Normal file
|
@ -0,0 +1 @@
|
||||||
|
"a|<ret>
|
1
test/normal/pipe-reg/in
Normal file
1
test/normal/pipe-reg/in
Normal file
|
@ -0,0 +1 @@
|
||||||
|
%(foo)
|
1
test/normal/pipe-reg/out
Normal file
1
test/normal/pipe-reg/out
Normal file
|
@ -0,0 +1 @@
|
||||||
|
bar
|
1
test/normal/pipe-reg/rc
Normal file
1
test/normal/pipe-reg/rc
Normal file
|
@ -0,0 +1 @@
|
||||||
|
set-register a 'sed s/foo/bar/'
|
1
test/normal/pipe-to-reg/cmd
Normal file
1
test/normal/pipe-to-reg/cmd
Normal file
|
@ -0,0 +1 @@
|
||||||
|
"a<a-|><ret>:e! out<ret>
|
1
test/normal/pipe-to-reg/in
Normal file
1
test/normal/pipe-to-reg/in
Normal file
|
@ -0,0 +1 @@
|
||||||
|
%(foobar)
|
1
test/normal/pipe-to-reg/out
Normal file
1
test/normal/pipe-to-reg/out
Normal file
|
@ -0,0 +1 @@
|
||||||
|
barbar
|
1
test/normal/pipe-to-reg/rc
Normal file
1
test/normal/pipe-to-reg/rc
Normal file
|
@ -0,0 +1 @@
|
||||||
|
set-register a %{sed -n "s/foo/bar/; P" > out}
|
Loading…
Reference in New Issue
Block a user