2

I very often open a lot of file system explorer windows (either under linux or windows). Then I make a big cleanup and close everything. Often, I also close Emacs by mistake.

I'd like to change the behaviour of the 'X' button to minimize instead of closing (leave closing to C-x C-c only). I'm almost sure it's possible, but I don't know how. Anyone to help?

2 Answers 2

1

One possible way to achieve this is to (ab-)use the confirm-kill-emacs mechanism: this is meant to be a function that asks the user for confirmation about killing emacs. However, instead of using an interactive function, you could introduce a special variable that is true only if the kill command has been invoked through C-x C-c, and the confirm function simple returns the value of that variable.

Put the following in your .emacs file:

(defvar killed-from-keyboard nil)

(setq confirm-kill-emacs '(lambda (prompt) killed-from-keyboard))

(defun save-buffers-kill-emacs-from-keyboard (&optional arg)
  (interactive)
  (condition-case nil
      (progn (setq killed-from-keyboard t)
             (save-buffers-kill-terminal arg))
    ((quit error) 
     (setq killed-from-keyboard nil))))

(global-set-key [(control x) (control c)] 'save-buffers-kill-emacs-from-keyboard)
1

If you advise the kill-emacs function, then you can get the functionality that you desire. I have code that makes my emacs frame invisible (hidden), but you can iconify it instead with code similar to the following.

(defvar bnb/really-kill-emacs nil)
(defadvice kill-emacs (around bnb/really-exit activate)
  "Only kill emacs if a prefix is set"
  (if bnb/really-kill-emacs
      ad-do-it)
    (iconify-frame))

(defun bnb/really-kill-emacs ()
  (interactive)
  (setq bnb/really-kill-emacs t)
  (kill-emacs))

The bnb/really-kill-emacs function is defined so that you can actually kill emacs when necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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