kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

face_registry.cc (8156B)


      1 #include "face_registry.hh"
      2 
      3 #include "exception.hh"
      4 #include "ranges.hh"
      5 #include "format.hh"
      6 
      7 namespace Kakoune
      8 {
      9 
     10 FaceSpec parse_face(StringView facedesc)
     11 {
     12     constexpr StringView invalid_face_error = "invalid face description, expected [<fg>][,<bg>[,<underline>]][+<attr>][@base] or just [base]";
     13     if (all_of(facedesc, [](char c){ return is_word(c); }) and not is_color_name(facedesc))
     14         return {Face{}, facedesc.str()};
     15 
     16     auto bg_it = find(facedesc, ',');
     17     auto underline_it = bg_it == facedesc.end() ? bg_it : std::find(bg_it+1, facedesc.end(), ',');
     18     auto attr_it = find(facedesc, '+');
     19     auto base_it = find(facedesc, '@');
     20     if (bg_it != facedesc.end()
     21         and (attr_it < bg_it or (bg_it + 1) == facedesc.end()))
     22         throw runtime_error(invalid_face_error.str());
     23     if (attr_it != facedesc.end()
     24         and (attr_it + 1) == facedesc.end())
     25         throw runtime_error(invalid_face_error.str());
     26 
     27     auto colors_end = std::min(attr_it, base_it);
     28     if (underline_it != facedesc.end()
     29         and underline_it > colors_end)
     30         throw runtime_error(invalid_face_error.str());
     31 
     32     auto parse_color = [](StringView spec) {
     33         return spec.empty() ? Color::Default : str_to_color(spec);
     34     };
     35 
     36     FaceSpec spec;
     37     auto& face = spec.face;
     38     face.fg = parse_color({facedesc.begin(), std::min(bg_it, colors_end)});
     39     if (bg_it != facedesc.end())
     40     {
     41         face.bg = parse_color({bg_it+1, std::min(underline_it, colors_end)});
     42         if (underline_it != facedesc.end())
     43             face.underline = parse_color({underline_it+1, colors_end});
     44     }
     45     if (attr_it != facedesc.end())
     46     {
     47         for (++attr_it; attr_it != base_it; ++attr_it)
     48         {
     49             switch (*attr_it)
     50             {
     51                 case 'u': face.attributes |= Attribute::Underline; break;
     52                 case 'c': face.attributes |= Attribute::CurlyUnderline; break;
     53                 case 'U': face.attributes |= Attribute::DoubleUnderline; break;
     54                 case 'r': face.attributes |= Attribute::Reverse; break;
     55                 case 'b': face.attributes |= Attribute::Bold; break;
     56                 case 'B': face.attributes |= Attribute::Blink; break;
     57                 case 'd': face.attributes |= Attribute::Dim; break;
     58                 case 'i': face.attributes |= Attribute::Italic; break;
     59                 case 's': face.attributes |= Attribute::Strikethrough; break;
     60                 case 'f': face.attributes |= Attribute::FinalFg; break;
     61                 case 'g': face.attributes |= Attribute::FinalBg; break;
     62                 case 'a': face.attributes |= Attribute::FinalAttr; break;
     63                 case 'F': face.attributes |= Attribute::Final; break;
     64                 default: throw runtime_error(format("no such face attribute: '{}'", StringView{*attr_it}));
     65             }
     66         }
     67     }
     68     if (base_it != facedesc.end())
     69         spec.base = String{base_it+1, facedesc.end()};
     70     return spec;
     71 }
     72 
     73 String to_string(Attribute attributes)
     74 {
     75     if (attributes == Attribute::Normal)
     76         return "";
     77 
     78     struct Attr { Attribute attr; StringView name; }
     79     attrs[] {
     80         { Attribute::Underline, "u" },
     81         { Attribute::CurlyUnderline, "c" },
     82         { Attribute::DoubleUnderline, "U" },
     83         { Attribute::Reverse, "r" },
     84         { Attribute::Blink, "B" },
     85         { Attribute::Bold, "b" },
     86         { Attribute::Dim, "d" },
     87         { Attribute::Italic, "i" },
     88         { Attribute::Strikethrough, "s" },
     89         { Attribute::Final, "F" },
     90         { Attribute::FinalFg, "f" },
     91         { Attribute::FinalBg, "g" },
     92         { Attribute::FinalAttr, "a" },
     93     };
     94 
     95     auto filteredAttrs = attrs |
     96                          filter([&](const Attr& a) {
     97                              if ((attributes & a.attr) != a.attr)
     98                                  return false;
     99                              attributes &= ~a.attr;
    100                              return true;
    101                          }) | transform([](const Attr& a) { return a.name; });
    102 
    103     return accumulate(filteredAttrs, "+"_str, std::plus<>{});
    104 }
    105 
    106 String to_string(Face face)
    107 {
    108     return format("{},{},{}{}", face.fg, face.bg, face.underline, face.attributes);
    109 }
    110 
    111 Face FaceRegistry::operator[](StringView facedesc) const
    112 {
    113     return resolve_spec(parse_face(facedesc));
    114 }
    115 
    116 Face FaceRegistry::operator[](const FaceSpec& spec) const
    117 {
    118     return resolve_spec(spec);
    119 }
    120 
    121 Face FaceRegistry::resolve_spec(const FaceSpec& spec) const
    122 {
    123     if (spec.base.empty())
    124         return spec.face;
    125 
    126     StringView base = spec.base;
    127     Face face = spec.face;
    128     for (auto* reg = this; reg != nullptr; reg = reg->m_parent.get())
    129     {
    130         auto it = reg->m_faces.find(base);
    131         if (it == reg->m_faces.end())
    132             continue;
    133 
    134         if (it->value.base.empty())
    135             return merge_faces(it->value.face, face);
    136         if (it->value.base != it->key)
    137             return merge_faces(reg->resolve_spec(it->value), face);
    138         else
    139         {
    140             face = merge_faces(it->value.face, face);
    141             base = it->value.base;
    142         }
    143     }
    144     return face;
    145 }
    146 
    147 void FaceRegistry::add_face(StringView name, StringView facedesc, bool override)
    148 {
    149     if (not override and m_faces.find(name) != m_faces.end())
    150         throw runtime_error(format("face '{}' already defined", name));
    151 
    152     if (name.empty() or is_color_name(name) or
    153         any_of(name, [](char c){ return not is_word(c); }))
    154         throw runtime_error(format("invalid face name: '{}'", name));
    155 
    156     FaceSpec spec = parse_face(facedesc);
    157     auto it = m_faces.find(spec.base);
    158     if (spec.base == name and it != m_faces.end())
    159     {
    160         it->value.face = merge_faces(it->value.face, spec.face);
    161         it->value.base = spec.base;
    162         return;
    163     }
    164 
    165     while (it != m_faces.end() and not it->value.base.empty())
    166     {
    167         if (it->value.base == name)
    168             throw runtime_error("face cycle detected");
    169         it = m_faces.find(it->value.base);
    170     }
    171     m_faces[name] = std::move(spec);
    172 }
    173 
    174 void FaceRegistry::remove_face(StringView name)
    175 {
    176     m_faces.remove(name);
    177 }
    178 
    179 FaceRegistry::FaceRegistry()
    180     : m_faces{
    181         { "Default", {Face{ Color::Default, Color::Default }} },
    182         { "PrimarySelection", {Face{ Color::White, Color::Blue }} },
    183         { "SecondarySelection", {Face{ Color::Black, Color::Blue }} },
    184         { "PrimaryCursor", {Face{ Color::Black, Color::White }} },
    185         { "SecondaryCursor", {Face{ Color::Black, Color::White }} },
    186         { "PrimaryCursorEol", {Face{ Color::Black, Color::Cyan }} },
    187         { "SecondaryCursorEol", {Face{ Color::Black, Color::Cyan }} },
    188         { "LineNumbers", {Face{ Color::Default, Color::Default }} },
    189         { "LineNumberCursor", {Face{ Color::Default, Color::Default, Attribute::Reverse }} },
    190         { "LineNumbersWrapped", {Face{ Color::Default, Color::Default, Attribute::Italic }} },
    191         { "WrapMarker", {Face{ Color::Blue, Color::Default }} },
    192         { "MenuForeground", {Face{ Color::White, Color::Blue }} },
    193         { "MenuBackground", {Face{ Color::Blue, Color::White }} },
    194         { "MenuInfo", {Face{ Color::Cyan, Color::Default }} },
    195         { "Information", {Face{ Color::Black, Color::Yellow }} },
    196         { "InlineInformation", {Face{}, "Information"} },
    197         { "Error", {Face{ Color::Black, Color::Red }} },
    198         { "DiagnosticError", {Face{ Color::Red, Color::Default }} },
    199         { "DiagnosticWarning", {Face{ Color::Yellow, Color::Default }} },
    200         { "StatusLine", {Face{ Color::Cyan, Color::Default }} },
    201         { "StatusLineMode", {Face{ Color::Yellow, Color::Default }} },
    202         { "StatusLineInfo", {Face{ Color::Blue, Color::Default }} },
    203         { "StatusLineValue", {Face{ Color::Green, Color::Default }} },
    204         { "StatusCursor", {Face{ Color::Black, Color::Cyan }} },
    205         { "Prompt", {Face{ Color::Yellow, Color::Default }} },
    206         { "MatchingChar", {Face{ Color::Default, Color::Default, Attribute::Bold }} },
    207         { "BufferPadding", {Face{ Color::Blue, Color::Default }} },
    208         { "Whitespace", {Face{ Color::Default, Color::Default, Attribute::FinalFg }} },
    209         { "WhitespaceIndent", {Face{}, "Whitespace"} },
    210       }
    211 {}
    212 
    213 }