mirror of
https://github.com/mawww/kakoune.git
synced 2024-11-27 12:16:22 +03:00
Add a join function for joining strings using a specific char
This commit is contained in:
parent
71bfe5498d
commit
8cc96ec36b
41
src/main.cc
41
src/main.cc
@ -59,15 +59,9 @@ void register_env_vars()
|
||||
}, {
|
||||
"buflist",
|
||||
[](StringView name, const Context& context)
|
||||
{
|
||||
String res;
|
||||
for (auto& buf : BufferManager::instance())
|
||||
{
|
||||
if (not res.empty())
|
||||
res += ":";
|
||||
res += buf->display_name();
|
||||
}
|
||||
return res; }
|
||||
{ return join(transformed(BufferManager::instance(),
|
||||
[](const safe_ptr<Buffer>& b)
|
||||
{ return b->display_name(); }), ':'); }
|
||||
}, {
|
||||
"timestamp",
|
||||
[](StringView name, const Context& context)
|
||||
@ -80,15 +74,7 @@ void register_env_vars()
|
||||
}, {
|
||||
"selections",
|
||||
[](StringView name, const Context& context)
|
||||
{ auto sels = context.selections_content();
|
||||
String res;
|
||||
for (size_t i = 0; i < sels.size(); ++i)
|
||||
{
|
||||
res += escape(sels[i], ':', '\\');
|
||||
if (i != sels.size() - 1)
|
||||
res += ':';
|
||||
}
|
||||
return res; }
|
||||
{ return join(context.selections_content(), ':'); }
|
||||
}, {
|
||||
"runtime",
|
||||
[](StringView name, const Context& context)
|
||||
@ -130,24 +116,17 @@ void register_env_vars()
|
||||
"selection_desc",
|
||||
[](StringView name, const Context& context)
|
||||
{ auto& sel = context.selections().main();
|
||||
auto beg = sel.min();
|
||||
return to_string(beg.line + 1) + "." + to_string(beg.column + 1) + "+" +
|
||||
to_string((int)context.buffer().distance(beg, sel.max())+1); }
|
||||
auto beg = sel.min();
|
||||
return to_string(beg.line + 1) + "." + to_string(beg.column + 1) + "+" +
|
||||
to_string((int)context.buffer().distance(beg, sel.max())+1); }
|
||||
}, {
|
||||
"selections_desc",
|
||||
[](StringView name, const Context& context)
|
||||
{
|
||||
String res;
|
||||
for (auto& sel : context.selections())
|
||||
{
|
||||
{ return join(transformed(context.selections(), [&](const Selection& sel) {
|
||||
auto beg = sel.min();
|
||||
if (not res.empty())
|
||||
res += ':';
|
||||
res += 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);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}), ':'); }
|
||||
}, {
|
||||
"window_width",
|
||||
[](StringView name, const Context& context)
|
||||
|
@ -721,7 +721,7 @@ void split_lines(Context& context, NormalParams)
|
||||
selections = std::move(res);
|
||||
}
|
||||
|
||||
void join_select_spaces(Context& context, NormalParams)
|
||||
void join_lines_select_spaces(Context& context, NormalParams)
|
||||
{
|
||||
auto& buffer = context.buffer();
|
||||
std::vector<Selection> selections;
|
||||
@ -746,7 +746,7 @@ void join_select_spaces(Context& context, NormalParams)
|
||||
context.selections().insert(" "_str, InsertMode::Replace);
|
||||
}
|
||||
|
||||
void join(Context& context, NormalParams params)
|
||||
void join_lines(Context& context, NormalParams params)
|
||||
{
|
||||
SelectionList sels{context.selections()};
|
||||
auto restore_sels = on_scope_end([&]{
|
||||
@ -754,7 +754,7 @@ void join(Context& context, NormalParams params)
|
||||
context.selections() = std::move(sels);
|
||||
});
|
||||
|
||||
join_select_spaces(context, params);
|
||||
join_lines_select_spaces(context, params);
|
||||
}
|
||||
|
||||
template<bool matching>
|
||||
@ -1430,8 +1430,8 @@ KeyMap keymap =
|
||||
{ alt('{'), { "extend to inner object start", select_object<ObjectFlags::ToBegin | ObjectFlags::Inner, SelectMode::Extend> } },
|
||||
{ alt('}'), { "extend to inner object end", select_object<ObjectFlags::ToEnd | ObjectFlags::Inner, SelectMode::Extend> } },
|
||||
|
||||
{ alt('j'), { "join lines", join } },
|
||||
{ alt('J'), { "join lines and select spaces", join_select_spaces } },
|
||||
{ alt('j'), { "join lines", join_lines } },
|
||||
{ alt('J'), { "join lines and select spaces", join_lines_select_spaces } },
|
||||
|
||||
{ alt('k'), { "keep selections matching given regex", keep<true> } },
|
||||
{ alt('K'), { "keep selections not matching given regex", keep<false> } },
|
||||
|
@ -235,6 +235,19 @@ std::vector<StringView> split(StringView str, char separator);
|
||||
String escape(StringView str, StringView characters, char escape);
|
||||
String unescape(StringView str, StringView characters, char escape);
|
||||
|
||||
template<typename Container>
|
||||
String join(const Container& container, char joiner)
|
||||
{
|
||||
String res;
|
||||
for (const auto& str : container)
|
||||
{
|
||||
if (not res.empty())
|
||||
res += joiner;
|
||||
res += escape(str, joiner, '\\');
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
inline String operator"" _str(const char* str, size_t)
|
||||
{
|
||||
return String(str);
|
||||
|
Loading…
Reference in New Issue
Block a user