2014-07-11 03:27:04 +04:00
|
|
|
#include "face_registry.hh"
|
2012-09-17 21:01:13 +04:00
|
|
|
|
|
|
|
#include "exception.hh"
|
2017-08-29 11:23:03 +03:00
|
|
|
#include "ranges.hh"
|
2017-10-09 17:12:42 +03:00
|
|
|
#include "string_utils.hh"
|
2012-09-17 21:01:13 +04:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2019-04-23 18:44:30 +03:00
|
|
|
static FaceRegistry::FaceSpec parse_face(StringView facedesc)
|
2012-09-17 21:01:13 +04:00
|
|
|
{
|
2019-04-23 18:44:30 +03:00
|
|
|
constexpr StringView invalid_face_error = "invalid face description, expected <fg>[,<bg>][+<attr>][@base] or just [base]";
|
|
|
|
if (all_of(facedesc, [](char c){ return is_word(c); }) and not is_color_name(facedesc))
|
|
|
|
return {Face{}, facedesc.str()};
|
|
|
|
|
2014-12-23 16:34:21 +03:00
|
|
|
auto bg_it = find(facedesc, ',');
|
|
|
|
auto attr_it = find(facedesc, '+');
|
2019-04-23 18:44:30 +03:00
|
|
|
auto base_it = find(facedesc, '@');
|
2015-12-11 22:58:28 +03:00
|
|
|
if (bg_it != facedesc.end()
|
|
|
|
and (attr_it < bg_it or (bg_it + 1) == facedesc.end()))
|
2015-12-12 12:00:52 +03:00
|
|
|
throw runtime_error(invalid_face_error.str());
|
2015-12-11 22:58:28 +03:00
|
|
|
if (attr_it != facedesc.end()
|
|
|
|
and (attr_it + 1) == facedesc.end())
|
2015-12-12 12:00:52 +03:00
|
|
|
throw runtime_error(invalid_face_error.str());
|
2019-04-23 18:44:30 +03:00
|
|
|
|
2019-04-28 02:35:52 +03:00
|
|
|
auto colors_end = std::min(attr_it, base_it);
|
|
|
|
|
2019-04-23 18:44:30 +03:00
|
|
|
FaceRegistry::FaceSpec spec;
|
|
|
|
auto& face = spec.face;
|
2019-04-28 02:35:52 +03:00
|
|
|
face.fg = colors_end != facedesc.begin() ?
|
|
|
|
str_to_color({facedesc.begin(), std::min(bg_it, colors_end)}) : Color::Default;
|
2014-07-11 03:44:59 +04:00
|
|
|
if (bg_it != facedesc.end())
|
2019-04-28 02:35:52 +03:00
|
|
|
face.bg = bg_it+1 != attr_it ? str_to_color({bg_it+1, colors_end}) : Color::Default;
|
2014-07-11 03:44:59 +04:00
|
|
|
if (attr_it != facedesc.end())
|
|
|
|
{
|
2019-04-23 18:44:30 +03:00
|
|
|
for (++attr_it; attr_it != base_it; ++attr_it)
|
2014-07-11 03:44:59 +04:00
|
|
|
{
|
|
|
|
switch (*attr_it)
|
|
|
|
{
|
2019-04-23 18:44:30 +03:00
|
|
|
case 'u': face.attributes |= Attribute::Underline; break;
|
|
|
|
case 'r': face.attributes |= Attribute::Reverse; break;
|
|
|
|
case 'b': face.attributes |= Attribute::Bold; break;
|
|
|
|
case 'B': face.attributes |= Attribute::Blink; break;
|
|
|
|
case 'd': face.attributes |= Attribute::Dim; break;
|
|
|
|
case 'i': face.attributes |= Attribute::Italic; break;
|
|
|
|
case 'f': face.attributes |= Attribute::FinalFg; break;
|
|
|
|
case 'g': face.attributes |= Attribute::FinalBg; break;
|
|
|
|
case 'a': face.attributes |= Attribute::FinalAttr; break;
|
|
|
|
case 'F': face.attributes |= Attribute::Final; break;
|
2018-04-06 17:56:53 +03:00
|
|
|
default: throw runtime_error(format("no such face attribute: '{}'", StringView{*attr_it}));
|
2014-07-11 03:44:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 18:44:30 +03:00
|
|
|
if (base_it != facedesc.end())
|
|
|
|
spec.base = String{base_it+1, facedesc.end()};
|
|
|
|
return spec;
|
2013-05-15 16:01:23 +04:00
|
|
|
}
|
2012-09-17 21:01:13 +04:00
|
|
|
|
2017-09-12 06:31:57 +03:00
|
|
|
String to_string(Attribute attributes)
|
2017-09-09 16:55:35 +03:00
|
|
|
{
|
|
|
|
if (attributes == Attribute::Normal)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
struct Attr { Attribute attr; StringView name; }
|
|
|
|
attrs[] {
|
|
|
|
{ Attribute::Underline, "u" },
|
|
|
|
{ Attribute::Reverse, "r" },
|
|
|
|
{ Attribute::Blink, "B" },
|
|
|
|
{ Attribute::Bold, "b" },
|
|
|
|
{ Attribute::Dim, "d" },
|
|
|
|
{ Attribute::Italic, "i" },
|
2018-09-23 16:17:12 +03:00
|
|
|
{ Attribute::Final, "F" },
|
|
|
|
{ Attribute::FinalFg, "f" },
|
2018-09-24 10:54:21 +03:00
|
|
|
{ Attribute::FinalBg, "g" },
|
2018-09-23 16:17:12 +03:00
|
|
|
{ Attribute::FinalAttr, "a" },
|
2017-09-09 16:55:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
auto filteredAttrs = attrs |
|
|
|
|
filter([=](const Attr& a) { return attributes & a.attr; }) |
|
|
|
|
transform([](const Attr& a) { return a.name; });
|
|
|
|
|
2018-01-18 01:00:54 +03:00
|
|
|
return accumulate(filteredAttrs, "+"_str, std::plus<>{});
|
2017-09-09 16:55:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
String to_string(Face face)
|
|
|
|
{
|
2017-09-12 06:31:57 +03:00
|
|
|
return format("{},{}{}", face.fg, face.bg, face.attributes);
|
2017-09-09 16:55:35 +03:00
|
|
|
}
|
|
|
|
|
2018-04-07 08:36:39 +03:00
|
|
|
Face FaceRegistry::operator[](StringView facedesc) const
|
2013-05-15 16:01:23 +04:00
|
|
|
{
|
2019-04-23 18:44:30 +03:00
|
|
|
return resolve_spec(parse_face(facedesc));
|
|
|
|
}
|
|
|
|
|
|
|
|
Face FaceRegistry::resolve_spec(const FaceSpec& spec) const
|
|
|
|
{
|
|
|
|
if (spec.base.empty())
|
|
|
|
return spec.face;
|
|
|
|
|
|
|
|
StringView base = spec.base;
|
|
|
|
Face face = spec.face;
|
|
|
|
for (auto* reg = this; reg != nullptr; reg = reg->m_parent.get())
|
2014-08-20 02:10:56 +04:00
|
|
|
{
|
2019-04-23 18:44:30 +03:00
|
|
|
auto it = reg->m_faces.find(base);
|
|
|
|
if (it == reg->m_faces.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (it->value.base.empty())
|
|
|
|
return merge_faces(it->value.face, face);
|
|
|
|
if (it->value.base != it->key)
|
|
|
|
return merge_faces(reg->resolve_spec(it->value), face);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
face = merge_faces(it->value.face, face);
|
|
|
|
base = it->value.base;
|
|
|
|
}
|
2014-08-20 02:10:56 +04:00
|
|
|
}
|
2019-04-23 18:44:30 +03:00
|
|
|
return face;
|
2012-09-17 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2018-04-07 08:36:39 +03:00
|
|
|
void FaceRegistry::add_face(StringView name, StringView facedesc, bool override)
|
2012-09-17 21:01:13 +04:00
|
|
|
{
|
2018-04-07 08:36:39 +03:00
|
|
|
if (not override and m_faces.find(name) != m_faces.end())
|
|
|
|
throw runtime_error(format("face '{}' 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
|
2019-04-23 18:44:30 +03:00
|
|
|
any_of(name, [](char c){ return not is_word(c); }))
|
2018-04-07 08:36:39 +03:00
|
|
|
throw runtime_error(format("invalid face name: '{}'", name));
|
2015-08-12 23:49:29 +03:00
|
|
|
|
2019-04-23 18:44:30 +03:00
|
|
|
FaceSpec spec = parse_face(facedesc);
|
|
|
|
auto it = m_faces.find(spec.base);
|
|
|
|
if (spec.base == name and it != m_faces.end())
|
2014-08-20 02:10:56 +04:00
|
|
|
{
|
2019-04-23 18:44:30 +03:00
|
|
|
it->value.face = merge_faces(it->value.face, spec.face);
|
|
|
|
it->value.base = spec.base;
|
|
|
|
return;
|
2014-08-20 02:10:56 +04:00
|
|
|
}
|
2018-04-10 12:57:16 +03:00
|
|
|
|
2019-04-23 18:44:30 +03:00
|
|
|
while (it != m_faces.end() and not it->value.base.empty())
|
2014-08-20 02:10:56 +04:00
|
|
|
{
|
2019-04-23 18:44:30 +03:00
|
|
|
if (it->value.base == name)
|
|
|
|
throw runtime_error("face cycle detected");
|
|
|
|
it = m_faces.find(it->value.base);
|
2014-08-20 02:10:56 +04:00
|
|
|
}
|
2019-04-23 18:44:30 +03:00
|
|
|
m_faces[name] = std::move(spec);
|
2012-09-17 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2018-04-07 08:36:39 +03:00
|
|
|
void FaceRegistry::remove_face(StringView name)
|
2014-03-29 17:18:46 +04:00
|
|
|
{
|
2018-04-07 08:36:39 +03:00
|
|
|
m_faces.remove(name);
|
2014-03-29 17:18:46 +04:00
|
|
|
}
|
|
|
|
|
2014-07-11 03:27:04 +04:00
|
|
|
FaceRegistry::FaceRegistry()
|
2018-04-07 08:36:39 +03:00
|
|
|
: m_faces{
|
2017-09-01 13:32:38 +03: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 }} },
|
2018-02-24 13:24:06 +03:00
|
|
|
{ "PrimaryCursorEol", {Face{ Color::Black, Color::Cyan }} },
|
|
|
|
{ "SecondaryCursorEol", {Face{ Color::Black, Color::Cyan }} },
|
2017-09-01 13:32:38 +03:00
|
|
|
{ "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 }} },
|
2018-09-23 16:17:12 +03:00
|
|
|
{ "Whitespace", {Face{ Color::Default, Color::Default, Attribute::FinalFg }} },
|
2013-03-06 23:31:07 +04:00
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
2012-09-17 21:01:13 +04:00
|
|
|
}
|