2011-09-02 20:51:20 +04:00
|
|
|
#ifndef buffer_hh_INCLUDED
|
|
|
|
#define buffer_hh_INCLUDED
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
#include "coord.hh"
|
2014-10-23 21:55:45 +04:00
|
|
|
#include "flags.hh"
|
2014-08-12 03:30:13 +04:00
|
|
|
#include "safe_ptr.hh"
|
2014-10-30 17:00:42 +03:00
|
|
|
#include "scope.hh"
|
2015-01-15 16:54:38 +03:00
|
|
|
#include "shared_string.hh"
|
2014-01-10 01:01:29 +04:00
|
|
|
#include "value.hh"
|
2015-01-12 16:58:41 +03:00
|
|
|
#include "vector.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-12-06 15:51:55 +03:00
|
|
|
enum class EolFormat
|
|
|
|
{
|
|
|
|
Lf,
|
|
|
|
Crlf
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr Array<EnumDesc<EolFormat>, 2> enum_desc(EolFormat)
|
|
|
|
{
|
|
|
|
return { {
|
|
|
|
{ EolFormat::Lf, "lf" },
|
|
|
|
{ EolFormat::Crlf, "crlf" },
|
|
|
|
} };
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class ByteOrderMark
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
Utf8
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr Array<EnumDesc<ByteOrderMark>, 2> enum_desc(ByteOrderMark)
|
|
|
|
{
|
|
|
|
return { {
|
|
|
|
{ ByteOrderMark::None, "none" },
|
|
|
|
{ ByteOrderMark::Utf8, "utf8" },
|
|
|
|
} };
|
|
|
|
}
|
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
class Buffer;
|
2011-09-08 04:13:19 +04:00
|
|
|
|
2015-09-27 13:55:34 +03:00
|
|
|
constexpr timespec InvalidTime = { -1, -1 };
|
2013-10-17 21:47:09 +04:00
|
|
|
|
2012-01-11 18:21:58 +04:00
|
|
|
// A BufferIterator permits to iterate over the characters of a buffer
|
2011-09-02 20:51:20 +04:00
|
|
|
class BufferIterator
|
|
|
|
{
|
|
|
|
public:
|
2014-01-09 23:50:01 +04:00
|
|
|
using value_type = char;
|
|
|
|
using difference_type = size_t;
|
|
|
|
using pointer = const value_type*;
|
|
|
|
using reference = const value_type&;
|
2015-11-04 03:58:56 +03:00
|
|
|
using iterator_category = std::bidirectional_iterator_tag;
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2012-04-04 17:56:19 +04:00
|
|
|
BufferIterator() : m_buffer(nullptr) {}
|
2014-05-07 22:51:01 +04:00
|
|
|
BufferIterator(const Buffer& buffer, ByteCoord coord);
|
2011-09-02 20:51:20 +04:00
|
|
|
|
|
|
|
bool operator== (const BufferIterator& iterator) const;
|
|
|
|
bool operator!= (const BufferIterator& iterator) const;
|
|
|
|
bool operator< (const BufferIterator& iterator) const;
|
|
|
|
bool operator<= (const BufferIterator& iterator) const;
|
2011-09-29 00:53:29 +04:00
|
|
|
bool operator> (const BufferIterator& iterator) const;
|
|
|
|
bool operator>= (const BufferIterator& iterator) const;
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2016-01-27 11:27:23 +03:00
|
|
|
const char& operator* () const;
|
|
|
|
const char& operator[](size_t n) const;
|
2012-08-24 01:56:35 +04:00
|
|
|
size_t operator- (const BufferIterator& iterator) const;
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2012-10-11 02:41:48 +04:00
|
|
|
BufferIterator operator+ (ByteCount size) const;
|
|
|
|
BufferIterator operator- (ByteCount size) const;
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2012-10-11 02:41:48 +04:00
|
|
|
BufferIterator& operator+= (ByteCount size);
|
|
|
|
BufferIterator& operator-= (ByteCount size);
|
2011-09-02 20:51:20 +04:00
|
|
|
|
|
|
|
BufferIterator& operator++ ();
|
|
|
|
BufferIterator& operator-- ();
|
|
|
|
|
2012-10-02 16:09:06 +04:00
|
|
|
BufferIterator operator++ (int);
|
|
|
|
BufferIterator operator-- (int);
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
const ByteCoord& coord() const { return m_coord; }
|
2012-03-30 15:37:18 +04:00
|
|
|
|
2012-11-22 21:54:37 +04:00
|
|
|
private:
|
2015-02-19 16:58:25 +03:00
|
|
|
SafePtr<const Buffer> m_buffer;
|
2014-05-07 22:51:01 +04:00
|
|
|
ByteCoord m_coord;
|
2015-11-07 19:55:48 +03:00
|
|
|
ByteCount m_line_length;
|
2015-11-12 16:59:36 +03:00
|
|
|
LineCount m_last_line;
|
2011-09-02 20:51:20 +04:00
|
|
|
};
|
|
|
|
|
2015-03-01 15:06:19 +03:00
|
|
|
using BufferLines = Vector<StringDataPtr, MemoryDomain::BufferContent>;
|
2015-01-22 16:39:29 +03:00
|
|
|
|
2012-01-11 18:21:58 +04:00
|
|
|
// A Buffer is a in-memory representation of a file
|
|
|
|
//
|
|
|
|
// The Buffer class permits to read and mutate this file
|
|
|
|
// representation. It also manage modifications undo/redo and
|
|
|
|
// provides tools to deal with the line/column nature of text.
|
2014-10-30 17:00:42 +03:00
|
|
|
class Buffer : public SafeCountable, public OptionManagerWatcher, public Scope
|
2011-09-02 20:51:20 +04:00
|
|
|
{
|
|
|
|
public:
|
2012-11-20 22:47:56 +04:00
|
|
|
enum class Flags
|
2011-10-07 18:15:55 +04:00
|
|
|
{
|
2012-11-20 22:47:56 +04:00
|
|
|
None = 0,
|
2016-03-12 19:44:55 +03:00
|
|
|
File = 1 << 0,
|
|
|
|
New = 1 << 1,
|
|
|
|
Fifo = 1 << 2,
|
|
|
|
NoUndo = 1 << 3,
|
|
|
|
Debug = 1 << 4
|
2011-10-07 18:15:55 +04:00
|
|
|
};
|
|
|
|
|
2015-10-16 15:52:14 +03:00
|
|
|
Buffer(String name, Flags flags, StringView data = {},
|
2015-09-27 13:55:34 +03:00
|
|
|
timespec fs_timestamp = InvalidTime);
|
2011-10-24 18:23:13 +04:00
|
|
|
Buffer(const Buffer&) = delete;
|
|
|
|
Buffer& operator= (const Buffer&) = delete;
|
|
|
|
~Buffer();
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2012-11-20 22:47:56 +04:00
|
|
|
Flags flags() const { return m_flags; }
|
|
|
|
Flags& flags() { return m_flags; }
|
2011-09-06 22:49:32 +04:00
|
|
|
|
2013-04-22 15:48:18 +04:00
|
|
|
bool set_name(String name);
|
2015-12-01 16:42:42 +03:00
|
|
|
void update_display_name();
|
2013-04-22 15:48:18 +04:00
|
|
|
|
2016-03-16 16:59:30 +03:00
|
|
|
ByteCoord insert(ByteCoord pos, StringView content);
|
|
|
|
ByteCoord erase(ByteCoord begin, ByteCoord end);
|
|
|
|
ByteCoord replace(ByteCoord begin, ByteCoord end, StringView content);
|
2011-12-07 18:26:40 +04:00
|
|
|
|
2014-04-01 21:54:46 +04:00
|
|
|
size_t timestamp() const;
|
2015-09-27 13:55:34 +03:00
|
|
|
timespec fs_timestamp() const;
|
|
|
|
void set_fs_timestamp(timespec ts);
|
2012-08-15 19:07:53 +04:00
|
|
|
|
2013-02-20 17:20:16 +04:00
|
|
|
void commit_undo_group();
|
2011-09-06 22:49:32 +04:00
|
|
|
bool undo();
|
|
|
|
bool redo();
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
String string(ByteCoord begin, ByteCoord end) const;
|
2013-06-03 20:56:48 +04:00
|
|
|
|
2016-01-27 11:27:23 +03:00
|
|
|
const char& byte_at(ByteCoord c) const;
|
2014-05-07 22:51:01 +04:00
|
|
|
ByteCount distance(ByteCoord begin, ByteCoord end) const;
|
2014-05-10 19:25:07 +04:00
|
|
|
ByteCoord advance(ByteCoord coord, ByteCount count) const;
|
|
|
|
ByteCoord next(ByteCoord coord) const;
|
|
|
|
ByteCoord prev(ByteCoord coord) const;
|
2013-06-03 20:56:48 +04:00
|
|
|
|
2014-05-10 19:25:07 +04:00
|
|
|
ByteCoord char_next(ByteCoord coord) const;
|
|
|
|
ByteCoord char_prev(ByteCoord coord) const;
|
2013-06-03 20:56:48 +04:00
|
|
|
|
2014-05-10 19:25:07 +04:00
|
|
|
ByteCoord back_coord() const;
|
|
|
|
ByteCoord end_coord() const;
|
2013-06-04 21:23:11 +04:00
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
bool is_valid(ByteCoord c) const;
|
|
|
|
bool is_end(ByteCoord c) const;
|
2013-04-23 20:46:18 +04:00
|
|
|
|
2014-05-10 19:25:07 +04:00
|
|
|
ByteCoord last_modification_coord() const;
|
2014-04-08 02:39:12 +04:00
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
BufferIterator begin() const;
|
|
|
|
BufferIterator end() const;
|
2012-08-23 01:33:52 +04:00
|
|
|
LineCount line_count() const;
|
2013-06-05 20:41:02 +04:00
|
|
|
|
2015-01-19 22:31:56 +03:00
|
|
|
StringView operator[](LineCount line) const
|
2014-05-24 20:08:01 +04:00
|
|
|
{ return m_lines[line]; }
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2015-11-07 19:55:20 +03:00
|
|
|
const StringDataPtr& line_storage(LineCount line) const
|
2015-01-27 16:11:32 +03:00
|
|
|
{ return m_lines.get_storage(line); }
|
2015-01-19 22:31:56 +03:00
|
|
|
|
2013-05-30 16:17:19 +04:00
|
|
|
// returns an iterator at given coordinates. clamp line_and_column
|
2014-05-07 22:51:01 +04:00
|
|
|
BufferIterator iterator_at(ByteCoord coord) const;
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2011-12-05 23:21:11 +04:00
|
|
|
// returns nearest valid coordinates from given ones
|
2014-05-10 19:25:07 +04:00
|
|
|
ByteCoord clamp(ByteCoord coord) const;
|
2011-09-02 20:51:20 +04:00
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
ByteCoord offset_coord(ByteCoord coord, CharCount offset);
|
2014-09-09 22:35:54 +04:00
|
|
|
ByteCoordAndTarget offset_coord(ByteCoordAndTarget coord, LineCount offset);
|
2013-12-15 18:14:52 +04:00
|
|
|
|
2012-11-22 21:54:37 +04:00
|
|
|
const String& name() const { return m_name; }
|
2015-09-01 15:59:40 +03:00
|
|
|
const String& display_name() const { return m_display_name; }
|
2012-11-22 21:54:37 +04:00
|
|
|
|
|
|
|
// returns true if the buffer is in a different state than
|
|
|
|
// the last time it was saved
|
|
|
|
bool is_modified() const;
|
|
|
|
|
|
|
|
// notify the buffer that it was saved in the current state
|
|
|
|
void notify_saved();
|
2012-03-30 15:37:18 +04:00
|
|
|
|
2014-01-10 01:01:29 +04:00
|
|
|
ValueMap& values() const { return m_values; }
|
|
|
|
|
2015-03-05 17:59:27 +03:00
|
|
|
void run_hook_in_own_context(StringView hook_name, StringView param);
|
2013-12-11 17:57:10 +04:00
|
|
|
|
2015-10-16 15:52:14 +03:00
|
|
|
void reload(StringView data, timespec fs_timestamp = InvalidTime);
|
2013-10-21 21:57:19 +04:00
|
|
|
|
2012-03-30 15:37:18 +04:00
|
|
|
void check_invariant() const;
|
2014-05-11 15:20:59 +04:00
|
|
|
|
|
|
|
struct Change
|
|
|
|
{
|
2015-01-30 01:44:07 +03:00
|
|
|
enum Type : char { Insert, Erase };
|
2014-05-11 15:20:59 +04:00
|
|
|
Type type;
|
2015-01-30 01:44:07 +03:00
|
|
|
bool at_end;
|
2014-05-11 15:20:59 +04:00
|
|
|
ByteCoord begin;
|
|
|
|
ByteCoord end;
|
|
|
|
};
|
2015-03-09 16:48:41 +03:00
|
|
|
ConstArrayView<Change> changes_since(size_t timestamp) const;
|
2014-09-22 22:19:34 +04:00
|
|
|
|
|
|
|
String debug_description() const;
|
2013-04-03 21:22:12 +04:00
|
|
|
private:
|
2013-11-13 00:36:42 +04:00
|
|
|
|
|
|
|
void on_option_changed(const Option& option) override;
|
|
|
|
|
2015-06-25 15:36:23 +03:00
|
|
|
ByteCoord do_insert(ByteCoord pos, StringView content);
|
|
|
|
ByteCoord do_erase(ByteCoord begin, ByteCoord end);
|
|
|
|
|
|
|
|
struct Modification;
|
|
|
|
|
|
|
|
void apply_modification(const Modification& modification);
|
|
|
|
void revert_modification(const Modification& modification);
|
|
|
|
|
2015-01-22 16:39:29 +03:00
|
|
|
struct LineList : BufferLines
|
2012-03-30 15:37:18 +04:00
|
|
|
{
|
2014-07-19 03:18:16 +04:00
|
|
|
[[gnu::always_inline]]
|
2015-03-01 15:06:19 +03:00
|
|
|
StringDataPtr& get_storage(LineCount line)
|
2015-01-22 16:39:29 +03:00
|
|
|
{ return BufferLines::operator[]((int)line); }
|
2012-03-30 16:00:40 +04:00
|
|
|
|
2014-07-19 03:18:16 +04:00
|
|
|
[[gnu::always_inline]]
|
2015-03-01 15:06:19 +03:00
|
|
|
const StringDataPtr& get_storage(LineCount line) const
|
2015-01-22 16:39:29 +03:00
|
|
|
{ return BufferLines::operator[]((int)line); }
|
2015-01-19 22:31:56 +03:00
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
StringView operator[](LineCount line) const
|
|
|
|
{ return get_storage(line)->strview(); }
|
|
|
|
|
2015-01-22 16:39:29 +03:00
|
|
|
StringView front() const { return BufferLines::front()->strview(); }
|
|
|
|
StringView back() const { return BufferLines::back()->strview(); }
|
2012-08-23 01:33:52 +04:00
|
|
|
};
|
|
|
|
LineList m_lines;
|
2012-03-30 15:37:18 +04:00
|
|
|
|
2015-07-27 22:43:18 +03:00
|
|
|
String m_name;
|
2015-09-01 15:59:40 +03:00
|
|
|
String m_display_name;
|
2015-07-27 22:43:18 +03:00
|
|
|
Flags m_flags;
|
2011-09-06 22:49:32 +04:00
|
|
|
|
2015-01-18 14:22:28 +03:00
|
|
|
using UndoGroup = Vector<Modification, MemoryDomain::BufferMeta>;
|
2015-06-25 15:36:23 +03:00
|
|
|
using History = Vector<UndoGroup, MemoryDomain::BufferMeta>;
|
2011-09-06 22:49:32 +04:00
|
|
|
|
2015-01-10 15:56:09 +03:00
|
|
|
History m_history;
|
|
|
|
History::iterator m_history_cursor;
|
|
|
|
UndoGroup m_current_undo_group;
|
2011-09-06 22:49:32 +04:00
|
|
|
|
2011-11-03 17:44:02 +04:00
|
|
|
size_t m_last_save_undo_index;
|
2014-05-11 15:20:59 +04:00
|
|
|
|
2015-01-10 15:56:09 +03:00
|
|
|
Vector<Change, MemoryDomain::BufferMeta> m_changes;
|
2011-10-18 02:05:06 +04:00
|
|
|
|
2015-09-27 13:55:34 +03:00
|
|
|
timespec m_fs_timestamp;
|
2013-10-15 21:51:31 +04:00
|
|
|
|
2015-06-25 15:36:23 +03:00
|
|
|
// Values are just data holding by the buffer, they are not part of its
|
2014-01-10 01:01:29 +04:00
|
|
|
// observable state
|
|
|
|
mutable ValueMap m_values;
|
2013-08-01 02:28:01 +04:00
|
|
|
};
|
2012-11-20 22:47:56 +04:00
|
|
|
|
2014-10-23 21:55:45 +04:00
|
|
|
template<> struct WithBitOps<Buffer::Flags> : std::true_type {};
|
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
}
|
|
|
|
|
2014-01-12 21:19:05 +04:00
|
|
|
#include "buffer.inl.hh"
|
2011-10-18 02:05:06 +04:00
|
|
|
|
2011-09-02 20:51:20 +04:00
|
|
|
#endif // buffer_hh_INCLUDED
|