Zed Lopez

New and Improved Emacs Launcher

I’m much happier with this combination. /usr/local/bin/editor is:

#!/bin/bash ALTERNATE_EDITOR=/usr/local/bin/editor2 emacsclient -c "$*"

/usr/local/bin/editor2 is:
#!/bin/bash
EMACS=/usr/bin/emacs
EMACSCLIENT=/usr/bin/emacsclient
SOCKET=/tmp/emacs`id -u`/server

$EMACS —daemon

count=25

while [ $[ count— ] -gt 0 ]; do
if [ -e $SOCKET ] && [ ! (lsof $SOCKET &> /dev/null) ]; then
$EMACSCLIENT -c “$*”
break
fi
sleep .2
done

Among the advantages here over the old one:

emacs —daemon is only run if emacsclient can’t find a socket, as opposed to emacsclient encountering any error

The old one slept for 1 second after launching the daemon. This was occasionally too short, but usually too long. This one checks for the socket five times a second, and gives up after five seconds. (There are imaginable race conditions that could give undesirable results, but I don’t expect to ever actually encounter them.)