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
|
|
|
|
{
|
|
|
|
|
2019-04-23 17:44:30 +02:00
|
|
|
static FaceRegistry::FaceSpec parse_face(StringView facedesc)
|
2012-09-17 19:01:13 +02:00
|
|
|
{
|
2021-09-07 00:21:26 +02:00
|
|
|
constexpr StringView invalid_face_error = "invalid face description, expected [<fg>][,<bg>[,<underline>]][+<attr>][@base] or just [base]";
|
2019-04-23 17:44:30 +02:00
|
|
|
if (all_of(facedesc, [](char c){ return is_word(c); }) and not is_color_name(facedesc))
|
|
|
|
return {Face{}, facedesc.str()};
|
|
|
|
|
2014-12-23 14:34:21 +01:00
|
|
|
auto bg_it = find(facedesc, ',');
|
2021-09-07 00:21:26 +02:00
|
|
|
auto underline_it = bg_it == facedesc.end() ? bg_it : std::find(bg_it+1, facedesc.end(), ',');
|
2014-12-23 14:34:21 +01:00
|
|
|
auto attr_it = find(facedesc, '+');
|
2019-04-23 17:44:30 +02:00
|
|
|
auto base_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());
|
2019-04-23 17:44:30 +02:00
|
|
|
|
2019-04-28 01:35:52 +02:00
|
|
|
auto colors_end = std::min(attr_it, base_it);
|
Faces: Check that underline colour comes before base/attributes markers
Parsing a (non-valid) font with a comma in the name of the base colour
makes Kakoune crash. It is not a valid face, but Kakoune should just
return an error message instead.
Reproducer:
:set-face global foo ,red@,blue
Note the comma "," after the "@". This is not a valid base name, and it
leads to a crash. Let's see what happens.
At the beginning of parse_face(), we have the following code:
auto bg_it = find(facedesc, ',');
auto underline_it = bg_it == facedesc.end() ? bg_it : std::find(bg_it+1, facedesc.end(), ',');
auto attr_it = find(facedesc, '+');
auto base_it = find(facedesc, '@');
[...]
auto colors_end = std::min(attr_it, base_it);
After this:
- bg_it points to ",red@,blue"
- bg_it != facedesc.end(), so we have underline_it pointing to the first
comma after bg_it. This means that underline_it points to ",blue"
- base_it points to "@,blue"
- attr_it points to the end of facedesc (no "+" marker), so colors_end
points to base_it, "@,blue"
Later in the code, just after parsing the foreground and background
colours, we have:
if (underline_it != facedesc.end())
face.underline = parse_color({underline_it+1, colors_end});
When passing {underline_it+1, colors_end} to parse_color(), we pass in
fact iterators pointing to {",blue", "@,blue"}. Because the second one
starts _before_ the first one in the string, this means that the
resulting string is considered to have a _negative_ length.
parse_color() passes the string to str_to_color(), who fails to turn up
the colour, and attempts to throw:
throw runtime_error(format("unable to parse color: '{}'", color));
The variable "color" still has this negative length, and this goes all
the way down to an assert in src/units.hh where we expect that string to
be >= 0, and we crash on the assertion failure.
For similar reasons, we also get a crash if the comma comes after the
marker for the face attributes:
:set-face global foo ,red+,a
To fix both cases, let's add a check to make sure that the underline_it,
marked with a comma, never gets detected as present and pointing after
colors_end, be it "@" or "+".
2022-02-10 21:21:47 +01:00
|
|
|
if (underline_it != facedesc.end()
|
|
|
|
and underline_it > colors_end)
|
|
|
|
throw runtime_error(invalid_face_error.str());
|
2019-04-28 01:35:52 +02:00
|
|
|
|
2021-09-07 00:21:26 +02:00
|
|
|
auto parse_color = [](StringView spec) {
|
|
|
|
return spec.empty() ? Color::Default : str_to_color(spec);
|
|
|
|
};
|
|
|
|
|
2019-04-23 17:44:30 +02:00
|
|
|
FaceRegistry::FaceSpec spec;
|
|
|
|
auto& face = spec.face;
|
2021-09-07 00:21:26 +02:00
|
|
|
face.fg = parse_color({facedesc.begin(), std::min(bg_it, colors_end)});
|
2014-07-11 01:44:59 +02:00
|
|
|
if (bg_it != facedesc.end())
|
2021-09-07 00:21:26 +02:00
|
|
|
{
|
|
|
|
face.bg = parse_color({bg_it+1, std::min(underline_it, colors_end)});
|
|
|
|
if (underline_it != facedesc.end())
|
|
|
|
face.underline = parse_color({underline_it+1, colors_end});
|
|
|
|
}
|
2014-07-11 01:44:59 +02:00
|
|
|
if (attr_it != facedesc.end())
|
|
|
|
{
|
2019-04-23 17:44:30 +02:00
|
|
|
for (++attr_it; attr_it != base_it; ++attr_it)
|
2014-07-11 01:44:59 +02:00
|
|
|
{
|
|
|
|
switch (*attr_it)
|
|
|
|
{
|
2019-04-23 17:44:30 +02:00
|
|
|
case 'u': face.attributes |= Attribute::Underline; break;
|
2021-09-07 00:21:26 +02:00
|
|
|
case 'c': face.attributes |= Attribute::CurlyUnderline; break;
|
2019-04-23 17:44:30 +02:00
|
|
|
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;
|
2020-11-02 20:55:15 +01:00
|
|
|
case 's': face.attributes |= Attribute::Strikethrough; break;
|
2019-04-23 17:44:30 +02:00
|
|
|
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 16:56:53 +02:00
|
|
|
default: throw runtime_error(format("no such face attribute: '{}'", StringView{*attr_it}));
|
2014-07-11 01:44:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 17:44:30 +02:00
|
|
|
if (base_it != facedesc.end())
|
|
|
|
spec.base = String{base_it+1, facedesc.end()};
|
|
|
|
return spec;
|
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::Underline, "u" },
|
2021-09-07 00:21:26 +02:00
|
|
|
{ Attribute::CurlyUnderline, "c" },
|
2017-09-09 15:55:35 +02:00
|
|
|
{ Attribute::Reverse, "r" },
|
|
|
|
{ Attribute::Blink, "B" },
|
|
|
|
{ Attribute::Bold, "b" },
|
|
|
|
{ Attribute::Dim, "d" },
|
|
|
|
{ Attribute::Italic, "i" },
|
2021-09-05 01:20:35 +02:00
|
|
|
{ Attribute::Strikethrough, "s" },
|
2018-09-23 15:17:12 +02:00
|
|
|
{ Attribute::Final, "F" },
|
|
|
|
{ Attribute::FinalFg, "f" },
|
2018-09-24 09:54:21 +02:00
|
|
|
{ Attribute::FinalBg, "g" },
|
2018-09-23 15:17:12 +02:00
|
|
|
{ Attribute::FinalAttr, "a" },
|
2017-09-09 15:55:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto filteredAttrs = attrs |
|
2020-08-30 02:12:21 +02:00
|
|
|
filter([&](const Attr& a) {
|
|
|
|
if ((attributes & a.attr) != a.attr)
|
|
|
|
return false;
|
|
|
|
attributes &= ~a.attr;
|
|
|
|
return true;
|
|
|
|
}) | transform([](const Attr& a) { return a.name; });
|
2017-09-09 15:55:35 +02:00
|
|
|
|
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)
|
|
|
|
{
|
2021-09-07 00:21:26 +02:00
|
|
|
return format("{},{},{}{}", face.fg, face.bg, face.underline, face.attributes);
|
2017-09-09 15:55:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-07 07:36:39 +02:00
|
|
|
Face FaceRegistry::operator[](StringView facedesc) const
|
2013-05-15 14:01:23 +02:00
|
|
|
{
|
2019-04-23 17:44:30 +02: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 00:10:56 +02:00
|
|
|
{
|
2019-04-23 17:44:30 +02: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 00:10:56 +02:00
|
|
|
}
|
2019-04-23 17:44:30 +02:00
|
|
|
return face;
|
2012-09-17 19:01:13 +02:00
|
|
|
}
|
|
|
|
|
2018-04-07 07:36:39 +02:00
|
|
|
void FaceRegistry::add_face(StringView name, StringView facedesc, bool override)
|
2012-09-17 19:01:13 +02:00
|
|
|
{
|
2018-04-07 07:36:39 +02:00
|
|
|
if (not override and m_faces.find(name) != m_faces.end())
|
|
|
|
throw runtime_error(format("face '{}' 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
|
2019-04-23 17:44:30 +02:00
|
|
|
any_of(name, [](char c){ return not is_word(c); }))
|
2018-04-07 07:36:39 +02:00
|
|
|
throw runtime_error(format("invalid face name: '{}'", name));
|
2015-08-12 22:49:29 +02:00
|
|
|
|
2019-04-23 17:44:30 +02: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 00:10:56 +02:00
|
|
|
{
|
2019-04-23 17:44:30 +02:00
|
|
|
it->value.face = merge_faces(it->value.face, spec.face);
|
|
|
|
it->value.base = spec.base;
|
|
|
|
return;
|
2014-08-20 00:10:56 +02:00
|
|
|
}
|
2018-04-10 11:57:16 +02:00
|
|
|
|
2019-04-23 17:44:30 +02:00
|
|
|
while (it != m_faces.end() and not it->value.base.empty())
|
2014-08-20 00:10:56 +02:00
|
|
|
{
|
2019-04-23 17:44:30 +02:00
|
|
|
if (it->value.base == name)
|
|
|
|
throw runtime_error("face cycle detected");
|
|
|
|
it = m_faces.find(it->value.base);
|
2014-08-20 00:10:56 +02:00
|
|
|
}
|
2019-04-23 17:44:30 +02:00
|
|
|
m_faces[name] = std::move(spec);
|
2012-09-17 19:01:13 +02:00
|
|
|
}
|
|
|
|
|
2018-04-07 07:36:39 +02:00
|
|
|
void FaceRegistry::remove_face(StringView name)
|
2014-03-29 14:18:46 +01:00
|
|
|
{
|
2018-04-07 07:36:39 +02:00
|
|
|
m_faces.remove(name);
|
2014-03-29 14:18:46 +01:00
|
|
|
}
|
|
|
|
|
2014-07-11 01:27:04 +02:00
|
|
|
FaceRegistry::FaceRegistry()
|
2018-04-07 07:36:39 +02:00
|
|
|
: m_faces{
|
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 }} },
|
2018-02-24 11:24:06 +01:00
|
|
|
{ "PrimaryCursorEol", {Face{ Color::Black, Color::Cyan }} },
|
|
|
|
{ "SecondaryCursorEol", {Face{ Color::Black, Color::Cyan }} },
|
2017-09-01 12:32:38 +02:00
|
|
|
{ "LineNumbers", {Face{ Color::Default, Color::Default }} },
|
|
|
|
{ "LineNumberCursor", {Face{ Color::Default, Color::Default, Attribute::Reverse }} },
|
|
|
|
{ "LineNumbersWrapped", {Face{ Color::Default, Color::Default, Attribute::Italic }} },
|
2019-10-25 06:12:37 +02:00
|
|
|
{ "WrapMarker", {Face{ Color::Blue, Color::Default }} },
|
2017-09-01 12:32:38 +02:00
|
|
|
{ "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 }} },
|
2021-09-07 08:55:01 +02:00
|
|
|
{ "DiagnosticError", {Face{ Color::Red, Color::Default }} },
|
|
|
|
{ "DiagnosticWarning", {Face{ Color::Yellow, Color::Default }} },
|
2017-09-01 12:32:38 +02:00
|
|
|
{ "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 15:17:12 +02:00
|
|
|
{ "Whitespace", {Face{ Color::Default, Color::Default, Attribute::FinalFg }} },
|
2013-03-06 20:31:07 +01:00
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
2012-09-17 19:01:13 +02:00
|
|
|
}
|