summaryrefslogtreecommitdiff
path: root/src/selection.hh
blob: dbc19fc05003965454c56d071c85c1dde09a8125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef selection_hh_INCLUDED
#define selection_hh_INCLUDED

#include "buffer.hh"

namespace Kakoune
{

// A Selection holds a buffer range
//
// The Selection class manage a (first, last) buffer iterators pair.
// Selections are oriented, first may be > last, and inclusive.
// Selection updates it's iterators according to modifications made
// in the buffer.
struct Selection : public ModificationListener
{
    Selection(const BufferIterator& first, const BufferIterator& last);
    Selection(const Selection& other);
    ~Selection();

    Selection& operator=(const Selection& other);

    // returns min(first, last)
    BufferIterator begin() const;
    // returns max(first, last) + 1
    BufferIterator end() const;

    const BufferIterator& first() const { return m_first; }
    const BufferIterator& last()  const { return m_last; }

    void merge_with(const Selection& selection);

private:
    BufferIterator m_first;
    BufferIterator m_last;

    void on_modification(const Modification& modification);

    void register_with_buffer();
    void unregister_with_buffer();
};

typedef std::vector<Selection> SelectionList;
typedef std::vector<BufferString> CaptureList;

// Selections are often associated with a capture list
// like when they are created from a regex match with
// capture groups.
struct SelectionAndCaptures
{
    Selection   selection;
    CaptureList captures;

    SelectionAndCaptures(const BufferIterator& first,
                         const BufferIterator& last,
                         CaptureList&& captures_list)
        : selection(first, last), captures(captures_list) {}
    SelectionAndCaptures(const Selection& sel)
        : selection(sel) {}
    SelectionAndCaptures(Selection&& sel)
        : selection(sel) {}
};

typedef std::vector<SelectionAndCaptures> SelectionAndCapturesList;

}

#endif // selection_hh_INCLUDED