Expose client pid as $kak_client_pid

As requested in #1414
This commit is contained in:
Maxime Coste 2017-08-28 13:12:15 +07:00
parent 430b7882eb
commit e80b36b495
9 changed files with 27 additions and 11 deletions

View File

@ -798,6 +798,7 @@ Some of Kakoune state is available through environment variables:
* `kak_reg_<r>`: value of register <r>
* `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

View File

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

View File

@ -24,11 +24,12 @@ namespace Kakoune
Client::Client(std::unique_ptr<UserInterface>&& ui,
std::unique_ptr<Window>&& 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)},

View File

@ -29,7 +29,7 @@ public:
Client(std::unique_ptr<UserInterface>&& ui,
std::unique_ptr<Window>&& 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<UserInterface> m_ui;
std::unique_ptr<Window> m_window;
const int m_pid;
OnExitCallback m_on_exit;
EnvVarMap m_env_vars;

View File

@ -38,7 +38,7 @@ String ClientManager::generate_name() const
}
}
Client* ClientManager::create_client(std::unique_ptr<UserInterface>&& ui,
Client* ClientManager::create_client(std::unique_ptr<UserInterface>&& ui, int pid,
EnvVarMap env_vars, StringView init_cmds,
Optional<BufferCoord> init_coord,
Client::OnExitCallback on_exit)
@ -46,8 +46,9 @@ Client* ClientManager::create_client(std::unique_ptr<UserInterface>&& 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)

View File

@ -19,7 +19,7 @@ public:
ClientManager();
~ClientManager();
Client* create_client(std::unique_ptr<UserInterface>&& ui,
Client* create_client(std::unique_ptr<UserInterface>&& ui, int pid,
EnvVarMap env_vars, StringView init_cmds,
Optional<BufferCoord> init_coord,
Client::OnExitCallback on_exit);

View File

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

View File

@ -552,7 +552,7 @@ bool check_session(StringView session)
}
RemoteClient::RemoteClient(StringView session, std::unique_ptr<UserInterface>&& ui,
const EnvVarMap& env_vars, StringView init_command,
int pid, const EnvVarMap& env_vars, StringView init_command,
Optional<BufferCoord> init_coord)
: m_ui(std::move(ui))
{
@ -560,6 +560,7 @@ RemoteClient::RemoteClient(StringView session, std::unique_ptr<UserInterface>&&
{
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<int>();
auto init_cmds = m_reader.read<String>();
auto init_coord = m_reader.read_optional<BufferCoord>();
auto dimensions = m_reader.read<DisplayCoord>();
auto env_vars = m_reader.read_hash_map<String, String, MemoryDomain::EnvVars>();
auto* ui = new RemoteUI{sock, dimensions};
if (auto* client = ClientManager::instance().create_client(
std::unique_ptr<UserInterface>(ui),
std::unique_ptr<UserInterface>(ui), pid,
std::move(env_vars), init_cmds, init_coord,
[ui](int status) { ui->exit(status); }))
ui->set_client(client);

View File

@ -30,7 +30,7 @@ class RemoteClient
{
public:
RemoteClient(StringView session, std::unique_ptr<UserInterface>&& ui,
const EnvVarMap& env_vars, StringView init_command,
int pid, const EnvVarMap& env_vars, StringView init_command,
Optional<BufferCoord> init_coord);
const Optional<int>& exit_status() const { return m_exit_status; }