From 76fadd81081d7bb762d143dfd2ecca73637328a7 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 16 Oct 2015 13:58:56 +0100 Subject: [PATCH] More cleanups in the buffer open/reload code --- src/buffer_utils.cc | 32 +++++++++++++++++++------------- src/buffer_utils.hh | 5 +++-- src/client.cc | 9 +++------ src/commands.cc | 13 ++++--------- src/file.cc | 9 ++++++++- src/file.hh | 13 +++++++------ src/main.cc | 5 ++--- src/normal.cc | 5 +---- 8 files changed, 47 insertions(+), 44 deletions(-) diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc index 045e4364..7220470b 100644 --- a/src/buffer_utils.cc +++ b/src/buffer_utils.cc @@ -47,24 +47,30 @@ ByteCount get_byte_to_column(const Buffer& buffer, CharCount tabstop, CharCoord return (int)(it - line.begin()); } -Buffer* create_file_buffer(StringView filename) +Buffer* open_file_buffer(StringView filename) { - if (MappedFile file_data{filename}) - return new Buffer(filename.str(), Buffer::Flags::File, - { file_data.data, (int)file_data.st.st_size }, - file_data.st.st_mtim); - return nullptr; + MappedFile file_data{filename}; + return new Buffer(filename.str(), Buffer::Flags::File, + file_data, file_data.st.st_mtim); } -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); - if (MappedFile file_data{buffer.name()}) - { - buffer.reload({ file_data.data, (int)file_data.st.st_size }, file_data.st.st_mtim); - return true; - } - return false; + MappedFile file_data{buffer.name()}; + buffer.reload(file_data, file_data.st.st_mtim); } Buffer* create_fifo_buffer(String name, int fd, bool scroll) diff --git a/src/buffer_utils.hh b/src/buffer_utils.hh index baf47890..3851ee65 100644 --- a/src/buffer_utils.hh +++ b/src/buffer_utils.hh @@ -31,8 +31,9 @@ ByteCount get_byte_to_column(const Buffer& buffer, CharCount tabstop, CharCoord coord); Buffer* create_fifo_buffer(String name, int fd, bool scroll = false); -Buffer* create_file_buffer(StringView filename); -bool reload_file_buffer(Buffer& buffer); +Buffer* open_file_buffer(StringView filename); +Buffer* open_or_create_file_buffer(StringView filename); +void reload_file_buffer(Buffer& buffer); void write_to_debug_buffer(StringView str); diff --git a/src/client.cc b/src/client.cc index 2a6ccb27..faeac7fd 100644 --- a/src/client.cc +++ b/src/client.cc @@ -186,12 +186,9 @@ void Client::force_redraw() void Client::reload_buffer() { Buffer& buffer = context().buffer(); - if (reload_file_buffer(buffer)) - context().print_status({ format("'{}' reloaded", buffer.display_name()), - get_face("Information") }); - else - context().print_status({ format("could not reload '{}'", buffer.display_name()), - get_face("Error") }); + reload_file_buffer(buffer); + context().print_status({ format("'{}' reloaded", buffer.display_name()), + get_face("Information") }); } void Client::on_buffer_reload_key(Key key) diff --git a/src/commands.cc b/src/commands.cc index 338686b1..311520c8 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -135,7 +135,7 @@ void edit(const ParametersParser& parser, Context& context) Buffer* buffer = buffer_manager.get_buffer_ifp(name); Buffer* oldbuf = &context.buffer(); - // TODO fifo reload + if (force_reload and buffer and buffer->flags() & Buffer::Flags::File) reload_file_buffer(*buffer); else @@ -154,16 +154,11 @@ void edit(const ParametersParser& parser, Context& context) buffer = open_fifo(name, *fifo, (bool)parser.get_switch("scroll")); else if (not buffer) { - buffer = create_file_buffer(name); - if (not buffer) - { - if (parser.get_switch("existing")) - throw runtime_error(format("unable to open '{}'", name)); - + buffer = parser.get_switch("existing") ? open_file_buffer(name) + : open_or_create_file_buffer(name); + if (buffer->flags() & Buffer::Flags::New) context.print_status({ format("new file '{}'", name), get_face("StatusLine") }); - buffer = new Buffer(name, Buffer::Flags::File | Buffer::Flags::New); - } } } diff --git a/src/file.cc b/src/file.cc index c84a9217..ced88614 100644 --- a/src/file.cc +++ b/src/file.cc @@ -165,7 +165,7 @@ MappedFile::MappedFile(StringView filename) if (fd == -1) { if (errno == ENOENT) - return; + throw file_not_found{real_filename}; 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) { const char* ptr = data.data(); diff --git a/src/file.hh b/src/file.hh index b3ba0578..98431adc 100644 --- a/src/file.hh +++ b/src/file.hh @@ -45,14 +45,14 @@ String read_file(StringView filename, bool text = false); struct MappedFile { - int fd; - const char* data = nullptr; - struct stat st {}; - - explicit operator bool() const { return fd != -1; } - MappedFile(StringView filename); ~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); @@ -60,6 +60,7 @@ void write_buffer_to_fd(Buffer& buffer, int fd); void write_buffer_to_backup_file(Buffer& buffer); String find_file(StringView filename, ConstArrayView paths); +bool file_exists(StringView filename); Vector list_files(StringView directory); diff --git a/src/main.cc b/src/main.cc index 63ddd53d..ba372954 100644 --- a/src/main.cc +++ b/src/main.cc @@ -500,8 +500,7 @@ int run_server(StringView session, StringView init_command, { try { - if (create_file_buffer(file) == nullptr) - new Buffer(file.str(), Buffer::Flags::New | Buffer::Flags::File); + open_or_create_file_buffer(file); } catch (Kakoune::runtime_error& error) { @@ -593,7 +592,7 @@ int run_filter(StringView keystr, ConstArrayView files, bool quiet) for (auto& file : files) { - Buffer* buffer = create_file_buffer(file); + Buffer* buffer = open_file_buffer(file); write_buffer_to_file(*buffer, file + ".kak-bak"); apply_keys_to_buffer(*buffer); write_buffer_to_file(*buffer, file); diff --git a/src/normal.cc b/src/normal.cc index eb50642e..db878909 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -193,10 +193,7 @@ void goto_commands(Context& context, NormalParams params) Buffer* buffer = BufferManager::instance().get_buffer_ifp(path); if (not buffer) - buffer = create_file_buffer(path); - - if (buffer == nullptr) - throw runtime_error(format("unable to open file '{}'", path)); + buffer = open_file_buffer(path); if (buffer != &context.buffer()) {