views:

170

answers:

2

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?

+1  A: 

I can think of a couple of ways, neither of them foolproof:

function emacs_server_is_running()
{
    ps -ef | grep emacsserver | grep -v grep > /dev/null
}

function emacs_server_is_running()
{
    netstat -a | grep esrv > /dev/null
}


Or instead of setting this up when the shell starts (after all, the server could die or the server could start after you have logged in), why not make the check at the time you need to edit something:

EDITOR=$HOME/emacs_try_client_first
VISUAL=$HOME/emacs_try_client_first

where $HOME/emacs_try_client_first could be as simple as

#!/bin/sh
# emacs_try_client_first
emacsclient $@ || emacs $@
mobrule
You don't have to do `grep -v grep` - you can make your first one look like this: `grep [e]macsserver` this is often done when grepping the output of `ps`.
Dennis Williamson
@DW You know? Everybody always complains about that `| grep -v grep` idiom, but now finally someone's done something about it!
mobrule
+1  A: 

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.
Just Some Guy