Support -e in filter mode to give commands to execute (executed before keys)

This commit is contained in:
Maxime Coste 2015-12-03 13:48:30 +00:00
parent d09dd09b6c
commit 84fc4bd6cb

View File

@ -556,23 +556,26 @@ int run_server(StringView session, StringView init_command,
return 0; return 0;
} }
int run_filter(StringView keystr, ConstArrayView<StringView> files, bool quiet) int run_filter(StringView keystr, StringView commands, ConstArrayView<StringView> files, bool quiet)
{ {
StringRegistry string_registry; StringRegistry string_registry;
GlobalScope global_scope; GlobalScope global_scope;
ShellManager shell_manager; ShellManager shell_manager;
CommandManager command_manager;
BufferManager buffer_manager; BufferManager buffer_manager;
RegisterManager register_manager; RegisterManager register_manager;
ClientManager client_manager;
register_options(); register_options();
register_env_vars(); register_env_vars();
register_registers(); register_registers();
register_commands();
try try
{ {
auto keys = parse_keys(keystr); auto keys = parse_keys(keystr);
auto apply_keys_to_buffer = [&](Buffer& buffer) auto apply_to_buffer = [&](Buffer& buffer)
{ {
try try
{ {
@ -581,6 +584,10 @@ int run_filter(StringView keystr, ConstArrayView<StringView> files, bool quiet)
Context::Flags::Transient Context::Flags::Transient
}; };
if (not commands.empty())
command_manager.execute(commands, input_handler.context(),
ShellContext{});
for (auto& key : keys) for (auto& key : keys)
input_handler.handle_key(key); input_handler.handle_key(key);
} }
@ -596,7 +603,7 @@ int run_filter(StringView keystr, ConstArrayView<StringView> files, bool quiet)
{ {
Buffer* buffer = open_file_buffer(file); Buffer* buffer = open_file_buffer(file);
write_buffer_to_file(*buffer, file + ".kak-bak"); write_buffer_to_file(*buffer, file + ".kak-bak");
apply_keys_to_buffer(*buffer); apply_to_buffer(*buffer);
write_buffer_to_file(*buffer, file); write_buffer_to_file(*buffer, file);
buffer_manager.delete_buffer(*buffer); buffer_manager.delete_buffer(*buffer);
} }
@ -604,7 +611,7 @@ int run_filter(StringView keystr, ConstArrayView<StringView> files, bool quiet)
{ {
Buffer* buffer = new Buffer("*stdin*", Buffer::Flags::None, Buffer* buffer = new Buffer("*stdin*", Buffer::Flags::None,
read_fd(0), InvalidTime); read_fd(0), InvalidTime);
apply_keys_to_buffer(*buffer); apply_to_buffer(*buffer);
write_buffer_to_fd(*buffer, 1); write_buffer_to_fd(*buffer, 1);
buffer_manager.delete_buffer(*buffer); buffer_manager.delete_buffer(*buffer);
} }
@ -697,17 +704,18 @@ int main(int argc, char* argv[])
} }
return run_pipe(*session); return run_pipe(*session);
} }
else if (auto keys = parser.get_switch("f"))
auto init_command = parser.get_switch("e").value_or(StringView{});
if (auto keys = parser.get_switch("f"))
{ {
Vector<StringView> files; Vector<StringView> files;
for (size_t i = 0; i < parser.positional_count(); ++i) for (size_t i = 0; i < parser.positional_count(); ++i)
files.emplace_back(parser[i]); files.emplace_back(parser[i]);
return run_filter(*keys, files, (bool)parser.get_switch("q")); return run_filter(*keys, init_command, files, (bool)parser.get_switch("q"));
} }
auto init_command = parser.get_switch("e").value_or(StringView{});
if (auto server_session = parser.get_switch("c")) if (auto server_session = parser.get_switch("c"))
{ {
for (auto opt : { "n", "s", "d" }) for (auto opt : { "n", "s", "d" })