More cleanups in the buffer open/reload code

This commit is contained in:
Maxime Coste 2015-10-16 13:58:56 +01:00
parent fe704b9b84
commit 76fadd8108
8 changed files with 47 additions and 44 deletions

View File

@ -47,24 +47,30 @@ ByteCount get_byte_to_column(const Buffer& buffer, CharCount tabstop, CharCoord
return (int)(it - line.begin()); return (int)(it - line.begin());
} }
Buffer* create_file_buffer(StringView filename) Buffer* open_file_buffer(StringView filename)
{ {
if (MappedFile file_data{filename}) MappedFile file_data{filename};
return new Buffer(filename.str(), Buffer::Flags::File, return new Buffer(filename.str(), Buffer::Flags::File,
{ file_data.data, (int)file_data.st.st_size }, file_data, file_data.st.st_mtim);
file_data.st.st_mtim);
return nullptr;
} }
bool reload_file_buffer(Buffer& buffer) Buffer* open_or_create_file_buffer(StringView filename)
{
if (file_exists(filename))
{
MappedFile file_data{filename};
return new Buffer(filename.str(), Buffer::Flags::File,
file_data, file_data.st.st_mtim);
}
return new Buffer(filename.str(), Buffer::Flags::File | Buffer::Flags::New,
{}, InvalidTime);
}
void reload_file_buffer(Buffer& buffer)
{ {
kak_assert(buffer.flags() & Buffer::Flags::File); kak_assert(buffer.flags() & Buffer::Flags::File);
if (MappedFile file_data{buffer.name()}) MappedFile file_data{buffer.name()};
{ buffer.reload(file_data, file_data.st.st_mtim);
buffer.reload({ file_data.data, (int)file_data.st.st_size }, file_data.st.st_mtim);
return true;
}
return false;
} }
Buffer* create_fifo_buffer(String name, int fd, bool scroll) Buffer* create_fifo_buffer(String name, int fd, bool scroll)

View File

@ -31,8 +31,9 @@ ByteCount get_byte_to_column(const Buffer& buffer, CharCount tabstop,
CharCoord coord); CharCoord coord);
Buffer* create_fifo_buffer(String name, int fd, bool scroll = false); Buffer* create_fifo_buffer(String name, int fd, bool scroll = false);
Buffer* create_file_buffer(StringView filename); Buffer* open_file_buffer(StringView filename);
bool reload_file_buffer(Buffer& buffer); Buffer* open_or_create_file_buffer(StringView filename);
void reload_file_buffer(Buffer& buffer);
void write_to_debug_buffer(StringView str); void write_to_debug_buffer(StringView str);

View File

@ -186,12 +186,9 @@ void Client::force_redraw()
void Client::reload_buffer() void Client::reload_buffer()
{ {
Buffer& buffer = context().buffer(); Buffer& buffer = context().buffer();
if (reload_file_buffer(buffer)) reload_file_buffer(buffer);
context().print_status({ format("'{}' reloaded", buffer.display_name()), context().print_status({ format("'{}' reloaded", buffer.display_name()),
get_face("Information") }); get_face("Information") });
else
context().print_status({ format("could not reload '{}'", buffer.display_name()),
get_face("Error") });
} }
void Client::on_buffer_reload_key(Key key) void Client::on_buffer_reload_key(Key key)

View File

@ -135,7 +135,7 @@ void edit(const ParametersParser& parser, Context& context)
Buffer* buffer = buffer_manager.get_buffer_ifp(name); Buffer* buffer = buffer_manager.get_buffer_ifp(name);
Buffer* oldbuf = &context.buffer(); Buffer* oldbuf = &context.buffer();
// TODO fifo reload
if (force_reload and buffer and buffer->flags() & Buffer::Flags::File) if (force_reload and buffer and buffer->flags() & Buffer::Flags::File)
reload_file_buffer(*buffer); reload_file_buffer(*buffer);
else else
@ -154,16 +154,11 @@ void edit(const ParametersParser& parser, Context& context)
buffer = open_fifo(name, *fifo, (bool)parser.get_switch("scroll")); buffer = open_fifo(name, *fifo, (bool)parser.get_switch("scroll"));
else if (not buffer) else if (not buffer)
{ {
buffer = create_file_buffer(name); buffer = parser.get_switch("existing") ? open_file_buffer(name)
if (not buffer) : open_or_create_file_buffer(name);
{ if (buffer->flags() & Buffer::Flags::New)
if (parser.get_switch("existing"))
throw runtime_error(format("unable to open '{}'", name));
context.print_status({ format("new file '{}'", name), context.print_status({ format("new file '{}'", name),
get_face("StatusLine") }); get_face("StatusLine") });
buffer = new Buffer(name, Buffer::Flags::File | Buffer::Flags::New);
}
} }
} }

