2012-09-17 19:01:13 +02:00
|
|
|
#include "color_registry.hh"
|
|
|
|
|
|
|
|
#include "exception.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
static Color parse_color(const String& color)
|
|
|
|
{
|
|
|
|
if (color == "default") return Color::Default;
|
|
|
|
if (color == "black") return Color::Black;
|
|
|
|
if (color == "red") return Color::Red;
|
|
|
|
if (color == "green") return Color::Green;
|
|
|
|
if (color == "yellow") return Color::Yellow;
|
|
|
|
if (color == "blue") return Color::Blue;
|
|
|
|
if (color == "magenta") return Color::Magenta;
|
|
|
|
if (color == "cyan") return Color::Cyan;
|
|
|
|
if (color == "white") return Color::White;
|
|
|
|
throw runtime_error("Unable to parse color '" + color + "'");
|
|
|
|
return Color::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{ parse_color(String(colordesc.begin(), it)),
|
|
|
|
it != colordesc.end() ?
|
|
|
|
parse_color(String(it+1, colordesc.end()))
|
|
|
|
: Color::Default };
|
|
|
|
|
2012-10-11 01:17:29 +02:00
|
|
|
return (m_aliases[colordesc] = colpair);
|
2012-09-17 19:01:13 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 21:01:11 +02:00
|
|
|
void ColorRegistry::register_alias(const String& name, const String& colordesc,
|
|
|
|
bool override)
|
2012-09-17 19:01:13 +02:00
|
|
|
{
|
2012-09-17 21:01:11 +02:00
|
|
|
if (not override and m_aliases.find(name) != m_aliases.end())
|
2012-09-17 19:01:13 +02:00
|
|
|
throw runtime_error("alias '" + name + "' already defined");
|
|
|
|
|
|
|
|
if (std::find_if(name.begin(), name.end(),
|
2012-10-08 19:33:53 +02:00
|
|
|
[](char c) { return not isalnum(c); }) != name.end())
|
2012-09-17 19:01:13 +02:00
|
|
|
throw runtime_error("alias names are limited to alpha numeric words");
|
|
|
|
|
|
|
|
auto it = std::find(colordesc.begin(), colordesc.end(), ',');
|
|
|
|
auto fg = parse_color(String(colordesc.begin(), it));
|
|
|
|
auto bg = Color::Default;
|
|
|
|
if (it != colordesc.end())
|
|
|
|
bg = parse_color(String(it+1, colordesc.end()));
|
|
|
|
|
|
|
|
m_aliases[name] = { fg, bg };
|
|
|
|
}
|
|
|
|
|
2013-03-06 20:31:07 +01:00
|
|
|
ColorRegistry::ColorRegistry()
|
|
|
|
: m_aliases{
|
|
|
|
{ "PrimarySelection", { Color::Cyan, Color::Blue } },
|
|
|
|
{ "SecondarySelection", { Color::Black, Color::Blue } },
|
|
|
|
{ "PrimaryCursor", { Color::Black, Color::White } },
|
|
|
|
{ "SecondaryCursor", { Color::Black, Color::White } },
|
|
|
|
{ "LineNumbers", { Color::Black, Color::White } },
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
2012-09-17 19:01:13 +02:00
|
|
|
}
|