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"
|
2017-11-12 06:01:18 +01:00
|
|
|
#include "constexpr_utils.hh"
|
2017-03-16 10:57:39 +01:00
|
|
|
#include "enum.hh"
|
2019-11-23 15:55:46 +01:00
|
|
|
#include "file.hh"
|
2018-03-13 04:00:57 +01:00
|
|
|
#include "optional.hh"
|
2019-12-18 01:36:17 +01:00
|
|
|
#include "range.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
|
|
|
|
2019-11-30 04:50:24 +01:00
|
|
|
#include <sys/types.h>
|
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
|
|
|
|
};
|
|
|
|
|
2017-08-12 17:11:58 +02:00
|
|
|
constexpr auto enum_desc(Meta::Type<EolFormat>)
|
2015-12-06 13:51:55 +01:00
|
|
|
{
|
2019-10-17 13:48:22 +02:00
|
|
|
return make_array<EnumDesc<EolFormat>>({
|
2015-12-06 13:51:55 +01:00
|
|
|
{ EolFormat::Lf, "lf" },
|
|
|
|
{ EolFormat::Crlf, "crlf" },
|
2017-08-12 17:11:58 +02:00
|
|
|
});
|
2015-12-06 13:51:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
enum class ByteOrderMark
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
Utf8
|
|
|
|
};
|
|
|
|
|
2017-08-12 17:11:58 +02:00
|
|
|
constexpr auto enum_desc(Meta::Type<ByteOrderMark>)
|
2015-12-06 13:51:55 +01:00
|
|
|
{
|
2019-10-17 13:48:22 +02:00
|
|
|
return make_array<EnumDesc<ByteOrderMark>>({
|
2015-12-06 13:51:55 +01:00
|
|
|
{ ByteOrderMark::None, "none" },
|
|
|
|
{ ByteOrderMark::Utf8, "utf8" },
|
2017-08-12 17:11:58 +02:00
|
|
|
});
|
2015-12-06 13:51:55 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-12-20 09:47:19 +01:00
|
|
|
BufferIterator() noexcept : m_buffer{nullptr}, m_line{} {}
|
2017-06-07 11:58:01 +02:00
|
|
|
BufferIterator(const Buffer& buffer, BufferCoord coord) noexcept;
|
|
|
|
|
|
|
|
bool operator== (const BufferIterator& iterator) const noexcept;
|
2020-11-14 22:57:19 +01:00
|
|
|
std::strong_ordering operator<=>(const BufferIterator& iterator) const noexcept;
|
2018-11-01 11:51:10 +01:00
|
|
|
bool operator== (const BufferCoord& coord) const noexcept;
|
|
|
|
|
2017-06-07 11:58:01 +02:00
|
|
|
const char& operator* () const noexcept;
|
|
|
|
const char& operator[](size_t n) const noexcept;
|
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);
|
|
|
|
|
2017-06-07 11:58:01 +02:00
|
|
|
const BufferCoord& coord() const noexcept { return m_coord; }
|
2018-11-01 11:51:10 +01:00
|
|
|
explicit operator BufferCoord() const noexcept { return m_coord; }
|
|
|
|
using Sentinel = BufferCoord;
|
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-09-22 21:36:26 +02:00
|
|
|
BufferCoord m_coord;
|
2017-06-11 13:01:40 +02:00
|
|
|
StringView m_line;
|
2011-09-02 18:51:20 +02:00
|
|
|
};
|
|
|
|
|
2015-03-01 13:06:19 +01:00
|
|
|
using BufferLines = Vector<StringDataPtr, MemoryDomain::BufferContent>;
|
2019-12-18 01:36:17 +01:00
|
|
|
using BufferRange = Range<BufferCoord>;
|
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.
|
2018-05-08 14:07:43 +02:00
|
|
|
class Buffer final : public SafeCountable, public Scope, private OptionManagerWatcher
|
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
|
|
|
};
|
2017-03-15 18:55:34 +01:00
|
|
|
friend constexpr bool with_bit_ops(Meta::Type<Flags>) { return true; }
|
2011-10-07 16:15:55 +02:00
|
|
|
|
2018-05-02 13:31:44 +02:00
|
|
|
enum class HistoryId : size_t { First = 0, Invalid = (size_t)-1 };
|
|
|
|
|
2021-05-28 09:03:06 +02:00
|
|
|
Buffer(String name, Flags flags, BufferLines lines,
|
|
|
|
ByteOrderMark bom = ByteOrderMark::None,
|
|
|
|
EolFormat eolformat = EolFormat::Lf,
|
|
|
|
FsStatus fs_status = {InvalidTime, {}, {}});
|
2011-10-24 16:23:13 +02:00
|
|
|
Buffer(const Buffer&) = delete;
|
|
|
|
Buffer& operator= (const Buffer&) = delete;
|
2017-05-22 11:31:56 +02:00
|
|
|
~Buffer();
|
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
|
|
|
|
2019-12-18 01:36:17 +01:00
|
|
|
BufferRange insert(BufferCoord pos, StringView content);
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord erase(BufferCoord begin, BufferCoord end);
|
2019-12-18 01:36:17 +01:00
|
|
|
BufferRange replace(BufferCoord begin, BufferCoord end, StringView content);
|
2011-12-07 15:26:40 +01:00
|
|
|
|
2019-11-23 15:55:46 +01:00
|
|
|
size_t timestamp() const;
|
|
|
|
void set_fs_status(FsStatus);
|
|
|
|
const FsStatus& fs_status() const;
|
2012-08-15 17:07:53 +02:00
|
|
|
|
2013-02-20 14:20:16 +01:00
|
|
|
void commit_undo_group();
|
2018-02-11 03:06:19 +01:00
|
|
|
bool undo(size_t count = 1);
|
|
|
|
bool redo(size_t count = 1);
|
2018-05-02 13:31:44 +02:00
|
|
|
bool move_to(HistoryId id);
|
|
|
|
HistoryId current_history_id() const noexcept { return m_history_id; }
|
|
|
|
HistoryId next_history_id() const noexcept { return (HistoryId)m_history.size(); }
|
2011-09-06 20:49:32 +02:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
String string(BufferCoord begin, BufferCoord end) const;
|
2017-02-20 20:19:26 +01:00
|
|
|
StringView substr(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
|
|
|
|
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
|
|
|
|
2018-03-13 04:00:57 +01:00
|
|
|
Optional<BufferCoord> last_modification_coord() const;
|
|
|
|
|
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
|
|
|
|
2022-06-08 11:43:03 +02:00
|
|
|
BufferCoord offset_coord(BufferCoord coord, CharCount offset, ColumnCount) const;
|
|
|
|
BufferCoordAndTarget offset_coord(BufferCoordAndTarget coord, LineCount offset, ColumnCount tabstop) const;
|
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
|
2019-12-19 03:39:30 +01:00
|
|
|
void notify_saved(FsStatus status);
|
2012-03-30 13:37:18 +02:00
|
|
|
|
2014-01-09 22:01:29 +01:00
|
|
|
ValueMap& values() const { return m_values; }
|
|
|
|
|
2018-10-22 23:15:53 +02:00
|
|
|
void run_hook_in_own_context(Hook hook, StringView param,
|
2017-06-07 10:54:11 +02:00
|
|
|
String client_name = {});
|
2013-12-11 14:57:10 +01:00
|
|
|
|
2021-05-28 09:03:06 +02:00
|
|
|
void reload(BufferLines lines, ByteOrderMark bom, EolFormat eolformat, FsStatus status);
|
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;
|
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-11-12 21:36:42 +01:00
|
|
|
|
2018-02-11 03:06:19 +01:00
|
|
|
void throw_if_read_only() const;
|
2018-12-31 18:40:49 +01:00
|
|
|
|
|
|
|
// A Modification holds a single atomic modification to Buffer
|
|
|
|
struct Modification
|
|
|
|
{
|
|
|
|
enum Type { Insert, Erase };
|
|
|
|
|
|
|
|
Type type;
|
|
|
|
BufferCoord coord;
|
|
|
|
StringDataPtr content;
|
|
|
|
|
|
|
|
Modification inverse() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
using UndoGroup = Vector<Modification, MemoryDomain::BufferMeta>;
|
|
|
|
|
|
|
|
struct HistoryNode : UseMemoryDomain<MemoryDomain::BufferMeta>
|
|
|
|
{
|
|
|
|
HistoryNode(HistoryId parent);
|
|
|
|
|
|
|
|
HistoryId parent;
|
|
|
|
HistoryId redo_child = HistoryId::Invalid;
|
|
|
|
TimePoint committed;
|
|
|
|
UndoGroup undo_group;
|
|
|
|
};
|
|
|
|
|
|
|
|
const Vector<HistoryNode>& history() const { return m_history; }
|
|
|
|
const UndoGroup& current_undo_group() const { return m_current_undo_group; }
|
|
|
|
|
2017-07-07 06:59:53 +02:00
|
|
|
private:
|
2013-11-12 21:36:42 +01:00
|
|
|
void on_option_changed(const Option& option) override;
|
|
|
|
|
2019-12-18 01:36:17 +01:00
|
|
|
BufferRange do_insert(BufferCoord pos, StringView content);
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord do_erase(BufferCoord begin, BufferCoord end);
|
2015-06-25 14:36:23 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-05-02 13:31:44 +02:00
|
|
|
Vector<HistoryNode> m_history;
|
|
|
|
HistoryId m_history_id = HistoryId::Invalid;
|
|
|
|
HistoryId m_last_save_history_id = HistoryId::Invalid;
|
|
|
|
UndoGroup m_current_undo_group;
|
2016-07-20 10:41:45 +02:00
|
|
|
|
2018-05-02 13:31:44 +02:00
|
|
|
HistoryNode& history_node(HistoryId id) { return m_history[(size_t)id]; }
|
|
|
|
const HistoryNode& history_node(HistoryId id) const { return m_history[(size_t)id]; }
|
|
|
|
HistoryNode& current_history_node() { return m_history[(size_t)m_history_id]; }
|
|
|
|
const HistoryNode& current_history_node() const { return m_history[(size_t)m_history_id]; }
|
2016-07-20 10:41:45 +02:00
|
|
|
|
2015-01-10 13:56:09 +01:00
|
|
|
Vector<Change, MemoryDomain::BufferMeta> m_changes;
|
2011-10-18 00:05:06 +02:00
|
|
|
|
2019-11-23 15:55:46 +01:00
|
|
|
FsStatus m_fs_status;
|
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
|
|
|
|
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
|