gnu-emacs-sources
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Emacs server init style script


From: Phil Jackson
Subject: Emacs server init style script
Date: Sat, 28 Jun 2008 13:40:38 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux)

Hi,

Please find a script that allows the 'backgrouding' of an emacs
server. I started this with the intention of being able to start emacs
from an init script.

At the moment it can't stop (and therefore restart) a server but I'll
work on that soon.

$ ./bin/emacs-server.bash start
  .....
  <messages buffer output>
$ emacsclient -e '(server-running-p)'
  t
$

#! /bin/bash

PATH=/sbin:/bin:/usr/bin:/usr/local/bin
SOCKET_PATH="${HOME}/.emacs-server"
TIMEOUT=20
REATTACH_CMD="dtach -a ${SOCKET_PATH}"

function command-or-die {
    for c in "address@hidden"; do
        if ! which "${c}" &>/dev/null; then
            echo "'${c}' not found and is required" >&2
            exit 1 
        fi
    done
}

function get-messages-string {
    emacsclient -e '
     (save-excursion
      (set-buffer (get-buffer "*Messages*"))
      (buffer-substring-no-properties (point-min) (point-max)))' \
          | sed 's/\\n/\n/g'
}

function get-socket {
    if [ "$(emacsclient -a /bin/false -e t)" != 't' ]; then
        return 1
    fi

    emacsclient -e '(expand-file-name server-name server-socket-dir)'
}


function do-start {
    sock="$(get-socket 2>/dev/null)"
    if [ -n "${sock}" ]; then
        echo -n "Sorry, no can do. There is already a " >&2
        echo    "server at running at ${sock}" >&2
        exit 1
    fi

    # 'background' an emacsen
    dtach -n "${SOCKET_PATH}" emacs -nw -f server-start || exit 1

    # wait until we can say hi to the server
    for (( i=0; i<${TIMEOUT}; i++ )); do
        echo -n .
        sleep 1

        if get-socket &>/dev/null; then
            get-messages-string
            echo "Use '${REATTACH_CMD}' to connect to the server"
            return 0
        fi
    done

    echo
    echo "Timeout reached whilst trying to determine socket." >&2
}

function do-stop {
    if get-socket >/dev/null; then
        echo -n "Sorry, can't stop a server at the moment. Please " >&2
        echo    "try running '${REATTACH_CMD}' and stopping " >&2
        echo    "emacs yourself." >&2
    fi

    return 1
}

function main {
    command-or-die emacsclient
    command-or-die dtach

    case "$1" in
        start|"")
            do-start
            ;;
        restart|reload|force-reload)
            do-stop && do-start
            ;;
        stop)
            do-stop
            ;;
        status)
            sock="$(get-socket 2>/dev/null)"
            if [ -n "${sock}" ]; then
                echo "emacs socket: ${sock}"
                echo "dtach socket: ${SOCKET_PATH}"
            else
                echo "Server not running"
            fi
            ;;
        *)
            echo "Usage: emacs-server.sh [start|stop|restart|status]" >&2
            exit 3
            ;;
    esac
}

main "address@hidden"
Cheers,
Phil Jackson

reply via email to

[Prev in Thread] Current Thread [Next in Thread]