2014-07-11 01:27:04 +02:00
|
|
|
#include "face_registry.hh"
|
2012-09-17 19:01:13 +02:00
|
|
|
|
|
|
|
#include "exception.hh"
|
2017-08-29 10:23:03 +02:00
|
|
|
#include "ranges.hh"
|
2017-10-09 16:12:42 +02:00
|
|
|
#include "string_utils.hh"
|
2012-09-17 19:01:13 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-07-11 01:44:59 +02:00
|
|
|
static Face parse_face(StringView facedesc)
|
2012-09-17 19:01:13 +02:00
|
|
|
{
|
2015-12-12 10:00:52 +01:00
|
|
|
constexpr StringView invalid_face_error = "invalid face description, expected <fg>[,<bg>][+<attr>]";
|
2014-12-23 14:34:21 +01:00
|
|
|
auto bg_it = find(facedesc, ',');
|
|
|
|
auto attr_it = find(facedesc, '+');
|
2015-12-11 20:58:28 +01:00
|
|
|
if (bg_it != facedesc.end()
|
|
|
|
and (attr_it < bg_it or (bg_it + 1) == facedesc.end()))
|
2015-12-12 10:00:52 +01:00
|
|
|
throw runtime_error(invalid_face_error.str());
|
2015-12-11 20:58:28 +01:00
|
|
|
if (attr_it != facedesc.end()
|
|
|
|
and (attr_it + 1) == facedesc.end())
|
2015-12-12 10:00:52 +01:00
|
|
|
throw runtime_error(invalid_face_error.str());
|
2014-07-11 01:44:59 +02:00
|
|
|
Face res;
|
2015-10-05 21:32:51 +02:00
|
|
|
res.fg = attr_it != facedesc.begin() ?
|
|
|
|
str_to_color({facedesc.begin(), std::min(attr_it, bg_it)}) : Color::Default;
|
2014-07-11 01:44:59 +02:00
|
|
|
if (bg_it != facedesc.end())
|
2015-10-05 21:32:51 +02:00
|
|
|
res.bg = bg_it+1 != attr_it ? str_to_color({bg_it+1, attr_it}) : Color::Default;
|
2014-07-11 01:44:59 +02:00
|
|
|
if (attr_it != facedesc.end())
|
|
|
|
{
|
|
|
|
for (++attr_it; attr_it != facedesc.end(); ++attr_it)
|
|
|
|
{
|
|
|
|
switch (*attr_it)
|
|
|
|
{
|
2015-10-23 14:34:03 +02:00
|
|
|
case 'e': res.attributes |= Attribute::Exclusive; break;
|
2014-07-15 21:11:47 +02: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 15:24:42 +02:00
|
|
|
case 'i': res.attributes |= Attribute::Italic; break;
|
2015-06-01 22:15:59 +02:00
|
|
|
default: throw runtime_error(format("unknown face attribute '{}'", StringView{*attr_it}));
|
2014-07-11 01:44:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
2013-05-15 14:01:23 +02:00
|
|
|
}
|
2012-09-17 19:01:13 +02:00
|
|
|
|
2017-09-12 05:31:57 +02:00
|
|
|
String to_string(Attribute attributes)
|
2017-09-09 15:55:35 +02:00
|
|
|
{
|
|
|
|
if (attributes == Attribute::Normal)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
struct Attr { Attribute attr; StringView name; }
|
|
|
|
attrs[] {
|
|
|
|
{ Attribute::Exclusive, "e" },
|
|
|
|
{ Attribute::Underline, "u" },
|
|
|
|
{ Attribute::Reverse, "r" },
|
|
|
|
{ Attribute::Blink, "B" },
|
|
|
|
{ Attribute::Bold, "b" },
|
|
|
|
{ Attribute::Dim, "d" },
|
|
|
|
{ Attribute::Italic, "i" },
|
|
|
|
};
|
|
|
|
|
|
|
|
auto filteredAttrs = attrs |
|
|
|
|
filter([=](const Attr& a) { return attributes & a.attr; }) |
|
|
|
|
transform([](const Attr& a) { return a.name; });
|
|
|
|
|
2018-01-17 23:00:54 +01:00
|
|
|
return accumulate(filteredAttrs, "+"_str, std::plus<>{});
|
2017-09-09 15:55:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String to_string(Face face)
|
|
|
|
{
|
2017-09-12 05:31:57 +02:00
|
|
|
return format("{},{}{}", face.fg, face.bg, face.attributes);
|
2017-09-09 15:55:35 +02:00
|
|
|
}
|
|
|
|
|
2018-02-09 12:08:29 +01:00
|
|
|
Face FaceRegistry::operator[](StringView facedesc)
|
2013-05-15 14:01:23 +02:00
|
|
|
{
|
2014-07-11 01:27:04 +02:00
|
|
|
auto it = m_aliases.find(facedesc);
|
2014-08-20 00:10:56 +02:00
|
|
|
while (it != m_aliases.end())
|
|
|
|
{
|
2017-03-07 01:30:54 +01:00
|
|
|
if (it->value.alias.empty())
|
|
|
|
return it->value.face;
|
|
|
|
it = m_aliases.find(it->value.alias);
|
2014-08-20 00:10:56 +02:00
|
|
|
}
|
2014-07-12 12:19:35 +02:00
|
|
|
return parse_face(facedesc);
|
2012-09-17 19:01:13 +02:00
|
|
|
}
|
|
|
|
|
2018-02-09 12:08:29 +01:00
|
|
|
void FaceRegistry::register_alias(StringView name, StringView facedesc,
|
2014-08-20 00:10:56 +02:00
|
|
|
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())
|
2015-06-01 22:15:59 +02:00
|
|
|
throw runtime_error(format("alias '{}' already defined", name));
|
2012-09-17 19:01:13 +02:00
|
|
|
|
2014-08-20 00:16:21 +02: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 22:15:59 +02:00
|
|
|
throw runtime_error(format("invalid alias name: '{}'", name));
|
2015-08-12 22:49:29 +02:00
|
|
|
|
|
|
|
if (name == facedesc)
|
|
|
|
throw runtime_error(format("cannot alias face '{}' to itself", name));
|
2012-09-17 19:01:13 +02:00
|
|
|
|
2014-08-20 00:10:56 +02:00
|
|
|
FaceOrAlias& alias = m_aliases[name];
|
2014-07-11 01:27:04 +02:00
|
|
|
auto it = m_aliases.find(facedesc);
|
2014-08-20 00:10:56 +02:00
|
|
|
if (it != m_aliases.end())
|
|
|
|
{
|
|
|
|
while (it != m_aliases.end())
|
|
|
|
{
|
2017-03-07 01:30:54 +01:00
|
|
|
if (it->value.alias.empty())
|
2014-08-20 00:10:56 +02:00
|
|
|
break;
|
2017-03-07 01:30:54 +01:00
|
|
|
if (it->value.alias == name)
|
2014-08-20 00:10:56 +02:00
|
|
|
throw runtime_error("face cycle detected");
|
2017-03-07 01:30:54 +01:00
|
|
|
it = m_aliases.find(it->value.alias);
|
2014-08-20 00:10:56 +02:00
|
|
|
}
|
|
|
|
|
2018-02-09 12:08:29 +01:00
|
|
|
alias.alias = facedesc.str();
|
2014-08-20 00:10:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
alias.alias = "";
|
|
|
|
alias.face = parse_face(facedesc);
|
|
|
|
}
|
2012-09-17 19:01:13 +02:00
|
|
|
}
|
|
|
|
|
2014-07-11 01:27:04 +02:00
|
|
|
CandidateList FaceRegistry::complete_alias_name(StringView prefix,
|
2014-12-23 14:54:09 +01:00
|
|
|
ByteCount cursor_pos) const
|
2014-03-29 14:18:46 +01:00
|
|
|
{
|
2014-12-23 14:54:09 +01:00
|
|
|
return complete(prefix, cursor_pos,
|
2017-03-07 01:30:54 +01:00
|
|
|
m_aliases | transform(std::mem_fn(&AliasMap::Item::key)));
|
2014-03-29 14:18:46 +01:00
|
|
|
}
|
|
|
|
|
2014-07-11 01:27:04 +02:00
|
|
|
FaceRegistry::FaceRegistry()
|
2013-03-06 20:31:07 +01:00
|
|
|
: m_aliases{
|
2017-09-01 12:32:38 +02:00
|
|
|
{ "Default", {Face{ Color::Default, Color::Default }} },
|
|
|
|
{ "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 }} },
|
|
|
|
{ "LineNumbersWrapped", {Face{ Color::Default, Color::Default, Attribute::Italic }} },
|
|
|
|
{ "MenuForeground", {Face{ Color::White, Color::Blue }} },
|
|
|
|
{ "MenuBackground", {Face{ Color::Blue, Color::White }} },
|
|
|
|
{ "MenuInfo", {Face{ Color::Cyan, Color::Default }} },
|
|
|
|
{ "Information", {Face{ Color::Black, Color::Yellow }} },
|
|
|
|
{ "Error", {Face{ Color::Black, Color::Red }} },
|
|
|
|
{ "StatusLine", {Face{ Color::Cyan, Color::Default }} },
|
|
|
|
{ "StatusLineMode", {Face{ Color::Yellow, Color::Default }} },
|
|
|
|
{ "StatusLineInfo", {Face{ Color::Blue, Color::Default }} },
|
|
|
|
{ "StatusLineValue", {Face{ Color::Green, Color::Default }} },
|
|
|
|
{ "StatusCursor", {Face{ Color::Black, Color::Cyan }} },
|
|
|
|
{ "Prompt", {Face{ Color::Yellow, Color::Default }} },
|
|
|
|
{ "MatchingChar", {Face{ Color::Default, Color::Default, Attribute::Bold }} },
|
|
|
|
{ "BufferPadding", {Face{ Color::Blue, Color::Default }} },
|
|
|
|
{ "Whitespace", {Face{ Color::Default, Color::Default }} },
|
2013-03-06 20:31:07 +01:00
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
2012-09-17 19:01:13 +02:00
|
|
|
}
|