2012-01-31 20:12:06 +01:00
|
|
|
#ifndef selection_hh_INCLUDED
|
|
|
|
#define selection_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "buffer.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-09-12 11:51:16 +02:00
|
|
|
using CaptureList = Vector<String, MemoryDomain::Selections>;
|
2014-03-29 09:55:45 +01:00
|
|
|
|
|
|
|
// A selection is a Selection, associated with a CaptureList
|
|
|
|
struct Selection
|
2012-01-31 20:12:06 +01:00
|
|
|
{
|
2015-01-16 14:58:21 +01:00
|
|
|
static constexpr MemoryDomain Domain = MemoryDomain::Selections;
|
|
|
|
|
2014-04-02 23:33:52 +02:00
|
|
|
Selection() = default;
|
2016-09-22 21:36:26 +02:00
|
|
|
Selection(BufferCoord pos) : Selection(pos,pos) {}
|
|
|
|
Selection(BufferCoord anchor, BufferCoord cursor,
|
2014-03-29 09:55:45 +01:00
|
|
|
CaptureList captures = {})
|
|
|
|
: m_anchor{anchor}, m_cursor{cursor},
|
|
|
|
m_captures(std::move(captures)) {}
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord& anchor() { return m_anchor; }
|
|
|
|
BufferCoordAndTarget& cursor() { return m_cursor; }
|
2012-11-30 18:32:49 +01:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
const BufferCoord& anchor() const { return m_anchor; }
|
|
|
|
const BufferCoordAndTarget& cursor() const { return m_cursor; }
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2017-01-13 01:17:31 +01:00
|
|
|
void set(BufferCoord anchor, BufferCoord cursor)
|
|
|
|
{
|
|
|
|
m_anchor = anchor;
|
|
|
|
m_cursor = cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(BufferCoord coord) { set(coord, coord); }
|
|
|
|
|
2014-03-29 09:55:45 +01:00
|
|
|
CaptureList& captures() { return m_captures; }
|
|
|
|
const CaptureList& captures() const { return m_captures; }
|
|
|
|
|
|
|
|
bool operator== (const Selection& other) const
|
2012-12-11 19:51:59 +01:00
|
|
|
{
|
2014-01-28 20:05:49 +01:00
|
|
|
return m_anchor == other.m_anchor and m_cursor == other.m_cursor;
|
2012-12-11 19:51:59 +01:00
|
|
|
}
|
|
|
|
|
2017-07-14 05:42:43 +02:00
|
|
|
// When selections are single char, we want the anchor to be considered min, and cursor max
|
|
|
|
const BufferCoord& min() const { return m_anchor <= m_cursor ? m_anchor : m_cursor; }
|
|
|
|
const BufferCoord& max() const { return m_anchor <= m_cursor ? m_cursor : m_anchor; }
|
2014-05-29 06:48:40 +02:00
|
|
|
|
2017-07-14 05:42:43 +02:00
|
|
|
BufferCoord& min() { return m_anchor <= m_cursor ? m_anchor : m_cursor; }
|
|
|
|
BufferCoord& max() { return m_anchor <= m_cursor ? m_cursor : m_anchor; }
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2012-11-30 18:32:49 +01:00
|
|
|
private:
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord m_anchor;
|
|
|
|
BufferCoordAndTarget m_cursor;
|
2014-03-29 09:55:45 +01:00
|
|
|
|
|
|
|
CaptureList m_captures;
|
2012-11-30 18:32:49 +01:00
|
|
|
};
|
2012-01-31 20:12:06 +01:00
|
|
|
|
2014-03-29 09:55:45 +01:00
|
|
|
inline bool overlaps(const Selection& lhs, const Selection& rhs)
|
2012-12-11 14:11:33 +01:00
|
|
|
{
|
2013-05-24 14:25:50 +02:00
|
|
|
return lhs.min() <= rhs.min() ? lhs.max() >= rhs.min()
|
|
|
|
: lhs.min() <= rhs.max();
|
2013-05-03 13:53:23 +02:00
|
|
|
}
|
|
|
|
|
2016-07-08 10:52:10 +02:00
|
|
|
void update_selections(Vector<Selection>& selections, size_t& main,
|
|
|
|
Buffer& buffer, size_t timestamp);
|
|
|
|
|
2014-05-25 21:28:32 +02:00
|
|
|
enum class InsertMode : unsigned
|
|
|
|
{
|
|
|
|
Insert,
|
2014-05-25 23:59:29 +02:00
|
|
|
InsertCursor,
|
2014-05-25 21:28:32 +02:00
|
|
|
Append,
|
|
|
|
Replace,
|
|
|
|
InsertAtLineBegin,
|
|
|
|
InsertAtNextLineBegin,
|
|
|
|
AppendAtLineEnd,
|
|
|
|
OpenLineBelow,
|
|
|
|
OpenLineAbove
|
|
|
|
};
|
|
|
|
|
2014-05-11 20:44:19 +02:00
|
|
|
struct SelectionList
|
2013-05-02 19:04:59 +02:00
|
|
|
{
|
2015-01-16 14:58:21 +01:00
|
|
|
static constexpr MemoryDomain Domain = MemoryDomain::Selections;
|
|
|
|
|
2014-05-25 21:28:32 +02:00
|
|
|
SelectionList(Buffer& buffer, Selection s);
|
|
|
|
SelectionList(Buffer& buffer, Selection s, size_t timestamp);
|
2015-01-12 14:58:41 +01:00
|
|
|
SelectionList(Buffer& buffer, Vector<Selection> s);
|
|
|
|
SelectionList(Buffer& buffer, Vector<Selection> s, size_t timestamp);
|
2013-05-02 19:04:59 +02:00
|
|
|
|
2014-05-13 00:25:15 +02:00
|
|
|
void update();
|
|
|
|
|
2013-05-03 13:38:48 +02:00
|
|
|
void check_invariant() const;
|
2013-12-13 00:17:06 +01:00
|
|
|
|
|
|
|
const Selection& main() const { return (*this)[m_main]; }
|
|
|
|
Selection& main() { return (*this)[m_main]; }
|
|
|
|
size_t main_index() const { return m_main; }
|
|
|
|
void set_main_index(size_t main) { kak_assert(main < size()); m_main = main; }
|
|
|
|
|
2014-05-14 21:56:27 +02:00
|
|
|
void avoid_eol();
|
|
|
|
|
2014-05-11 20:44:19 +02:00
|
|
|
void push_back(const Selection& sel) { m_selections.push_back(sel); }
|
|
|
|
void push_back(Selection&& sel) { m_selections.push_back(std::move(sel)); }
|
|
|
|
|
|
|
|
Selection& operator[](size_t i) { return m_selections[i]; }
|
|
|
|
const Selection& operator[](size_t i) const { return m_selections[i]; }
|
|
|
|
|
2017-03-15 17:51:55 +01:00
|
|
|
void set(Vector<Selection> list, size_t main);
|
2015-01-12 14:58:41 +01:00
|
|
|
SelectionList& operator=(Vector<Selection> list)
|
2014-05-14 00:22:54 +02:00
|
|
|
{
|
2017-01-04 00:17:08 +01:00
|
|
|
const size_t main_index = list.size()-1;
|
|
|
|
set(std::move(list), main_index);
|
2017-01-03 19:41:45 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-01-12 14:58:41 +01:00
|
|
|
using iterator = Vector<Selection>::iterator;
|
2014-05-11 20:44:19 +02:00
|
|
|
iterator begin() { return m_selections.begin(); }
|
|
|
|
iterator end() { return m_selections.end(); }
|
|
|
|
|
2015-01-12 14:58:41 +01:00
|
|
|
using const_iterator = Vector<Selection>::const_iterator;
|
2014-05-11 20:44:19 +02:00
|
|
|
const_iterator begin() const { return m_selections.begin(); }
|
|
|
|
const_iterator end() const { return m_selections.end(); }
|
|
|
|
|
2017-03-03 19:47:11 +01:00
|
|
|
void remove(size_t index);
|
2014-05-11 20:44:19 +02:00
|
|
|
|
|
|
|
size_t size() const { return m_selections.size(); }
|
|
|
|
|
2014-05-13 00:25:15 +02:00
|
|
|
bool operator==(const SelectionList& other) const { return m_buffer == other.m_buffer and m_selections == other.m_selections; }
|
2015-12-28 00:28:34 +01:00
|
|
|
bool operator!=(const SelectionList& other) const { return not ((*this) == other); }
|
2014-05-11 20:44:19 +02:00
|
|
|
|
2016-07-30 16:32:47 +02:00
|
|
|
void sort();
|
|
|
|
void merge_overlapping();
|
2016-08-27 12:19:07 +02:00
|
|
|
void merge_consecutive();
|
2014-05-13 00:25:15 +02:00
|
|
|
void sort_and_merge_overlapping();
|
|
|
|
|
2014-05-25 21:28:32 +02:00
|
|
|
Buffer& buffer() const { return *m_buffer; }
|
2014-05-13 00:25:15 +02:00
|
|
|
|
|
|
|
size_t timestamp() const { return m_timestamp; }
|
2014-05-17 13:13:49 +02:00
|
|
|
void update_timestamp() { m_timestamp = m_buffer->timestamp(); }
|
2013-12-13 00:17:06 +01:00
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
void insert(ConstArrayView<String> strings, InsertMode mode,
|
2017-01-01 18:31:47 +01:00
|
|
|
Vector<BufferCoord>* out_insert_pos = nullptr);
|
2014-05-25 21:28:32 +02:00
|
|
|
void erase();
|
|
|
|
|
2013-12-13 00:17:06 +01:00
|
|
|
private:
|
|
|
|
size_t m_main = 0;
|
2015-01-12 14:58:41 +01:00
|
|
|
Vector<Selection> m_selections;
|
2014-05-13 00:25:15 +02:00
|
|
|
|
2015-02-19 14:58:25 +01:00
|
|
|
SafePtr<Buffer> m_buffer;
|
2014-05-13 00:25:15 +02:00
|
|
|
size_t m_timestamp;
|
2013-05-02 19:04:59 +02:00
|
|
|
};
|
2012-02-10 00:47:55 +01:00
|
|
|
|
2015-01-12 14:58:41 +01:00
|
|
|
Vector<Selection> compute_modified_ranges(Buffer& buffer, size_t timestamp);
|
2014-05-14 00:22:54 +02:00
|
|
|
|
2015-06-25 15:00:50 +02:00
|
|
|
String selection_to_string(const Selection& selection);
|
2015-04-13 16:21:26 +02:00
|
|
|
String selection_list_to_string(const SelectionList& selection);
|
2015-06-25 15:00:50 +02:00
|
|
|
Selection selection_from_string(StringView desc);
|
2015-04-13 16:21:26 +02:00
|
|
|
SelectionList selection_list_from_string(Buffer& buffer, StringView desc);
|
|
|
|
|
2012-01-31 20:12:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // selection_hh_INCLUDED
|