Implement a readonly
mode
This commit introduces the `readonly` variable as well as the `-ro` command line option which prevent buffers from being overwritten on disk when the `write` command is used without arguments. Some buffers can selectively be put in readonly mode by setting the `readonly` variable on the `buffer` scope, the `global` mode will affect all buffers (even those who will be open subsequently), using the `window` scope will have no effect. Closes #685
This commit is contained in:
parent
88a9607552
commit
8a4596bea9
|
@ -591,6 +591,13 @@ void Buffer::set_fs_timestamp(timespec ts)
|
|||
|
||||
void Buffer::on_option_changed(const Option& option)
|
||||
{
|
||||
if (option.name() == "readonly")
|
||||
{
|
||||
if (option.get<bool>())
|
||||
m_flags |= Flags::ReadOnly;
|
||||
else
|
||||
m_flags &= ~Flags::ReadOnly;
|
||||
}
|
||||
run_hook_in_own_context("BufSetOption",
|
||||
format("{}={}", option.name(), option.get_as_string()));
|
||||
}
|
||||
|
|
|
@ -106,7 +106,8 @@ public:
|
|||
New = 1 << 1,
|
||||
Fifo = 1 << 2,
|
||||
NoUndo = 1 << 3,
|
||||
Debug = 1 << 4
|
||||
Debug = 1 << 4,
|
||||
ReadOnly = 1 << 5,
|
||||
};
|
||||
|
||||
Buffer(String name, Flags flags, StringView data = {},
|
||||
|
|
|
@ -78,7 +78,8 @@ void BufferManager::backup_modified_buffers()
|
|||
{
|
||||
for (auto& buf : m_buffers)
|
||||
{
|
||||
if ((buf->flags() & Buffer::Flags::File) and buf->is_modified())
|
||||
if ((buf->flags() & Buffer::Flags::File) and buf->is_modified()
|
||||
and !(buf->flags() & Buffer::Flags::ReadOnly))
|
||||
write_buffer_to_backup_file(*buf);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -239,6 +239,12 @@ void write_buffer(const ParametersParser& parser, Context& context, const ShellC
|
|||
if (parser.positional_count() == 0 and !(buffer.flags() & Buffer::Flags::File))
|
||||
throw runtime_error("cannot write a non file buffer without a filename");
|
||||
|
||||
// if the buffer is in read-only mode and we try to save it directly
|
||||
// or we try to write to it indirectly using e.g. a symlink, throw an error
|
||||
if ((context.buffer().flags() & Buffer::Flags::ReadOnly)
|
||||
&& (parser.positional_count() == 0 || real_path(parser[0]) == buffer.name()))
|
||||
throw runtime_error("cannot overwrite the buffer when in readonly mode");
|
||||
|
||||
auto filename = parser.positional_count() == 0 ?
|
||||
buffer.name() : parse_filename(parser[0]);
|
||||
write_buffer_to_file(buffer, filename);
|
||||
|
@ -260,7 +266,8 @@ void write_all_buffers()
|
|||
{
|
||||
for (auto& buffer : BufferManager::instance())
|
||||
{
|
||||
if ((buffer->flags() & Buffer::Flags::File) and buffer->is_modified())
|
||||
if ((buffer->flags() & Buffer::Flags::File) and buffer->is_modified()
|
||||
and !(buffer->flags() & Buffer::Flags::ReadOnly))
|
||||
write_buffer_to_file(*buffer, buffer->name());
|
||||
}
|
||||
}
|
||||
|
|
16
src/main.cc
16
src/main.cc
|
@ -265,6 +265,7 @@ void register_options()
|
|||
reg.declare_option("modelinefmt", "format string used to generate the modeline",
|
||||
"%val{bufname} %val{cursor_line}:%val{cursor_char_column} "_str);
|
||||
reg.declare_option("debug", "various debug flags", DebugFlags::None);
|
||||
reg.declare_option("readonly", "prevent buffers from being modified", false);
|
||||
}
|
||||
|
||||
struct convert_to_client_mode
|
||||
|
@ -460,7 +461,7 @@ int run_client(StringView session, StringView init_command, UIType ui_type)
|
|||
}
|
||||
|
||||
int run_server(StringView session, StringView init_command,
|
||||
bool ignore_kakrc, bool daemon, UIType ui_type,
|
||||
bool ignore_kakrc, bool daemon, bool readonly, UIType ui_type,
|
||||
ConstArrayView<StringView> files, ByteCoord target_coord)
|
||||
{
|
||||
static bool terminate = false;
|
||||
|
@ -503,6 +504,8 @@ int run_server(StringView session, StringView init_command,
|
|||
|
||||
write_to_debug_buffer("*** This is the debug buffer, where debug info will be written ***");
|
||||
|
||||
GlobalScope::instance().options().get_local_option("readonly").set(readonly);
|
||||
|
||||
Server server(session.empty() ? to_string(getpid()) : session.str());
|
||||
|
||||
bool startup_error = false;
|
||||
|
@ -536,7 +539,9 @@ int run_server(StringView session, StringView init_command,
|
|||
{
|
||||
try
|
||||
{
|
||||
open_or_create_file_buffer(file);
|
||||
Buffer *buffer = open_or_create_file_buffer(file);
|
||||
if (readonly)
|
||||
buffer->flags() |= Buffer::Flags::ReadOnly;
|
||||
}
|
||||
catch (Kakoune::runtime_error& error)
|
||||
{
|
||||
|
@ -741,7 +746,8 @@ int main(int argc, char* argv[])
|
|||
{ "q", { false, "in filter mode, be quiet about errors applying keys" } },
|
||||
{ "ui", { true, "set the type of user interface to use (ncurses, dummy, or json)" } },
|
||||
{ "l", { false, "list existing sessions" } },
|
||||
{ "clear", { false, "clear dead sessions" } } }
|
||||
{ "clear", { false, "clear dead sessions" } },
|
||||
{ "ro", { false, "readonly mode" } } }
|
||||
};
|
||||
try
|
||||
{
|
||||
|
@ -793,7 +799,8 @@ int main(int argc, char* argv[])
|
|||
for (size_t i = 0; i < parser.positional_count(); ++i)
|
||||
files.emplace_back(parser[i]);
|
||||
|
||||
return run_filter(*keys, init_command, files, (bool)parser.get_switch("q"));
|
||||
return run_filter(*keys, init_command, files,
|
||||
(bool)parser.get_switch("q"));
|
||||
}
|
||||
|
||||
if (auto server_session = parser.get_switch("c"))
|
||||
|
@ -840,6 +847,7 @@ int main(int argc, char* argv[])
|
|||
return run_server(session, init_command,
|
||||
(bool)parser.get_switch("n"),
|
||||
(bool)parser.get_switch("d"),
|
||||
(bool)parser.get_switch("ro"),
|
||||
ui_type, files, target_coord);
|
||||
}
|
||||
catch (convert_to_client_mode& convert)
|
||||
|
|
Loading…
Reference in New Issue
Block a user