Expand env vars as list of strings

This makes it possible to do :select `%val{selections_decs}` and to
correctly combine $kak_quoted with those.
This commit is contained in:
Maxime Coste 2020-03-01 13:28:06 +11:00
parent b8eef27e04
commit 0a66eb9c47
23 changed files with 144 additions and 119 deletions

View File

@ -3,6 +3,11 @@
This changelog contains major and/or breaking changes to Kakoune between This changelog contains major and/or breaking changes to Kakoune between
released versions. released versions.
== Development version
* `%var{...}` now expands to list of strings, `$kak_quoted_...` now work
as expected with these.
== Kakoune v2020.01.16 == Kakoune v2020.01.16
* Expose history tree through `$kak_history` and * Expose history tree through `$kak_history` and

View File

@ -228,30 +228,35 @@ auto to_string(Buffer::HistoryId id)
return to_string(static_cast<size_t>(id)); return to_string(static_cast<size_t>(id));
} }
String history_as_string(const Vector<Buffer::HistoryNode>& history, Quoting quoting) static String modification_as_string(const Buffer::Modification& modification)
{ {
auto format_history_node = [&](const Buffer::HistoryNode& node) { return format("{}{}.{}|{}",
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(node.committed.time_since_epoch());
return format("{} {} {}{}{}",
node.parent,
seconds.count(),
node.redo_child,
node.undo_group.empty() ? "" : " ",
undo_group_as_string(node.undo_group, quoting));
};
return join(history | transform(format_history_node), ' ', false);
}
String undo_group_as_string(const Buffer::UndoGroup& undo_group, Quoting quoting)
{
auto modification_as_string = [&](const Buffer::Modification& modification) {
auto quote = quoter(quoting);
return quote(format("{}{}.{}|{}",
modification.type == Buffer::Modification::Type::Insert ? '+' : '-', modification.type == Buffer::Modification::Type::Insert ? '+' : '-',
modification.coord.line, modification.coord.column, modification.coord.line, modification.coord.column,
modification.content->strview())); modification.content->strview());
}
Vector<String> history_as_strings(const Vector<Buffer::HistoryNode>& history)
{
Vector<String> res;
for (auto& node : history)
{
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(node.committed.time_since_epoch());
res.push_back(to_string(node.parent));
res.push_back(to_string(seconds.count()));
res.push_back(to_string(node.redo_child));
for (auto& modification : node.undo_group)
res.push_back(modification_as_string(modification));
}; };
return join(undo_group | transform(modification_as_string), ' ', false); return res;
}
Vector<String> undo_group_as_strings(const Buffer::UndoGroup& undo_group)
{
Vector<String> res;
for (auto& modification : undo_group)
res.push_back(modification_as_string(modification));
return res;
} }
} }

View File

@ -84,8 +84,8 @@ void reload_file_buffer(Buffer& buffer);
void write_to_debug_buffer(StringView str); void write_to_debug_buffer(StringView str);
String history_as_string(const Vector<Buffer::HistoryNode>& history, Quoting quoting); Vector<String> history_as_strings(const Vector<Buffer::HistoryNode>& history);
String undo_group_as_string(const Buffer::UndoGroup& undo_group, Quoting quoting); Vector<String> undo_group_as_strings(const Buffer::UndoGroup& undo_group);
} }

View File

