I'd like to execute some code in my init file only if emacs server is running (specifically if emac开发者_C百科s is started with the --daemon
flag). There doesn't seem to be any hook that runs when server-start
is called and there's no variable I can look at to see if the server is running.
A hack is to use (featurep 'server)
, since the server feature is not loaded unless the server is started, and this does seem to work for my purposes, but I'd like to know what the right way of doing this is. Thanks.
If a server process is running, the associated process object is server-process
. Testing if server-process
is non-nil tells you whether the server is supposed to be running; you can test its status to check that it's in an acceptable state.
(and (boundp 'server-process)
(memq (process-status server-process) '(connect listen open run)))
You can test whether Emacs was invoked as a daemon with (daemonp)
.
Update: the code posted by Gilles throws if a buffer has no process, like "Buffer scratch has no process". When this code is used in ~/.emacs.el then we risk that Emacs does not start up. To catch the error:
(defun --running-as-server ()
"Returns true if `server-start' has been called."
(condition-case nil
(and (boundp 'server-process)
(memq (process-status server-process)
'(connect listen open run)))
(error)))
精彩评论