I want to have my .bashrc detect if running emacsclient would work in lieu of running emacs. Basically:
if emacs_server_is_running; then
EDITOR=emacsclient
VISUAL=emacsclient
else
EDITOR=emacs
VISUAL=emacs
fi
What would I put in the emacs_server_is_running
function to accomplish this aim?
You're making this too hard. From the the emacsclient(1)
man page:
-a, --alternate-editor=EDITOR if the Emacs server is not running, run the specified editor instead. This can also be specified via the `ALTERNATE_EDITOR' environment variable. If the value of EDITOR is the empty string, then Emacs is started in daemon mode and emacsclient will try to connect to it.
From a shell script, you could do something like this:
emacsclient -ca false -e '(delete-frame)'
This will open a new emacs window then quickly close it, returning true on success. If no emacs server exists, the alternate editor is /bin/false, which returns false.
I read the answers, but none of this did fit my use case, where I did not want to start an server if not running and also wanted to avoid blocking forever.
Looking at the server-force-stop
function in emacs, it simply deletes the server file in either server-auth-dir
or server-socket-dir
which in my case are /tmp/emacs1000/server
or ~/.emacs.d/server
.
Knowing this, to test if a server is running we can simply do:
test -e "/tmp/emacs1000/server" || test -e "~/.emacs.d/server"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With