summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2014-12-28 11:16:51 +0000
committerMaxime Coste <frrrwww@gmail.com>2014-12-28 11:16:51 +0000
commit8cc96ec36bb6ac189fac339150185a30482c8556 (patch)
treebab9766f44e16262e7478519672a75e162855798 /src
parent71bfe5498d40ff5bf7b512b3ab9e43ead8a3ce51 (diff)
Add a join function for joining strings using a specific char
Diffstat (limited to 'src')
-rw-r--r--src/main.cc41
-rw-r--r--src/normal.cc10
-rw-r--r--src/string.hh13
3 files changed, 28 insertions, 36 deletions
diff --git a/src/main.cc b/src/main.cc
index e80f5717..6aa35004 100644
--- a/src/main.cc
+++ b/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)
diff --git a/src/normal.cc b/src/normal.cc
index 13f1fb7c..35eb4db3 100644
--- a/src/normal.cc
+++ b/src/normal.cc
@@ -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> } },
diff --git a/src/string.hh b/src/string.hh
index 91df1b98..8cbd7a68 100644
--- a/src/string.hh
+++ b/src/string.hh
@@ -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);