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& 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 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 @@ -1430,8 +1430,8 @@ KeyMap keymap = { alt('{'), { "extend to inner object start", select_object } }, { alt('}'), { "extend to inner object end", select_object } }, - { 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 } }, { alt('K'), { "keep selections not matching given regex", keep } }, 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 split(StringView str, char separator); String escape(StringView str, StringView characters, char escape); String unescape(StringView str, StringView characters, char escape); +template +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);