2011-09-02 18:51:20 +02:00
|
|
|
#include "display_buffer.hh"
|
|
|
|
|
2011-09-29 11:10:27 +02:00
|
|
|
#include "assert.hh"
|
2014-11-12 22:27:07 +01:00
|
|
|
#include "buffer.hh"
|
2016-07-28 10:41:47 +02:00
|
|
|
#include "buffer_utils.hh"
|
2014-11-12 22:27:07 +01:00
|
|
|
#include "utf8.hh"
|
2011-09-29 10:55:08 +02:00
|
|
|
|
2015-09-19 13:19:17 +02:00
|
|
|
#include "face_registry.hh"
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-12-17 05:07:09 +01:00
|
|
|
String option_to_string(BufferRange range)
|
|
|
|
{
|
|
|
|
return format("{}.{},{}.{}",
|
|
|
|
range.begin.line+1, range.begin.column+1,
|
2016-10-26 23:47:24 +02:00
|
|
|
range.end.line, range.end.column);
|
2015-12-17 05:07:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void option_from_string(StringView str, BufferRange& opt)
|
|
|
|
{
|
2016-10-26 23:47:24 +02:00
|
|
|
auto sep = find_if(str, [](char c){ return c == ',' or c == '+'; });
|
|
|
|
auto dot_beg = find(StringView{str.begin(), sep}, '.');
|
|
|
|
auto dot_end = find(StringView{sep, str.end()}, '.');
|
2015-12-17 05:07:09 +01:00
|
|
|
|
2016-10-26 23:47:24 +02:00
|
|
|
if (sep == str.end() or dot_beg == sep or
|
|
|
|
(*sep == ',' and dot_end == str.end()))
|
|
|
|
throw runtime_error(format("'{}' does not follow <line>.<column>,<line>.<column> or <line>.<column>+<len> format", str));
|
2015-12-17 05:07:09 +01:00
|
|
|
|
2016-10-26 23:47:24 +02:00
|
|
|
const BufferCoord beg{str_to_int({str.begin(), dot_beg}) - 1,
|
|
|
|
str_to_int({dot_beg+1, sep}) - 1};
|
2015-12-17 05:07:09 +01:00
|
|
|
|
2016-10-26 23:47:24 +02:00
|
|
|
const bool len = (*sep == '+');
|
|
|
|
const BufferCoord end{len ? beg.line : str_to_int({sep+1, dot_end}) - 1,
|
|
|
|
len ? beg.column + str_to_int({sep+1, str.end()})
|
|
|
|
: str_to_int({dot_end+1, str.end()}) };
|
2015-12-17 05:07:09 +01:00
|
|
|
|
2016-10-26 23:47:24 +02:00
|
|
|
if (beg.line < 0 or beg.column < 0 or end.line < 0 or end.column < 0)
|
|
|
|
throw runtime_error("coordinates elements should be >= 1");
|
2015-12-17 05:07:09 +01:00
|
|
|
|
2016-10-26 23:47:24 +02:00
|
|
|
opt = { beg, end };
|
|
|
|
}
|
2015-12-17 05:07:09 +01:00
|
|
|
|
2014-11-12 22:27:07 +01:00
|
|
|
StringView DisplayAtom::content() const
|
|
|
|
{
|
|
|
|
switch (m_type)
|
|
|
|
{
|
2016-10-13 20:55:15 +02:00
|
|
|
case Range:
|
2014-11-12 22:27:07 +01:00
|
|
|
{
|
2015-04-23 22:38:45 +02:00
|
|
|
auto line = (*m_buffer)[m_range.begin.line];
|
|
|
|
if (m_range.begin.line == m_range.end.line)
|
|
|
|
return line.substr(m_range.begin.column, m_range.end.column - m_range.begin.column);
|
|
|
|
else if (m_range.begin.line+1 == m_range.end.line and m_range.end.column == 0)
|
|
|
|
return line.substr(m_range.begin.column);
|
2014-11-12 22:27:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Text:
|
2016-10-13 20:55:15 +02:00
|
|
|
case ReplacedRange:
|
2014-11-12 22:27:07 +01:00
|
|
|
return m_text;
|
|
|
|
}
|
|
|
|
kak_assert(false);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
ColumnCount DisplayAtom::length() const
|
2014-11-12 22:27:07 +01:00
|
|
|
{
|
|
|
|
switch (m_type)
|
|
|
|
{
|
2016-10-13 20:55:15 +02:00
|
|
|
case Range:
|
2016-10-04 20:37:17 +02:00
|
|
|
return column_length(*m_buffer, m_range.begin, m_range.end);
|
2014-11-12 22:27:07 +01:00
|
|
|
case Text:
|
2016-10-13 20:55:15 +02:00
|
|
|
case ReplacedRange:
|
2016-10-04 20:37:17 +02:00
|
|
|
return m_text.column_length();
|
2014-11-12 22:27:07 +01:00
|
|
|
}
|
|
|
|
kak_assert(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
void DisplayAtom::trim_begin(ColumnCount count)
|
2013-06-28 00:03:11 +02:00
|
|
|
{
|
2016-10-13 20:55:15 +02:00
|
|
|
if (m_type == Range)
|
2015-04-23 22:38:45 +02:00
|
|
|
m_range.begin = utf8::advance(m_buffer->iterator_at(m_range.begin),
|
2016-09-22 21:36:26 +02:00
|
|
|
m_buffer->iterator_at(m_range.end),
|
|
|
|
count).coord();
|
2013-06-28 00:03:11 +02:00
|
|
|
else
|
2015-03-10 20:33:46 +01:00
|
|
|
m_text = m_text.substr(count).str();
|
2013-12-12 14:45:08 +01:00
|
|
|
check_invariant();
|
2013-06-28 00:03:11 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
void DisplayAtom::trim_end(ColumnCount count)
|
2013-06-28 00:03:11 +02:00
|
|
|
{
|
2016-10-13 20:55:15 +02:00
|
|
|
if (m_type == Range)
|
2015-04-23 22:38:45 +02:00
|
|
|
m_range.end = utf8::advance(m_buffer->iterator_at(m_range.end),
|
2016-09-22 21:36:26 +02:00
|
|
|
m_buffer->iterator_at(m_range.begin),
|
|
|
|
-count).coord();
|
2013-06-28 00:03:11 +02:00
|
|
|
else
|
2016-09-22 21:36:26 +02:00
|
|
|
m_text = m_text.substr(0, m_text.column_length() - count).str();
|
2013-12-12 14:45:08 +01:00
|
|
|
check_invariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisplayAtom::check_invariant() const
|
|
|
|
{
|
|
|
|
#ifdef KAK_DEBUG
|
|
|
|
if (has_buffer_range())
|
|
|
|
{
|
2015-04-23 22:38:45 +02:00
|
|
|
kak_assert(m_buffer->is_valid(m_range.begin));
|
|
|
|
kak_assert(m_buffer->is_valid(m_range.end));
|
2013-12-12 14:45:08 +01:00
|
|
|
}
|
|
|
|
#endif
|
2013-06-28 00:03:11 +02:00
|
|
|
}
|
|
|
|
|
2013-07-23 20:11:26 +02:00
|
|
|
DisplayLine::DisplayLine(AtomList atoms)
|
|
|
|
: m_atoms(std::move(atoms))
|
|
|
|
{
|
|
|
|
compute_range();
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
DisplayLine::iterator DisplayLine::split(iterator it, BufferCoord pos)
|
2011-10-15 06:45:49 +02:00
|
|
|
{
|
2016-10-13 20:55:15 +02:00
|
|
|
kak_assert(it->type() == DisplayAtom::Range);
|
2013-07-24 14:55:57 +02:00
|
|
|
kak_assert(it->begin() < pos);
|
|
|
|
kak_assert(it->end() > pos);
|
2011-10-15 06:45:49 +02:00
|
|
|
|
2012-07-12 23:19:10 +02:00
|
|
|
DisplayAtom atom = *it;
|
2015-04-23 22:38:45 +02:00
|
|
|
atom.m_range.end = pos;
|
|
|
|
it->m_range.begin = pos;
|
2013-12-12 14:45:08 +01:00
|
|
|
atom.check_invariant();
|
|
|
|
it->check_invariant();
|
2012-07-12 23:19:10 +02:00
|
|
|
return m_atoms.insert(it, std::move(atom));
|
2011-09-29 11:10:27 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
DisplayLine::iterator DisplayLine::split(iterator it, ColumnCount pos)
|
2015-07-23 14:58:23 +02:00
|
|
|
{
|
|
|
|
kak_assert(it->type() == DisplayAtom::Text);
|
|
|
|
kak_assert(pos > 0);
|
|
|
|
kak_assert(pos < it->length());
|
|
|
|
|
|
|
|
DisplayAtom atom(it->m_text.substr(0, pos).str());
|
|
|
|
it->m_text = it->m_text.substr(pos).str();
|
|
|
|
atom.check_invariant();
|
|
|
|
it->check_invariant();
|
|
|
|
return m_atoms.insert(it, std::move(atom));
|
|
|
|
}
|
|
|
|
|
2013-07-23 20:11:26 +02:00
|
|
|
DisplayLine::iterator DisplayLine::insert(iterator it, DisplayAtom atom)
|
|
|
|
{
|
2013-07-24 14:55:57 +02:00
|
|
|
if (atom.has_buffer_range())
|
2013-07-23 20:11:26 +02:00
|
|
|
{
|
2015-04-23 22:38:45 +02:00
|
|
|
m_range.begin = std::min(m_range.begin, atom.begin());
|
|
|
|
m_range.end = std::max(m_range.end, atom.end());
|
2013-07-23 20:11:26 +02:00
|
|
|
}
|
|
|
|
return m_atoms.insert(it, std::move(atom));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisplayLine::push_back(DisplayAtom atom)
|
|
|
|
{
|
2013-07-24 14:55:57 +02:00
|
|
|
if (atom.has_buffer_range())
|
2013-07-23 20:11:26 +02:00
|
|
|
{
|
2015-04-23 22:38:45 +02:00
|
|
|
m_range.begin = std::min(m_range.begin, atom.begin());
|
|
|
|
m_range.end = std::max(m_range.end, atom.end());
|
2013-07-23 20:11:26 +02:00
|
|
|
}
|
|
|
|
m_atoms.push_back(std::move(atom));
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:38:43 +01:00
|
|
|
DisplayLine::iterator DisplayLine::erase(iterator beg, iterator end)
|
|
|
|
{
|
|
|
|
iterator res = m_atoms.erase(beg, end);
|
|
|
|
compute_range();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-07-11 01:27:04 +02:00
|
|
|
if (atom.face == next_atom.face and
|
2013-07-24 14:55:57 +02:00
|
|
|
atom.type() == next_atom.type())
|
2013-05-02 18:57:15 +02:00
|
|
|
{
|
2013-07-24 14:55:57 +02:00
|
|
|
auto type = atom.type();
|
2016-10-13 20:55:15 +02:00
|
|
|
if ((type == DisplayAtom::Range or
|
|
|
|
type == DisplayAtom::ReplacedRange) and
|
2013-07-24 14:55:57 +02:00
|
|
|
next_atom.begin() == atom.end())
|
2012-10-22 13:20:02 +02:00
|
|
|
{
|
2015-04-23 22:38:45 +02:00
|
|
|
atom.m_range.end = next_atom.end();
|
2016-10-13 20:55:15 +02:00
|
|
|
if (type == DisplayAtom::ReplacedRange)
|
2013-07-24 14:55:57 +02:00
|
|
|
atom.m_text += next_atom.m_text;
|
2013-05-02 18:57:15 +02:00
|
|
|
merged = true;
|
|
|
|
}
|
2013-07-24 14:55:57 +02:00
|
|
|
if (type == DisplayAtom::Text)
|
2013-05-02 18:57:15 +02:00
|
|
|
{
|
2013-07-24 14:55:57 +02:00
|
|
|
atom.m_text += next_atom.m_text;
|
2013-05-02 18:57:15 +02:00
|
|
|
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++;
|
2013-12-12 14:45:08 +01:00
|
|
|
atom_it->check_invariant();
|
2012-10-22 13:20:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
ColumnCount DisplayLine::length() const
|
2013-04-04 18:50:00 +02:00
|
|
|
{
|
2016-09-22 21:36:26 +02:00
|
|
|
ColumnCount len = 0;
|
2013-04-04 18:50:00 +02:00
|
|
|
for (auto& atom : m_atoms)
|
2013-07-24 14:55:57 +02:00
|
|
|
len += atom.length();
|
2013-04-04 18:50:00 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
void DisplayLine::trim(ColumnCount first_col, ColumnCount col_count, bool only_buffer)
|
2013-06-28 00:03:11 +02:00
|
|
|
{
|
2016-09-22 21:36:26 +02:00
|
|
|
for (auto it = begin(); first_col > 0 and it != end(); )
|
2013-06-28 00:03:11 +02:00
|
|
|
{
|
2015-04-23 22:11:50 +02:00
|
|
|
if (only_buffer and not it->has_buffer_range())
|
2013-06-28 00:03:11 +02:00
|
|
|
{
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-24 14:55:57 +02:00
|
|
|
auto len = it->length();
|
2016-09-22 21:36:26 +02:00
|
|
|
if (len <= first_col)
|
2013-06-28 00:03:11 +02:00
|
|
|
{
|
|
|
|
m_atoms.erase(it);
|
2016-09-22 21:36:26 +02:00
|
|
|
first_col -= len;
|
2013-06-28 00:03:11 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-22 21:36:26 +02:00
|
|
|
it->trim_begin(first_col);
|
|
|
|
first_col = 0;
|
2013-06-28 00:03:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
auto it = begin();
|
2016-09-22 21:36:26 +02:00
|
|
|
for (; it != end() and col_count > 0; ++it)
|
|
|
|
col_count -= it->length();
|
2013-06-28 00:03:11 +02:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
if (col_count < 0)
|
|
|
|
(it-1)->trim_end(-col_count);
|
2013-06-28 00:03:11 +02:00
|
|
|
m_atoms.erase(it, end());
|
2013-07-23 20:11:26 +02:00
|
|
|
|
|
|
|
compute_range();
|
|
|
|
}
|
|
|
|
|
2014-04-02 23:41:06 +02:00
|
|
|
const BufferRange init_range{ {INT_MAX, INT_MAX}, {INT_MIN, INT_MIN} };
|
2013-12-11 22:38:43 +01:00
|
|
|
|
2013-07-23 20:11:26 +02:00
|
|
|
void DisplayLine::compute_range()
|
|
|
|
{
|
2013-12-11 22:38:43 +01:00
|
|
|
m_range = init_range;
|
2013-07-23 20:11:26 +02:00
|
|
|
for (auto& atom : m_atoms)
|
|
|
|
{
|
2013-07-24 14:55:57 +02:00
|
|
|
if (not atom.has_buffer_range())
|
2013-07-23 20:11:26 +02:00
|
|
|
continue;
|
2015-04-23 22:38:45 +02:00
|
|
|
m_range.begin = std::min(m_range.begin, atom.begin());
|
|
|
|
m_range.end = std::max(m_range.end, atom.end());
|
2013-07-23 20:11:26 +02:00
|
|
|
}
|
2013-12-11 22:38:43 +01:00
|
|
|
if (m_range == init_range)
|
|
|
|
m_range = { { 0, 0 }, { 0, 0 } };
|
2015-04-23 22:38:45 +02:00
|
|
|
kak_assert(m_range.begin <= m_range.end);
|
2013-06-28 00:03:11 +02:00
|
|
|
}
|
|
|
|
|
2012-07-12 23:51:13 +02:00
|
|
|
void DisplayBuffer::compute_range()
|
|
|
|
{
|
2013-12-11 22:38:43 +01:00
|
|
|
m_range = init_range;
|
2012-07-12 23:51:13 +02:00
|
|
|
for (auto& line : m_lines)
|
|
|
|
{
|
2015-04-23 22:38:45 +02:00
|
|
|
m_range.begin = std::min(line.range().begin, m_range.begin);
|
|
|
|
m_range.end = std::max(line.range().end, m_range.end);
|
2012-07-12 23:51:13 +02:00
|
|
|
}
|
2013-12-11 22:38:43 +01:00
|
|
|
if (m_range == init_range)
|
|
|
|
m_range = { { 0, 0 }, { 0, 0 } };
|
2015-04-23 22:38:45 +02:00
|
|
|
kak_assert(m_range.begin <= m_range.end);
|
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();
|
|
|
|
}
|
2015-09-19 13:19:17 +02:00
|
|
|
|
2015-09-20 12:34:13 +02:00
|
|
|
DisplayLine parse_display_line(StringView line)
|
2015-09-19 13:19:17 +02:00
|
|
|
{
|
|
|
|
DisplayLine res;
|
|
|
|
bool was_antislash = false;
|
|
|
|
auto pos = line.begin();
|
|
|
|
String content;
|
2015-09-20 12:34:13 +02:00
|
|
|
Face face;
|
2015-09-19 13:19:17 +02:00
|
|
|
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");
|
2015-09-20 12:34:13 +02:00
|
|
|
face = get_face({it+1, closing});
|
2015-09-19 13:19:17 +02:00
|
|
|
it = closing;
|
|
|
|
pos = closing + 1;
|
|
|
|
}
|
|
|
|
}
|
2016-12-07 14:43:27 +01:00
|
|
|
if (c == '\n') // line breaks are forbidden, replace with space
|
|
|
|
{
|
|
|
|
content += StringView{pos, it+1};
|
|
|
|
content.back() = ' ';
|
|
|
|
pos = it + 1;
|
|
|
|
}
|
|
|
|
|
2015-09-19 13:19:17 +02:00
|
|
|
if (c == '\\')
|
|
|
|
{
|
|
|
|
if (was_antislash)
|
|
|
|
{
|
|
|
|
content += StringView{pos, it};
|
|
|
|
pos = it + 1;
|
|
|
|
was_antislash = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
was_antislash = true;
|
|
|
|
}
|
2016-12-07 14:43:27 +01:00
|
|
|
else
|
2015-09-20 12:23:10 +02:00
|
|
|
was_antislash = false;
|
2015-09-19 13:19:17 +02:00
|
|
|
}
|
|
|
|
content += StringView{pos, line.end()};
|
|
|
|
if (not content.empty())
|
|
|
|
res.push_back({std::move(content), face});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|