diff --git a/src/buffer_manager.cc b/src/buffer_manager.cc index e7d7d5c2b..efd0c9641 100644 --- a/src/buffer_manager.cc +++ b/src/buffer_manager.cc @@ -89,15 +89,6 @@ Buffer& BufferManager::get_buffer(StringView name) return *res; } -void BufferManager::set_last_used_buffer(Buffer& buffer) -{ - auto it = find_if(m_buffers, [&buffer](const SafePtr& p) - { return p.get() == &buffer; }); - kak_assert(it != m_buffers.end()); - m_buffers.erase(it); - m_buffers.emplace(m_buffers.begin(), &buffer); -} - void BufferManager::backup_modified_buffers() { for (auto& buf : m_buffers) diff --git a/src/buffer_manager.hh b/src/buffer_manager.hh index ec321b7f4..014d241ee 100644 --- a/src/buffer_manager.hh +++ b/src/buffer_manager.hh @@ -29,7 +29,6 @@ public: Buffer* get_buffer_ifp(StringView name); Buffer& get_buffer(StringView name); - void set_last_used_buffer(Buffer& buffer); void backup_modified_buffers(); diff --git a/src/client.cc b/src/client.cc index faeac7fd6..79d1df7aa 100644 --- a/src/client.cc +++ b/src/client.cc @@ -137,6 +137,8 @@ void Client::change_buffer(Buffer& buffer) if (m_buffer_reload_dialog_opened) close_buffer_reload_dialog(); + m_last_buffer = &m_window->buffer(); + auto& client_manager = ClientManager::instance(); m_window->options().unregister_watcher(*this); client_manager.add_free_window(std::move(m_window), diff --git a/src/client.hh b/src/client.hh index 58df582e5..f16bd09bf 100644 --- a/src/client.hh +++ b/src/client.hh @@ -52,6 +52,9 @@ public: StringView get_env_var(StringView name) const; + Buffer* last_buffer() const { return m_last_buffer.get(); } + void set_last_buffer(Buffer* last_buffer) { m_last_buffer = last_buffer; } + private: void on_option_changed(const Option& option) override; @@ -77,6 +80,8 @@ private: Vector m_pending_keys; bool m_buffer_reload_dialog_opened = false; + + SafePtr m_last_buffer; }; } diff --git a/src/client_manager.cc b/src/client_manager.cc index ceceb15fd..40bf1d021 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -109,6 +109,8 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer) for (auto& client : m_clients) { client->context().forget_jumps_to_buffer(buffer); + if (client->last_buffer() == &buffer) + client->set_last_buffer(nullptr); if (&client->context().buffer() != &buffer) continue; diff --git a/src/commands.cc b/src/commands.cc index fc6860ed3..8f705ba78 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -134,7 +134,6 @@ void edit(const ParametersParser& parser, Context& context, const ShellContext&) auto& buffer_manager = BufferManager::instance(); Buffer* buffer = buffer_manager.get_buffer_ifp(name); - Buffer* oldbuf = &context.buffer(); if (force_reload and buffer and buffer->flags() & Buffer::Flags::File) reload_file_buffer(*buffer); @@ -143,11 +142,7 @@ void edit(const ParametersParser& parser, Context& context, const ShellContext&) if (parser.get_switch("scratch")) { if (Buffer* buf = buffer_manager.get_buffer_ifp(name)) - { buffer_manager.delete_buffer(*buf); - if (buf == oldbuf) - oldbuf = nullptr; - } buffer = new Buffer(name, Buffer::Flags::None); } else if (auto fifo = parser.get_switch("fifo")) @@ -162,9 +157,6 @@ void edit(const ParametersParser& parser, Context& context, const ShellContext&) } } - if (oldbuf) - buffer_manager.set_last_used_buffer(*oldbuf); - const size_t param_count = parser.positional_count(); if (buffer != &context.buffer() or param_count > 1) context.push_jump(); @@ -379,12 +371,9 @@ const CommandDesc buffer_cmd = { buffer_completer, [](const ParametersParser& parser, Context& context, const ShellContext&) { - Buffer* oldbuf = &context.buffer(); Buffer& buffer = BufferManager::instance().get_buffer(parser[0]); - - if (&buffer != oldbuf) + if (&buffer != &context.buffer()) { - BufferManager::instance().set_last_used_buffer(*oldbuf); context.push_jump(); context.change_buffer(buffer); } @@ -417,7 +406,6 @@ void cycle_buffer(const ParametersParser& parser, Context& context, const ShellC if (newbuf != oldbuf) { - BufferManager::instance().set_last_used_buffer(*oldbuf); context.push_jump(); context.change_buffer(*newbuf); } diff --git a/src/normal.cc b/src/normal.cc index 283474133..a7e0a5159 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -160,14 +160,15 @@ void goto_commands(Context& context, NormalParams params) break; case 'a': { - auto& buffer_manager = BufferManager::instance(); - auto it = buffer_manager.begin(); - if (it->get() == &buffer and ++it == buffer_manager.end()) + Buffer* target = nullptr; + if (not context.has_client() or + not (target = context.client().last_buffer())) + { + context.print_status({"no last buffer", get_face("Error")}); break; - Buffer& target = **it; - BufferManager::instance().set_last_used_buffer(buffer); + } context.push_jump(); - context.change_buffer(target); + context.change_buffer(*target); break; } case 'f': @@ -195,8 +196,6 @@ void goto_commands(Context& context, NormalParams params) if (buffer != &context.buffer()) { - Buffer* oldbuf = &context.buffer(); - BufferManager::instance().set_last_used_buffer(*oldbuf); context.push_jump(); context.change_buffer(*buffer); } @@ -1113,7 +1112,6 @@ void jump(Context& context, NormalParams) Buffer* oldbuf = &context.buffer(); Buffer& buffer = const_cast(jump.buffer()); - BufferManager::instance().set_last_used_buffer(buffer); if (&buffer != oldbuf) context.change_buffer(buffer); context.selections_write_only() = jump; diff --git a/src/ref_ptr.hh b/src/ref_ptr.hh index 2c284dae6..33a561354 100644 --- a/src/ref_ptr.hh +++ b/src/ref_ptr.hh @@ -38,6 +38,14 @@ struct RefPtr return *this; } + RefPtr& operator=(T* ptr) + { + release(); + m_ptr = ptr; + acquire(); + return *this; + } + [[gnu::always_inline]] T* operator->() const { return m_ptr; } [[gnu::always_inline]]