1 #ifndef buffer_utils_hh_INCLUDED
2 #define buffer_utils_hh_INCLUDED
3 
4 #include "buffer.hh"
5 #include "selection.hh"
6 
7 #include "utf8_iterator.hh"
8 #include "unicode.hh"
9 
10 namespace Kakoune
11 {
12 
content(const Buffer & buffer,const Selection & range)13 inline String content(const Buffer& buffer, const Selection& range)
14 {
15     return buffer.string(range.min(), buffer.char_next(range.max()));
16 }
17 
erase(Buffer & buffer,const Selection & range)18 inline BufferCoord erase(Buffer& buffer, const Selection& range)
19 {
20     return buffer.erase(range.min(), buffer.char_next(range.max()));
21 }
22 
23 void replace(Buffer& buffer, ArrayView<BufferRange> ranges, ConstArrayView<String> strings);
24 
char_length(const Buffer & buffer,const Selection & range)25 inline CharCount char_length(const Buffer& buffer, const Selection& range)
26 {
27     return utf8::distance(buffer.iterator_at(range.min()),
28                           buffer.iterator_at(buffer.char_next(range.max())));
29 }
30 
char_length(const Buffer & buffer,const BufferCoord & begin,const BufferCoord & end)31 inline CharCount char_length(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
32 {
33     return utf8::distance(buffer.iterator_at(begin), buffer.iterator_at(end));
34 }
35 
column_length(const Buffer & buffer,const BufferCoord & begin,const BufferCoord & end)36 inline ColumnCount column_length(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
37 {
38     return utf8::column_distance(buffer.iterator_at(begin), buffer.iterator_at(end));
39 }
40 
is_bol(BufferCoord coord)41 inline bool is_bol(BufferCoord coord)
42 {
43     return coord.column == 0;
44 }
45 
is_eol(const Buffer & buffer,BufferCoord coord)46 inline bool is_eol(const Buffer& buffer, BufferCoord coord)
47 {
48     return buffer.is_end(coord) or buffer[coord.line].length() == coord.column+1;
49 }
50 
is_bow(const Buffer & buffer,BufferCoord coord)51 inline bool is_bow(const Buffer& buffer, BufferCoord coord)
52 {
53     auto it = utf8::iterator<BufferIterator>(buffer.iterator_at(coord), buffer);
54     if (coord == BufferCoord{0,0})
55         return is_word(*it);
56 
57     return not is_word(*(it-1)) and is_word(*it);
58 }
59 
is_eow(const Buffer & buffer,BufferCoord coord)60 inline bool is_eow(const Buffer& buffer, BufferCoord coord)
61 {
62     if (buffer.is_end(coord) or coord == BufferCoord{0,0})
63         return false;
64 
65     auto it = utf8::iterator<BufferIterator>(buffer.iterator_at(coord), buffer);
66     return is_word(*(it-1)) and not is_word(*it);
67 }
68 
69 ColumnCount get_column(const Buffer& buffer, ColumnCount tabstop, BufferCoord coord);
70 ColumnCount column_length(const Buffer& buffer, ColumnCount tabstop, LineCount line);
71 
72 ByteCount get_byte_to_column(const Buffer& buffer, ColumnCount tabstop,
73                              DisplayCoord coord);
74 
75 Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll = false);
76 Buffer* create_buffer_from_string(String name, Buffer::Flags flags, StringView data);
77 Buffer* open_file_buffer(StringView filename,
78                          Buffer::Flags flags = Buffer::Flags::None);
79 Buffer* open_or_create_file_buffer(StringView filename,
80                                    Buffer::Flags flags = Buffer::Flags::None);
81 void reload_file_buffer(Buffer& buffer);
82 
83 void write_to_debug_buffer(StringView str);
84 
85 Vector<String> history_as_strings(const Vector<Buffer::HistoryNode>& history);
86 Vector<String> undo_group_as_strings(const Buffer::UndoGroup& undo_group);
87 
88 String generate_buffer_name(StringView pattern);
89 
90 }
91 
92 #endif // buffer_utils_hh_INCLUDED
93