Prosthetic Conscience

Jason McBrayer's weblog; occasional personal notes and commentary

Sun, 12 Oct 2008

Wrapper script for emacs using –daemon

For quite a while, I’ve used a variety of scripts for starting emacs, starting with dtemacs from gnuclient, which I modified in various ways to work the way I wanted it with Gnome and from the command line. The new daemon-mode in CVS emacs, combined with multi-tty, makes it a bit simpler to write a script that you can use to edit files from your desktop environment, from a command-line with or without X available, or as an external editor from a mailer, VCS or web-browser. I’ve re-written the latest version of my emacs wrapper in terms of daemon-mode, and present it here. It’s rather shorter than my pre-daemon-mode script, and does rather more. Apologies for my bad shell-scripting style.

#!/bin/sh  

# em: a script for starting emacs as needed.  

EMACSCLIENT=emacsclient  
EMACS=emacs  

if [ -z "$DISPLAY" ]  
then  
    CLIENTARGS='-t'  
    CLIENTNEWARGS='-t'  
else  
    CLIENTNEWARGS='-c -n'  
fi  

function start_daemon() {  
    echo -n "Starting emacs in the background…"  
    $EMACS –daemon  
    while ! $EMACSCLIENT –eval t >/dev/null 2>&1  
    do  
        sleep 1  
    done  
    echo "ok."  
}  

if [ -z "$@" ]  
then  
    if $EMACSCLIENT $CLIENTNEWARGS 
    then  
        exit 0  
    else  
        start_daemon  
        $EMACSCLIENT $CLIENTNEWARGS  
    fi  
else  
    if ! $EMACSCLIENT $CLIENTARGS "$@" 
    then  
        start_daemon  
        $EMACSCLIENT $CLIENTARGS "$@"  
    fi  
fi

[ Posted: 19:00] | [ Category: ] | Permalink | Comments: 1 ]

 


Powered by PyBlosxom