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 begin() const { return iterator(m_buffers.begin()); }
iterator end() const { return iterator(m_buffers.end()); } iterator end() const { return iterator(m_buffers.end()); }
size_t count() const { return m_buffers.size(); }
Buffer* get_buffer(const String& name); Buffer* get_buffer(const String& name);

View File

@ -295,9 +295,38 @@ void show_buffer(const CommandParameters& params, const Context& context)
Buffer* buffer = BufferManager::instance().get_buffer(params[0]); Buffer* buffer = BufferManager::instance().get_buffer(params[0]);
if (not buffer) if (not buffer)
NCurses::print_status("buffer " + params[0] + " does not exists"); throw runtime_error("buffer " + params[0] + " does not exists");
else
main_context = Context(*buffer->get_or_create_window()); 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) void add_highlighter(const CommandParameters& params, const Context& context)
@ -685,6 +714,12 @@ void register_commands()
[](const String& prefix, size_t cursor_pos) [](const String& prefix, size_t cursor_pos)
{ return BufferManager::instance().complete_buffername(prefix, 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, cm.register_commands({ "ah", "addhl" }, add_highlighter,
CommandManager::None, CommandManager::None,
[](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token) [](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token)