BufferManager: replace Buffer* get_buffer with Buffer& get_buffer

This commit is contained in:
Maxime Coste 2013-03-21 19:09:31 +01:00
parent df79b7610a
commit e5d93c2194
5 changed files with 24 additions and 28 deletions

View File

@ -42,7 +42,7 @@ void BufferManager::unregister_buffer(Buffer& buffer)
assert(false); assert(false);
} }
Buffer* BufferManager::get_buffer(const String& name) Buffer* BufferManager::get_buffer_ifp(const String& name)
{ {
for (auto& buf : m_buffers) for (auto& buf : m_buffers)
{ {
@ -52,6 +52,14 @@ Buffer* BufferManager::get_buffer(const String& name)
return nullptr; return nullptr;
} }
Buffer& BufferManager::get_buffer(const String& name)
{
Buffer* res = get_buffer_ifp(name);
if (not res)
throw runtime_error("no such buffer '" + name + "'");
return *res;
}
void BufferManager::set_last_used_buffer(Buffer& buffer) void BufferManager::set_last_used_buffer(Buffer& buffer)
{ {
auto it = m_buffers.begin(); auto it = m_buffers.begin();

View File

@ -26,7 +26,8 @@ public:
iterator end() const { return m_buffers.cend(); } iterator end() const { return m_buffers.cend(); }
size_t count() const { return m_buffers.size(); } size_t count() const { return m_buffers.size(); }
Buffer* get_buffer(const String& name); Buffer* get_buffer_ifp(const String& name);
Buffer& get_buffer(const String& name);
void set_last_used_buffer(Buffer& buffer); void set_last_used_buffer(Buffer& buffer);
CandidateList complete_buffername(const String& prefix, CandidateList complete_buffername(const String& prefix,

View File

@ -98,7 +98,7 @@ void edit(const CommandParameters& params, Context& context)
Buffer* buffer = nullptr; Buffer* buffer = nullptr;
if (not force_reload) if (not force_reload)
buffer = BufferManager::instance().get_buffer(name); buffer = BufferManager::instance().get_buffer_ifp(name);
if (not buffer) if (not buffer)
{ {
if (parser.has_option("scratch")) if (parser.has_option("scratch"))
@ -209,18 +209,14 @@ void show_buffer(const CommandParameters& params, Context& context)
if (params.size() != 1) if (params.size() != 1)
throw wrong_argument_count(); throw wrong_argument_count();
const String& buffer_name = params[0]; Buffer& buffer = BufferManager::instance().get_buffer(params[0]);
Buffer* buffer = BufferManager::instance().get_buffer(buffer_name); BufferManager::instance().set_last_used_buffer(buffer);
if (not buffer)
throw runtime_error("buffer " + buffer_name + " does not exists");
BufferManager::instance().set_last_used_buffer(*buffer); if (&buffer != &context.buffer())
if (buffer != &context.buffer())
{ {
context.push_jump(); context.push_jump();
auto& manager = ClientManager::instance(); auto& manager = ClientManager::instance();
context.change_editor(manager.get_unused_window_for_buffer(*buffer)); context.change_editor(manager.get_unused_window_for_buffer(buffer));
} }
} }
@ -231,24 +227,15 @@ void delete_buffer(const CommandParameters& params, Context& context)
throw wrong_argument_count(); throw wrong_argument_count();
BufferManager& manager = BufferManager::instance(); BufferManager& manager = BufferManager::instance();
Buffer* buffer = nullptr; Buffer& buffer = params.empty() ? context.buffer() : manager.get_buffer(params[0]);
if (params.empty()) if (not force and (buffer.flags() & Buffer::Flags::File) and buffer.is_modified())
buffer = &context.buffer(); throw runtime_error("buffer " + buffer.name() + " is modified");
else
{
const String& buffer_name = params[0];
buffer = manager.get_buffer(buffer_name);
if (not buffer)
throw runtime_error("buffer " + buffer_name + " does not exists");
}
if (not force and (buffer->flags() & Buffer::Flags::File) and buffer->is_modified())
throw runtime_error("buffer " + buffer->name() + " is modified");
if (manager.count() == 1) if (manager.count() == 1)
throw runtime_error("buffer " + buffer->name() + " is the last one"); throw runtime_error("buffer " + buffer.name() + " is the last one");
ClientManager::instance().ensure_no_client_uses_buffer(*buffer); ClientManager::instance().ensure_no_client_uses_buffer(buffer);
delete buffer; delete &buffer;
} }
template<typename Group> template<typename Group>

View File

@ -10,7 +10,7 @@ namespace Kakoune
static Buffer& get_or_create_debug_buffer() static Buffer& get_or_create_debug_buffer()
{ {
static const String debug_buffer_name("*debug*"); static const String debug_buffer_name("*debug*");
Buffer* buffer = BufferManager::instance().get_buffer(debug_buffer_name); Buffer* buffer = BufferManager::instance().get_buffer_ifp(debug_buffer_name);
if (not buffer) if (not buffer)
buffer = new Buffer(debug_buffer_name, Buffer::Flags::NoUndo); buffer = new Buffer(debug_buffer_name, Buffer::Flags::NoUndo);

View File

@ -91,7 +91,7 @@ Buffer* create_buffer_from_file(const String& filename)
const char* data = (const char*)mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); const char* data = (const char*)mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
auto cleanup = on_scope_end([&]{ munmap((void*)data, st.st_size); close(fd); }); auto cleanup = on_scope_end([&]{ munmap((void*)data, st.st_size); close(fd); });
if (Buffer* buffer = BufferManager::instance().get_buffer(filename)) if (Buffer* buffer = BufferManager::instance().get_buffer_ifp(filename))
delete buffer; delete buffer;
const char* pos = data; const char* pos = data;