summaryrefslogtreecommitdiff
path: root/src/buffer_utils.hh
blob: bd6175954f5afdf3e7584927e0ed0ca553a7c347 (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
#ifndef buffer_utils_hh_INCLUDED
#define buffer_utils_hh_INCLUDED

#include "buffer.hh"
#include "selection.hh"

namespace Kakoune
{

inline String content(const Buffer& buffer, const Selection& range)
{
    return buffer.string(range.min(), buffer.char_next(range.max()));
}

inline BufferIterator erase(Buffer& buffer, const Selection& range)
{
    return buffer.erase(buffer.iterator_at(range.min()),
                        utf8::next(buffer.iterator_at(range.max())));
}

inline CharCount char_length(const Buffer& buffer, const Selection& range)
{
    return utf8::distance(buffer.iterator_at(range.min()),
                          utf8::next(buffer.iterator_at(range.max())));
}

inline void avoid_eol(const Buffer& buffer, ByteCoord& coord)
{
    const auto column = coord.column;
    const auto& line = buffer[coord.line];
    if (column != 0 and column == line.length() - 1)
        coord.column = line.byte_count_to(line.char_length() - 2);
}

inline void avoid_eol(const Buffer& buffer, Selection& sel)
{
    avoid_eol(buffer, sel.anchor());
    avoid_eol(buffer, sel.cursor());
}

CharCount get_column(const Buffer& buffer,
                     CharCount tabstop, ByteCoord coord);

Buffer* create_fifo_buffer(String name, int fd, bool scroll = false);

}

#endif // buffer_utils_hh_INCLUDED