clean up env var registration

This commit is contained in:
Maxime Coste 2013-10-01 19:47:37 +02:00
parent c312b06e39
commit fb80ef092e

View File

@ -57,21 +57,24 @@ String runtime_directory()
void register_env_vars() void register_env_vars()
{ {
ShellManager& shell_manager = ShellManager::instance(); struct EnvVarDesc { const char* name; String (*func)(const String&, const Context&); };
EnvVarDesc env_vars[] = { {
shell_manager.register_env_var("bufname", "bufname",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ return context.buffer().display_name(); }); { return context.buffer().display_name(); }
shell_manager.register_env_var("timestamp", }, {
"timestamp",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ return to_string(context.buffer().timestamp()); }); { return to_string(context.buffer().timestamp()); }
shell_manager.register_env_var("selection", }, {
"selection",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ const Range& sel = context.editor().main_selection(); { const Range& sel = context.editor().main_selection();
return content(context.buffer(), sel); }); return content(context.buffer(), sel); }
shell_manager.register_env_var("selections", }, {
[](const String& name, const Context& context) { "selections",
auto sels = context.editor().selections_content(); [](const String& name, const Context& context)
{ auto sels = context.editor().selections_content();
String res; String res;
for (size_t i = 0; i < sels.size(); ++i) for (size_t i = 0; i < sels.size(); ++i)
{ {
@ -79,41 +82,55 @@ void register_env_vars()
if (i != sels.size() - 1) if (i != sels.size() - 1)
res += ':'; res += ':';
} }
return res; return res; }
}); }, {
shell_manager.register_env_var("runtime", "runtime",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ return runtime_directory(); }); { return runtime_directory(); }
shell_manager.register_env_var("opt_.+", }, {
"opt_.+",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ return context.options()[name.substr(4_byte)].get_as_string(); }); { return context.options()[name.substr(4_byte)].get_as_string(); }
shell_manager.register_env_var("reg_.+", }, {
"reg_.+",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ return RegisterManager::instance()[name[4]].values(context)[0]; }); { return RegisterManager::instance()[name[4]].values(context)[0]; }
shell_manager.register_env_var("session", }, {
"session",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ return Server::instance().session(); }); { return Server::instance().session(); }
shell_manager.register_env_var("client", }, {
"client",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ return context.client().name(); }); { return context.client().name(); }
shell_manager.register_env_var("cursor_line", }, {
"cursor_line",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ return to_string(context.editor().main_selection().last().line + 1); }); { return to_string(context.editor().main_selection().last().line + 1); }
shell_manager.register_env_var("cursor_column", }, {
"cursor_column",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ return to_string(context.editor().main_selection().last().column + 1); }); { return to_string(context.editor().main_selection().last().column + 1); }
shell_manager.register_env_var("selection_desc", }, {
"selection_desc",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ auto& sel = context.editor().main_selection(); { auto& sel = context.editor().main_selection();
auto beg = sel.min(); auto beg = sel.min();
return to_string(beg.line + 1) + ':' + to_string(beg.column + 1) + '+' + return to_string(beg.line + 1) + ':' + to_string(beg.column + 1) + '+' +
to_string((int)context.buffer().distance(beg, sel.max())+1); }); to_string((int)context.buffer().distance(beg, sel.max())+1); }
shell_manager.register_env_var("window_width", }, {
"window_width",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ return to_string(context.window().dimensions().column); }); { return to_string(context.window().dimensions().column); }
shell_manager.register_env_var("window_height", }, {
"window_height",
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ return to_string(context.window().dimensions().line); }); { return to_string(context.window().dimensions().line); }
} };
ShellManager& shell_manager = ShellManager::instance();
for (auto& env_var : env_vars)
shell_manager.register_env_var(env_var.name, env_var.func);
} }
void register_registers() void register_registers()