From 6bc5f8c3a3f0ef2e0a4bfb51beb756608554597e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 19 Sep 2015 12:19:17 +0100 Subject: Add simple markup support to generate display lines from strings The syntax is simply {face} to enable the given face, use \{ to escape a {, and \\ to escape a \. --- src/display_buffer.cc | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'src/display_buffer.cc') diff --git a/src/display_buffer.cc b/src/display_buffer.cc index 8cda0c47..0a1b3503 100644 --- a/src/display_buffer.cc +++ b/src/display_buffer.cc @@ -4,6 +4,8 @@ #include "buffer.hh" #include "utf8.hh" +#include "face_registry.hh" + namespace Kakoune { @@ -250,4 +252,55 @@ void DisplayBuffer::optimize() for (auto& line : m_lines) line.optimize(); } + +DisplayLine parse_display_line(StringView line, Face default_face) +{ + DisplayLine res; + bool was_antislash = false; + auto pos = line.begin(); + String content; + Face face = default_face; + for (auto it = line.begin(), end = line.end(); it != end; ++it) + { + const char c = *it; + if (c == '{') + { + if (was_antislash) + { + content += StringView{pos, it}; + content.back() = '{'; + pos = it + 1; + } + else + { + content += StringView{pos, it}; + res.push_back({std::move(content), face}); + content.clear(); + auto closing = std::find(it+1, end, '}'); + if (closing == end) + throw runtime_error("unclosed face definition"); + face = merge_faces(default_face, get_face({it+1, closing})); + it = closing; + pos = closing + 1; + } + was_antislash = false; + } + if (c == '\\') + { + if (was_antislash) + { + content += StringView{pos, it}; + pos = it + 1; + was_antislash = false; + } + else + was_antislash = true; + } + } + content += StringView{pos, line.end()}; + if (not content.empty()) + res.push_back({std::move(content), face}); + return res; +} + } -- cgit v1.2.3