4

I activated setq-local confirm-kill-emacs (quote y-or-n-p) on a hook for a local mode which has a shortcut very similar to C-x C-c, in order to avoid accidentally closing Emacs when trying to execute the said shortcut. That way, Emacs will ask me to confirm it and I will be able to cancel it.

But this has an unfortunate consequence, that is, when I click on the close button on the top of Emacs' window, it also asks the same question. This is often annoying.

Is there a way to only ask for confirmation when using the keyboard shortcut, but not the mouse click?

anol
  • 195
  • 4

1 Answers1

5

you may define a separate command for C-x C-c like:

(defun keyboard-kill-emacs ()
  (interactive)
  (let ((confirm-kill-emacs 'yes-on-no-p)) ; temporarily enable the confirmation
    (save-buffers-kill-emacs)))

(global-set-key (kbd "C-x C-c") 'keyboard-kill-emacs)
JeanPierre
  • 7,323
  • 1
  • 18
  • 37
zk_phi
  • 66
  • 1
  • Thanks, i'd edit it to fix the typo (`yes-or-no-p`) but it's below the 6-character minimum edit. Also, for lazy people, I'd add the code to put it as a hook to a local mode: `(add-hook 'whatever-mode-hook (lambda () (local-set-key (kbd "C-x C-c") 'keyboard-kill-emacs)))` In any case, it worked just as intended, thanks. – anol Feb 22 '16 at 14:18