From ef7d90cbfaa684b268cb3225492edf947df70dce Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 15 May 2013 14:01:23 +0200 Subject: [PATCH] colalias can reference another alias --- src/color_registry.cc | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/color_registry.cc b/src/color_registry.cc index 2b68ea22..e4f7b293 100644 --- a/src/color_registry.cc +++ b/src/color_registry.cc @@ -5,19 +5,20 @@ namespace Kakoune { +static ColorPair parse_color_pair(const String& colordesc) +{ + auto it = std::find(colordesc.begin(), colordesc.end(), ','); + return { str_to_color({colordesc.begin(), it}), + it != colordesc.end() ? str_to_color({it+1, colordesc.end()}) + : Colors::Default }; +} + const ColorPair& ColorRegistry::operator[](const String& colordesc) { - auto alias_it = m_aliases.find(colordesc); - if (alias_it != m_aliases.end()) - return alias_it->second; - - auto it = std::find(colordesc.begin(), colordesc.end(), ','); - ColorPair colpair{ str_to_color(String(colordesc.begin(), it)), - it != colordesc.end() ? - str_to_color(String(it+1, colordesc.end())) - : Colors::Default }; - - return (m_aliases[colordesc] = colpair); + auto it = m_aliases.find(colordesc); + if (it != m_aliases.end()) + return it->second; + return (m_aliases[colordesc] = parse_color_pair(colordesc)); } void ColorRegistry::register_alias(const String& name, const String& colordesc, @@ -26,17 +27,13 @@ void ColorRegistry::register_alias(const String& name, const String& colordesc, if (not override and m_aliases.find(name) != m_aliases.end()) throw runtime_error("alias '" + name + "' already defined"); - if (std::find_if(name.begin(), name.end(), - [](char c) { return not isalnum(c); }) != name.end()) - throw runtime_error("alias names are limited to alpha numeric words"); + if (name.empty() or + find_if(name, [](char c){ return not isalnum(c); }) != name.end()) + throw runtime_error("invalid alias name"); - auto it = std::find(colordesc.begin(), colordesc.end(), ','); - auto fg = str_to_color(String(colordesc.begin(), it)); - auto bg = Color{Colors::Default}; - if (it != colordesc.end()) - bg = str_to_color(String(it+1, colordesc.end())); - - m_aliases[name] = { fg, bg }; + auto it = m_aliases.find(colordesc); + m_aliases[name] = (it != m_aliases.end()) ? + it->second : parse_color_pair(colordesc); } ColorRegistry::ColorRegistry()