summaryrefslogtreecommitdiff
path: root/bin/emacsclient
diff options
context:
space:
mode:
Diffstat (limited to 'bin/emacsclient')
-rwxr-xr-xbin/emacsclient179
1 files changed, 89 insertions, 90 deletions
diff --git a/bin/emacsclient b/bin/emacsclient
index b4a2238b..1ac1b993 100755
--- a/bin/emacsclient
+++ b/bin/emacsclient
@@ -28,8 +28,9 @@ want_eval=false
want_version=false
devel_running=false
installed_running=false
-pass_to_gdbmacs=false
-status=
+make=
+branch=
+gud_status=
daemon_name=
gdbmacs=
args=
@@ -64,10 +65,14 @@ spw_flock () {
fi
}
-die_wedged () {
- maybe_notify "in-tree Emacs appears to be wedged"
- # Only exit non-zero if we were expected to start the daemon.
- ! $may_start; exit
+pass_to_gdbmacs () {
+ if $want_eval || $want_update; then
+ maybe_notify "in-tree Emacs appears to be wedged"
+ # Only exit non-zero if we were expected to start the daemon.
+ ! $may_start; exit
+ else
+ exec "$installed_emacsclient" -sgdbmacs "$@" 3>&- 4>&-
+ fi
}
egrep_negate () {
@@ -115,7 +120,8 @@ if [ -n "$listener" ]; then
esac
fi
-# ---- Special case for primary session that's always under gdb
+# Make it possible, with primary session, to quickly replace in-tree Emacs
+# with installed Emacs. See 'C-i E' Sway/i3 binding.
if [ -z "$daemon_name" ] && $devel_running && $want_installed; then
inotifywait -qq -e delete "$socket" &
inotifywait=$!
@@ -133,88 +139,90 @@ if [ -z "$daemon_name" ] && $devel_running && $want_installed; then
(gud-basic-call "quit")))))'
wait $inotifywait
+ listener=
devel_running=false
fi
-# ---- End special case for primary session that's always under gdb
[ -z "$listener" ] && ! $may_start && ! $want_version && exit 0
-# We check whether there is something which looks like the installed Emacs
-# running because we do not want the in-tree emacsclient to try to connect to
-# that, and we check whether there is a build in progress and no running
-# daemon, as we don't want to start a new daemon until the build is finished.
-# The idea is that I can always hit C-i e without worrying about builds.
+# We start up an in-tree Emacs daemon only when a branch named after this host
+# is checked out, packages compiled against my emacs-snapshot Debian packages
+# are available, and there isn't a build still in progress -- avoids having to
+# consider whether a build is in progress when hitting 'C-i e'.
+#
+# The purpose of a branch for localhost is to keep track of what upstream
+# commit we want to be running on this host (e.g. emacs-29 vs. master), and
+# for WIP commits. It doesn't usually need to be pushed anywhere.
+#
+# We also examine $installed_running here to avoid having the in-tree
+# emacsclient(1) connect to the installed Emacs.
+
if ! $want_installed && ! $installed_running \
- && [ -e "$HOME/src/emacs/admin/flagfile.melete" \
- -a -x "$HOME/src/emacs/lib-src/emacsclient" \
- -a -d "/usr/share/emacs-snapshot/site-lisp/elpa" ] \
- && ! ( ! $devel_running && ps h -o pid -C make \
- | xargs pwdx 2>/dev/null \
- | grep -q "$HOME/src/emacs" ); then
+ && [ -x "$HOME/src/emacs/lib-src/emacsclient" \
+ -a -d "/usr/share/emacs-snapshot/site-lisp/elpa" ]; then
+ make="$(ps h -o pid -C make)"
+ branch="$(git -C $HOME/src/emacs rev-parse --abbrev-ref HEAD)"
+fi
+
+if [ "$branch" = "$(hostname -s)" ] \
+ && ! ( ! $devel_running && [ -n "$make" ] \
+ && echo "$make" | xargs pwdx \
+ | grep -q "$HOME/src/emacs" ); then
emacs="$devel_emacs"
emacsclient="$devel_emacsclient"
-
- # ---- Two special cases for primary session that's always under gdb
if [ -z "$daemon_name" ] && ! $want_version; then
spw_flock 4 gdbmacs
- # If devel Emacs is stopped or gdbmacs can't start it, ask gdbmacs to
- # handle the request: it's probs. C-i e or editing a file via EDITOR.
-
[ -n "${gdbmacs:=$(get_listener ${socket_dir}gdbmacs)}" ] \
- && status=$("$installed_emacsclient" \
- -sgdbmacs \
- --eval \
- '(and (boundp '"'"'gud-comint-buffer)
- (get-buffer-process gud-comint-buffer)
- (string= "signal-received"
- gdb-inferior-status))')
-
- if [ "$status" = t ]; then
- if $want_eval || $want_update; then
- die_wedged
- else
- pass_to_gdbmacs=true
- fi
- elif [ -z "$listener" ]; then
- # inotifywait(1) in Debian "bullseye" doesn't have --include.
- inotifywait -qq -e create "${socket_dir}" \
- --exclude "$(egrep_negate ${socket_dir}server)" &
- inotifywait=$!
-
- if "$installed_emacsclient" \
- -a '' -sgdbmacs \
- --eval '(spw/gdbmacs-attach)' 3>&- 4>&-; then
- wait $inotifywait
- listener=true
- elif $want_eval || $want_update; then
- kill $inotifywait
- die_wedged
- else
- kill $inotifywait
- pass_to_gdbmacs=true
- fi
- fi
-
- if $pass_to_gdbmacs; then
- emacs="$installed_emacs"
- emacsclient="$installed_emacsclient"
- socket="${socket_dir}gdbmacs"
- listener="$gdbmacs"
- set -- "$@" -sgdbmacs
- fi
- exec 4>&-
+ && gud_status=$("$installed_emacsclient" \
+ -sgdbmacs \
+ --eval \
+ '(and (boundp '"'"'gud-comint-buffer)
+ (get-buffer-process gud-comint-buffer)
+ (string= "signal-received"
+ gdb-inferior-status))')
fi
- # ---- End two special cases for primary session that's always under gdb
else
emacs="$installed_emacs"
emacsclient="$installed_emacsclient"
fi
-# Update Emacs daemon environment vars for a new interactive session. This
-# 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 $want_update && [ -n "$listener" ]; then
+# Except in the case above where ss(1) is not available, this wrapper script
+# is responsible for starting up the daemon if it's not running. This is to
+# ensure that only the in-tree emacsclient(1) talks to the in-tree Emacs, and
+# only the installed emacsclient(1) talks to the installed Emacs. (We used to
+# patch the in-tree emacsclient.c to only execute the in-tree Emacs, instead
+# of handling it here.)
+
+if [ "$gud_status" = t ]; then
+ # Primary session is stopped. Ask gdbmacs to handle the request: it's
+ # probably C-i e or editing a file via EDITOR.
+ pass_to_gdbmacs "$@"
+elif [ -z "$listener" ] && ! $want_version; then
+ if [ -z "$daemon_name" -a "$emacs" = "$devel_emacs" ]; then
+ # inotifywait(1) in Debian "bullseye" doesn't have --include.
+ inotifywait -qq -e create "${socket_dir}" \
+ --exclude "$(egrep_negate ${socket_dir}server)" &
+ inotifywait=$!
+ # If gdbmacs fails to start main session, have gdbmacs handle the
+ # request; again, probably C-i e or editing via EDITOR.
+ if "$installed_emacsclient" \
+ -a '' -sgdbmacs --eval '(spw/gdbmacs-attach)' 3>&- 4>&-; then
+ wait $inotifywait
+ else
+ kill $inotifywait
+ pass_to_gdbmacs "$@"
+ fi
+ else
+ "$emacs" --daemon"${daemon_name:+=$daemon_name}" 3>&-
+ fi
+elif $want_update && ! $want_version; then
+ # Facility to update a (running) daemon's env for a new login session.
+ # This 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.
+ #
+ # --spw/update-environment may be combined with a regular request or be
+ # the only thing asked of this script.
for var in DISPLAY WAYLAND_DISPLAY \
XAUTHORITY WINDOWID \
XDG_SESSION_TYPE XDG_CURRENT_DESKTOP \
@@ -232,21 +240,12 @@ if $want_update && [ -n "$listener" ]; then
--eval "(spw/update-environment$args))"
fi
-maybe_hold_lock () {
- # Hold on to the lock until daemon has started up.
- $want_version || [ -n "$listener" ] \
- || inotifywait -qq -e create "${socket_dir}" \
- --exclude "$(egrep_negate $socket)" &
-}
-
-# emacsclient(1) requires an argument. As a special case, if there are no
-# 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
- maybe_hold_lock
- exec "$emacs" --daemon"${daemon_name:+=$daemon_name}" 3>&-
-elif ! ( [ "$#" -eq "$min_arg" ] && $want_update ); then
- maybe_hold_lock
- exec "$emacsclient" -a "" "$@" 3>&-
-fi
+# emacsclient(1) requires argument(s) beyond just -s/--socket-name.
+# However, in the absence of any such arguments, if the daemon wasn't already
+# running, or if --spw/update-environment, this wrapper starts the daemon if
+# it wasn't already running, and exits zero.
+#
+# Note that if the daemon *was* already running and the only argument was
+# -s/--socket-name, we do exec emacsclient(1) and it'll immediately exit.
+[ "$#" -eq "$min_arg" ] && ( $want_update || [ -z "$listener" ] ) \
+ || exec "$emacsclient" "$@" 3>&- 4>&-