Autoname client on creation, and access client name from shell with $kak_client

This commit is contained in:
Maxime Coste 2013-01-07 13:59:09 +01:00
parent 7acd4e3e6a
commit 8d4678a82e
3 changed files with 40 additions and 3 deletions

View File

@ -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<UserInterface>&& 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>& client)

View File

@ -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<UserInterface>&& ui, Window& window)
Client(std::unique_ptr<UserInterface>&& 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;

View File

@ -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()