mirror of
https://github.com/mawww/kakoune.git
synced 2024-12-11 12:44:59 +03:00
Use StringView in shell manager
This commit is contained in:
parent
63a0b9d22a
commit
7cdb94d5c7
36
src/main.cc
36
src/main.cc
@ -59,27 +59,27 @@ void register_env_vars()
|
|||||||
{
|
{
|
||||||
static const struct {
|
static const struct {
|
||||||
const char* name;
|
const char* name;
|
||||||
String (*func)(const String&, const Context&);
|
String (*func)(StringView, const Context&);
|
||||||
} env_vars[] = { {
|
} env_vars[] = { {
|
||||||
"bufname",
|
"bufname",
|
||||||
[](const String& name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return context.buffer().display_name(); }
|
{ return context.buffer().display_name(); }
|
||||||
}, {
|
}, {
|
||||||
"buffile",
|
"buffile",
|
||||||
[](const String& name, const Context& context) -> String
|
[](StringView name, const Context& context) -> String
|
||||||
{ return context.buffer().name(); }
|
{ return context.buffer().name(); }
|
||||||
}, {
|
}, {
|
||||||
"timestamp",
|
"timestamp",
|
||||||
[](const String& name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return to_string(context.buffer().timestamp()); }
|
{ return to_string(context.buffer().timestamp()); }
|
||||||
}, {
|
}, {
|
||||||
"selection",
|
"selection",
|
||||||
[](const String& name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ const Selection& sel = context.selections().main();
|
{ const Selection& sel = context.selections().main();
|
||||||
return content(context.buffer(), sel); }
|
return content(context.buffer(), sel); }
|
||||||
}, {
|
}, {
|
||||||
"selections",
|
"selections",
|
||||||
[](const String& name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ auto sels = context.selections_content();
|
{ auto sels = context.selections_content();
|
||||||
String res;
|
String res;
|
||||||
for (size_t i = 0; i < sels.size(); ++i)
|
for (size_t i = 0; i < sels.size(); ++i)
|
||||||
@ -91,55 +91,55 @@ void register_env_vars()
|
|||||||
return res; }
|
return res; }
|
||||||
}, {
|
}, {
|
||||||
"runtime",
|
"runtime",
|
||||||
[](const String& name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return runtime_directory(); }
|
{ return runtime_directory(); }
|
||||||
}, {
|
}, {
|
||||||
"opt_.+",
|
"opt_.+",
|
||||||
[](const String& name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return context.options()[name.substr(4_byte)].get_as_string(); }
|
{ return context.options()[name.substr(4_byte)].get_as_string(); }
|
||||||
}, {
|
}, {
|
||||||
"reg_.+",
|
"reg_.+",
|
||||||
[](const String& name, const Context& context) -> String
|
[](StringView name, const Context& context) -> String
|
||||||
{ return RegisterManager::instance()[name[4]].values(context)[0]; }
|
{ return RegisterManager::instance()[name[4]].values(context)[0]; }
|
||||||
}, {
|
}, {
|
||||||
"client_env_.+",
|
"client_env_.+",
|
||||||
[](const String& name, const Context& context) -> String
|
[](StringView name, const Context& context) -> String
|
||||||
{ return context.client().get_env_var(name.substr(11_byte)); }
|
{ return context.client().get_env_var(name.substr(11_byte)); }
|
||||||
}, {
|
}, {
|
||||||
"session",
|
"session",
|
||||||
[](const String& name, const Context& context) -> String
|
[](StringView name, const Context& context) -> String
|
||||||
{ return Server::instance().session(); }
|
{ return Server::instance().session(); }
|
||||||
}, {
|
}, {
|
||||||
"client",
|
"client",
|
||||||
[](const String& name, const Context& context) -> String
|
[](StringView name, const Context& context) -> String
|
||||||
{ return context.name(); }
|
{ return context.name(); }
|
||||||
}, {
|
}, {
|
||||||
"cursor_line",
|
"cursor_line",
|
||||||
[](const String& name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return to_string(context.selections().main().cursor().line + 1); }
|
{ return to_string(context.selections().main().cursor().line + 1); }
|
||||||
}, {
|
}, {
|
||||||
"cursor_column",
|
"cursor_column",
|
||||||
[](const String& name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return to_string(context.selections().main().cursor().column + 1); }
|
{ return to_string(context.selections().main().cursor().column + 1); }
|
||||||
}, {
|
}, {
|
||||||
"cursor_char_column",
|
"cursor_char_column",
|
||||||
[](const String& name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ auto coord = context.selections().main().cursor();
|
{ auto coord = context.selections().main().cursor();
|
||||||
return to_string(context.buffer()[coord.line].char_count_to(coord.column) + 1); }
|
return to_string(context.buffer()[coord.line].char_count_to(coord.column) + 1); }
|
||||||
}, {
|
}, {
|
||||||
"selection_desc",
|
"selection_desc",
|
||||||
[](const String& name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ auto& sel = context.selections().main();
|
{ auto& sel = context.selections().main();
|
||||||
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); }
|
||||||
}, {
|
}, {
|
||||||
"window_width",
|
"window_width",
|
||||||
[](const String& name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return to_string(context.window().dimensions().column); }
|
{ return to_string(context.window().dimensions().column); }
|
||||||
}, {
|
}, {
|
||||||
"window_height",
|
"window_height",
|
||||||
[](const String& name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return to_string(context.window().dimensions().line); }
|
{ return to_string(context.window().dimensions().line); }
|
||||||
} };
|
} };
|
||||||
|
|
||||||
|
@ -16,15 +16,15 @@ ShellManager::ShellManager()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
String ShellManager::eval(const String& cmdline, const Context& context,
|
String ShellManager::eval(StringView cmdline, const Context& context,
|
||||||
memoryview<String> params,
|
memoryview<String> params,
|
||||||
const EnvVarMap& env_vars)
|
const EnvVarMap& env_vars)
|
||||||
{
|
{
|
||||||
return pipe("", cmdline, context, params, env_vars);
|
return pipe("", cmdline, context, params, env_vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
String ShellManager::pipe(const String& input,
|
String ShellManager::pipe(StringView input,
|
||||||
const String& cmdline, const Context& context,
|
StringView cmdline, const Context& context,
|
||||||
memoryview<String> params,
|
memoryview<String> params,
|
||||||
const EnvVarMap& env_vars)
|
const EnvVarMap& env_vars)
|
||||||
{
|
{
|
||||||
@ -43,8 +43,7 @@ String ShellManager::pipe(const String& input,
|
|||||||
close(read_pipe[1]);
|
close(read_pipe[1]);
|
||||||
close(error_pipe[1]);
|
close(error_pipe[1]);
|
||||||
|
|
||||||
auto data = input.data();
|
write(write_pipe[1], input.data(), (int)input.length());
|
||||||
write(write_pipe[1], data.pointer(), data.size());
|
|
||||||
close(write_pipe[1]);
|
close(write_pipe[1]);
|
||||||
|
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
@ -79,18 +78,18 @@ String ShellManager::pipe(const String& input,
|
|||||||
dup2(error_pipe[1], 2); close(error_pipe[1]);
|
dup2(error_pipe[1], 2); close(error_pipe[1]);
|
||||||
dup2(write_pipe[0], 0); close(write_pipe[0]);
|
dup2(write_pipe[0], 0); close(write_pipe[0]);
|
||||||
|
|
||||||
boost::regex_iterator<String::const_iterator> it(cmdline.begin(), cmdline.end(), env_var_regex);
|
boost::regex_iterator<StringView::iterator> it(cmdline.begin(), cmdline.end(), env_var_regex);
|
||||||
boost::regex_iterator<String::const_iterator> end;
|
boost::regex_iterator<StringView::iterator> end;
|
||||||
|
|
||||||
while (it != end)
|
while (it != end)
|
||||||
{
|
{
|
||||||
auto& match = *it;
|
auto& match = *it;
|
||||||
|
|
||||||
String name;
|
StringView name;
|
||||||
if (match[1].matched)
|
if (match[1].matched)
|
||||||
name = String(match[1].first, match[1].second);
|
name = StringView(match[1].first, match[1].second);
|
||||||
else if (match[2].matched)
|
else if (match[2].matched)
|
||||||
name = String(match[2].first, match[2].second);
|
name = StringView(match[2].first, match[2].second);
|
||||||
else
|
else
|
||||||
kak_assert(false);
|
kak_assert(false);
|
||||||
kak_assert(name.length() > 0);
|
kak_assert(name.length() > 0);
|
||||||
@ -111,7 +110,7 @@ String ShellManager::pipe(const String& input,
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
String value = env_var->second(name, context);
|
String value = env_var->second(name, context);
|
||||||
setenv(("kak_" + name).c_str(), value.c_str(), 1);
|
setenv(("kak_"_str + name).c_str(), value.c_str(), 1);
|
||||||
}
|
}
|
||||||
catch (runtime_error&) {}
|
catch (runtime_error&) {}
|
||||||
}
|
}
|
||||||
@ -119,22 +118,23 @@ String ShellManager::pipe(const String& input,
|
|||||||
|
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
String shell = "/bin/sh";
|
const char* shell = "/bin/sh";
|
||||||
std::vector<const char*> execparams = { shell.c_str(), "-c", cmdline.c_str() };
|
auto cmdlinezstr = cmdline.zstr();
|
||||||
|
std::vector<const char*> execparams = { shell, "-c", cmdlinezstr };
|
||||||
if (not params.empty())
|
if (not params.empty())
|
||||||
execparams.push_back(shell.c_str());
|
execparams.push_back(shell);
|
||||||
for (auto& param : params)
|
for (auto& param : params)
|
||||||
execparams.push_back(param.c_str());
|
execparams.push_back(param.c_str());
|
||||||
execparams.push_back(nullptr);
|
execparams.push_back(nullptr);
|
||||||
|
|
||||||
execvp(shell.c_str(), (char* const*)execparams.data());
|
execvp(shell, (char* const*)execparams.data());
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
catch (...) { exit(-1); }
|
catch (...) { exit(-1); }
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShellManager::register_env_var(const String& regex,
|
void ShellManager::register_env_var(StringView regex,
|
||||||
EnvVarRetriever retriever)
|
EnvVarRetriever retriever)
|
||||||
{
|
{
|
||||||
m_env_vars.push_back({ Regex(regex.begin(), regex.end()), std::move(retriever) });
|
m_env_vars.push_back({ Regex(regex.begin(), regex.end()), std::move(retriever) });
|
||||||
|
@ -9,23 +9,23 @@ namespace Kakoune
|
|||||||
{
|
{
|
||||||
|
|
||||||
class Context;
|
class Context;
|
||||||
using EnvVarRetriever = std::function<String (const String& name, const Context&)>;
|
using EnvVarRetriever = std::function<String (StringView name, const Context&)>;
|
||||||
|
|
||||||
class ShellManager : public Singleton<ShellManager>
|
class ShellManager : public Singleton<ShellManager>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ShellManager();
|
ShellManager();
|
||||||
|
|
||||||
String eval(const String& cmdline, const Context& context,
|
String eval(StringView cmdline, const Context& context,
|
||||||
memoryview<String> params,
|
memoryview<String> params,
|
||||||
const EnvVarMap& env_vars);
|
const EnvVarMap& env_vars);
|
||||||
|
|
||||||
String pipe(const String& input,
|
String pipe(StringView input,
|
||||||
const String& cmdline, const Context& context,
|
StringView cmdline, const Context& context,
|
||||||
memoryview<String> params,
|
memoryview<String> params,
|
||||||
const EnvVarMap& env_vars);
|
const EnvVarMap& env_vars);
|
||||||
|
|
||||||
void register_env_var(const String& regex, EnvVarRetriever retriever);
|
void register_env_var(StringView regex, EnvVarRetriever retriever);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::pair<Regex, EnvVarRetriever>> m_env_vars;
|
std::vector<std::pair<Regex, EnvVarRetriever>> m_env_vars;
|
||||||
|
Loading…
Reference in New Issue
Block a user