Make it possible to edit an existing scratch buffer again

And make `edit! -scratch *scratch*` for the recreation of an empty
buffer.

Fixes #2852
This commit is contained in:
Maxime Coste 2019-04-12 13:52:26 +10:00
parent ef1fd3acb9
commit 91337d6091

View File

@ -322,9 +322,9 @@ struct CommandDesc
template<bool force_reload> template<bool force_reload>
void edit(const ParametersParser& parser, Context& context, const ShellContext&) void edit(const ParametersParser& parser, Context& context, const ShellContext&)
{ {
if (parser.positional_count() == 0 and const bool scratch = (bool)parser.get_switch("scratch");
not force_reload and
not parser.get_switch("scratch")) if (parser.positional_count() == 0 and not force_reload and not scratch)
throw wrong_argument_count(); throw wrong_argument_count();
const bool no_hooks = context.hooks_disabled(); const bool no_hooks = context.hooks_disabled();
@ -332,35 +332,36 @@ void edit(const ParametersParser& parser, Context& context, const ShellContext&)
(parser.get_switch("debug") ? Buffer::Flags::Debug : Buffer::Flags::None); (parser.get_switch("debug") ? Buffer::Flags::Debug : Buffer::Flags::None);
auto& buffer_manager = BufferManager::instance(); auto& buffer_manager = BufferManager::instance();
if (parser.get_switch("scratch")) auto generate_scratch_name = [&] {
for (int i = 0; true; ++i)
{
String name = format("*scratch-{}*", i);
if (buffer_manager.get_buffer_ifp(name) == nullptr)
return name;
}
};
const auto& name = parser.positional_count() > 0 ?
parser[0] : (scratch ? generate_scratch_name() : context.buffer().name());
Buffer* buffer = buffer_manager.get_buffer_ifp(name);
if (scratch)
{ {
if (parser.get_switch("readonly") or parser.get_switch("fifo") or parser.get_switch("scroll")) if (parser.get_switch("readonly") or parser.get_switch("fifo") or parser.get_switch("scroll"))
throw runtime_error("scratch is not compatible with readonly, fifo or scroll"); throw runtime_error("scratch is not compatible with readonly, fifo or scroll");
String name; if (buffer == nullptr or force_reload)
if (parser.positional_count() > 0)
name = parser[0];
else
{ {
for (int i = 0; true; ++i) if (buffer != nullptr and force_reload)
buffer_manager.delete_buffer(*buffer);
buffer = buffer_manager.create_buffer(std::move(name), flags);
}
else if (buffer->flags() & Buffer::Flags::File)
throw runtime_error(format("buffer '{}' exists but is not a scratch buffer", name));
}
else if (force_reload and buffer and buffer->flags() & Buffer::Flags::File)
{ {
name = format("*scratch-{}*", i);
if (buffer_manager.get_buffer_ifp(name) == nullptr)
break;
}
}
Buffer* buffer = buffer_manager.create_buffer(std::move(name), flags);
context.change_buffer(*buffer);
return;
}
auto& name = parser.positional_count() > 0 ? parser[0]
: context.buffer().name();
Buffer* buffer = buffer_manager.get_buffer_ifp(name);
if (force_reload and buffer and buffer->flags() & Buffer::Flags::File)
reload_file_buffer(*buffer); reload_file_buffer(*buffer);
}
else else
{ {
if (auto fifo = parser.get_switch("fifo")) if (auto fifo = parser.get_switch("fifo"))