From 89fa2342d34ab9a8b94f6e11dfd30660f22cb6e5 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 18 Feb 2013 01:02:56 +0000 Subject: [PATCH] Add new emacs-gui-server and emacs-gui-client commands The idea behind these commands is to easily launch a Emacs server which runs in the background, but with font/theme/etc settings as if it was launched like a normal GUI. I used to just launch Emacs.app directly, but, now I use these binaries to open a full GUI frame by executing `emacs-gui-client -c &`. After starting the GUI server with `emacs-gui-server start` of course. The main difference between calling `emacs --daemon` and `emacs-gui-server start` is that the latter uses a custom socket as to not interfere with any console-base Emacs server, and it also sets the `EMACS_GUI_SERVER` environment variable to `1`, letting my Emacs config figure out which theme/fonts/etc to load. --- bin/emacs-gui-client | 15 ++++++++++++ bin/emacs-gui-server | 58 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100755 bin/emacs-gui-client create mode 100755 bin/emacs-gui-server diff --git a/bin/emacs-gui-client b/bin/emacs-gui-client new file mode 100755 index 0000000..8ce8dd5 --- /dev/null +++ b/bin/emacs-gui-client @@ -0,0 +1,15 @@ +#! /bin/bash + +# Defaults +SOCKET_DIR="$TMPDIR/emacs-gui-server$(id -u)" +SOCKET_FILE="$SOCKET_DIR/server" +EMACSCLIENT="emacsclient" +ALTERNATE_EDITOR="nano" + +# Set to binary bundled in Emacs.app if it exists +if [ -f "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient" ]; then + EMACSCLIENT="/Applications/Emacs.app/Contents/MacOS/bin/emacsclient" +fi + +# Execute emacsclient +exec $EMACSCLIENT -s "$SOCKET_FILE" --alternate-editor=$ALTERNATE_EDITOR "$@" diff --git a/bin/emacs-gui-server b/bin/emacs-gui-server new file mode 100755 index 0000000..867c83e --- /dev/null +++ b/bin/emacs-gui-server @@ -0,0 +1,58 @@ +#! /bin/bash + +# Config +SOCKET_DIR="$TMPDIR/emacs-gui-server$(id -u)" +SOCKET_FILE="$SOCKET_DIR/server" + +EMACS="emacs -nw" +EMACSCLIENT="emacsclient" + +# Set to binaries bundled in Emacs.app if they exists +if [ -f "/Applications/Emacs.app/Contents/MacOS/Emacs" ]; then + EMACS="/Applications/Emacs.app/Contents/MacOS/Emacs -nw" +fi +if [ -f "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient" ]; then + EMACSCLIENT="/Applications/Emacs.app/Contents/MacOS/bin/emacsclient" +fi + + +# Functions +start () { + EMACS_GUI_SERVER=1 env $EMACS \ + --eval "(setq server-socket-dir \"$SOCKET_DIR\")" --daemon +} + +stop () { + env $EMACSCLIENT -s "$SOCKET_FILE" --eval "(kill-emacs)" +} + +status () { + echo -n 'Emacs GUI Server... ' + ! result=`eval $EMACSCLIENT -s "$SOCKET_FILE" --eval \"\(print \'OK\)\" 2>/dev/null` + if [[ $result == "OK" ]]; then + echo 'RUNNING' + else + echo 'STOPPED' + fi +} + +# Command Parsing +case "$1" in + start) + start + ;; + stop) + stop + ;; + status) + status + ;; + *) + echo 'usage: emacs-gui-server [command]' + echo '' + echo 'Available commands:' + echo ' start: Start Emacs GUI Server.' + echo ' stop: Stop Emacs GUI Server.' + echo ' status: Show status of Emacs GUI Server.' + ;; +esac