From 419d5691a19c26b81b00278507bd4889a64b165e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 4 Jun 2012 14:27:34 +0000 Subject: [PATCH] add a try command use try catch --- src/commands.cc | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/commands.cc b/src/commands.cc index 24990f75..ebcb4590 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -743,6 +743,30 @@ void menu(const CommandParameters& params, CommandManager::instance().execute(parser[(i-1)*2+1], context); } +void try_catch(const CommandParameters& params, + const Context& context) +{ + size_t i = 0; + for (; i < params.size(); ++i) + { + if (params[i] == "catch") + break; + } + + if (i == 0 or i == params.size()) + throw wrong_argument_count(); + + CommandManager& command_manager = CommandManager::instance(); + try + { + command_manager.execute(params.subrange(0, i), context); + } + catch (Kakoune::runtime_error& e) + { + command_manager.execute(params.subrange(i+1, params.size() - i - 1), context); + } +} + } void register_commands() @@ -817,6 +841,7 @@ void register_commands() cm.register_command("exec", exec_string); cm.register_command("eval", eval_string); cm.register_command("menu", menu); + cm.register_command("try", try_catch, CommandManager::IgnoreSemiColons | CommandManager::DeferredShellEval); cm.register_command("def", define_command, CommandManager::IgnoreSemiColons | CommandManager::DeferredShellEval); cm.register_command("echo", echo_message);