Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect that emacs-server is running from a shell prompt?

Tags:

bash

shell

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?

like image 202
Chris R Avatar asked Oct 02 '09 16:10

Chris R


3 Answers

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.
like image 145
Kirk Strauser Avatar answered Oct 21 '22 03:10

Kirk Strauser


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.

like image 36
madumlao Avatar answered Oct 21 '22 02:10

madumlao


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"

like image 36
Markus1189 Avatar answered Oct 21 '22 03:10

Markus1189