diff --git a/src/buffer.cc b/src/buffer.cc index 9106d595..368d530e 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -70,6 +70,35 @@ Buffer::~Buffer() kak_assert(m_change_listeners.empty()); } +void Buffer::reload(std::vector lines, time_t fs_timestamp) +{ + for (auto listener : m_change_listeners) + listener->on_erase(*this, {0,0}, end_coord()); + + m_history.clear(); + m_current_undo_group.clear(); + m_history_cursor = m_history.begin(); + m_last_save_undo_index = 0; + m_lines.clear(); + ++m_timestamp; + + if (lines.empty()) + lines.emplace_back("\n"); + + ByteCount pos = 0; + m_lines.reserve(lines.size()); + for (auto& line : lines) + { + kak_assert(not line.empty() and line.back() == '\n'); + m_lines.emplace_back(Line{ pos, std::move(line) }); + pos += m_lines.back().length(); + } + m_fs_timestamp = fs_timestamp; + + for (auto listener : m_change_listeners) + listener->on_insert(*this, {0,0}, end_coord()); +} + String Buffer::display_name() const { if (m_flags & Flags::File) diff --git a/src/buffer.hh b/src/buffer.hh index cf537749..4b67ce48 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -163,6 +163,8 @@ public: std::unordered_set& change_listeners() const { return m_change_listeners; } + void reload(std::vector lines, time_t fs_timestamp = InvalidTime); + void check_invariant() const; private: struct Line diff --git a/src/file.cc b/src/file.cc index c10f936d..c28833f8 100644 --- a/src/file.cc +++ b/src/file.cc @@ -135,8 +135,6 @@ Buffer* create_buffer_from_file(String filename) const char* data = (const char*)mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); auto cleanup = on_scope_end([&]{ munmap((void*)data, st.st_size); close(fd); }); - BufferManager::instance().delete_buffer_if_exists(filename); - const char* pos = data; bool crlf = false; bool bom = false; @@ -175,8 +173,12 @@ Buffer* create_buffer_from_file(String filename) else pos = line_end + 1; } - Buffer* buffer = new Buffer{filename, Buffer::Flags::File, - std::move(lines), st.st_mtime}; + Buffer* buffer = BufferManager::instance().get_buffer_ifp(filename); + if (buffer) + buffer->reload(std::move(lines), st.st_mtime); + else + buffer = new Buffer{filename, Buffer::Flags::File, + std::move(lines), st.st_mtime}; OptionManager& options = buffer->options(); options.get_local_option("eolformat").set(crlf ? "crlf" : "lf");