@ -362,7 +362,10 @@ expand_token(const Token& token, const Context& context, const ShellContext& she
auto it = shell_context.env_vars.find(content); auto it = shell_context.env_vars.find(content);
if (it != shell_context.env_vars.end()) if (it != shell_context.env_vars.end())
return {it->value}; return {it->value};
return {ShellManager::instance().get_val(content, context, Quoting::Kakoune)}; if constexpr (single)
return join(ShellManager::instance().get_val(content, context), false, ' ');
else
return ShellManager::instance().get_val(content, context);
} }
case Token::Type::ArgExpand: case Token::Type::ArgExpand:
{ {

View File

@ -156,179 +156,193 @@ String config_directory()
return format("{}/.config/kak", homedir()); return format("{}/.config/kak", homedir());
} }
static auto main_sel_first(const SelectionList& selections)
{
auto beg = &*selections.begin(), end = &*selections.end();
auto main = beg + selections.main_index();
using View = ConstArrayView<Selection>;
return concatenated(View{main, end}, View{beg, main});
}
static const EnvVarDesc builtin_env_vars[] = { { static const EnvVarDesc builtin_env_vars[] = { {
"bufname", false, "bufname", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return context.buffer().display_name(); } { return {context.buffer().display_name()}; }
}, { }, {
"buffile", false, "buffile", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return context.buffer().name(); } { return {context.buffer().name()}; }
}, { }, {
"buflist", false, "buflist", false,
[](StringView name, const Context& context, Quoting quoting) [](StringView name, const Context& context) -> Vector<String>
{ return join(BufferManager::instance() | { return BufferManager::instance() | transform(&Buffer::display_name) | gather<Vector>(); }
transform(&Buffer::display_name) | transform(quoter(quoting)), ' ', false); }
}, { }, {
"buf_line_count", false, "buf_line_count", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return to_string(context.buffer().line_count()); } { return {to_string(context.buffer().line_count())}; }
}, { }, {
"timestamp", false, "timestamp", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return to_string(context.buffer().timestamp()); } { return {to_string(context.buffer().timestamp())}; }
}, { }, {
"history_id", false, "history_id", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return to_string((size_t)context.buffer().current_history_id()); } { return {to_string((size_t)context.buffer().current_history_id())}; }
}, { }, {
"selection", false, "selection", false,
[](StringView name, const Context& context, Quoting quoting) [](StringView name, const Context& context) -> Vector<String>
{ const Selection& sel = context.selections().main(); { const Selection& sel = context.selections().main();
return content(context.buffer(), sel); } return {content(context.buffer(), sel)}; }
}, { }, {
"selections", false, "selections", false,
[](StringView name, const Context& context, Quoting quoting) [](StringView name, const Context& context) -> Vector<String>
{ return join(context.selections_content() | transform(quoter(quoting)), ' ', false); } { return context.selections_content(); }
}, { }, {
"runtime", false, "runtime", false,
[](StringView name, const Context& context, Quoting quoting) [](StringView name, const Context& context) -> Vector<String>
{ return runtime_directory(); } { return {runtime_directory()}; }
}, { }, {
"config", false, "config", false,
[](StringView name, const Context& context, Quoting quoting) [](StringView name, const Context& context) -> Vector<String>
{ return config_directory(); } { return {config_directory()}; }
}, { }, {
"version", false, "version", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return version; } { return {version}; }
}, { }, {
"opt_", true, "opt_", true,
[](StringView name, const Context& context, Quoting quoting) [](StringView name, const Context& context) -> Vector<String>
{ return context.options()[name.substr(4_byte)].get_as_string(quoting); } { return context.options()[name.substr(4_byte)].get_as_strings(); }
}, { }, {
"main_reg_", true, "main_reg_", true,
[](StringView name, const Context& context, Quoting quoting) [](StringView name, const Context& context) -> Vector<String>
{ return context.main_sel_register_value(name.substr(9_byte)).str(); } { return {context.main_sel_register_value(name.substr(9_byte)).str()}; }
}, { }, {
"reg_", true, "reg_", true,
[](StringView name, const Context& context, Quoting quoting) [](StringView name, const Context& context)
{ return join(RegisterManager::instance()[name.substr(4_byte)].get(context) | { return RegisterManager::instance()[name.substr(4_byte)].get(context) |
transform(quoter(quoting)), ' ', false); } gather<Vector<String>>(); }
}, { }, {
"client_env_", true, "client_env_", true,
[](StringView name, const Context& context, Quoting quoting) [](StringView name, const Context& context) -> Vector<String>
{ return context.client().get_env_var(name.substr(11_byte)).str(); } { return {context.client().get_env_var(name.substr(11_byte)).str()}; }
}, { }, {
"session", false, "session", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return Server::instance().session(); } { return {Server::instance().session()}; }
}, { }, {
"client", false, "client", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return context.name(); } { return {context.name()}; }
}, { }, {
"client_pid", false, "client_pid", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return to_string(context.client().pid()); } { return {to_string(context.client().pid())}; }
}, { }, {
"client_list", false, "client_list", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return join(ClientManager::instance() | { return ClientManager::instance() |
transform([](const std::unique_ptr<Client>& c) -> const String& transform([](const std::unique_ptr<Client>& c) -> const String&
{ return c->context().name(); }), ' ', false); } { return c->context().name(); }) | gather<Vector>(); }
}, { }, {
"modified", false, "modified", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return context.buffer().is_modified() ? "true" : "false"; } { return {context.buffer().is_modified() ? "true" : "false"}; }
}, { }, {
"cursor_line", false, "cursor_line", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return to_string(context.selections().main().cursor().line + 1); } { return {to_string(context.selections().main().cursor().line + 1)}; }
}, { }, {
"cursor_column", false, "cursor_column", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return to_string(context.selections().main().cursor().column + 1); } { return {to_string(context.selections().main().cursor().column + 1)}; }
}, { }, {
"cursor_char_value", false, "cursor_char_value", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ auto coord = context.selections().main().cursor(); { auto coord = context.selections().main().cursor();
auto& buffer = context.buffer(); auto& buffer = context.buffer();
return to_string((size_t)utf8::codepoint(buffer.iterator_at(coord), buffer.end())); } return {to_string((size_t)utf8::codepoint(buffer.iterator_at(coord), buffer.end()))}; }
}, { }, {
"cursor_char_column", false, "cursor_char_column", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ 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)}; }
}, { }, {
"cursor_display_column", false, "cursor_display_column", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ auto coord = context.selections().main().cursor(); { auto coord = context.selections().main().cursor();
return to_string(get_column(context.buffer(), return {to_string(get_column(context.buffer(),
context.options()["tabstop"].get<int>(), context.options()["tabstop"].get<int>(),
coord) + 1); } coord) + 1)}; }
}, { }, {
"cursor_byte_offset", false, "cursor_byte_offset", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ auto cursor = context.selections().main().cursor(); { auto cursor = context.selections().main().cursor();
return to_string(context.buffer().distance({0,0}, cursor)); } return {to_string(context.buffer().distance({0,0}, cursor))}; }
}, { }, {
"selection_desc", false, "selection_desc", false,
[](StringView name, const Context& context, Quoting quoting) [](StringView name, const Context& context) -> Vector<String>
{ return selection_to_string(ColumnType::Byte, context.buffer(), { return {selection_to_string(ColumnType::Byte, context.buffer(), context.selections().main())}; }
context.selections().main()); }
}, { }, {
"selections_desc", false, "selections_desc", false,
[](StringView name, const Context& context, Quoting quoting) [](StringView name, const Context& context) -> Vector<String>
{ return selection_list_to_string(ColumnType::Byte, context.selections()); } { return main_sel_first(context.selections()) |
transform([&buffer=context.buffer()](const Selection& sel) {
return selection_to_string(ColumnType::Byte, buffer, sel);
}) | gather<Vector>(); }
}, { }, {
"selections_char_desc", false, "selections_char_desc", false,
[](StringView name, const Context& context, Quoting quoting) [](StringView name, const Context& context) -> Vector<String>
{ return selection_list_to_string(ColumnType::Codepoint, context.selections()); } { return main_sel_first(context.selections()) |
transform([&buffer=context.buffer()](const Selection& sel) {
return selection_to_string(ColumnType::Codepoint, buffer, sel);
}) | gather<Vector>(); }
}, { }, {
"selections_display_column_desc", false, "selections_display_column_desc", false,
[](StringView name, const Context& context, Quoting quoting) [](StringView name, const Context& context) -> Vector<String>
{ return selection_list_to_string(ColumnType::DisplayColumn, { return main_sel_first(context.selections()) |
context.selections(), transform([&buffer=context.buffer(), tabstop=context.options()["tabstop"].get<int>()](const Selection& sel) {
context.options()["tabstop"].get<int>()); } return selection_to_string(ColumnType::DisplayColumn, buffer, sel, tabstop);
}) | gather<Vector>(); }
}, { }, {
"selection_length", false, "selection_length", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return to_string(char_length(context.buffer(), context.selections().main())); } { return {to_string(char_length(context.buffer(), context.selections().main()))}; }
}, { }, {
"selections_length", false, "selections_length", false,
[](StringView name, const Context& context, Quoting quoting) [](StringView name, const Context& context) -> Vector<String>
{ return join(context.selections() | { return context.selections() |
transform([&](const Selection& s) transform([&](const Selection& s) {
{ return to_string(char_length(context.buffer(), s)); }), ' ', false); } return to_string(char_length(context.buffer(), s));
}) | gather<Vector<String>>(); }
}, { }, {
"window_width", false, "window_width", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return to_string(context.window().dimensions().column); } { return {to_string(context.window().dimensions().column)}; }
}, { }, {
"window_height", false, "window_height", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return to_string(context.window().dimensions().line); } { return {to_string(context.window().dimensions().line)}; }
}, { }, {
"user_modes", false, "user_modes", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return join(context.keymaps().user_modes(), ' ', false); } { return context.keymaps().user_modes(); }
}, { }, {
"window_range", false, "window_range", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ {
auto setup = context.window().compute_display_setup(context); auto setup = context.window().compute_display_setup(context);
return format("{} {} {} {}", setup.window_pos.line, setup.window_pos.column, return {format("{} {} {} {}", setup.window_pos.line, setup.window_pos.column,
setup.window_range.line, setup.window_range.column); setup.window_range.line, setup.window_range.column)};
} }
}, { }, {
"history", false, "history", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return history_as_string(context.buffer().history(), quoting); } { return history_as_strings(context.buffer().history()); }
}, { }, {
"uncommitted_modifications", false, "uncommitted_modifications", false,
[](StringView name, const Context& context, Quoting quoting) -> String [](StringView name, const Context& context) -> Vector<String>
{ return undo_group_as_string(context.buffer().current_undo_group(), quoting); } { return undo_group_as_strings(context.buffer().current_undo_group()); }
} }
}; };

