I normally start Emacs in daemon mode and reload my last session with the desktop package. This causes whichever file desktop-read
open last to remain visited in a buffer which is selected in the window which is selected in the “dummy” frame created for the daemon. As far as Emacs is concerned, this file is visible in a frame. But as far as I'm concerned, it's not.
I could fix all my code to skip the daemon's frame… if I could identify it. But it would be easier to just make the daemon frame visit a buffer whose visibility I don't particularly care about, like *Messages*
or *Minibuf-0
.
What can I put in my init file to
- identify whether Emacs was started in daemon mode;
- identify the frame corresponding to the daemon;
- make this frame visit a harmless buffer, or kill it (what can go wrong if I do that), or otherwise not let it continue “showing” some interesting buffer?
Test by running emacs --daemon -q -l foo.emacs
where foo.emacs
is
;; -*-emacs-lisp-*-
(setq server-name "foo")
;; Simulate loading a desktop session
(find-file "~/.emacs")
;; Test code
(defun foo ()
(interactive)
(let* ((buffer (find-buffer-visiting "~/.emacs"))
(window (get-buffer-window buffer t)))
(if (interactive-p) (message "%S" window) window)))
;; Here goes the code I want
then I want emacsclient -s foo -e '(foo)'
to return nil
.