Created
March 3, 2014 18:16
-
-
Save xy4n/9331187 to your computer and use it in GitHub Desktop.
emacsd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# | |
# Modify the EMACS and EMACS_FLAGS variable to match both the name of the | |
# Emacs binary, and the flags desired at start. Most people will not need | |
# to perform any modifications to this. | |
# | |
# This script also requires an Elisp snippet to be present in your .emacs | |
# (~/.emacs or ~/.emacs.d/init.el) in order to function properly: | |
# | |
# (defun server-shutdown () | |
# "Save buffers, Quit, and Shutdown (kill) server" | |
# (interactive) | |
# (save-some-buffers 't) | |
# (kill-emacs)) | |
# | |
# | |
# Copyright 2013, Riley. | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
################################################################################ | |
# Set the name or path of the Emacs binary, and the startup flags | |
export EMACS="emacs" | |
export EMACS_FLAGS="--daemon" | |
function emacsd-start () { | |
if [ '$(pgrep -f "$EMACS $EMACS_FLAGS")' ] ; then | |
printf "The Emacs daemon is already running.\n" | |
else | |
printf "Starting the Emacs daemon...\n" | |
$EMACS $EMACS_FLAGS | |
fi | |
} | |
function emacsd-status () { | |
if [ '$(pgrep -f "$EMACS $EMACS_FLAGS")' ] ; then | |
printf "The Emacs daemon is running at PID `pgrep -f \"$EMACS $EMACS_FLAGS\"`.\n" | |
else | |
no-emacsd | |
fi | |
} | |
function emacsd-save () { | |
if [ '$(pgrep -f "$EMACS $EMACS_FLAGS")' ] ; then | |
printf "Saving… " | |
emacsclient -e "(save-some-buffers 't)" | |
printf "Done.\n" | |
else | |
no-emacsd | |
fi | |
} | |
function emacsd-stop () { | |
if [ '$(pgrep -f "$EMACS $EMACS_FLAGS")' ] ; then | |
printf "Stopping the Emacs daemon.\n" | |
emacsclient -e '(server-shutdown)' | |
else | |
no-emacsd | |
fi | |
} | |
function emacsd-restart () { | |
if [ '$(pgrep -f "$EMACS $EMACS_FLAGS")' ] ; then | |
emacsd-stop | |
sleep 5 | |
emacsd-start | |
else | |
no-emacsd | |
emacsd-start | |
fi | |
} | |
# Best you check yourself | |
function emacsd-kill () { | |
if [ '$(pgrep -f "$EMACS $EMACS_FLAGS")' ] ; then | |
printf "KILLING the Emacs daemon.\n" | |
printf "This will likely result in lost work, \n" | |
printf "check the status of the autosaves of all your open files.\n" | |
kill $(pgrep -f '\"$EMACS $EMACS_FLAGS\"') | |
else | |
no-emacsd | |
fi | |
} | |
function emacsd-force-restart () { | |
if [ '$(pgrep -f "$EMACS $EMACS_FLAGS")' ] ; then | |
emacsd-kill | |
sleep 5 | |
emacsd-start | |
else | |
no-emacsd | |
emacsd-start | |
fi | |
} | |
# This should almost never be the case. | |
function no-emacsd () { | |
printf "The Emacs daemon is not currently running.\n" | |
} | |
if [ "$1" == "restart" ] ; then | |
emacsd-restart | |
elif [ -z "$1" ] ; then | |
emacsd-status | |
elif [ "$1" == "stop" ] ; then | |
emacsd-stop | |
elif [ "$1" == "start" ] ; then | |
emacsd-start | |
elif [ "$1" == "status" ] ; then | |
emacsd-status | |
elif [ "$1" == "save" ] ; then | |
emacsd-save | |
elif [ "$1" == "force-restart" ] ; then | |
emacsd-force-restart # For when you really fuck something up | |
elif [ "$1" == "kill" ] ; then | |
emacsd-kill | |
else | |
printf "Proper usage: emacsd {status || start || stop |" | |
printf "| restart || kill || force-restart}\n" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment