summaryrefslogtreecommitdiff
path: root/bin/emacsclient
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2022-04-15 18:08:51 -0700
committerSean Whitton <spwhitton@spwhitton.name>2022-04-15 18:12:39 -0700
commitb45434316738d02ad7b660b0609420cde19aa2a6 (patch)
treefde01dfa690eb7c74441054a81ba84f159a3ef7a /bin/emacsclient
parent9dd6d67f0fc6ddf766ab6ac783b10f9cc52eeb87 (diff)
downloaddotfiles-b45434316738d02ad7b660b0609420cde19aa2a6.tar.gz
combine s/d/emacsclient & update-emacs-daemon-environment
Diffstat (limited to 'bin/emacsclient')
-rwxr-xr-xbin/emacsclient94
1 files changed, 94 insertions, 0 deletions
diff --git a/bin/emacsclient b/bin/emacsclient
new file mode 100755
index 00000000..91cef0aa
--- /dev/null
+++ b/bin/emacsclient
@@ -0,0 +1,94 @@
+#!/bin/sh
+
+# Copyright (C) 2022 Sean Whitton
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or (at
+# your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+
+# emacsclient(1) wrapper to handle updating the environment of existing
+# daemons and choosing between installed and in-tree builds of Emacs.
+
+for arg do
+ shift
+ case "$arg" in
+ '--spw/installed') mode=--spw/installed ;;
+ '--spw/update-environment') update=true ;;
+ *) 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
+
+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
+ devel_running=false
+fi
+
+# 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.
+if [ "$mode" != --spw/installed -a "$installed_running" != true \
+ -a -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" != true ] && ps h -o pid -C make \
+ | xargs pwdx | grep -q "$HOME/src/emacs" ); then
+ emacs="$HOME/src/emacs/src/emacs"
+ emacsclient="$HOME/src/emacs/lib-src/emacsclient"
+else
+ emacs=$(command -v emacs)
+ emacsclient=$(PATH=$(echo "$PATH" | sed -e "s#$HOME/src/dotfiles/bin:##") \
+ command -v 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 [ "$update" = true \
+ -a \( "$devel_running" = true \
+ -o "$installed_running" = true \) ]; then
+ for var in DISPLAY WAYLAND_DISPLAY \
+ XAUTHORITY WINDOWID \
+ XDG_SESSION_TYPE XDG_CURRENT_DESKTOP \
+ SWAYSOCK I3SOCK \
+ SSH_ASKPASS SSH_CONNECTION \
+ SSH_AUTH_SOCK SSH_AGENT_PID; do
+ eval isset=\${$var+x}
+ if [ "$isset" = "x" ]; then
+ eval val=\$$var
+ args="$args"' (setenv "'$var'" "'$val'")'
+ fi
+ done
+ "$emacsclient" --eval "(progn$args (spw/propagate-new-environment))"
+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
+ exec "$emacsclient" -a "" "$@"
+fi