Support empty file-backed buffers.

This commit is contained in:
2025-11-29 18:03:29 -08:00
parent 69e7959fa4
commit 548d760df5
6 changed files with 72 additions and 59 deletions

View File

@@ -2,6 +2,7 @@
#include <fstream>
#include <sstream>
#include <filesystem>
Buffer::Buffer() = default;
@@ -17,11 +18,29 @@ Buffer::Buffer(const std::string &path)
bool
Buffer::OpenFromFile(const std::string &path, std::string &err)
{
std::ifstream in(path, std::ios::in | std::ios::binary);
if (!in) {
err = "Failed to open file: " + path;
return false;
}
// If the file doesn't exist, initialize an empty, non-file-backed buffer
// with the provided filename. Do not touch the filesystem until Save/SaveAs.
if (!std::filesystem::exists(path)) {
rows_.clear();
nrows_ = 0;
filename_ = path;
is_file_backed_ = false;
dirty_ = false;
// Reset cursor/viewport state
curx_ = cury_ = rx_ = 0;
rowoffs_ = coloffs_ = 0;
mark_set_ = false;
mark_curx_ = mark_cury_ = 0;
return true;
}
std::ifstream in(path, std::ios::in | std::ios::binary);
if (!in) {
err = "Failed to open file: " + path;
return false;
}
rows_.clear();
std::string line;