From e9af61eb9f7211a4d901c383772c46634cfbfc20 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 15 Feb 2012 13:58:08 +0000 Subject: [PATCH] def command takes an optional argument for parameter policy -env-params puts parameters in kak_param{0..9} -append-params appends parameters to the ones defined in the command --- src/main.cc | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/src/main.cc b/src/main.cc index 0136fd83..0c61006a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -724,11 +724,44 @@ void define_command(const CommandParameters& params, const Context& context) if (params.size() < 2) throw wrong_argument_count(); - std::vector cmd_params(params.begin()+1, params.end()); - auto func = [=](const CommandParameters& params, const Context& context) { - CommandManager::instance().execute(cmd_params, context); - }; - CommandManager::instance().register_command(params[0], func); + if (params[0] == "-env-params") + { + std::vector cmd_params(params.begin() + 2, params.end()); + CommandManager::instance().register_command(params[1], + [=](const CommandParameters& params, const Context& context) { + char param_name[] = "kak_param0"; + for (size_t i = 0; i < 10; ++i) + { + param_name[sizeof(param_name) - 2] = '0' + i; + if (params.size() > i) + setenv(param_name, params[i].c_str(), 1); + else + unsetenv(param_name); + } + CommandManager::instance().execute(cmd_params, context); + }); + } + else if (params[0] == "-append-params") + { + std::vector cmd_params(params.begin() + 2, params.end()); + CommandManager::instance().register_command(params[1], + [=](const CommandParameters& params, const Context& context) { + std::vector merged_params = cmd_params; + for (auto& param : params) + merged_params.push_back(param); + CommandManager::instance().execute(merged_params, context); + }); + } + else + { + std::vector cmd_params(params.begin() + 1, params.end()); + CommandManager::instance().register_command(params[0], + [=](const CommandParameters& params, const Context& context) { + if (not params.empty()) + throw wrong_argument_count(); + CommandManager::instance().execute(cmd_params, context); + }); + } } void exec_commands_in_file(const CommandParameters& params,