0

I use

(setq desktop-save 'ask)

in my init.el, so every time I C-x C-c to exit Emacs, it will prompt to ask if saving desktop or not; I use this trick to avoid accidentally exiting Emacs.

But after using emacsclient to speed up Emacs's startup time,

emacsclient -a "" -t 

C-x C-c will not prompt to ask for saving desktop, it will exit Emacs immediately.

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.

BTW: I use Emacs in terminal, inside a server without GUI.

  • 1
    C-x C-c doesn't exit Emacs when in daemon mode, it only closes the current frame, while the editing server is still hanging around. – DoMiNeLa10 Mar 28 at 7:08
1

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)

  • Thanks, but your first solution is not working, it kills Emacs daemon too, and the second solution doesn't work either, it exits emacsclient(not daemon) without prompt. I just want to kill the emacsclient with confirmation instead of the daemon if there is daemon. and (setq confirm-kill-emacs #'y-or-n-p) is for Emacs running without daemon – CodyChan Mar 28 at 6:03
1

I found the answer from https://emacs.stackexchange.com/a/30475/794

The version is tweaked version from the answer in the link

(defun ask-before-closing ()
  "Prompt for confirmation for emacsclient(not daemon) like confirm-kill-emacs for running Emacs without daemon."
  (interactive)
  (if (y-or-n-p (format "Really exit Emacs? "))
          (save-buffers-kill-terminal)
        (message "Canceled frame close!")))
(when (daemonp)
  (global-set-key (kbd "C-x C-c") 'ask-before-closing))

Use it to avoid accidentally exiting emacsclient (not exit emacs daemon) running with daemon.

And use

(setq confirm-kill-emacs #'y-or-n-p)

to avoid accidentally exiting Emacs if running without daemon.

  • This is exactly the idea I had and was going to answer with. However, I found an alternative you might want to consider. – Sam Mar 28 at 2:21
0

You could try setting confirm-kill-emacs to y-or-n-p in your config:

(setq confirm-kill-emacs #'y-or-n-p)

Works for me.

  • This is just a replacement for desktop-save to avoid accidentally exiting Emacs, putting this line in init.el doesn't prevent exiting emacsclient, did't you tried it? – CodyChan Mar 28 at 5:53
  • Yes, I did try it and it worked for me. I didn't test extensively, though, so I might not have tested under the same conditions that you were operating under when you saw the problem. – Sam Mar 28 at 12:38
  • Adding onto my previous comment. What I actually did was start Emacs normally and then enable server mode using server-start. Then my solution Works On My Machine™ – Sam Mar 28 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.