View File

@ -152,8 +152,8 @@ Vector<String> generate_env(StringView cmdline, const Context& context, const Sh
auto var_it = shell_context.env_vars.find(name); auto var_it = shell_context.env_vars.find(name);
try try
{ {
const String& value = var_it != shell_context.env_vars.end() ? String value = var_it != shell_context.env_vars.end() ?
var_it->value : ShellManager::instance().get_val(name, context, quoting); var_it->value : join(ShellManager::instance().get_val(name, context) | transform(quoter(quoting)), ' ', false);
StringView quoted{match[1].first, match[1].second}; StringView quoted{match[1].first, match[1].second};
kak_env.push_back(format("kak_{}{}={}", quoted, name, value)); kak_env.push_back(format("kak_{}{}={}", quoted, name, value));
@ -327,7 +327,7 @@ std::pair<String, int> ShellManager::eval(
return { std::move(stdout_contents), WIFEXITED(status) ? WEXITSTATUS(status) : -1 }; return { std::move(stdout_contents), WIFEXITED(status) ? WEXITSTATUS(status) : -1 };
} }
String ShellManager::get_val(StringView name, const Context& context, Quoting quoting) const Vector<String> ShellManager::get_val(StringView name, const Context& context) const
{ {
auto env_var = find_if(m_env_vars, [name](const EnvVarDesc& desc) { auto env_var = find_if(m_env_vars, [name](const EnvVarDesc& desc) {
return desc.prefix ? prefix_match(name, desc.str) : name == desc.str; return desc.prefix ? prefix_match(name, desc.str) : name == desc.str;
@ -336,7 +336,7 @@ String ShellManager::get_val(StringView name, const Context& context, Quoting qu
if (env_var == m_env_vars.end()) if (env_var == m_env_vars.end())
throw runtime_error("no such env var: " + name); throw runtime_error("no such env var: " + name);
return env_var->func(name, context, quoting); return env_var->func(name, context);
} }
CandidateList ShellManager::complete_env_var(StringView prefix, CandidateList ShellManager::complete_env_var(StringView prefix,

View File

@ -18,11 +18,9 @@ struct ShellContext
EnvVarMap env_vars; EnvVarMap env_vars;
}; };
enum class Quoting;
struct EnvVarDesc struct EnvVarDesc
{ {
using Retriever = String (*)(StringView name, const Context&, Quoting quoting); using Retriever = Vector<String> (*)(StringView name, const Context&);
StringView str; StringView str;
bool prefix; bool prefix;
@ -46,7 +44,7 @@ public:
Flags flags = Flags::WaitForStdout, Flags flags = Flags::WaitForStdout,
const ShellContext& shell_context = {}); const ShellContext& shell_context = {});
String get_val(StringView name, const Context& context, Quoting quoting) const; Vector<String> get_val(StringView name, const Context& context) const;
CandidateList complete_env_var(StringView prefix, ByteCount cursor_pos) const; CandidateList complete_env_var(StringView prefix, ByteCount cursor_pos) const;

View File

@ -1 +1 @@
- $timestamp 1 0 $timestamp - '+0.5|m' '+0.6|i' '+0.7|d' '+0.8|d' '+0.9|l' '+0.10|e' '-0.8|dle' '-' '$timestamp' '1' '0' '$timestamp' '-' '+0.5|m' '+0.6|i' '+0.7|d' '+0.8|d' '+0.9|l' '+0.10|e' '-0.8|dle'

View File

@ -1,6 +1,6 @@
# Make our expansion have a predictable timestamp # Make our expansion have a predictable timestamp
hook global ClientClose .* %{ hook global ClientClose .* %{
nop %sh{ nop %sh{
kak -f 'ghf<space>ec$timestamp<esc>2f<space>ec$timestamp<esc>' kak_quoted_history kak -f 'ghf<space>lwc$timestamp<esc>3f<space>lwc$timestamp<esc>' kak_quoted_history
} }
} }

View File

@ -1 +1 @@
1 '1.1,1.1|a b' '1.2,1.2|b c' '1' '1.1,1.1|a b' '1.2,1.2|b c'