I know that if I set:

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

Emacs prompts me before I kill it.

But I want emacs to prompt me before I close my last gui frame (prompting me before killing any frame would be very helpful already). When you run emacs in daemon-mode, closing the last gui frame does not kill emacs and thus the aforementioned setting does not work.

The problem is that sometimes I overpress my keybind to quit emacs windows (evil-quit) and I endup quiting also my current gui frame, which is annoying.

I know that before deleting the frame, emacs runs the functions that are listed in the variable delete-frame-functions, so I thought I could include a function there that would prompt me to query if I really wanted to kill the frame. But it is not clear from the documentation what I would have to do inside that function to stop the process of killing the frame.

Another idea would be to add an advice to evil-quit, like: if daemon-p is t then evil-quit have to prompt me to quit my current frame. Something like that?

I run GUI emacs from both OSx and GNU/Linux.


You may want to define a function that will ask you to confirm before closing a frame. Then you'll check if Emacs was started as a daemon, if it is the case, update the C-x C-c shortcut. Something in that spirit:

(defun ask-before-closing ()
  "Close only if y was pressed."
  (if (y-or-n-p (format "Are you sure you want to close this frame? "))
    (message "Canceled frame close")))

(when (daemonp)
  (global-set-key (kbd "C-x C-c") 'ask-before-closing))

UPDATE: As the OP is not near to know how to do that

Just copy the snippet above and paste it inside your Emacs init file. It may be ~/.emacs, ~/.emacs.el, or ~/.emacs.d/init.el. Then, relaunch Emacs.

  • that 's a nice idea, but I really use just evil-quit to close emacs, frames and windows. I never use C-x C-c. The trick here would be to query if there is only one window active at the moment, and if that is indeed the case, evil-quit would close the current frame without asking me, which is annoying. So instead we would y-or-n-p the user. I think that could be the way to go. – ninrod Feb 6 '17 at 0:47
  • Except I am not near to know how to do that. – ninrod Feb 7 '17 at 16:24
  • 1
    @ninrod Answer updated. – Nsukami _ Feb 7 '17 at 16:51
  • thanks but I meant I did not know how to advice a function. – ninrod Feb 11 '17 at 1:05
  • @ninrod My answer is not about function advice. And I do not use evil, so I cannot help you for that point, sorry. – Nsukami _ Feb 12 '17 at 10:28

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.