diff --git a/src/buffer.cc b/src/buffer.cc index 0470884d4..9106d5955 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -12,11 +12,13 @@ namespace Kakoune { -Buffer::Buffer(String name, Flags flags, std::vector lines) +Buffer::Buffer(String name, Flags flags, std::vector lines, + time_t fs_timestamp) : m_name(std::move(name)), m_flags(flags | Flags::NoUndo), m_history(), m_history_cursor(m_history.begin()), m_last_save_undo_index(0), m_timestamp(0), + m_fs_timestamp(fs_timestamp), m_hooks(GlobalHooks::instance()), m_options(GlobalOptions::instance()) { @@ -44,7 +46,10 @@ Buffer::Buffer(String name, Flags flags, std::vector lines) if (flags & Flags::New) m_hooks.run_hook("BufNew", m_name, context); else + { + kak_assert(m_fs_timestamp != InvalidTime); m_hooks.run_hook("BufOpen", m_name, context); + } } m_hooks.run_hook("BufCreate", m_name, context); @@ -607,6 +612,7 @@ void Buffer::notify_saved() ++m_timestamp; m_last_save_undo_index = history_cursor_index; } + m_fs_timestamp = get_fs_timestamp(m_name); } BufferCoord Buffer::advance(BufferCoord coord, ByteCount count) const diff --git a/src/buffer.hh b/src/buffer.hh index d1bad814c..cf5377490 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -17,6 +17,8 @@ namespace Kakoune class Buffer; +constexpr time_t InvalidTime = 0; + struct BufferCoord : LineAndColumn { constexpr BufferCoord(LineCount line = 0, ByteCount column = 0) @@ -90,7 +92,8 @@ public: NoUndo = 8, }; - Buffer(String name, Flags flags, std::vector lines = { "\n" }); + Buffer(String name, Flags flags, std::vector lines = { "\n" }, + time_t fs_timestamp = InvalidTime); Buffer(const Buffer&) = delete; Buffer& operator= (const Buffer&) = delete; ~Buffer(); diff --git a/src/commands.cc b/src/commands.cc index f8325a097..26a3b75f2 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -147,9 +147,6 @@ void write_buffer(CommandParameters params, Context& context) : parse_filename(params[0]); write_buffer_to_file(buffer, filename); - - if (filename == buffer.name()) - buffer.notify_saved(); } void write_all_buffers(CommandParameters params, Context& context) @@ -160,10 +157,7 @@ void write_all_buffers(CommandParameters params, Context& context) for (auto& buffer : BufferManager::instance()) { if ((buffer->flags() & Buffer::Flags::File) and buffer->is_modified()) - { write_buffer_to_file(*buffer, buffer->name()); - buffer->notify_saved(); - } } } diff --git a/src/file.cc b/src/file.cc index 18682ac3b..c10f936dd 100644 --- a/src/file.cc +++ b/src/file.cc @@ -175,8 +175,8 @@ Buffer* create_buffer_from_file(String filename) else pos = line_end + 1; } - Buffer* buffer = new Buffer(filename, Buffer::Flags::File, std::move(lines)); - buffer->set_fs_timestamp(st.st_mtime); + Buffer* 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"); @@ -228,7 +228,7 @@ void write_buffer_to_file(Buffer& buffer, const String& filename) write(fd, eoldata, filename); } if ((buffer.flags() & Buffer::Flags::File) and filename == buffer.name()) - buffer.set_fs_timestamp(get_fs_timestamp(filename)); + buffer.notify_saved(); } String find_file(const String& filename, memoryview paths) @@ -312,7 +312,7 @@ time_t get_fs_timestamp(const String& filename) { struct stat st; if (stat(filename.c_str(), &st) != 0) - throw runtime_error("stat failed on " + filename); + return InvalidTime; return st.st_mtime; }