From 8d4678a82e1dbafb429782d4f90070e415290e8c Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 7 Jan 2013 13:59:09 +0100 Subject: [PATCH] Autoname client on creation, and access client name from shell with $kak_client --- src/client_manager.cc | 32 +++++++++++++++++++++++++++++++- src/client_manager.hh | 8 ++++++-- src/main.cc | 3 +++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/client_manager.cc b/src/client_manager.cc index 020a4d9e..8e2c3024 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -7,11 +7,31 @@ namespace Kakoune { +String ClientManager::generate_name() const +{ + for (int i = 0; true; ++i) + { + String name = "unnamed" + int_to_str(i); + bool found = false; + for (auto& client : m_clients) + { + if (client->name == name) + { + found = true; + break; + } + } + if (not found) + return name; + } +} + void ClientManager::create_client(std::unique_ptr&& ui, int event_fd, const String& init_commands) { Buffer& buffer = **BufferManager::instance().begin(); - m_clients.emplace_back(new Client{std::move(ui), get_unused_window_for_buffer(buffer)}); + m_clients.emplace_back(new Client{std::move(ui), get_unused_window_for_buffer(buffer), + generate_name()}); InputHandler* input_handler = &m_clients.back()->input_handler; Context* context = &m_clients.back()->context; @@ -131,6 +151,16 @@ void ClientManager::set_client_name(Context& context, String name) throw runtime_error("no client for current context"); } +String ClientManager::get_client_name(const Context& context) +{ + for (auto& client : m_clients) + { + if (&client->context == &context) + return client->name; + } + throw runtime_error("no client for current context"); +} + Context& ClientManager::get_client_context(const String& name) { auto it = find_if(m_clients, [&name](std::unique_ptr& client) diff --git a/src/client_manager.hh b/src/client_manager.hh index 1b00c50f..234df8b9 100644 --- a/src/client_manager.hh +++ b/src/client_manager.hh @@ -24,15 +24,19 @@ public: void redraw_clients() const; void set_client_name(Context& context, String name); + String get_client_name(const Context& context); Context& get_client_context(const String& name); private: void remove_client_by_context(Context& context); + String generate_name() const; struct Client { - Client(std::unique_ptr&& ui, Window& window) + Client(std::unique_ptr&& ui, Window& window, + String name) : user_interface(std::move(ui)), - context(input_handler, window, *user_interface) {} + context(input_handler, window, *user_interface), + name(std::move(name)) {} Client(Client&&) = delete; Client& operator=(Client&& other) = delete; diff --git a/src/main.cc b/src/main.cc index 19513ff6..f21dcde6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -640,6 +640,9 @@ void register_env_vars() shell_manager.register_env_var("socket", [](const String& name, const Context& context) { return Server::instance().filename(); }); + shell_manager.register_env_var("client", + [](const String& name, const Context& context) + { return ClientManager::instance().get_client_name(context); }); } void register_registers()