summaryrefslogtreecommitdiff
path: root/src/option_types.hh
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2018-07-22 15:51:32 +1000
committerMaxime Coste <mawww@kakoune.org>2018-07-22 15:51:32 +1000
commit7b9f162e7d8bccee2eb47cecb4cf0430b9afef2d (patch)
treedeb404928cdd77bc3034f7b3de4731388cb78d95 /src/option_types.hh
parent019150ba527532e5a7ace10363ab73505c116117 (diff)
Opt-in types for quoting of option lists
This avoid quoting ints in int-lists for example, as they do not risk containing whitespaces. Fixes #2223
Diffstat (limited to 'src/option_types.hh')
-rw-r--r--src/option_types.hh24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/option_types.hh b/src/option_types.hh
index 8a782e15..d3fd9364 100644
--- a/src/option_types.hh
+++ b/src/option_types.hh
@@ -18,6 +18,20 @@
namespace Kakoune
{
+template<typename T, typename... Rest>
+constexpr bool option_needs_quoting(Meta::Type<T> type, Meta::Type<Rest>... rest)
+{
+ return option_needs_quoting(type) or option_needs_quoting(rest...);
+}
+
+template<typename... Ts>
+String quote_ifn(String str)
+{
+ if (option_needs_quoting(Meta::Type<Ts>{}...))
+ return quote(std::move(str));
+ return str;
+}
+
template<typename T>
constexpr decltype(T::option_type_name) option_type_name(Meta::Type<T>)
{
@@ -66,6 +80,7 @@ inline Codepoint option_from_string(Meta::Type<Codepoint>, StringView str)
return str[0_char];
}
constexpr StringView option_type_name(Meta::Type<Codepoint>) { return "codepoint"; }
+constexpr bool option_needs_quoting(Meta::Type<Codepoint>) { return true; }
template<typename T, MemoryDomain domain>
Vector<String> option_to_strings(const Vector<T, domain>& opt)
@@ -76,7 +91,7 @@ Vector<String> option_to_strings(const Vector<T, domain>& opt)
template<typename T, MemoryDomain domain>
String option_to_string(const Vector<T, domain>& opt)
{
- return join(opt | transform([](const T& t) { return quote(option_to_string(t)); }), ' ', false);
+ return join(opt | transform([](const T& t) { return quote_ifn<T>(option_to_string(t)); }), ' ', false);
}
template<typename T, MemoryDomain domain>
@@ -123,9 +138,10 @@ template<typename Key, typename Value, MemoryDomain domain>
String option_to_string(const HashMap<Key, Value, domain>& opt)
{
return join(opt | transform([](auto&& item) {
- return quote(format("{}={}",
- escape(option_to_string(item.key), '=', '\\'),
- escape(option_to_string(item.value), '=', '\\')));
+ return quote_ifn<Key, Value>(
+ format("{}={}",
+ escape(option_to_string(item.key), '=', '\\'),
+ escape(option_to_string(item.value), '=', '\\')));
}), ' ', false);
}