Add a delbuf (db) command which delete an existing buffer

This commit is contained in:
Maxime Coste 2012-05-28 22:51:12 +00:00
parent 393b9b24e2
commit d96427b831
2 changed files with 39 additions and 3 deletions

View File

@ -31,6 +31,7 @@ public:
iterator begin() const { return iterator(m_buffers.begin()); }
iterator end() const { return iterator(m_buffers.end()); }
size_t count() const { return m_buffers.size(); }
Buffer* get_buffer(const String& name);

View File

@ -295,11 +295,40 @@ void show_buffer(const CommandParameters& params, const Context& context)
Buffer* buffer = BufferManager::instance().get_buffer(params[0]);
if (not buffer)
NCurses::print_status("buffer " + params[0] + " does not exists");
else
throw runtime_error("buffer " + params[0] + " does not exists");
main_context = Context(*buffer->get_or_create_window());
}
void delete_buffer(const CommandParameters& params, const Context& context)
{
if (params.size() != 1)
throw wrong_argument_count();
BufferManager& manager = BufferManager::instance();
Buffer* buffer = manager.get_buffer(params[0]);
if (not buffer)
throw runtime_error("buffer " + params[0] + " does not exists");
if (buffer->type()!= Buffer::Type::Scratch and buffer->is_modified())
throw runtime_error("buffer " + params[0] + " is modified");
if (&main_context.buffer() == buffer)
{
if (manager.count() == 1)
throw runtime_error("buffer " + params[0] + " is the last one");
for (Buffer& buf : manager)
{
if (&buf != buffer)
{
main_context = Context(*buf.get_or_create_window());
break;
}
}
}
delete buffer;
}
void add_highlighter(const CommandParameters& params, const Context& context)
{
ParametersParser parser(params, { { "group", true } });
@ -685,6 +714,12 @@ void register_commands()
[](const String& prefix, size_t cursor_pos)
{ return BufferManager::instance().complete_buffername(prefix, cursor_pos); }
}));
cm.register_commands({ "db", "delbuf" }, delete_buffer,
CommandManager::None,
PerArgumentCommandCompleter({
[](const String& prefix, size_t cursor_pos)
{ return BufferManager::instance().complete_buffername(prefix, cursor_pos); }
}));
cm.register_commands({ "ah", "addhl" }, add_highlighter,
CommandManager::None,
[](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token)