10

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.

Nsukami _
  • 6,341
  • 2
  • 22
  • 35
ninrod
  • 1,436
  • 2
  • 13
  • 28
  • What OS are you running? (OSX, for example, has a different way of handling things -- e.g., https://gist.github.com/DarwinAwardWinner/5882719 ) – lawlist Feb 05 '17 at 02:07

1 Answers1

7

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."
  (interactive)
  (if (y-or-n-p (format "Are you sure you want to close this frame? "))
      (save-buffers-kill-emacs)                                                                                            
    (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.

Nsukami _
  • 6,341
  • 2
  • 22
  • 35
  • 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 06 '17 at 00:47
  • Except I am not near to know how to do that. – ninrod Feb 07 '17 at 16:24
  • 1
    @ninrod Answer updated. – Nsukami _ Feb 07 '17 at 16:51
  • thanks but I meant I did not know how to advice a function. – ninrod Feb 11 '17 at 01: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
  • 1
    Note that if you use Emacs in terminal, the `(save-buffers-kill-emacs)` should be `(save-buffers-kill-terminal)` – CodyChan Mar 26 '19 at 06:22