summaryrefslogtreecommitdiff
path: root/src/main.cc
diff options
context:
space:
mode:
authorFrank LENORMAND <lenormf@gmail.com>2016-07-20 20:45:50 +0300
committerFrank LENORMAND <lenormf@gmail.com>2016-07-23 10:03:21 +0300
commit8a4596bea9d85c86d03efa129c41530faa2a34a7 (patch)
tree8cd6a789c734e8507042fb5126cf692dd3ae789c /src/main.cc
parent88a9607552fd3c614e6d70a271b663323306a8db (diff)
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
Diffstat (limited to 'src/main.cc')
-rw-r--r--src/main.cc16
1 files changed, 12 insertions, 4 deletions
diff --git a/src/main.cc b/src/main.cc
index 82444571..020c1e59 100644
--- a/src/main.cc
+++ b/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)