开发者

Emacs : prevent from closing from window manager button

开发者 https://www.devze.com 2023-01-30 02:36 出处:网络
I very often open a lot of file system explorer windows (either under linux or windows). Then I make a big cleanup and close every开发者_C百科thing. Often, I also close Emacs by mistake.

I very often open a lot of file system explorer windows (either under linux or windows). Then I make a big cleanup and close every开发者_C百科thing. 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?


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消