2014-04-28 20:48:23 +02:00
|
|
|
#ifndef buffer_utils_hh_INCLUDED
|
|
|
|
#define buffer_utils_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "buffer.hh"
|
|
|
|
#include "selection.hh"
|
|
|
|
|
2015-12-23 22:43:07 +01:00
|
|
|
#include "utf8_iterator.hh"
|
|
|
|
#include "unicode.hh"
|
|
|
|
|
2014-04-28 20:48:23 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
inline String content(const Buffer& buffer, const Selection& range)
|
|
|
|
{
|
|
|
|
return buffer.string(range.min(), buffer.char_next(range.max()));
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
inline BufferCoord erase(Buffer& buffer, const Selection& range)
|
2014-04-28 20:48:23 +02:00
|
|
|
{
|
2016-03-16 14:59:30 +01:00
|
|
|
return buffer.erase(range.min(), buffer.char_next(range.max()));
|
2014-04-28 20:48:23 +02:00
|
|
|
}
|
|
|
|
|
2019-12-18 01:36:17 +01:00
|
|
|
inline BufferRange replace(Buffer& buffer, const Selection& range, StringView content)
|
2016-03-16 14:48:11 +01:00
|
|
|
{
|
2016-03-16 14:59:30 +01:00
|
|
|
return buffer.replace(range.min(), buffer.char_next(range.max()), content);
|
2016-03-16 14:48:11 +01:00
|
|
|
}
|
|
|
|
|
2020-06-27 05:33:50 +02:00
|
|
|
void replace(Buffer& buffer, ArrayView<BufferRange> ranges, ConstArrayView<String> strings);
|
|
|
|
|
2014-04-28 20:48:23 +02:00
|
|
|
inline CharCount char_length(const Buffer& buffer, const Selection& range)
|
|
|
|
{
|
|
|
|
return utf8::distance(buffer.iterator_at(range.min()),
|
2014-09-22 14:45:07 +02:00
|
|
|
buffer.iterator_at(buffer.char_next(range.max())));
|
2014-04-28 20:48:23 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
inline CharCount char_length(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
|
2016-07-28 10:41:47 +02:00
|
|
|
{
|
|
|
|
return utf8::distance(buffer.iterator_at(begin), buffer.iterator_at(end));
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
inline ColumnCount column_length(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
|
|
|
|
{
|
|
|
|
return utf8::column_distance(buffer.iterator_at(begin), buffer.iterator_at(end));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool is_bol(BufferCoord coord)
|
2015-12-23 22:43:07 +01:00
|
|
|
{
|
|
|
|
return coord.column == 0;
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
inline bool is_eol(const Buffer& buffer, BufferCoord coord)
|
2015-12-23 22:43:07 +01:00
|
|
|
{
|
|
|
|
return buffer.is_end(coord) or buffer[coord.line].length() == coord.column+1;
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
inline bool is_bow(const Buffer& buffer, BufferCoord coord)
|
2016-02-18 00:40:14 +01:00
|
|
|
{
|
|
|
|
auto it = utf8::iterator<BufferIterator>(buffer.iterator_at(coord), buffer);
|
2016-09-22 21:36:26 +02:00
|
|
|
if (coord == BufferCoord{0,0})
|
2016-02-18 00:40:14 +01:00
|
|
|
return is_word(*it);
|
|
|
|
|
|
|
|
return not is_word(*(it-1)) and is_word(*it);
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
inline bool is_eow(const Buffer& buffer, BufferCoord coord)
|
2015-12-23 22:43:07 +01:00
|
|
|
{
|
2016-09-22 21:36:26 +02:00
|
|
|
if (buffer.is_end(coord) or coord == BufferCoord{0,0})
|
2018-10-08 03:35:24 +02:00
|
|
|
return false;
|
2015-12-23 22:43:07 +01:00
|
|
|
|
|
|
|
auto it = utf8::iterator<BufferIterator>(buffer.iterator_at(coord), buffer);
|
|
|
|
return is_word(*(it-1)) and not is_word(*it);
|
|
|
|
}
|
|
|
|
|
2019-11-13 01:45:48 +01:00
|
|
|
ColumnCount get_column(const Buffer& buffer, ColumnCount tabstop, BufferCoord coord);
|
|
|
|
ColumnCount column_length(const Buffer& buffer, ColumnCount tabstop, LineCount line);
|
2014-04-28 20:48:23 +02:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
ByteCount get_byte_to_column(const Buffer& buffer, ColumnCount tabstop,
|
|
|
|
DisplayCoord coord);
|
2015-02-25 14:40:19 +01:00
|
|
|
|
2017-03-08 20:33:25 +01:00
|
|
|
Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll = false);
|
2021-05-28 09:03:06 +02:00
|
|
|
Buffer* create_buffer_from_string(String name, Buffer::Flags flags, StringView data);
|
2016-11-14 14:59:33 +01:00
|
|
|
Buffer* open_file_buffer(StringView filename,
|
|
|
|
Buffer::Flags flags = Buffer::Flags::None);
|
|
|
|
Buffer* open_or_create_file_buffer(StringView filename,
|
|
|
|
Buffer::Flags flags = Buffer::Flags::None);
|
2015-10-16 14:58:56 +02:00
|
|
|
void reload_file_buffer(Buffer& buffer);
|
2014-08-15 14:21:54 +02:00
|
|
|
|
2015-06-06 12:54:48 +02:00
|
|
|
void write_to_debug_buffer(StringView str);
|
|
|
|
|
2020-03-01 03:28:06 +01:00
|
|
|
Vector<String> history_as_strings(const Vector<Buffer::HistoryNode>& history);
|
|
|
|
Vector<String> undo_group_as_strings(const Buffer::UndoGroup& undo_group);
|
2018-12-31 18:40:49 +01:00
|
|
|
|
2020-05-10 07:21:49 +02:00
|
|
|
String generate_buffer_name(StringView pattern);
|
|
|
|
|
2014-04-28 20:48:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // buffer_utils_hh_INCLUDED
|