2014-07-11 03:27:04 +04:00
|
|
|
#include "face_registry.hh"
|
2012-09-17 21:01:13 +04:00
|
|
|
|
|
|
|
#include "exception.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-07-11 03:44:59 +04:00
|
|
|
static Face parse_face(StringView facedesc)
|
2012-09-17 21:01:13 +04:00
|
|
|
{
|
2014-07-11 03:44:59 +04:00
|
|
|
auto bg_it = std::find(facedesc.begin(), facedesc.end(), ',');
|
|
|
|
auto attr_it = std::find(facedesc.begin(), facedesc.end(), '+');
|
|
|
|
if (bg_it != facedesc.end() and attr_it < bg_it)
|
|
|
|
throw runtime_error("invalid face description, expected <fg>[,<bg>][+<attr>]");
|
|
|
|
Face res;
|
|
|
|
res.fg = str_to_color({facedesc.begin(), std::min(attr_it, bg_it)});
|
|
|
|
if (bg_it != facedesc.end())
|
|
|
|
res.bg = str_to_color({bg_it+1, attr_it});
|
|
|
|
if (attr_it != facedesc.end())
|
|
|
|
{
|
|
|
|
for (++attr_it; attr_it != facedesc.end(); ++attr_it)
|
|
|
|
{
|
|
|
|
switch (*attr_it)
|
|
|
|
{
|
2014-07-15 23:11:47 +04:00
|
|
|
case 'u': res.attributes |= Attribute::Underline; break;
|
|
|
|
case 'r': res.attributes |= Attribute::Reverse; break;
|
|
|
|
case 'b': res.attributes |= Attribute::Bold; break;
|
|
|
|
case 'B': res.attributes |= Attribute::Blink; break;
|
|
|
|
case 'd': res.attributes |= Attribute::Dim; break;
|
2014-07-11 03:44:59 +04:00
|
|
|
default: throw runtime_error("unknown face attribute '" + String(*attr_it) + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
2013-05-15 16:01:23 +04:00
|
|
|
}
|
2012-09-17 21:01:13 +04:00
|
|
|
|
2014-07-12 14:19:35 +04:00
|
|
|
Face FaceRegistry::operator[](const String& facedesc)
|
2013-05-15 16:01:23 +04:00
|
|
|
{
|
2014-07-11 03:27:04 +04:00
|
|
|
auto it = m_aliases.find(facedesc);
|
2014-08-20 02:10:56 +04:00
|
|
|
while (it != m_aliases.end())
|
|
|
|
{
|
|
|
|
if (it->second.alias.empty())
|
|
|
|
return it->second.face;
|
|
|
|
it = m_aliases.find(it->second.alias);
|
|
|
|
}
|
2014-07-12 14:19:35 +04:00
|
|
|
return parse_face(facedesc);
|
2012-09-17 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2014-07-11 03:27:04 +04:00
|
|
|
void FaceRegistry::register_alias(const String& name, const String& facedesc,
|
2014-08-20 02:10:56 +04:00
|
|
|
bool override)
|
2012-09-17 21:01:13 +04:00
|
|
|
{
|
2012-09-17 23:01:11 +04:00
|
|
|
if (not override and m_aliases.find(name) != m_aliases.end())
|
2013-12-03 23:48:53 +04:00
|
|
|
throw runtime_error("alias '" + name + "' already defined");
|
2012-09-17 21:01:13 +04:00
|
|
|
|
2014-08-20 02:16:21 +04:00
|
|
|
if (name.empty() or is_color_name(name) or
|
|
|
|
std::any_of(name.begin(), name.end(),
|
|
|
|
[](char c){ return not isalnum(c); }))
|
2013-05-15 16:01:23 +04:00
|
|
|
throw runtime_error("invalid alias name");
|
2012-09-17 21:01:13 +04:00
|
|
|
|
2014-08-20 02:10:56 +04:00
|
|
|
FaceOrAlias& alias = m_aliases[name];
|
2014-07-11 03:27:04 +04:00
|
|
|
auto it = m_aliases.find(facedesc);
|
2014-08-20 02:10:56 +04:00
|
|
|
if (it != m_aliases.end())
|
|
|
|
{
|
|
|
|
while (it != m_aliases.end())
|
|
|
|
{
|
|
|
|
if (it->second.alias.empty())
|
|
|
|
break;
|
|
|
|
if (it->second.alias == name)
|
|
|
|
throw runtime_error("face cycle detected");
|
|
|
|
it = m_aliases.find(it->second.alias);
|
|
|
|
}
|
|
|
|
|
|
|
|
alias.alias = facedesc;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
alias.alias = "";
|
|
|
|
alias.face = parse_face(facedesc);
|
|
|
|
}
|
2012-09-17 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2014-07-11 03:27:04 +04:00
|
|
|
CandidateList FaceRegistry::complete_alias_name(StringView prefix,
|
2014-03-29 17:18:46 +04:00
|
|
|
ByteCount cursor_pos) const
|
|
|
|
{
|
|
|
|
CandidateList res;
|
2014-04-18 17:02:14 +04:00
|
|
|
auto real_prefix = prefix.substr(0, cursor_pos);
|
2014-03-29 17:18:46 +04:00
|
|
|
for (auto& alias : m_aliases)
|
|
|
|
{
|
|
|
|
if (prefix_match(alias.first, real_prefix))
|
|
|
|
res.push_back(alias.first);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-07-11 03:27:04 +04:00
|
|
|
FaceRegistry::FaceRegistry()
|
2013-03-06 23:31:07 +04:00
|
|
|
: m_aliases{
|
2014-12-01 16:06:26 +03:00
|
|
|
{ "PrimarySelection", Face{ Colors::White, Colors::Blue } },
|
2014-08-20 02:10:56 +04:00
|
|
|
{ "SecondarySelection", Face{ Colors::Black, Colors::Blue } },
|
|
|
|
{ "PrimaryCursor", Face{ Colors::Black, Colors::White } },
|
|
|
|
{ "SecondaryCursor", Face{ Colors::Black, Colors::White } },
|
|
|
|
{ "LineNumbers", Face{ Colors::Default, Colors::Default } },
|
|
|
|
{ "MenuForeground", Face{ Colors::White, Colors::Blue } },
|
|
|
|
{ "MenuBackground", Face{ Colors::Blue, Colors::White } },
|
|
|
|
{ "Information", Face{ Colors::Black, Colors::Yellow } },
|
|
|
|
{ "Error", Face{ Colors::Black, Colors::Red } },
|
|
|
|
{ "StatusLine", Face{ Colors::Cyan, Colors::Default } },
|
|
|
|
{ "StatusCursor", Face{ Colors::Black, Colors::Cyan } },
|
|
|
|
{ "Prompt", Face{ Colors::Yellow, Colors::Default } },
|
|
|
|
{ "MatchingChar", Face{ Colors::Default, Colors::Default, Attribute::Underline } },
|
|
|
|
{ "Search", Face{ Colors::Default, Colors::Default, Attribute::Underline } },
|
2013-03-06 23:31:07 +04:00
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
2012-09-17 21:01:13 +04:00
|
|
|
}
|