Emacs: Overhaul daemon startup script

master
Pierre Neidhardt 2017-04-28 10:54:51 +05:30
parent 5166c7ce5b
commit fd7baa10b0
2 changed files with 35 additions and 20 deletions

View File

@ -254,8 +254,10 @@
(when (not (emacs-process-p ad-return-value)) (when (not (emacs-process-p ad-return-value))
(setq ad-return-value nil))) (setq ad-return-value nil)))
;; Let Emacs auto-load/save sessions. ;; Let Emacs auto-load/save sessions only when running the daemon.
(when (getenv "EMACS_SERVER") ;; (server-running-p) is only useful once the daemon is started and cannot be
;; used for initialization.
(when (daemonp)
(desktop-save-mode 1) (desktop-save-mode 1)
(setq history-length 250) (setq history-length 250)
(setq desktop-dirname (concat emacs-cache-folder "desktop")) (setq desktop-dirname (concat emacs-cache-folder "desktop"))

View File

@ -1,20 +1,16 @@
#!/bin/sh #!/bin/sh
## Note: we do not use the (-a "") parameters of emacsclient to auto start the
## daemon since we want to pass additional parameters to emacs.
if [ "$1" = "-h" ]; then if [ "$1" = "-h" ]; then
cat <<EOF>&2 cat <<EOF>&2
Usage: ${0##*/} [OPTIONS] [FILES] Usage: ${0##*/} [OPTIONS] [FILES]
Start emacsclient in terminal or in X window if possible. If server is not Start emacsclient in terminal or in X window if possible. If no server is
found, it is started with --no-site-file parameter. running, it is started with the '--no-site-file' parameter.
Calling script can have different names. The calling script can have different names:
* emc: emacs instance is opened in current terminal.
* emw: if graphical, tell client to wait.
When starting the server, the EMACS_SERVER variable is set so that Emacs can - emc: the Emacs instance is opened in the current terminal.
know in its initialization file whether the server is going to be started. - emw: if graphical, tell the client to wait.
All emacsclient(1) options are supported: All emacsclient(1) options are supported:
@ -28,22 +24,39 @@ EOF
exit exit
fi fi
if [ "$1" = "--kill" ]; then if [ "$1" = "--kill" ]; then
emacsclient -e '(kill-emacs)' emacsclient -e '(kill-emacs)'
exit exit
fi fi
## Emacs will start in console mode if it cannot start in graphical mode or if
if [ "${0##*/}" = "emc" ] || [ -z "$DISPLAY" ] || \ ## called from "emc".
[ "$(emacs --batch -Q --eval='(if (fboundp '"'"'tool-bar-mode) (message "X") (message "TTY"))' 2>&1)" = TTY ]; then if [ "${0##*/}" = "emc" ]; then
param="-t" param="-t"
else else
if [ "${0##*/}" = "emw" ]; then param="-c"
param="-c" ## Emacs should always wait unless running graphical and "emw" was not called.
else if [ "${0##*/}" != "emw" ] && [ "$(emacs --batch -Q --eval='(if (fboundp '"'"'tool-bar-mode) (message "X") (message "TTY"))' 2>&1)" = X ] && [ -n "$DISPLAY" ]; then
param="-nc" param="$param -n"
fi fi
fi fi
[ ! -e "/tmp/emacs$(id -u)/server" ] && EMACS_SERVER=t emacs --daemon --no-site-file ## Simple invokation:
##
## emacsclient $param -a "" "$@"
##
## The '-a ""' parameter starts "emacs --daemon", but we want to skip the "site
## files", i.e. the extra init files some distros try to force into Emacs. To
## achieve this we have to start the daemon manually.
socket="/tmp/emacs$(id -u)/server"
[ ! -e "$socket" ] && emacs --daemon --no-site-file
emacsclient $param "$@" emacsclient $param "$@"
## If Emacs was forcefully closed or crashed, the dead socket will be left
## behind and Emacs cannot reset it automatically without the '-a ""' parameter.
if [ $? -ne 0 ]; then
echo >&2 "Removing existing socket and trying again."
rm "$socket"
[ ! -e "$socket" ] && emacs --daemon --no-site-file
emacsclient $param "$@"
fi