2011-09-02 18:51:20 +02:00
|
|
|
#include "display_buffer.hh"
|
|
|
|
|
2011-09-29 11:10:27 +02:00
|
|
|
#include "assert.hh"
|
2011-09-29 10:55:08 +02:00
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-06-28 00:03:11 +02:00
|
|
|
void AtomContent::trim_begin(CharCount count)
|
|
|
|
{
|
|
|
|
if (m_type == BufferRange)
|
|
|
|
m_begin = utf8::advance(m_buffer->iterator_at(m_begin),
|
|
|
|
m_buffer->iterator_at(m_end), count).coord();
|
|
|
|
else
|
|
|
|
m_text = m_text.substr(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomContent::trim_end(CharCount count)
|
|
|
|
{
|
|
|
|
if (m_type == BufferRange)
|
|
|
|
m_end = utf8::advance(m_buffer->iterator_at(m_end),
|
|
|
|
m_buffer->iterator_at(m_begin), -count).coord();
|
|
|
|
else
|
|
|
|
m_text = m_text.substr(0, m_text.char_length() - count);
|
|
|
|
}
|
|
|
|
|
2013-05-23 13:59:33 +02:00
|
|
|
DisplayLine::iterator DisplayLine::split(iterator it, BufferCoord pos)
|
2011-10-15 06:45:49 +02:00
|
|
|
{
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(it->content.type() == AtomContent::BufferRange);
|
|
|
|
kak_assert(it->content.begin() < pos);
|
|
|
|
kak_assert(it->content.end() > pos);
|
2011-10-15 06:45:49 +02:00
|
|
|
|
2012-07-12 23:19:10 +02:00
|
|
|
DisplayAtom atom = *it;
|
2012-10-08 14:28:38 +02:00
|
|
|
atom.content.m_end = pos;
|
|
|
|
it->content.m_begin = pos;
|
2012-07-12 23:19:10 +02:00
|
|
|
return m_atoms.insert(it, std::move(atom));
|
2011-09-29 11:10:27 +02:00
|
|
|
}
|
|
|
|
|
2012-10-22 13:20:02 +02:00
|
|
|
void DisplayLine::optimize()
|
|
|
|
{
|
2013-05-02 18:57:15 +02:00
|
|
|
if (m_atoms.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto atom_it = m_atoms.begin();
|
|
|
|
auto next_atom_it = atom_it + 1;
|
|
|
|
while (next_atom_it != m_atoms.end())
|
2012-10-22 13:20:02 +02:00
|
|
|
{
|
2013-05-02 18:57:15 +02:00
|
|
|
auto& atom = *atom_it;
|
|
|
|
auto& next_atom = *next_atom_it;
|
|
|
|
bool merged = false;
|
2012-10-22 13:20:02 +02:00
|
|
|
|
2013-05-02 18:57:15 +02:00
|
|
|
if (atom.colors == next_atom.colors and
|
|
|
|
atom.attribute == next_atom.attribute and
|
|
|
|
atom.content.type() == next_atom.content.type())
|
|
|
|
{
|
|
|
|
auto type = atom.content.type();
|
|
|
|
if ((type == AtomContent::BufferRange or
|
|
|
|
type == AtomContent::ReplacedBufferRange) and
|
2012-10-22 13:20:02 +02:00
|
|
|
next_atom.content.begin() == atom.content.end())
|
|
|
|
{
|
|
|
|
atom.content.m_end = next_atom.content.end();
|
2013-05-02 18:57:15 +02:00
|
|
|
if (type == AtomContent::ReplacedBufferRange)
|
|
|
|
atom.content.m_text += next_atom.content.m_text;
|
|
|
|
merged = true;
|
|
|
|
}
|
|
|
|
if (type == AtomContent::Text)
|
|
|
|
{
|
|
|
|
atom.content.m_text += next_atom.content.m_text;
|
|
|
|
merged = true;
|
2012-10-22 13:20:02 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-02 18:57:15 +02:00
|
|
|
if (merged)
|
|
|
|
next_atom_it = m_atoms.erase(next_atom_it);
|
|
|
|
else
|
|
|
|
atom_it = next_atom_it++;
|
2012-10-22 13:20:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-04 18:50:00 +02:00
|
|
|
CharCount DisplayLine::length() const
|
|
|
|
{
|
|
|
|
CharCount len = 0;
|
|
|
|
for (auto& atom : m_atoms)
|
|
|
|
len += atom.content.length();
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2013-06-28 00:03:11 +02:00
|
|
|
void DisplayLine::trim(CharCount first_char, CharCount char_count)
|
|
|
|
{
|
|
|
|
for (auto it = begin(); first_char > 0 and it != end(); )
|
|
|
|
{
|
|
|
|
if (not it->content.has_buffer_range())
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto len = it->content.length();
|
|
|
|
if (len <= first_char)
|
|
|
|
{
|
|
|
|
m_atoms.erase(it);
|
|
|
|
first_char -= len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it->content.trim_begin(first_char);
|
|
|
|
first_char = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto it = begin();
|
|
|
|
for (; it != end() and char_count > 0; ++it)
|
|
|
|
char_count -= it->content.length();
|
|
|
|
|
|
|
|
if (char_count < 0)
|
|
|
|
(it-1)->content.trim_end(-char_count);
|
|
|
|
m_atoms.erase(it, end());
|
|
|
|
}
|
|
|
|
|
2012-07-12 23:51:13 +02:00
|
|
|
void DisplayBuffer::compute_range()
|
|
|
|
{
|
2013-05-23 13:59:33 +02:00
|
|
|
m_range.first = {INT_MAX,INT_MAX};
|
|
|
|
m_range.second = {0,0};
|
2012-07-12 23:51:13 +02:00
|
|
|
for (auto& line : m_lines)
|
|
|
|
{
|
|
|
|
for (auto& atom : line)
|
|
|
|
{
|
|
|
|
if (not atom.content.has_buffer_range())
|
|
|
|
continue;
|
|
|
|
|
2013-05-23 13:59:33 +02:00
|
|
|
if (m_range.first > atom.content.begin())
|
2012-07-12 23:51:13 +02:00
|
|
|
m_range.first = atom.content.begin();
|
|
|
|
|
2013-05-23 13:59:33 +02:00
|
|
|
if (m_range.second < atom.content.end())
|
2012-07-12 23:51:13 +02:00
|
|
|
m_range.second = atom.content.end();
|
|
|
|
}
|
|
|
|
}
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(m_range.first <= m_range.second);
|
2012-07-12 23:51:13 +02:00
|
|
|
}
|
|
|
|
|
2012-10-22 13:20:02 +02:00
|
|
|
void DisplayBuffer::optimize()
|
|
|
|
{
|
|
|
|
for (auto& line : m_lines)
|
|
|
|
line.optimize();
|
|
|
|
}
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|