C-x C-c
will not prompt to ask for saving desktop, it will exit Emacs immediately.
C-xC-c (save-buffers-kill-terminal
) does not actually kill the server by default, unless the client invoking it was started with the --no-wait
switch and there are no other remaining Emacs frames[1][2].
Is there anything I can do to prompt for confirmation when C-x C-c
in emacsclient
? I doesn't have to be prompting for saving desktop, any confirmation will be OK to me.
As Sam mentions, there is the user option confirm-kill-emacs
you can configure[1]. This is the best place to hook into, as it is the final thing done before calling kill-emacs
. This means the standard checks for modified buffers, active processes, and the hook kill-emacs-query-functions
all get a chance to run first. Alternatively, you could add a function to kill-emacs-query-functions
, but note that its ordering can be important[3].
Since save-buffers-kill-terminal
does not usually kill a daemon Emacs, though, you must somehow ensure save-buffers-kill-emacs
gets called instead, if you truly want to kill the server. FWIW, I achieve this in my own configuration by globally remapping[4] save-buffers-kill-terminal
to save-buffers-kill-emacs
. YMMV. Here are the relevant incantations:
(global-set-key [remap save-buffers-kill-terminal] #'save-buffers-kill-emacs)
(setq confirm-kill-emacs #'yes-or-no-p)
If you want to get prompted only when you're running emacsclient
, however, just modify the confirm-kill-emacs
predicate:
(defun my-confirm-kill-daemon (prompt)
"Ask whether to kill daemon Emacs with PROMPT.
Intended as a predicate for `confirm-kill-emacs'."
(or (not (daemonp))
(yes-or-no-p prompt)))
(setq confirm-kill-emacs #'my-confirm-kill-daemon)