1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-25 12:36:11 +03:00

Add Buffer::reload for reloading buffers without loosing options and hooks

This commit is contained in:
Maxime Coste 2013-10-21 18:57:19 +01:00
parent 9e9b503f5d
commit 60dbec4949
3 changed files with 37 additions and 4 deletions

View File

@ -70,6 +70,35 @@ Buffer::~Buffer()
kak_assert(m_change_listeners.empty());
}
void Buffer::reload(std::vector<String> 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)

View File

@ -163,6 +163,8 @@ public:
std::unordered_set<BufferChangeListener*>& change_listeners() const { return m_change_listeners; }
void reload(std::vector<String> lines, time_t fs_timestamp = InvalidTime);
void check_invariant() const;
private:
struct Line

View File

@ -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<String>(crlf ? "crlf" : "lf");