2011-09-02 20:51:20 +04:00
|
|
|
#ifndef display_buffer_hh_INCLUDED
|
|
|
|
#define display_buffer_hh_INCLUDED
|
|
|
|
|
2013-04-09 22:05:40 +04:00
|
|
|
#include "buffer.hh"
|
2012-09-17 21:01:13 +04:00
|
|
|
#include "color.hh"
|
2011-10-14 18:29:53 +04:00
|
|
|
#include "line_and_column.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
#include "string.hh"
|
2012-10-11 02:41:48 +04:00
|
|
|
#include "utf8.hh"
|
2011-09-29 00:54:11 +04:00
|
|
|
|
2013-04-09 22:05:40 +04:00
|
|
|
#include <vector>
|
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-10-11 02:41:48 +04:00
|
|
|
struct DisplayCoord : LineAndColumn<DisplayCoord, LineCount, CharCount>
|
2011-10-14 18:29:53 +04:00
|
|
|
{
|
2012-09-05 01:54:10 +04:00
|
|
|
constexpr DisplayCoord(LineCount line = 0, CharCount column = 0)
|
2011-10-14 18:29:53 +04:00
|
|
|
: LineAndColumn(line, column) {}
|
|
|
|
};
|
|
|
|
|
2012-10-24 00:43:12 +04:00
|
|
|
typedef char Attribute;
|
2011-09-02 20:51:20 +04:00
|
|
|
|
|
|
|
enum Attributes
|
|
|
|
{
|
2011-09-26 03:51:12 +04:00
|
|
|
Normal = 0,
|
|
|
|
Underline = 1,
|
|
|
|
Reverse = 2,
|
|
|
|
Blink = 4,
|
2012-07-13 01:19:10 +04:00
|
|
|
Bold = 8
|
2011-09-26 03:51:12 +04:00
|
|
|
};
|
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
struct AtomContent
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2012-07-13 01:19:10 +04:00
|
|
|
public:
|
|
|
|
enum Type { BufferRange, ReplacedBufferRange, Text };
|
|
|
|
|
2013-05-22 21:53:17 +04:00
|
|
|
AtomContent(const Buffer& buffer, BufferCoord begin, BufferCoord end)
|
|
|
|
: m_type(BufferRange), m_buffer(&buffer), m_begin(begin), m_end(end) {}
|
2012-07-13 01:19:10 +04:00
|
|
|
|
|
|
|
AtomContent(String str)
|
|
|
|
: m_type(Text), m_text(std::move(str)) {}
|
|
|
|
|
|
|
|
String content() const
|
|
|
|
{
|
|
|
|
switch (m_type)
|
|
|
|
{
|
|
|
|
case BufferRange:
|
2013-05-22 21:53:17 +04:00
|
|
|
return m_buffer->string(m_begin, m_end);
|
2012-07-13 01:19:10 +04:00
|
|
|
case Text:
|
|
|
|
case ReplacedBufferRange:
|
|
|
|
return m_text;
|
|
|
|
}
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(false);
|
2012-10-11 03:17:29 +04:00
|
|
|
return 0;
|
2012-07-13 01:19:10 +04:00
|
|
|
}
|
|
|
|
|
2012-09-30 18:21:20 +04:00
|
|
|
CharCount length() const
|
|
|
|
{
|
|
|
|
switch (m_type)
|
|
|
|
{
|
|
|
|
case BufferRange:
|
2013-05-22 21:53:17 +04:00
|
|
|
return utf8::distance(m_buffer->iterator_at(m_begin),
|
|
|
|
m_buffer->iterator_at(m_end));
|
2012-09-30 18:21:20 +04:00
|
|
|
case Text:
|
|
|
|
case ReplacedBufferRange:
|
2012-10-11 02:41:48 +04:00
|
|
|
return m_text.char_length();
|
2012-09-30 18:21:20 +04:00
|
|
|
}
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(false);
|
2012-10-11 03:17:29 +04:00
|
|
|
return 0;
|
2012-09-30 18:21:20 +04:00
|
|
|
}
|
|
|
|
|
2013-05-22 21:53:17 +04:00
|
|
|
const BufferCoord& begin() const
|
2012-07-04 01:23:07 +04:00
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(has_buffer_range());
|
2012-07-13 01:19:10 +04:00
|
|
|
return m_begin;
|
|
|
|
}
|
2011-10-15 08:45:49 +04:00
|
|
|
|
2013-05-22 21:53:17 +04:00
|
|
|
const BufferCoord& end() const
|
2012-07-13 01:19:10 +04:00
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(has_buffer_range());
|
2012-07-13 01:19:10 +04:00
|
|
|
return m_end;
|
|
|
|
}
|
2011-10-15 08:45:49 +04:00
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
void replace(String text)
|
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(m_type == BufferRange);
|
2012-07-13 01:19:10 +04:00
|
|
|
m_type = ReplacedBufferRange;
|
|
|
|
m_text = std::move(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_buffer_range() const
|
|
|
|
{
|
|
|
|
return m_type == BufferRange or m_type == ReplacedBufferRange;
|
|
|
|
}
|
2011-10-15 08:45:49 +04:00
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
Type type() const { return m_type; }
|
2011-10-17 23:01:04 +04:00
|
|
|
|
2013-06-28 02:03:11 +04:00
|
|
|
void trim_begin(CharCount count);
|
|
|
|
void trim_end(CharCount count);
|
2011-10-15 08:45:49 +04:00
|
|
|
private:
|
2012-10-08 16:28:38 +04:00
|
|
|
friend class DisplayLine;
|
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
Type m_type;
|
|
|
|
|
2013-05-22 21:53:17 +04:00
|
|
|
const Buffer* m_buffer = nullptr;
|
|
|
|
BufferCoord m_begin;
|
|
|
|
BufferCoord m_end;
|
2012-07-13 01:19:10 +04:00
|
|
|
String m_text;
|
2011-09-02 20:51:20 +04:00
|
|
|
};
|
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
struct DisplayAtom
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
2013-03-06 23:25:23 +04:00
|
|
|
ColorPair colors;
|
2012-07-13 01:19:10 +04:00
|
|
|
Attribute attribute;
|
|
|
|
AtomContent content;
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2013-04-04 20:50:00 +04:00
|
|
|
DisplayAtom(AtomContent content,
|
2013-05-07 20:52:23 +04:00
|
|
|
ColorPair colors = {Colors::Default, Colors::Default},
|
2013-04-04 20:50:00 +04:00
|
|
|
Attribute attribute = Normal)
|
|
|
|
: content{std::move(content)}, colors{colors}, attribute{attribute}
|
|
|
|
{}
|
2012-07-13 01:19:10 +04:00
|
|
|
};
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2013-07-23 22:11:26 +04:00
|
|
|
using BufferRange = std::pair<BufferCoord, BufferCoord>;
|
2013-07-24 03:34:00 +04:00
|
|
|
using AtomList = std::vector<DisplayAtom>;
|
2013-07-23 22:11:26 +04:00
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
class DisplayLine
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using iterator = AtomList::iterator;
|
|
|
|
using const_iterator = AtomList::const_iterator;
|
|
|
|
|
2013-07-23 22:11:26 +04:00
|
|
|
DisplayLine() = default;
|
|
|
|
DisplayLine(AtomList atoms);
|
2013-04-04 20:50:00 +04:00
|
|
|
DisplayLine(String str, ColorPair color)
|
2013-07-23 22:11:26 +04:00
|
|
|
{ push_back({ std::move(str), color }); }
|
2011-10-15 08:45:49 +04:00
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
iterator begin() { return m_atoms.begin(); }
|
2012-07-13 01:19:10 +04:00
|
|
|
iterator end() { return m_atoms.end(); }
|
2011-09-02 20:51:20 +04:00
|
|
|
|
|
|
|
const_iterator begin() const { return m_atoms.begin(); }
|
2012-07-13 01:19:10 +04:00
|
|
|
const_iterator end() const { return m_atoms.end(); }
|
2011-09-29 13:10:27 +04:00
|
|
|
|
2012-10-24 00:55:44 +04:00
|
|
|
const AtomList& atoms() const { return m_atoms; }
|
|
|
|
|
2013-04-04 20:50:00 +04:00
|
|
|
CharCount length() const;
|
2013-07-23 22:11:26 +04:00
|
|
|
const BufferRange& range() const { return m_range; }
|
2013-04-04 20:50:00 +04:00
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
// Split atom pointed by it at pos, returns an iterator to the first atom
|
2013-05-23 15:59:33 +04:00
|
|
|
iterator split(iterator it, BufferCoord pos);
|
2011-10-17 23:00:38 +04:00
|
|
|
|
2013-07-23 22:11:26 +04:00
|
|
|
iterator insert(iterator it, DisplayAtom atom);
|
|
|
|
void push_back(DisplayAtom atom);
|
2011-10-15 08:45:49 +04:00
|
|
|
|
2013-06-28 02:03:11 +04:00
|
|
|
// remove first_char from the begining of the line, and make sure
|
|
|
|
// the line is less that char_count character
|
|
|
|
void trim(CharCount first_char, CharCount char_count);
|
|
|
|
|
2012-10-22 15:20:02 +04:00
|
|
|
void optimize();
|
2011-09-02 20:51:20 +04:00
|
|
|
private:
|
2013-07-23 22:11:26 +04:00
|
|
|
void compute_range();
|
|
|
|
BufferRange m_range = { { INT_MAX, INT_MAX }, { INT_MIN, INT_MIN } };
|
2012-08-23 01:33:52 +04:00
|
|
|
AtomList m_atoms;
|
2011-09-02 20:51:20 +04:00
|
|
|
};
|
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
class DisplayBuffer
|
|
|
|
{
|
|
|
|
public:
|
2012-10-24 00:55:04 +04:00
|
|
|
using LineList = std::vector<DisplayLine>;
|
2012-07-13 01:19:10 +04:00
|
|
|
DisplayBuffer() {}
|
|
|
|
|
|
|
|
LineList& lines() { return m_lines; }
|
|
|
|
const LineList& lines() const { return m_lines; }
|
2012-07-13 01:51:13 +04:00
|
|
|
|
2013-05-23 15:59:33 +04:00
|
|
|
// returns the smallest BufferRange which contains every DisplayAtoms
|
2012-07-13 01:51:13 +04:00
|
|
|
const BufferRange& range() const { return m_range; }
|
2012-10-22 15:20:02 +04:00
|
|
|
void optimize();
|
2012-07-13 01:51:13 +04:00
|
|
|
void compute_range();
|
|
|
|
|
2012-07-13 01:19:10 +04:00
|
|
|
private:
|
|
|
|
LineList m_lines;
|
2012-07-13 01:51:13 +04:00
|
|
|
BufferRange m_range;
|
2012-07-13 01:19:10 +04:00
|
|
|
};
|
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // display_buffer_hh_INCLUDED
|