From e659ea2dab4b91d6f5a8905f4a854f7be82343e3 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 29 Sep 2011 08:55:08 +0000 Subject: [PATCH] DisplayBuffer: add a split method to split an atom --- src/display_buffer.cc | 15 +++++++++++++ src/display_buffer.hh | 12 ++++++---- src/window.cc | 51 +++++++++++++++---------------------------- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/display_buffer.cc b/src/display_buffer.cc index ea5342bc..03cc5b18 100644 --- a/src/display_buffer.cc +++ b/src/display_buffer.cc @@ -1,5 +1,7 @@ #include "display_buffer.hh" +#include "assert.h" + namespace Kakoune { @@ -7,4 +9,17 @@ DisplayBuffer::DisplayBuffer() { } +DisplayBuffer::iterator DisplayBuffer::split(iterator atom, size_t pos_in_atom) +{ + assert(atom < end()); + assert(pos_in_atom < atom->content.length()); + DisplayAtom new_atom(atom->begin, atom->begin + pos_in_atom, + atom->content.substr(0, pos_in_atom), + atom->fg_color, atom->bg_color, atom->attribute); + + atom->begin = atom->begin + pos_in_atom; + atom->content = atom->content.substr(pos_in_atom); + return insert(atom, std::move(new_atom)); +} + } diff --git a/src/display_buffer.hh b/src/display_buffer.hh index d1a1eebb..e422e38a 100644 --- a/src/display_buffer.hh +++ b/src/display_buffer.hh @@ -43,13 +43,16 @@ struct DisplayAtom Attribute attribute; DisplayAtom(BufferIterator begin, BufferIterator end, - const std::string& content) + const std::string& content, + Color fg_color = Color::Default, + Color bg_color = Color::Default, + Attribute attribute = Attributes::Normal) : content(content), begin(begin), end(end), - fg_color(Color::Default), - bg_color(Color::Default), - attribute(Attributes::Normal) + fg_color(fg_color), + bg_color(bg_color), + attribute(attribute) {} }; @@ -65,6 +68,7 @@ public: void clear() { m_atoms.clear(); } void append(const DisplayAtom& atom) { m_atoms.push_back(atom); } iterator insert(iterator where, const DisplayAtom& atom) { return m_atoms.insert(where, atom); } + iterator split(iterator atom, size_t pos_in_atom); iterator begin() { return m_atoms.begin(); } iterator end() { return m_atoms.end(); } diff --git a/src/window.cc b/src/window.cc index 58a85f48..e94394c9 100644 --- a/src/window.cc +++ b/src/window.cc @@ -61,45 +61,34 @@ public: if (atom.begin >= sel.begin() and atom.begin < sel.end() and atom.end > sel.end()) { size_t length = sel.end() - atom.begin; - DisplayAtom selected(atom.begin, sel.end(), - atom.content.substr(0, length)); - selected.attribute = Attributes::Underline; - atom.content = atom.content.substr(length); - atom.begin = sel.end(); - atom_it = display_buffer.insert(atom_it, selected) + 1; + atom_it = display_buffer.split(atom_it, length); + atom_it->attribute |= Attributes::Underline; + ++atom_it; ++sel_it; } // [---###---] else if (atom.begin < sel.begin() and atom.end > sel.end()) { size_t prefix_length = sel.begin() - atom.begin; - DisplayAtom prefix(atom.begin, sel.begin(), - atom.content.substr(0, prefix_length)); + atom_it = display_buffer.split(atom_it, prefix_length); size_t sel_length = sel.end() - sel.begin(); - DisplayAtom selected(sel.begin(), sel.end(), - atom.content.substr(prefix_length, sel_length)); - selected.attribute = Attributes::Underline; - atom.content = atom.content.substr(prefix_length + sel_length); - atom_it = display_buffer.insert(atom_it, selected); - atom_it = display_buffer.insert(atom_it, prefix); - atom_it += 2; + atom_it = display_buffer.split(atom_it + 1, sel_length); + atom_it->attribute |= Attributes::Underline; + ++atom_it; ++sel_it; } // [------###] else if (atom.begin < sel.begin() and atom.end > sel.begin()) { - size_t length = sel.end() - atom.begin; - DisplayAtom prefix(atom.begin, sel.end(), - atom.content.substr(0, length)); - atom.content = atom.content.substr(length); - atom.begin = sel.end(); - atom.attribute = Attributes::Underline; - atom_it = display_buffer.insert(atom_it, prefix) + 2; + size_t length = sel.begin() - atom.begin; + atom_it = display_buffer.split(atom_it, length) + 1; + atom_it->attribute |= Attributes::Underline; + ++atom_it; } // [#########] else if (atom.begin >= sel.begin() and atom.end <= sel.end()) { - atom.attribute = Attributes::Underline; + atom_it->attribute |= Attributes::Underline; ++atom_it; } // [---------] @@ -122,19 +111,13 @@ static void blink_void(DisplayBuffer& display_buffer) for (auto atom_it = display_buffer.begin(); atom_it != display_buffer.end();) { - DisplayAtom& atom = *atom_it; - size_t pos = atom.content.find("void"); + size_t pos = atom_it->content.find("void"); if (pos != std::string::npos) { - DisplayAtom prefix(atom.begin, atom.begin + pos, - atom.content.substr(0, pos)); - prefix.attribute = atom.attribute; - DisplayAtom match(prefix.end, prefix.end + 4, "void"); - match.attribute = atom.attribute | Attributes::Blink; - atom.begin = prefix.end + 4; - atom.content = atom.content.substr(pos + 4); - atom_it = display_buffer.insert(atom_it, match); - atom_it = display_buffer.insert(atom_it, prefix) + 2; + atom_it = display_buffer.split(atom_it, pos) + 1; + atom_it = display_buffer.split(atom_it, 4); + atom_it->attribute |= Attributes::Blink; + ++atom_it; } else ++atom_it;