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
released versions.
== Development version
* `%var{...}` now expands to list of strings, `$kak_quoted_...` now work
as expected with these.
== Kakoune v2020.01.16
* 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));
}
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) {
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("{}{}.{}|{}",
return format("{}{}.{}|{}",
modification.type == Buffer::Modification::Type::Insert ? '+' : '-',
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);
String history_as_string(const Vector<Buffer::HistoryNode>& history, Quoting quoting);
String undo_group_as_string(const Buffer::UndoGroup& undo_group, Quoting quoting);
Vector<String> history_as_strings(const Vector<Buffer::HistoryNode>& history);
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);
if (it != shell_context.env_vars.end())
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:
{

View File

@ -156,179 +156,193 @@ String config_directory()
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[] = { {
"bufname", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return context.buffer().display_name(); }
[](StringView name, const Context& context) -> Vector<String>
{ return {context.buffer().display_name()}; }
}, {
"buffile", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return context.buffer().name(); }
[](StringView name, const Context& context) -> Vector<String>
{ return {context.buffer().name()}; }
}, {
"buflist", false,
[](StringView name, const Context& context, Quoting quoting)
{ return join(BufferManager::instance() |
transform(&Buffer::display_name) | transform(quoter(quoting)), ' ', false); }
[](StringView name, const Context& context) -> Vector<String>
{ return BufferManager::instance() | transform(&Buffer::display_name) | gather<Vector>(); }
}, {
"buf_line_count", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(context.buffer().line_count()); }
[](StringView name, const Context& context) -> Vector<String>
{ return {to_string(context.buffer().line_count())}; }
}, {
"timestamp", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(context.buffer().timestamp()); }
[](StringView name, const Context& context) -> Vector<String>
{ return {to_string(context.buffer().timestamp())}; }
}, {
"history_id", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string((size_t)context.buffer().current_history_id()); }
[](StringView name, const Context& context) -> Vector<String>
{ return {to_string((size_t)context.buffer().current_history_id())}; }
}, {
"selection", false,
[](StringView name, const Context& context, Quoting quoting)
[](StringView name, const Context& context) -> Vector<String>
{ const Selection& sel = context.selections().main();
return content(context.buffer(), sel); }
return {content(context.buffer(), sel)}; }
}, {
"selections", false,
[](StringView name, const Context& context, Quoting quoting)
{ return join(context.selections_content() | transform(quoter(quoting)), ' ', false); }
[](StringView name, const Context& context) -> Vector<String>
{ return context.selections_content(); }
}, {
"runtime", false,
[](StringView name, const Context& context, Quoting quoting)
{ return runtime_directory(); }
[](StringView name, const Context& context) -> Vector<String>
{ return {runtime_directory()}; }
}, {
"config", false,
[](StringView name, const Context& context, Quoting quoting)
{ return config_directory(); }
[](StringView name, const Context& context) -> Vector<String>
{ return {config_directory()}; }
}, {
"version", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return version; }
[](StringView name, const Context& context) -> Vector<String>
{ return {version}; }
}, {
"opt_", true,
[](StringView name, const Context& context, Quoting quoting)
{ return context.options()[name.substr(4_byte)].get_as_string(quoting); }
[](StringView name, const Context& context) -> Vector<String>
{ return context.options()[name.substr(4_byte)].get_as_strings(); }
}, {
"main_reg_", true,
[](StringView name, const Context& context, Quoting quoting)
{ return context.main_sel_register_value(name.substr(9_byte)).str(); }
[](StringView name, const Context& context) -> Vector<String>
{ return {context.main_sel_register_value(name.substr(9_byte)).str()}; }
}, {
"reg_", true,
[](StringView name, const Context& context, Quoting quoting)
{ return join(RegisterManager::instance()[name.substr(4_byte)].get(context) |
transform(quoter(quoting)), ' ', false); }
[](StringView name, const Context& context)
{ return RegisterManager::instance()[name.substr(4_byte)].get(context) |
gather<Vector<String>>(); }
}, {
"client_env_", true,
[](StringView name, const Context& context, Quoting quoting)
{ return context.client().get_env_var(name.substr(11_byte)).str(); }
[](StringView name, const Context& context) -> Vector<String>
{ return {context.client().get_env_var(name.substr(11_byte)).str()}; }
}, {
"session", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return Server::instance().session(); }
[](StringView name, const Context& context) -> Vector<String>
{ return {Server::instance().session()}; }
}, {
"client", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return context.name(); }
[](StringView name, const Context& context) -> Vector<String>
{ return {context.name()}; }
}, {
"client_pid", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(context.client().pid()); }
[](StringView name, const Context& context) -> Vector<String>
{ return {to_string(context.client().pid())}; }
}, {
"client_list", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return join(ClientManager::instance() |
[](StringView name, const Context& context) -> Vector<String>
{ return ClientManager::instance() |
transform([](const std::unique_ptr<Client>& c) -> const String&
{ return c->context().name(); }), ' ', false); }
{ return c->context().name(); }) | gather<Vector>(); }
}, {
"modified", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return context.buffer().is_modified() ? "true" : "false"; }
[](StringView name, const Context& context) -> Vector<String>
{ return {context.buffer().is_modified() ? "true" : "false"}; }
}, {
"cursor_line", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(context.selections().main().cursor().line + 1); }
[](StringView name, const Context& context) -> Vector<String>
{ return {to_string(context.selections().main().cursor().line + 1)}; }
}, {
"cursor_column", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(context.selections().main().cursor().column + 1); }
[](StringView name, const Context& context) -> Vector<String>
{ return {to_string(context.selections().main().cursor().column + 1)}; }
}, {
"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& 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,
[](StringView name, const Context& context, Quoting quoting) -> String
[](StringView name, const Context& context) -> Vector<String>
{ 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,
[](StringView name, const Context& context, Quoting quoting) -> String
[](StringView name, const Context& context) -> Vector<String>
{ 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>(),
coord) + 1); }
coord) + 1)}; }
}, {
"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();
return to_string(context.buffer().distance({0,0}, cursor)); }
return {to_string(context.buffer().distance({0,0}, cursor))}; }
}, {
"selection_desc", false,
[](StringView name, const Context& context, Quoting quoting)
{ return selection_to_string(ColumnType::Byte, context.buffer(),
context.selections().main()); }
[](StringView name, const Context& context) -> Vector<String>
{ return {selection_to_string(ColumnType::Byte, context.buffer(), context.selections().main())}; }
}, {
"selections_desc", false,
[](StringView name, const Context& context, Quoting quoting)
{ return selection_list_to_string(ColumnType::Byte, context.selections()); }
[](StringView name, const Context& context) -> Vector<String>
{ 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,
[](StringView name, const Context& context, Quoting quoting)
{ return selection_list_to_string(ColumnType::Codepoint, context.selections()); }
[](StringView name, const Context& context) -> Vector<String>
{ 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,
[](StringView name, const Context& context, Quoting quoting)
{ return selection_list_to_string(ColumnType::DisplayColumn,
context.selections(),
context.options()["tabstop"].get<int>()); }
[](StringView name, const Context& context) -> Vector<String>
{ return main_sel_first(context.selections()) |
transform([&buffer=context.buffer(), tabstop=context.options()["tabstop"].get<int>()](const Selection& sel) {
return selection_to_string(ColumnType::DisplayColumn, buffer, sel, tabstop);
}) | gather<Vector>(); }
}, {
"selection_length", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(char_length(context.buffer(), context.selections().main())); }
[](StringView name, const Context& context) -> Vector<String>
{ return {to_string(char_length(context.buffer(), context.selections().main()))}; }
}, {
"selections_length", false,
[](StringView name, const Context& context, Quoting quoting)
{ return join(context.selections() |
transform([&](const Selection& s)
{ return to_string(char_length(context.buffer(), s)); }), ' ', false); }
[](StringView name, const Context& context) -> Vector<String>
{ return context.selections() |
transform([&](const Selection& s) {
return to_string(char_length(context.buffer(), s));
}) | gather<Vector<String>>(); }
}, {
"window_width", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(context.window().dimensions().column); }
[](StringView name, const Context& context) -> Vector<String>
{ return {to_string(context.window().dimensions().column)}; }
}, {
"window_height", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(context.window().dimensions().line); }
[](StringView name, const Context& context) -> Vector<String>
{ return {to_string(context.window().dimensions().line)}; }
}, {
"user_modes", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return join(context.keymaps().user_modes(), ' ', false); }
[](StringView name, const Context& context) -> Vector<String>
{ return context.keymaps().user_modes(); }
}, {
"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);
return format("{} {} {} {}", setup.window_pos.line, setup.window_pos.column,
setup.window_range.line, setup.window_range.column);
return {format("{} {} {} {}", setup.window_pos.line, setup.window_pos.column,
setup.window_range.line, setup.window_range.column)};
}
}, {
"history", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return history_as_string(context.buffer().history(), quoting); }
[](StringView name, const Context& context) -> Vector<String>
{ return history_as_strings(context.buffer().history()); }
}, {
"uncommitted_modifications", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return undo_group_as_string(context.buffer().current_undo_group(), quoting); }
[](StringView name, const Context& context) -> Vector<String>
{ 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);
try
{
const String& value = var_it != shell_context.env_vars.end() ?
var_it->value : ShellManager::instance().get_val(name, context, quoting);
String value = var_it != shell_context.env_vars.end() ?
var_it->value : join(ShellManager::instance().get_val(name, context) | transform(quoter(quoting)), ' ', false);
StringView quoted{match[1].first, match[1].second};
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 };
}
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) {
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())
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,

View File

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

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
hook global ClientClose .* %{
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'