View File

@ -165,7 +165,7 @@ MappedFile::MappedFile(StringView filename)
if (fd == -1) if (fd == -1)
{ {
if (errno == ENOENT) if (errno == ENOENT)
return; throw file_not_found{real_filename};
throw file_access_error(real_filename, strerror(errno)); throw file_access_error(real_filename, strerror(errno));
} }
@ -186,6 +186,13 @@ MappedFile::~MappedFile()
} }
} }
bool file_exists(StringView filename)
{
String real_filename = real_path(parse_filename(filename));
struct stat st;
return stat(real_filename.c_str(), &st) == 0;
}
static void write(int fd, StringView data) static void write(int fd, StringView data)
{ {
const char* ptr = data.data(); const char* ptr = data.data();

View File

@ -45,14 +45,14 @@ String read_file(StringView filename, bool text = false);
struct MappedFile struct MappedFile
{ {
int fd;
const char* data = nullptr;
struct stat st {};
explicit operator bool() const { return fd != -1; }
MappedFile(StringView filename); MappedFile(StringView filename);
~MappedFile(); ~MappedFile();
operator StringView() const { return { data, (int)st.st_size }; }
int fd;
const char* data;
struct stat st {};
}; };
void write_buffer_to_file(Buffer& buffer, StringView filename); void write_buffer_to_file(Buffer& buffer, StringView filename);
@ -60,6 +60,7 @@ void write_buffer_to_fd(Buffer& buffer, int fd);
void write_buffer_to_backup_file(Buffer& buffer); void write_buffer_to_backup_file(Buffer& buffer);
String find_file(StringView filename, ConstArrayView<String> paths); String find_file(StringView filename, ConstArrayView<String> paths);
bool file_exists(StringView filename);
Vector<String> list_files(StringView directory); Vector<String> list_files(StringView directory);

View File

@ -500,8 +500,7 @@ int run_server(StringView session, StringView init_command,
{ {
try try
{ {
if (create_file_buffer(file) == nullptr) open_or_create_file_buffer(file);
new Buffer(file.str(), Buffer::Flags::New | Buffer::Flags::File);
} }
catch (Kakoune::runtime_error& error) catch (Kakoune::runtime_error& error)
{ {
@ -593,7 +592,7 @@ int run_filter(StringView keystr, ConstArrayView<StringView> files, bool quiet)
for (auto& file : files) for (auto& file : files)
{ {
Buffer* buffer = create_file_buffer(file); Buffer* buffer = open_file_buffer(file);
write_buffer_to_file(*buffer, file + ".kak-bak"); write_buffer_to_file(*buffer, file + ".kak-bak");
apply_keys_to_buffer(*buffer); apply_keys_to_buffer(*buffer);
write_buffer_to_file(*buffer, file); write_buffer_to_file(*buffer, file);

View File

@ -193,10 +193,7 @@ void goto_commands(Context& context, NormalParams params)
Buffer* buffer = BufferManager::instance().get_buffer_ifp(path); Buffer* buffer = BufferManager::instance().get_buffer_ifp(path);
if (not buffer) if (not buffer)
buffer = create_file_buffer(path); buffer = open_file_buffer(path);
if (buffer == nullptr)
throw runtime_error(format("unable to open file '{}'", path));
if (buffer != &context.buffer()) if (buffer != &context.buffer())
{ {