diff --git a/README.asciidoc b/README.asciidoc index 7b1a69d9..c59605eb 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -798,6 +798,7 @@ Some of Kakoune state is available through environment variables: * `kak_reg_`: value of register * `kak_session`: name of the current session * `kak_client`: name of current client + * `kak_client_pid`: pid of the current client * `kak_modified`: buffer has modifications not saved * `kak_source`: path of the file currently getting executed (through the source command) * `kak_cursor_line`: line of the end of the main selection diff --git a/doc/manpages/expansions.asciidoc b/doc/manpages/expansions.asciidoc index 96783a25..5e4bdc79 100644 --- a/doc/manpages/expansions.asciidoc +++ b/doc/manpages/expansions.asciidoc @@ -86,6 +86,8 @@ informations about Kakoune's state: name of the current session *kak_client*:: name of current client +*kak_client_pid*:: + process id of current client *kak_source*:: path of the file currently getting executed (through the source command) diff --git a/src/client.cc b/src/client.cc index dea670d6..f47f3189 100644 --- a/src/client.cc +++ b/src/client.cc @@ -24,11 +24,12 @@ namespace Kakoune Client::Client(std::unique_ptr&& ui, std::unique_ptr&& window, - SelectionList selections, + SelectionList selections, int pid, EnvVarMap env_vars, String name, OnExitCallback on_exit) : m_ui{std::move(ui)}, m_window{std::move(window)}, + m_pid{pid}, m_on_exit{std::move(on_exit)}, m_input_handler{std::move(selections), Context::Flags::None, std::move(name)}, diff --git a/src/client.hh b/src/client.hh index d870871b..fd80ba24 100644 --- a/src/client.hh +++ b/src/client.hh @@ -29,7 +29,7 @@ public: Client(std::unique_ptr&& ui, std::unique_ptr&& window, SelectionList selections, - EnvVarMap env_vars, + int pid, EnvVarMap env_vars, String name, OnExitCallback on_exit); ~Client(); @@ -69,6 +69,8 @@ public: void exit(int status) { m_on_exit(status); } + int pid() const { return m_pid; } + private: void on_option_changed(const Option& option) override; @@ -83,6 +85,8 @@ private: std::unique_ptr m_ui; std::unique_ptr m_window; + const int m_pid; + OnExitCallback m_on_exit; EnvVarMap m_env_vars; diff --git a/src/client_manager.cc b/src/client_manager.cc index 6525b854..bec1b2ea 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -38,7 +38,7 @@ String ClientManager::generate_name() const } } -Client* ClientManager::create_client(std::unique_ptr&& ui, +Client* ClientManager::create_client(std::unique_ptr&& ui, int pid, EnvVarMap env_vars, StringView init_cmds, Optional init_coord, Client::OnExitCallback on_exit) @@ -46,8 +46,9 @@ Client* ClientManager::create_client(std::unique_ptr&& ui, Buffer& buffer = BufferManager::instance().get_first_buffer(); WindowAndSelections ws = get_free_window(buffer); Client* client = new Client{std::move(ui), std::move(ws.window), - std::move(ws.selections), std::move(env_vars), - generate_name(), std::move(on_exit)}; + std::move(ws.selections), pid, + std::move(env_vars), generate_name(), + std::move(on_exit)}; m_clients.emplace_back(client); if (init_coord) diff --git a/src/client_manager.hh b/src/client_manager.hh index f0a3a59d..5508b519 100644 --- a/src/client_manager.hh +++ b/src/client_manager.hh @@ -19,7 +19,7 @@ public: ClientManager(); ~ClientManager(); - Client* create_client(std::unique_ptr&& ui, + Client* create_client(std::unique_ptr&& ui, int pid, EnvVarMap env_vars, StringView init_cmds, Optional init_coord, Client::OnExitCallback on_exit); diff --git a/src/main.cc b/src/main.cc index 0d4f0c78..81ad37bc 100644 --- a/src/main.cc +++ b/src/main.cc @@ -126,6 +126,10 @@ void register_env_vars() "client", false, [](StringView name, const Context& context) -> String { return context.name(); } + }, { + "client_pid", false, + [](StringView name, const Context& context) -> String + { return to_string(context.client().pid()); } }, { "modified", false, [](StringView name, const Context& context) -> String @@ -475,7 +479,8 @@ int run_client(StringView session, StringView client_init, try { EventManager event_manager; - RemoteClient client{session, make_ui(ui_type), get_env_vars(), client_init, std::move(init_coord)}; + RemoteClient client{session, make_ui(ui_type), getpid(), get_env_vars(), + client_init, std::move(init_coord)}; while (not client.exit_status()) event_manager.handle_next_events(EventMode::Normal); return *client.exit_status(); @@ -614,7 +619,7 @@ int run_server(StringView session, StringView server_init, if (not (flags & ServerFlags::Daemon)) { local_client = client_manager.create_client( - create_local_ui(ui_type), get_env_vars(), client_init, std::move(init_coord), + create_local_ui(ui_type), getpid(), get_env_vars(), client_init, std::move(init_coord), [](int status) { local_client_exit = status; }); if (startup_error) diff --git a/src/remote.cc b/src/remote.cc index d88195f8..c26aabb7 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -552,7 +552,7 @@ bool check_session(StringView session) } RemoteClient::RemoteClient(StringView session, std::unique_ptr&& ui, - const EnvVarMap& env_vars, StringView init_command, + int pid, const EnvVarMap& env_vars, StringView init_command, Optional init_coord) : m_ui(std::move(ui)) { @@ -560,6 +560,7 @@ RemoteClient::RemoteClient(StringView session, std::unique_ptr&& { MsgWriter msg{m_send_buffer, MessageType::Connect}; + msg.write(pid); msg.write(init_command); msg.write(init_coord); msg.write(m_ui->dimensions()); @@ -706,13 +707,14 @@ private: { case MessageType::Connect: { + auto pid = m_reader.read(); auto init_cmds = m_reader.read(); auto init_coord = m_reader.read_optional(); auto dimensions = m_reader.read(); auto env_vars = m_reader.read_hash_map(); auto* ui = new RemoteUI{sock, dimensions}; if (auto* client = ClientManager::instance().create_client( - std::unique_ptr(ui), + std::unique_ptr(ui), pid, std::move(env_vars), init_cmds, init_coord, [ui](int status) { ui->exit(status); })) ui->set_client(client); diff --git a/src/remote.hh b/src/remote.hh index 84382dc4..f716d243 100644 --- a/src/remote.hh +++ b/src/remote.hh @@ -30,7 +30,7 @@ class RemoteClient { public: RemoteClient(StringView session, std::unique_ptr&& ui, - const EnvVarMap& env_vars, StringView init_command, + int pid, const EnvVarMap& env_vars, StringView init_command, Optional init_coord); const Optional& exit_status() const { return m_exit_status; }