From 84fc4bd6cb8e49a26af8f64866f0236f00fdefbe Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 3 Dec 2015 13:48:30 +0000 Subject: [PATCH] Support -e in filter mode to give commands to execute (executed before keys) --- src/main.cc | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main.cc b/src/main.cc index 5dbd908a..a0f08527 100644 --- a/src/main.cc +++ b/src/main.cc @@ -556,23 +556,26 @@ int run_server(StringView session, StringView init_command, return 0; } -int run_filter(StringView keystr, ConstArrayView files, bool quiet) +int run_filter(StringView keystr, StringView commands, ConstArrayView files, bool quiet) { StringRegistry string_registry; GlobalScope global_scope; ShellManager shell_manager; + CommandManager command_manager; BufferManager buffer_manager; RegisterManager register_manager; + ClientManager client_manager; register_options(); register_env_vars(); register_registers(); + register_commands(); try { auto keys = parse_keys(keystr); - auto apply_keys_to_buffer = [&](Buffer& buffer) + auto apply_to_buffer = [&](Buffer& buffer) { try { @@ -581,6 +584,10 @@ int run_filter(StringView keystr, ConstArrayView files, bool quiet) Context::Flags::Transient }; + if (not commands.empty()) + command_manager.execute(commands, input_handler.context(), + ShellContext{}); + for (auto& key : keys) input_handler.handle_key(key); } @@ -596,7 +603,7 @@ int run_filter(StringView keystr, ConstArrayView files, bool quiet) { Buffer* buffer = open_file_buffer(file); write_buffer_to_file(*buffer, file + ".kak-bak"); - apply_keys_to_buffer(*buffer); + apply_to_buffer(*buffer); write_buffer_to_file(*buffer, file); buffer_manager.delete_buffer(*buffer); } @@ -604,7 +611,7 @@ int run_filter(StringView keystr, ConstArrayView files, bool quiet) { Buffer* buffer = new Buffer("*stdin*", Buffer::Flags::None, read_fd(0), InvalidTime); - apply_keys_to_buffer(*buffer); + apply_to_buffer(*buffer); write_buffer_to_fd(*buffer, 1); buffer_manager.delete_buffer(*buffer); } @@ -697,17 +704,18 @@ int main(int argc, char* argv[]) } 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 files; for (size_t i = 0; i < parser.positional_count(); ++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")) { for (auto opt : { "n", "s", "d" })