Using daemon mode

Posted on Sun 09 August 2009 by alex in misc

Now the Fedora 11 has pushed Emacs 23 into it's testing repository I can now have 23.1 goodness on all the machines I regularly interact with (although having checked my Gentoo box is still 23.0.96.1, must fix that). I've previously implemented rather painful hacks to make a --server mode work well but now the mainline supported --daemon mode is the new shizzle. I had a quick play with my .bashrc shell scripts and came up with:

#!/bin/bash
#
# Alex's .bashrc_emacs
#
# Emacs Specific setup
#
# There is only one editor (although I can get to it in different ways).
# For most stuff I want to use emacsclient to spawn a quick shell and
# for emacs 23 I want to ensure the daemon is always running for the user.
#
# Luckily this is covered by specifying -a '' which will spawn a daemon if
# one is not running
#

if [[ "$DISPLAY" == "" ]]; then
    # Can we use muti-tty?
    emacsclient --help | grep "\-\-tty" > /dev/null
    if [[ "$?" == "0" ]]; then
        # Thats a yes
        EMACS_CMD="emacsclient -a '' -t"
    else
        # Hmmm, opening in another pane would be a pain?
        EMACS_CMD="emacs -nw "
    fi
else
    # otherwise don't wait and open a new frame
    EMACS_CMD="emacsclient -n -a '' -c"
fi

# Set the environment variables for the editors
export EDITOR=${EMACS_CMD}
export VISUAL=${EMACS_CMD}
export ALTERNATE_EDITOR=emacs

# shortcut
alias ec="${EMACS_CMD}"

# And finally lets get the status of the emacs server
DT=`emacsclient -a '' -e "(server-running-p)" 2> /dev/null`
echo "loading .bashrc_emacs (server-running-p)=${DT}"

The "emacs -nw" fallback is a little redundant but I suppose it's worth keeping for the day someone gives me shell access without the latest goodies :-)