Prereqs for this hack being useful to you: (1) you use GNU Emacs; (2) you keep a single Emacs running in your session and use emacsclient
to open files for editing in it; (3) you have Linux computers that use systemd
on which you keep multiple concurrent login sessions open; and (4) you want emacsclient
on those computers to automatically send your edits to the Emacs running in your currently active session.
My use case: I log into my computer while sitting in front of it (GNOME). I log into the same computer remotely over SSH and connect to a persistent Screen session. I keep Emacs running persistently in both contexts (GNOME, Screen). I want emacsclient
to instantly and invisibly do the right thing regardless of which session (GNOME, Screen) I happen to be in at the moment.
Solution: Have Emacs launch the server on startup. After that, check every few seconds to see if the current systemd
session has transitioned from idle to active. When that happens, restart the server in this Emacs process if it doesn’t already own it.
To use it: download server-sucker.el
(gist below), save it somewhere in your Emacs lisp search path, and put (require 'server-sucker)
in your .emacs
file.
Code: