Add -scratch and -file switches to the rename-buffer command

This commit is contained in:
Maxime Coste 2019-12-07 15:39:54 +11:00
parent 42094209fd
commit 15df0fc781
2 changed files with 20 additions and 4 deletions

View File

@ -106,8 +106,9 @@ of the file onto the filesystem
*alias* db +
delete current buffer or the buffer <name> if specified
*rename-buffer* <name>::
set current buffer name
*rename-buffer* [-file|-scratch] <name>::
set current buffer name, if *-scratch* or *-file* is given, ensure
the buffer is set to the corresponding type.
*source* <filename> <param>...::
execute commands in <filename>

View File

@ -823,13 +823,28 @@ const CommandDesc rename_buffer_cmd = {
"rename-buffer",
nullptr,
"rename-buffer <name>: 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<false>,
[](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]));
}
};