diff --git a/src/commands.cc b/src/commands.cc index 0a13938e..fbdd8379 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1895,6 +1895,25 @@ const CommandDesc change_working_directory_cmd = { } }; +const CommandDesc change_session_name = { + "session", + nullptr, + "session : change remote session name", + ParameterDesc{{}, ParameterDesc::Flags::None, 1, 1}, + CommandFlags::None, + CommandHelper{}, + CommandCompleter{}, + [](const ParametersParser& parser, Context&, const ShellContext&) + { + if (not Server::instance().rename_session(parser[0])) + throw runtime_error(format("Cannot rename current session: '{}' may be already in use", parser[0])); + + // update modeline + for (auto& client : ClientManager::instance()) + client->redraw_ifn(); + } +}; + } void register_commands() @@ -1952,6 +1971,7 @@ void register_commands() register_command(set_register_cmd); register_command(select_cmd); register_command(change_working_directory_cmd); + register_command(change_session_name); } } diff --git a/src/remote.cc b/src/remote.cc index 1c5a2135..1130e120 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -651,13 +651,26 @@ Server::Server(String session_name) m_listener.reset(new FDWatcher{listen_sock, accepter}); } +bool Server::rename_session(const String& name) +{ + String old_socket_file = format("/tmp/kakoune/{}/{}", getpwuid(geteuid())->pw_name, m_session); + String new_socket_file = format("/tmp/kakoune/{}/{}", getpwuid(geteuid())->pw_name, name); + + if (link(old_socket_file.c_str(), new_socket_file.c_str()) != 0) + return false; + + unlink(old_socket_file.c_str()); + + m_session = name; + return true; +} + void Server::close_session(bool do_unlink) { if (do_unlink) { - char socket_file[128]; - format_to(socket_file, "/tmp/kakoune/{}/{}", getpwuid(geteuid())->pw_name, m_session); - unlink(socket_file); + String socket_file = format("/tmp/kakoune/{}/{}", getpwuid(geteuid())->pw_name, m_session); + unlink(socket_file.c_str()); } m_listener->close_fd(); m_listener.reset(); diff --git a/src/remote.hh b/src/remote.hh index f127e180..fd214b3d 100644 --- a/src/remote.hh +++ b/src/remote.hh @@ -48,6 +48,7 @@ struct Server : public Singleton ~Server(); const String& session() const { return m_session; } + bool rename_session(const String& name); void close_session(bool do_unlink = true); private: