kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

buffer.hh (10741B)


      1 #ifndef buffer_hh_INCLUDED
      2 #define buffer_hh_INCLUDED
      3 
      4 #include "clock.hh"
      5 #include "coord.hh"
      6 #include "array.hh"
      7 #include "enum.hh"
      8 #include "file.hh"
      9 #include "flags.hh"
     10 #include "optional.hh"
     11 #include "option.hh"
     12 #include "range.hh"
     13 #include "safe_ptr.hh"
     14 #include "scope.hh"
     15 #include "shared_string.hh"
     16 #include "value.hh"
     17 #include "vector.hh"
     18 
     19 #include <sys/types.h>
     20 #include <ctime>
     21 
     22 namespace Kakoune
     23 {
     24 
     25 enum class Hook;
     26 
     27 enum class EolFormat
     28 {
     29     Lf,
     30     Crlf
     31 };
     32 
     33 constexpr auto enum_desc(Meta::Type<EolFormat>)
     34 {
     35     return make_array<EnumDesc<EolFormat>>({
     36         { EolFormat::Lf, "lf" },
     37         { EolFormat::Crlf, "crlf" },
     38     });
     39 }
     40 
     41 enum class ByteOrderMark
     42 {
     43     None,
     44     Utf8
     45 };
     46 
     47 constexpr auto enum_desc(Meta::Type<ByteOrderMark>)
     48 {
     49     return make_array<EnumDesc<ByteOrderMark>>({
     50         { ByteOrderMark::None, "none" },
     51         { ByteOrderMark::Utf8, "utf8" },
     52     });
     53 }
     54 
     55 enum class FinalEol
     56 {
     57     Present,
     58     Missing,
     59     IfNotEmpty,
     60 };
     61 
     62 constexpr auto enum_desc(Meta::Type<FinalEol>)
     63 {
     64     return make_array<EnumDesc<FinalEol>>({
     65         { FinalEol::Present, "present" },
     66         { FinalEol::Missing, "missing" },
     67         { FinalEol::IfNotEmpty, "ifnotempty" },
     68     });
     69 }
     70 
     71 class Buffer;
     72 
     73 // A BufferIterator permits to iterate over the characters of a buffer
     74 class BufferIterator
     75 {
     76 public:
     77     using value_type = char;
     78     using difference_type = ssize_t;
     79     using pointer = const value_type*;
     80     using reference = const value_type&;
     81     // computing the distance between two iterator can be
     82     // costly, so this is not strictly random access
     83     using iterator_category = std::bidirectional_iterator_tag;
     84 
     85     BufferIterator() = default;
     86     BufferIterator(const Buffer& buffer, BufferCoord coord) noexcept;
     87     BufferIterator(const StringDataPtr* lines, LineCount line_count, BufferCoord coord) noexcept;
     88 
     89     bool operator== (const BufferIterator& iterator) const noexcept;
     90     auto operator<=>(const BufferIterator& iterator) const noexcept;
     91     bool operator== (const BufferCoord& coord) const noexcept;
     92 
     93     const char& operator* () const noexcept;
     94     const char& operator[](size_t n) const noexcept;
     95     size_t operator- (const BufferIterator& iterator) const;
     96 
     97     explicit operator bool() const { return m_lines; }
     98 
     99     BufferIterator operator+ (ByteCount size) const;
    100     BufferIterator operator- (ByteCount size) const;
    101 
    102     BufferIterator& operator+= (ByteCount size);
    103     BufferIterator& operator-= (ByteCount size);
    104 
    105     BufferIterator& operator++ ();
    106     BufferIterator& operator-- ();
    107 
    108     BufferIterator operator++ (int);
    109     BufferIterator operator-- (int);
    110 
    111     const BufferCoord& coord() const noexcept { return m_coord; }
    112     explicit operator BufferCoord() const noexcept { return m_coord; }
    113     using Sentinel = BufferCoord;
    114 
    115 private:
    116     const StringDataPtr* m_lines;
    117     [[no_unique_address]] StringView m_line;
    118     [[no_unique_address]] LineCount m_line_count;
    119     BufferCoord m_coord;
    120 };
    121 
    122 using BufferLines = Vector<StringDataPtr, MemoryDomain::BufferContent>;
    123 using BufferRange = Range<BufferCoord>;
    124 
    125 // A Buffer is a in-memory representation of a file
    126 //
    127 // The Buffer class permits to read and mutate this file
    128 // representation. It also manage modifications undo/redo and
    129 // provides tools to deal with the line/column nature of text.
    130 class Buffer final : public SafeCountable, public Scope, private OptionWatcher
    131 {
    132 public:
    133     enum class Flags
    134     {
    135         None     = 0,
    136         File     = 1 << 0,
    137         New      = 1 << 1,
    138         Fifo     = 1 << 2,
    139         NoUndo   = 1 << 3,
    140         NoHooks  = 1 << 4,
    141         Debug    = 1 << 5,
    142         ReadOnly = 1 << 6,
    143         NoBufSetOption = 1 << 7,
    144         Locked = 1 << 8,
    145     };
    146     friend constexpr bool with_bit_ops(Meta::Type<Flags>) { return true; }
    147     friend class BufferIterator;
    148 
    149     enum class HistoryId : size_t { First = 0, Invalid = (size_t)-1 };
    150 
    151     Buffer(String name, Flags flags, BufferLines lines,
    152            ByteOrderMark bom = ByteOrderMark::None,
    153            EolFormat eolformat = EolFormat::Lf,
    154            FinalEol = FinalEol::Present,
    155            FsStatus fs_status = {InvalidTime, {}, {}});
    156     Buffer(const Buffer&) = delete;
    157     Buffer& operator= (const Buffer&) = delete;
    158     ~Buffer();
    159 
    160     Flags flags() const { return m_flags; }
    161     Flags& flags() { return m_flags; }
    162 
    163     bool set_name(String name);
    164     void update_display_name();
    165 
    166     BufferRange insert(BufferCoord pos, StringView content);
    167     BufferCoord erase(BufferCoord begin, BufferCoord end);
    168     BufferRange replace(BufferCoord begin, BufferCoord end, StringView content);
    169 
    170     size_t          timestamp() const;
    171     void            set_fs_status(FsStatus);
    172     const FsStatus& fs_status() const;
    173 
    174     void           commit_undo_group();
    175     bool           undo(size_t count = 1);
    176     bool           redo(size_t count = 1);
    177     bool           move_to(HistoryId id);
    178     HistoryId      current_history_id() const noexcept { return m_history_id; }
    179     HistoryId      next_history_id() const noexcept { return (HistoryId)m_history.size(); }
    180 
    181     String         string(BufferCoord begin, BufferCoord end) const;
    182     StringView     substr(BufferCoord begin, BufferCoord end) const;
    183 
    184     static BufferCoord advance(ArrayView<const StringDataPtr> lines, BufferCoord coord, ByteCount count);
    185     static ByteCount   distance(ArrayView<const StringDataPtr> lines, BufferCoord begin, BufferCoord end);
    186 
    187     const char&    byte_at(BufferCoord c) const;
    188     ByteCount      distance(BufferCoord begin, BufferCoord end) const { return distance(m_lines, begin, end); }
    189     BufferCoord    advance(BufferCoord coord, ByteCount count) const { return advance(m_lines, coord, count); }
    190     BufferCoord    next(BufferCoord coord) const;
    191     BufferCoord    prev(BufferCoord coord) const;
    192 
    193     BufferCoord    char_next(BufferCoord coord) const;
    194     BufferCoord    char_prev(BufferCoord coord) const;
    195 
    196     BufferCoord    back_coord() const;
    197     BufferCoord    end_coord() const;
    198 
    199     bool           is_valid(BufferCoord c) const;
    200     bool           is_end(BufferCoord c) const;
    201 
    202     BufferIterator begin() const;
    203     BufferIterator end() const;
    204     LineCount      line_count() const;
    205 
    206     Optional<BufferCoord> last_modification_coord() const;
    207 
    208     StringView operator[](LineCount line) const
    209     { return m_lines[line]; }
    210 
    211     const StringDataPtr& line_storage(LineCount line) const
    212     { return m_lines.get_storage(line); }
    213 
    214     // returns an iterator at given coordinates. clamp line_and_column
    215     BufferIterator iterator_at(BufferCoord coord) const;
    216 
    217     // returns nearest valid coordinates from given ones
    218     BufferCoord clamp(BufferCoord coord) const;
    219 
    220     BufferCoord offset_coord(BufferCoord coord, CharCount offset, ColumnCount) const;
    221     BufferCoordAndTarget offset_coord(BufferCoordAndTarget coord, LineCount offset, ColumnCount tabstop) const;
    222 
    223     const String& name() const { return m_flags & Flags::File ? m_filename : m_display_name; }
    224     const String& filename() const { return m_filename; }
    225     const String& display_name() const { return m_display_name; }
    226 
    227     // returns true if the buffer is in a different state than
    228     // the last time it was saved
    229     bool is_modified() const;
    230 
    231     // notify the buffer that it was saved in the current state
    232     void notify_saved(FsStatus status);
    233 
    234     ValueMap& values() const { return m_values; }
    235 
    236     void run_hook_in_own_context(Hook hook, StringView param,
    237                                  String client_name = {});
    238 
    239     void reload(BufferLines lines, ByteOrderMark bom, EolFormat eolformat, FinalEol finaleol, FsStatus status);
    240 
    241     void check_invariant() const;
    242 
    243     struct Change
    244     {
    245         enum Type : char { Insert, Erase };
    246         Type type;
    247         BufferCoord begin;
    248         BufferCoord end;
    249 
    250         #ifdef KAK_DEBUG
    251         bool operator==(const Change&) const = default;
    252         #endif
    253     };
    254     ConstArrayView<Change> changes_since(size_t timestamp) const;
    255 
    256     String debug_description() const;
    257 
    258     // Methods called by the buffer manager
    259     void on_registered();
    260     void on_unregistered();
    261 
    262     void throw_if_read_only() const;
    263 
    264     // A Modification holds a single atomic modification to Buffer
    265     struct Modification
    266     {
    267         enum Type { Insert, Erase };
    268 
    269         Type type;
    270         BufferCoord coord;
    271         StringDataPtr content;
    272 
    273         Modification inverse() const;
    274     };
    275 
    276     using UndoGroup = Vector<Modification, MemoryDomain::BufferMeta>;
    277 
    278     struct HistoryNode : UseMemoryDomain<MemoryDomain::BufferMeta>
    279     {
    280         HistoryNode(HistoryId parent);
    281 
    282         HistoryId parent;
    283         HistoryId redo_child = HistoryId::Invalid;
    284         TimePoint committed;
    285         UndoGroup undo_group;
    286     };
    287 
    288     const Vector<HistoryNode>& history() const { return m_history; }
    289     const UndoGroup& current_undo_group() const { return m_current_undo_group; }
    290 
    291 private:
    292     void on_option_changed(const Option& option) override;
    293 
    294     BufferRange do_insert(BufferCoord pos, StringView content);
    295     BufferCoord do_erase(BufferCoord begin, BufferCoord end);
    296 
    297     void apply_modification(const Modification& modification);
    298     void revert_modification(const Modification& modification);
    299 
    300     struct LineList : BufferLines
    301     {
    302         [[gnu::always_inline]]
    303         StringDataPtr& get_storage(LineCount line)
    304         { return BufferLines::operator[]((int)line); }
    305 
    306         [[gnu::always_inline]]
    307         const StringDataPtr& get_storage(LineCount line) const
    308         { return BufferLines::operator[]((int)line); }
    309 
    310         [[gnu::always_inline]]
    311         StringView operator[](LineCount line) const
    312         { return get_storage(line)->strview(); }
    313 
    314         StringView front() const { return BufferLines::front()->strview(); }
    315         StringView back() const { return BufferLines::back()->strview(); }
    316     };
    317     LineList m_lines;
    318 
    319     String m_filename;
    320     String m_display_name;
    321     Flags  m_flags;
    322 
    323     Vector<HistoryNode> m_history;
    324     HistoryId           m_history_id = HistoryId::Invalid;
    325     HistoryId           m_last_save_history_id = HistoryId::Invalid;
    326     UndoGroup           m_current_undo_group;
    327 
    328           HistoryNode& history_node(HistoryId id)       { return m_history[(size_t)id]; }
    329     const HistoryNode& history_node(HistoryId id) const { return m_history[(size_t)id]; }
    330           HistoryNode& current_history_node()           { return m_history[(size_t)m_history_id]; }
    331     const HistoryNode& current_history_node()     const { return m_history[(size_t)m_history_id]; }
    332 
    333     Vector<Change, MemoryDomain::BufferMeta> m_changes;
    334 
    335     FsStatus m_fs_status;
    336 
    337     // Values are just data holding by the buffer, they are not part of its
    338     // observable state
    339     mutable ValueMap m_values;
    340 };
    341 
    342 }
    343 
    344 #include "buffer.inl.hh"
    345 
    346 #endif // buffer_hh_INCLUDED