More cleanups in the buffer open/reload code
This commit is contained in:
parent
fe704b9b84
commit
76fadd8108
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
13
src/file.hh
13
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<String> paths);
|
||||
bool file_exists(StringView filename);
|
||||
|
||||
Vector<String> list_files(StringView directory);
|
||||
|
||||
|
|
|
@ -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<StringView> 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);
|
||||
|
|
|
@ -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())
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user