2014-07-11 03:27:04 +04:00
|
|
|
#include "face_registry.hh"
|
2012-09-17 21:01:13 +04:00
|
|
|
|
2014-12-23 16:34:21 +03:00
|
|
|
#include "containers.hh"
|
2012-09-17 21:01:13 +04:00
|
|
|
#include "exception.hh"
|
2014-12-23 16:54:09 +03:00
|
|
|
#include "containers.hh"
|
2012-09-17 21:01:13 +04:00
|
|
|
|
|
|
|
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-12-23 16:34:21 +03:00
|
|
|
auto bg_it = find(facedesc, ',');
|
|
|
|
auto attr_it = find(facedesc, '+');
|
2014-07-11 03:44:59 +04:00
|
|
|
if (bg_it != facedesc.end() and attr_it < bg_it)
|
|
|
|
throw runtime_error("invalid face description, expected <fg>[,<bg>][+<attr>]");
|
|
|
|
Face res;
|
2015-10-05 22:32:51 +03:00
|
|
|
res.fg = attr_it != facedesc.begin() ?
|
|
|
|
str_to_color({facedesc.begin(), std::min(attr_it, bg_it)}) : Color::Default;
|
2014-07-11 03:44:59 +04:00
|
|
|
if (bg_it != facedesc.end())
|
2015-10-05 22:32:51 +03:00
|
|
|
res.bg = bg_it+1 != attr_it ? str_to_color({bg_it+1, attr_it}) : Color::Default;
|
2014-07-11 03:44:59 +04:00
|
|
|
if (attr_it != facedesc.end())
|
|
|
|
{
|
|
|
|
for (++attr_it; attr_it != facedesc.end(); ++attr_it)
|
|
|
|
{
|
|
|
|
switch (*attr_it)
|
|
|
|
{
|
2015-10-23 15:34:03 +03:00
|
|
|
case 'e': res.attributes |= Attribute::Exclusive; break;
|
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;
|
2015-09-27 16:24:42 +03:00
|
|
|
case 'i': res.attributes |= Attribute::Italic; break;
|
2015-06-01 23:15:59 +03:00
|
|
|
default: throw runtime_error(format("unknown face attribute '{}'", StringView{*attr_it}));
|
2014-07-11 03:44:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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())
|
2015-06-01 23:15:59 +03:00
|
|
|
throw runtime_error(format("alias '{}' already defined", name));
|
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); }))
|
2015-06-01 23:15:59 +03:00
|
|
|
throw runtime_error(format("invalid alias name: '{}'", name));
|
2015-08-12 23:49:29 +03:00
|
|
|
|
|
|
|
if (name == facedesc)
|
|
|
|
throw runtime_error(format("cannot alias face '{}' to itself", 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-12-23 16:54:09 +03:00
|
|
|
ByteCount cursor_pos) const
|
2014-03-29 17:18:46 +04:00
|
|
|
{
|
2014-12-23 16:54:09 +03:00
|
|
|
using ValueType = std::pair<String, FaceOrAlias>;
|
|
|
|
return complete(prefix, cursor_pos,
|
|
|
|
transformed(m_aliases,
|
|
|
|
[](const ValueType& v){ return v.first; }));
|
2014-03-29 17:18:46 +04:00
|
|
|
}
|
|
|
|
|
2014-07-11 03:27:04 +04:00
|
|
|
FaceRegistry::FaceRegistry()
|
2013-03-06 23:31:07 +04:00
|
|
|
: m_aliases{
|
2015-06-04 15:49:28 +03:00
|
|
|
{ "Default", Face{ Color::Default, Color::Default } },
|
2015-04-25 12:47:39 +03:00
|
|
|
{ "PrimarySelection", Face{ Color::White, Color::Blue } },
|
|
|
|
{ "SecondarySelection", Face{ Color::Black, Color::Blue } },
|
|
|
|
{ "PrimaryCursor", Face{ Color::Black, Color::White } },
|
|
|
|
{ "SecondaryCursor", Face{ Color::Black, Color::White } },
|
|
|
|
{ "LineNumbers", Face{ Color::Default, Color::Default } },
|
|
|
|
{ "LineNumberCursor", Face{ Color::Default, Color::Default, Attribute::Reverse } },
|
|
|
|
{ "MenuForeground", Face{ Color::White, Color::Blue } },
|
|
|
|
{ "MenuBackground", Face{ Color::Blue, Color::White } },
|
2015-10-05 03:48:00 +03:00
|
|
|
{ "MenuInfo", Face{ Color::Cyan, Color::Default } },
|
2015-04-25 12:47:39 +03:00
|
|
|
{ "Information", Face{ Color::Black, Color::Yellow } },
|
|
|
|
{ "Error", Face{ Color::Black, Color::Red } },
|
|
|
|
{ "StatusLine", Face{ Color::Cyan, Color::Default } },
|
2015-11-24 16:53:15 +03:00
|
|
|
{ "StatusLineMode", Face{ Color::Yellow, Color::Default } },
|
|
|
|
{ "StatusLineInfo", Face{ Color::Blue, Color::Default } },
|
|
|
|
{ "StatusLineValue", Face{ Color::Green, Color::Default } },
|
2015-04-25 12:47:39 +03:00
|
|
|
{ "StatusCursor", Face{ Color::Black, Color::Cyan } },
|
|
|
|
{ "Prompt", Face{ Color::Yellow, Color::Default } },
|
|
|
|
{ "MatchingChar", Face{ Color::Default, Color::Default, Attribute::Bold } },
|
2013-03-06 23:31:07 +04:00
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
2012-09-17 21:01:13 +04:00
|
|
|
}
|