From 15df0fc78133059b28545bf0508fbd20c6b02c92 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 7 Dec 2019 15:39:54 +1100 Subject: [PATCH] Add -scratch and -file switches to the rename-buffer command --- doc/pages/commands.asciidoc | 5 +++-- src/commands.cc | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/doc/pages/commands.asciidoc b/doc/pages/commands.asciidoc index a7ee11243..e182696e2 100644 --- a/doc/pages/commands.asciidoc +++ b/doc/pages/commands.asciidoc @@ -106,8 +106,9 @@ of the file onto the filesystem *alias* db + delete current buffer or the buffer if specified -*rename-buffer* :: - set current buffer name +*rename-buffer* [-file|-scratch] :: + set current buffer name, if *-scratch* or *-file* is given, ensure + the buffer is set to the corresponding type. *source* ...:: execute commands in diff --git a/src/commands.cc b/src/commands.cc index b3277ebe5..2d5b2d815 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -823,13 +823,28 @@ const CommandDesc rename_buffer_cmd = { "rename-buffer", nullptr, "rename-buffer : change current buffer name", - single_param, + ParameterDesc{ + { + { "scratch", { false, "convert a file buffer to a scratch buffer" } }, + { "file", { false, "convert a scratch buffer to a file buffer" } } + }, + ParameterDesc::Flags::None, 1, 1 + }, CommandFlags::None, CommandHelper{}, filename_completer, [](const ParametersParser& parser, Context& context, const ShellContext&) { - if (not context.buffer().set_name(parser[0])) + if (parser.get_switch("scratch") and parser.get_switch("file")) + throw runtime_error("scratch and file are incompatible switches"); + + auto& buffer = context.buffer(); + if (parser.get_switch("scratch")) + buffer.flags() &= ~(Buffer::Flags::File | Buffer::Flags::New); + if (parser.get_switch("file")) + buffer.flags() |= Buffer::Flags::File; + + if (not buffer.set_name(parser[0])) throw runtime_error(format("unable to change buffer name to '{}': a buffer with this name already exists", parser[0])); } };