summaryrefslogtreecommitdiff
path: root/src/display_buffer.cc
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2015-09-19 12:19:17 +0100
committerMaxime Coste <frrrwww@gmail.com>2015-09-19 12:19:17 +0100
commit6bc5f8c3a3f0ef2e0a4bfb51beb756608554597e (patch)
tree5c6333282cd0c3bf2e71b0528567180a0b974720 /src/display_buffer.cc
parentdb8c12fd2a9ccb3d44445e4539bc10a0f4e5c7e6 (diff)
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 \.
Diffstat (limited to 'src/display_buffer.cc')
-rw-r--r--src/display_buffer.cc53
1 files changed, 53 insertions, 0 deletions
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;
+}
+
}