From 0b473ffe5801452652c2c3a95c7d1c6db44df54b Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 7 Dec 2013 13:44:23 +0000 Subject: [PATCH] eval and exec can use a -try-client option When -try-client is used, the context used for the command is either the given client's context if the client is found, or the current one if not. --- src/commands.cc | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 1fbf7324..bd4bd4ec 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -551,17 +551,25 @@ void map_key(CommandParameters params, Context& context) template void context_wrap(CommandParameters params, Context& context, Func func) { - ParametersParser parser(params, { { "client", true }, { "draft", false }, { "itersel", false } }, + ParametersParser parser(params, { { "client", true }, { "try-client", true }, + { "draft", false }, { "itersel", false } }, ParametersParser::Flags::OptionsOnlyAtStart, 1); - Context& real_context = parser.has_option("client") ? - ClientManager::instance().get_client(parser.option_value("client")).context() - : context; + ClientManager& cm = ClientManager::instance(); + Context* real_context = &context; + if (parser.has_option("client")) + real_context = &cm.get_client(parser.option_value("client")).context(); + else if (parser.has_option("try-client")) + { + Client* client = cm.get_client_ifp(parser.option_value("try-client")); + if (client) + real_context = &client->context(); + } if (parser.has_option("draft")) { - Editor& editor = real_context.editor(); - InputHandler input_handler(editor, real_context.name()); + Editor& editor = real_context->editor(); + InputHandler input_handler(editor, real_context->name()); DynamicSelectionList sels{editor.buffer(), editor.selections()}; auto restore_sels = on_scope_end([&]{ editor.select(sels); }); @@ -580,12 +588,12 @@ void context_wrap(CommandParameters params, Context& context, Func func) { if (parser.has_option("itersel")) throw runtime_error("-itersel makes no sense without -draft"); - func(parser, real_context); + func(parser, *real_context); } // force redraw of this client window - if (parser.has_option("client") and real_context.has_window()) - real_context.window().forget_timestamp(); + if (real_context != &context and real_context->has_window()) + real_context->window().forget_timestamp(); } void exec_string(CommandParameters params, Context& context)