move {exec,eval}_string common code to a helper context_wrap function

This commit is contained in:
Maxime Coste 2013-01-17 14:06:06 +01:00
parent c45838cc57
commit edb5dbc56f

View File

@ -536,61 +536,51 @@ void exec_keys(const KeyList& keys, Context& context)
context.change_editor(new_context.editor()); context.change_editor(new_context.editor());
} }
void exec_string(const CommandParameters& params, Context& context) template<typename Func>
void context_wrap(const CommandParameters& params, Context& context, Func func)
{ {
ParametersParser parser(params, { { "client", true }, { "restore-selections", false }}); ParametersParser parser(params, { { "client", true }, { "restore-selections", false }});
if (parser.positional_count() == 0) if (parser.positional_count() == 0)
throw wrong_argument_count(); throw wrong_argument_count();
Context& real_context = parser.has_option("client") ?
ClientManager::instance().get_client_context(parser.option_value("client"))
: context;
const bool restore_selections = parser.has_option("restore-selections");
Editor& editor = real_context.editor();
DynamicSelectionList sels(editor.buffer());
if (restore_selections)
sels = editor.selections();
auto restore_editor = on_scope_end([&]{
if (restore_selections)
editor.select(sels);
});
func(parser, real_context);
}
void exec_string(const CommandParameters& params, Context& context)
{
context_wrap(params, context, [](const ParametersParser& parser, Context& context) {
KeyList keys; KeyList keys;
for (auto& param : parser) for (auto& param : parser)
{ {
KeyList param_keys = parse_keys(param); KeyList param_keys = parse_keys(param);
keys.insert(keys.end(), param_keys.begin(), param_keys.end()); keys.insert(keys.end(), param_keys.begin(), param_keys.end());
} }
exec_keys(keys, context);
Context& keys_context = parser.has_option("client") ?
ClientManager::instance().get_client_context(parser.option_value("client"))
: context;
const bool restore_selections = parser.has_option("restore-selections");
Editor& editor = keys_context.editor();
DynamicSelectionList sels(editor.buffer());
if (restore_selections)
sels = editor.selections();
auto restore_editor = on_scope_end([&]{
if (restore_selections)
editor.select(sels);
}); });
exec_keys(keys, keys_context);
} }
void eval_string(const CommandParameters& params, Context& context) void eval_string(const CommandParameters& params, Context& context)
{ {
ParametersParser parser(params, { { "client", true }, { "restore-selections", false } }); context_wrap(params, context, [](const ParametersParser& parser, Context& context) {
if (parser.positional_count() == 0)
throw wrong_argument_count();
String command; String command;
for (auto& param : parser) for (auto& param : parser)
command += param + " "; command += param + " ";
CommandManager::instance().execute(command, context);
Context& command_context = parser.has_option("client") ?
ClientManager::instance().get_client_context(parser.option_value("client"))
: context;
const bool restore_selections = parser.has_option("restore-selections");
Editor& editor = command_context.editor();
DynamicSelectionList sels(editor.buffer());
if (restore_selections)
sels = editor.selections();
auto restore_editor = on_scope_end([&]{
if (restore_selections)
editor.select(sels);
}); });
CommandManager::instance().execute(command, command_context);
} }
void menu(const CommandParameters& params, Context& context) void menu(const CommandParameters& params, Context& context)