From 42ee9e7c8560ebdc9ef79c46a1b0f3ffb33eb926 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sun, 25 Sep 2022 22:34:09 -0700 Subject: add --socket-name support to emacsclient(1) wrapper --- bin/emacsclient | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) (limited to 'bin/emacsclient') diff --git a/bin/emacsclient b/bin/emacsclient index e11cbe9c..0e6ab7a6 100755 --- a/bin/emacsclient +++ b/bin/emacsclient @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Copyright (C) 2022 Sean Whitton # @@ -20,25 +20,38 @@ # emacsclient(1) wrapper to handle updating the environment of existing # daemons and choosing between installed and in-tree builds of Emacs. +min_arg=0 + for arg do shift case "$arg" in '--spw/installed') mode=--spw/installed ;; '--spw/update-environment') update=true ;; + '-s'|'--socket-name') min_arg=2; daemon_name="$1" ;;& + '-s'?*) min_arg=1; daemon_name="${arg:2}" ;;& + '--socket-name='?*) min_arg=1; daemon_name="${arg:14}" ;;& *) set -- "$@" "$arg" ;; esac done -processes=$(ps -o exe -u "$LOGNAME" -U "$LOGNAME") -echo "$processes" | grep -q "$HOME/src/emacs/src/emacs" && devel_running=true -echo "$processes" | grep -q $(command -v emacs) && installed_running=true +command -v ss >/dev/null || exit 1 +socket="/run/user/$(id -u)/emacs/${daemon_name:-server}" +listener="$(ss -Hplx src $socket | perl -wne'/pid=(\d+)/ and print $1')" + +if [ -z "$listener" -a -e "$socket" ]; then + # Nothing is listening: remove dangling socket. + rm "$socket" +elif [ -n "$listener" ]; then + case "$(ps h -o exe $listener | sed -e 's/ (deleted)$//')" in + "$HOME/src/emacs/src/emacs") devel_running=true ;; + "$(realpath $(command -v emacs))") installed_running=true ;; + *) echo >&2 "Unknown Emacs listening on $socket"; exit 1 ;; + esac +fi if [ "$devel_running" = true -a "$mode" = --spw/installed ]; then - pgrep --full "$HOME/src/emacs/src/emacs" | while read pid; do - kill $pid - done - socket="/run/user/$(id -u)/emacs/server" - [ -e $socket ] && inotifywait -qqt 2 -e delete $socket + kill "$listener" + [ -e "$socket" ] && inotifywait -qqt 2 -e delete "$socket" devel_running=false fi @@ -65,9 +78,7 @@ fi # allows us to have the Emacs daemon survive restarts of the graphical desktop # environment, and to update Emacs on a remote host when there is a new # forwarded SSH agent socket. See 'upenv' in .bash_defns. -if [ "$update" = true \ - -a \( "$devel_running" = true \ - -o "$installed_running" = true \) ]; then +if [ "$update" = true -a -n "$listener" ]; then for var in DISPLAY WAYLAND_DISPLAY \ XAUTHORITY WINDOWID \ XDG_SESSION_TYPE XDG_CURRENT_DESKTOP \ @@ -81,15 +92,16 @@ if [ "$update" = true \ fi done [ -n "$args" ] \ - && "$emacsclient" --eval "(spw/update-environment$args))" + && "$emacsclient" -s"${daemon_name:-server}" \ + --eval "(spw/update-environment$args))" fi # emacsclient(1) requires an argument. As a special case, if there are no -# arguments and no daemon is running, just start one. And if there were no -# arguments but we were requested just to update env vars, exit peacefully. -if [ "$#" -eq 0 \ - -a "$devel_running" != true -a "$installed_running" != true ]; then - exec "$emacs" --daemon -elif ! [ "$#" -eq 0 -a "$update" = true ]; then +# arguments except -s/--socket-name, and no daemon is running, just start one. +# And if there were no arguments but we were requested just to update env +# vars, exit peacefully. +if [ "$#" -eq "$min_arg" -a -z "$listener" ]; then + exec "$emacs" --daemon"${daemon_name:+=$daemon_name}" +elif ! [ "$#" -eq "$min_arg" -a "$update" = true ]; then exec "$emacsclient" -a "" "$@" fi -- cgit v1.2.3