2011-09-02 18:51:20 +02:00
|
|
|
#ifndef buffer_hh_INCLUDED
|
|
|
|
#define buffer_hh_INCLUDED
|
|
|
|
|
2016-07-20 09:50:38 +02:00
|
|
|
#include "clock.hh"
|
2014-05-07 20:51:01 +02:00
|
|
|
#include "coord.hh"
|
2014-10-23 19:55:45 +02:00
|
|
|
#include "flags.hh"
|
2014-08-12 01:30:13 +02:00
|
|
|
#include "safe_ptr.hh"
|
2014-10-30 15:00:42 +01:00
|
|
|
#include "scope.hh"
|
2015-01-15 14:54:38 +01:00
|
|
|
#include "shared_string.hh"
|
2014-01-09 22:01:29 +01:00
|
|
|
#include "value.hh"
|
2015-01-12 14:58:41 +01:00
|
|
|
#include "vector.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
|
2017-01-08 23:30:15 +01:00
|
|
|
#include <ctime>
|
2016-07-27 01:14:11 +02:00
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-12-06 13:51:55 +01: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 18:51:20 +02:00
|
|
|
class Buffer;
|
2011-09-08 02:13:19 +02:00
|
|
|
|
2016-07-27 01:36:53 +02:00
|
|
|
constexpr timespec InvalidTime = { -1, -1 };
|
2013-10-17 19:47:09 +02:00
|
|
|
|
2012-01-11 15:21:58 +01:00
|
|
|
// A BufferIterator permits to iterate over the characters of a buffer
|
2011-09-02 18:51:20 +02:00
|
|
|
class BufferIterator
|
|
|
|
{
|
|
|
|
public:
|
2014-01-09 20:50:01 +01:00
|
|
|
using value_type = char;
|
2016-04-21 21:26:07 +02:00
|
|
|
using difference_type = ssize_t;
|
2014-01-09 20:50:01 +01:00
|
|
|
using pointer = const value_type*;
|
|
|
|
using reference = const value_type&;
|
2017-01-28 14:06:03 +01:00
|
|
|
// computing the distance between two iterator can be
|
|
|
|
// costly, so this is not strictly random access
|
|
|
|
using iterator_category = std::bidirectional_iterator_tag;
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2012-04-04 15:56:19 +02:00
|
|
|
BufferIterator() : m_buffer(nullptr) {}
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferIterator(const Buffer& buffer, BufferCoord coord);
|
2011-09-02 18:51:20 +02: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-28 22:53:29 +02:00
|
|
|
bool operator> (const BufferIterator& iterator) const;
|
|
|
|
bool operator>= (const BufferIterator& iterator) const;
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2016-01-27 09:27:23 +01:00
|
|
|
const char& operator* () const;
|
|
|
|
const char& operator[](size_t n) const;
|
2012-08-23 23:56:35 +02:00
|
|
|
size_t operator- (const BufferIterator& iterator) const;
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2012-10-11 00:41:48 +02:00
|
|
|
BufferIterator operator+ (ByteCount size) const;
|
|
|
|
BufferIterator operator- (ByteCount size) const;
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2012-10-11 00:41:48 +02:00
|
|
|
BufferIterator& operator+= (ByteCount size);
|
|
|
|
BufferIterator& operator-= (ByteCount size);
|
2011-09-02 18:51:20 +02:00
|
|
|
|
|
|
|
BufferIterator& operator++ ();
|
|
|
|
BufferIterator& operator-- ();
|
|
|
|
|
2012-10-02 14:09:06 +02:00
|
|
|
BufferIterator operator++ (int);
|
|
|
|
BufferIterator operator-- (int);
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
const BufferCoord& coord() const { return m_coord; }
|
2012-03-30 13:37:18 +02:00
|
|
|
|
2012-11-22 18:54:37 +01:00
|
|
|
private:
|
2015-02-19 14:58:25 +01:00
|
|
|
SafePtr<const Buffer> m_buffer;
|
2016-07-15 20:48:13 +02:00
|
|
|
StringView m_line;
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord m_coord;
|
2015-11-12 14:59:36 +01:00
|
|
|
LineCount m_last_line;
|
2011-09-02 18:51:20 +02:00
|
|
|
};
|
|
|
|
|
2015-03-01 13:06:19 +01:00
|
|
|
using BufferLines = Vector<StringDataPtr, MemoryDomain::BufferContent>;
|
2015-01-22 14:39:29 +01:00
|
|
|
|
2012-01-11 15:21:58 +01: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 15:00:42 +01:00
|
|
|
class Buffer : public SafeCountable, public OptionManagerWatcher, public Scope
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
|
|
|
public:
|
2012-11-20 19:47:56 +01:00
|
|
|
enum class Flags
|
2011-10-07 16:15:55 +02:00
|
|
|
{
|
2016-07-20 19:45:50 +02:00
|
|
|
None = 0,
|
|
|
|
File = 1 << 0,
|
|
|
|
New = 1 << 1,
|
|
|
|
Fifo = 1 << 2,
|
|
|
|
NoUndo = 1 << 3,
|
2016-11-14 14:59:33 +01:00
|
|
|
NoHooks = 1 << 4,
|
|
|
|
Debug = 1 << 5,
|
|
|
|
ReadOnly = 1 << 6,
|
2011-10-07 16:15:55 +02:00
|
|
|
};
|
|
|
|
|
2015-10-16 14:52:14 +02:00
|
|
|
Buffer(String name, Flags flags, StringView data = {},
|
2016-07-27 01:36:53 +02:00
|
|
|
timespec fs_timestamp = InvalidTime);
|
2011-10-24 16:23:13 +02:00
|
|
|
Buffer(const Buffer&) = delete;
|
|
|
|
Buffer& operator= (const Buffer&) = delete;
|
2017-01-08 23:30:15 +01:00
|
|
|
~Buffer() override;
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2012-11-20 19:47:56 +01:00
|
|
|
Flags flags() const { return m_flags; }
|
|
|
|
Flags& flags() { return m_flags; }
|
2011-09-06 20:49:32 +02:00
|
|
|
|
2013-04-22 13:48:18 +02:00
|
|
|
bool set_name(String name);
|
2015-12-01 14:42:42 +01:00
|
|
|
void update_display_name();
|
2013-04-22 13:48:18 +02:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord insert(BufferCoord pos, StringView content);
|
|
|
|
BufferCoord erase(BufferCoord begin, BufferCoord end);
|
|
|
|
BufferCoord replace(BufferCoord begin, BufferCoord end, StringView content);
|
2011-12-07 15:26:40 +01:00
|
|
|
|
2014-04-01 19:54:46 +02:00
|
|
|
size_t timestamp() const;
|
2016-07-27 01:36:53 +02:00
|
|
|
timespec fs_timestamp() const;
|
|
|
|
void set_fs_timestamp(timespec ts);
|
2012-08-15 17:07:53 +02:00
|
|
|
|
2013-02-20 14:20:16 +01:00
|
|
|
void commit_undo_group();
|
2016-07-20 21:20:03 +02:00
|
|
|
bool undo(size_t count = 1) noexcept;
|
|
|
|
bool redo(size_t count = 1) noexcept;
|
2016-07-20 10:41:45 +02:00
|
|
|
bool move_to(size_t history_id) noexcept;
|
2016-07-20 21:29:45 +02:00
|
|
|
size_t current_history_id() const noexcept;
|
2011-09-06 20:49:32 +02:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
String string(BufferCoord begin, BufferCoord end) const;
|
2013-06-03 18:56:48 +02:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
const char& byte_at(BufferCoord c) const;
|
|
|
|
ByteCount distance(BufferCoord begin, BufferCoord end) const;
|
|
|
|
BufferCoord advance(BufferCoord coord, ByteCount count) const;
|
|
|
|
BufferCoord next(BufferCoord coord) const;
|
|
|
|
BufferCoord prev(BufferCoord coord) const;
|
2013-06-03 18:56:48 +02:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord char_next(BufferCoord coord) const;
|
|
|
|
BufferCoord char_prev(BufferCoord coord) const;
|
2013-06-03 18:56:48 +02:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord back_coord() const;
|
|
|
|
BufferCoord end_coord() const;
|
2013-06-04 19:23:11 +02:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
bool is_valid(BufferCoord c) const;
|
|
|
|
bool is_end(BufferCoord c) const;
|
2013-04-23 18:46:18 +02:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord last_modification_coord() const;
|
2014-04-08 00:39:12 +02:00
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
BufferIterator begin() const;
|
|
|
|
BufferIterator end() const;
|
2012-08-22 23:33:52 +02:00
|
|
|
LineCount line_count() const;
|
2013-06-05 18:41:02 +02:00
|
|
|
|
2015-01-19 20:31:56 +01:00
|
|
|
StringView operator[](LineCount line) const
|
2014-05-24 18:08:01 +02:00
|
|
|
{ return m_lines[line]; }
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2015-11-07 17:55:20 +01:00
|
|
|
const StringDataPtr& line_storage(LineCount line) const
|
2015-01-27 14:11:32 +01:00
|
|
|
{ return m_lines.get_storage(line); }
|
2015-01-19 20:31:56 +01:00
|
|
|
|
2013-05-30 14:17:19 +02:00
|
|
|
// returns an iterator at given coordinates. clamp line_and_column
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferIterator iterator_at(BufferCoord coord) const;
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2011-12-05 20:21:11 +01:00
|
|
|
// returns nearest valid coordinates from given ones
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord clamp(BufferCoord coord) const;
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord offset_coord(BufferCoord coord, CharCount offset);
|
|
|
|
BufferCoordAndTarget offset_coord(BufferCoordAndTarget coord, LineCount offset);
|
2013-12-15 15:14:52 +01:00
|
|
|
|
2012-11-22 18:54:37 +01:00
|
|
|
const String& name() const { return m_name; }
|
2015-09-01 14:59:40 +02:00
|
|
|
const String& display_name() const { return m_display_name; }
|
2012-11-22 18:54:37 +01: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 13:37:18 +02:00
|
|
|
|
2014-01-09 22:01:29 +01:00
|
|
|
ValueMap& values() const { return m_values; }
|
|
|
|
|
2016-11-24 14:35:42 +01:00
|
|
|
void run_hook_in_own_context(StringView hook_name, StringView param,
|
|
|
|
String client_name = "");
|
2013-12-11 14:57:10 +01:00
|
|
|
|
2016-07-27 01:36:53 +02:00
|
|
|
void reload(StringView data, timespec fs_timestamp = InvalidTime);
|
2013-10-21 19:57:19 +02:00
|
|
|
|
2012-03-30 13:37:18 +02:00
|
|
|
void check_invariant() const;
|
2014-05-11 13:20:59 +02:00
|
|
|
|
|
|
|
struct Change
|
|
|
|
{
|
2015-01-29 23:44:07 +01:00
|
|
|
enum Type : char { Insert, Erase };
|
2014-05-11 13:20:59 +02:00
|
|
|
Type type;
|
2015-01-29 23:44:07 +01:00
|
|
|
bool at_end;
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord begin;
|
|
|
|
BufferCoord end;
|
2014-05-11 13:20:59 +02:00
|
|
|
};
|
2015-03-09 14:48:41 +01:00
|
|
|
ConstArrayView<Change> changes_since(size_t timestamp) const;
|
2014-09-22 20:19:34 +02:00
|
|
|
|
|
|
|
String debug_description() const;
|
2016-07-10 17:01:33 +02:00
|
|
|
|
|
|
|
// Methods called by the buffer manager
|
|
|
|
void on_registered();
|
|
|
|
void on_unregistered();
|
2013-04-03 19:22:12 +02:00
|
|
|
private:
|
2013-11-12 21:36:42 +01:00
|
|
|
|
|
|
|
void on_option_changed(const Option& option) override;
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord do_insert(BufferCoord pos, StringView content);
|
|
|
|
BufferCoord do_erase(BufferCoord begin, BufferCoord end);
|
2015-06-25 14:36:23 +02:00
|
|
|
|
|
|
|
struct Modification;
|
|
|
|
|
|
|
|
void apply_modification(const Modification& modification);
|
|
|
|
void revert_modification(const Modification& modification);
|
|
|
|
|
2015-01-22 14:39:29 +01:00
|
|
|
struct LineList : BufferLines
|
2012-03-30 13:37:18 +02:00
|
|
|
{
|
2014-07-19 01:18:16 +02:00
|
|
|
[[gnu::always_inline]]
|
2015-03-01 13:06:19 +01:00
|
|
|
StringDataPtr& get_storage(LineCount line)
|
2015-01-22 14:39:29 +01:00
|
|
|
{ return BufferLines::operator[]((int)line); }
|
2012-03-30 14:00:40 +02:00
|
|
|
|
2014-07-19 01:18:16 +02:00
|
|
|
[[gnu::always_inline]]
|
2015-03-01 13:06:19 +01:00
|
|
|
const StringDataPtr& get_storage(LineCount line) const
|
2015-01-22 14:39:29 +01:00
|
|
|
{ return BufferLines::operator[]((int)line); }
|
2015-01-19 20:31:56 +01:00
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
StringView operator[](LineCount line) const
|
|
|
|
{ return get_storage(line)->strview(); }
|
|
|
|
|
2015-01-22 14:39:29 +01:00
|
|
|
StringView front() const { return BufferLines::front()->strview(); }
|
|
|
|
StringView back() const { return BufferLines::back()->strview(); }
|
2012-08-22 23:33:52 +02:00
|
|
|
};
|
|
|
|
LineList m_lines;
|
2012-03-30 13:37:18 +02:00
|
|
|
|
2015-07-27 21:43:18 +02:00
|
|
|
String m_name;
|
2015-09-01 14:59:40 +02:00
|
|
|
String m_display_name;
|
2015-07-27 21:43:18 +02:00
|
|
|
Flags m_flags;
|
2011-09-06 20:49:32 +02:00
|
|
|
|
2017-02-06 14:33:20 +01:00
|
|
|
using UndoGroup = Vector<Modification, MemoryDomain::BufferMeta>;
|
2011-09-06 20:49:32 +02:00
|
|
|
|
2016-07-20 09:39:36 +02:00
|
|
|
struct HistoryNode : SafeCountable, UseMemoryDomain<MemoryDomain::BufferMeta>
|
2016-07-19 23:03:47 +02:00
|
|
|
{
|
2016-07-20 10:41:45 +02:00
|
|
|
HistoryNode(size_t id, HistoryNode* parent);
|
2016-07-19 23:03:47 +02:00
|
|
|
|
|
|
|
UndoGroup undo_group;
|
2016-07-20 09:39:36 +02:00
|
|
|
Vector<std::unique_ptr<HistoryNode>, MemoryDomain::BufferMeta> childs;
|
2017-02-06 14:33:20 +01:00
|
|
|
SafePtr<HistoryNode> parent;
|
2016-07-19 23:03:47 +02:00
|
|
|
SafePtr<HistoryNode> redo_child;
|
2017-02-06 14:33:20 +01:00
|
|
|
size_t id;
|
2016-07-20 09:50:38 +02:00
|
|
|
TimePoint timepoint;
|
2016-07-19 23:03:47 +02:00
|
|
|
};
|
2011-09-06 20:49:32 +02:00
|
|
|
|
2016-07-20 10:41:45 +02:00
|
|
|
size_t m_next_history_id = 0;
|
2016-07-19 23:03:47 +02:00
|
|
|
HistoryNode m_history;
|
|
|
|
SafePtr<HistoryNode> m_history_cursor;
|
|
|
|
SafePtr<HistoryNode> m_last_save_history_cursor;
|
|
|
|
UndoGroup m_current_undo_group;
|
2014-05-11 13:20:59 +02:00
|
|
|
|
2016-07-20 10:41:45 +02:00
|
|
|
void move_to(HistoryNode* history_node) noexcept;
|
|
|
|
|
|
|
|
template<typename Func> HistoryNode* find_history_node(HistoryNode* node, const Func& func);
|
|
|
|
|
2015-01-10 13:56:09 +01:00
|
|
|
Vector<Change, MemoryDomain::BufferMeta> m_changes;
|
2011-10-18 00:05:06 +02:00
|
|
|
|
2016-07-27 01:36:53 +02:00
|
|
|
timespec m_fs_timestamp;
|
2013-10-15 19:51:31 +02:00
|
|
|
|
2015-06-25 14:36:23 +02:00
|
|
|
// Values are just data holding by the buffer, they are not part of its
|
2014-01-09 22:01:29 +01:00
|
|
|
// observable state
|
|
|
|
mutable ValueMap m_values;
|
2013-08-01 00:28:01 +02:00
|
|
|
};
|
2012-11-20 19:47:56 +01:00
|
|
|
|
2014-10-23 19:55:45 +02:00
|
|
|
template<> struct WithBitOps<Buffer::Flags> : std::true_type {};
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
2014-01-12 18:19:05 +01:00
|
|
|
#include "buffer.inl.hh"
|
2011-10-18 00:05:06 +02:00
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
#endif // buffer_hh_INCLUDED
|