emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Variable for Confirming Killing a Buffer


From: Oliver Taylor
Subject: Variable for Confirming Killing a Buffer
Date: Thu, 2 Dec 2021 13:06:52 -0800

Emacs asks for confirmation when killing modified file-visiting buffers, but
does not do so for non-file-visiting buffers.

The option `buffer-offer-save' tells Emacs to prompt to you save modified
non-file-visiting buffers when EXITING Emacs, but no such option exists for
killing buffers (as described in the docstring for `buffer-offer-save').

I’ve wanted this feature for a while, and managed to create a working solution
with this:

--------------------------------
(defvar-local buffer-confirm-kill nil
 "Non-nil means confirm killing buffer when modified.
Variable is checked by `buffer-confirm-kill-p'.")

(defun buffer-confirm-kill-p ()
 "Return nil if buffer is modified and `buffer-confirm-kill' is t.
This function is designed to be called from `kill-buffer-query-functions'."
 (if (and (buffer-modified-p)
          buffer-confirm-kill)
     (yes-or-no-p
      (format "Buffer %S is modified; kill anyway? " (buffer-name)))
   t))

(add-hook 'kill-buffer-query-functions #'buffer-confirm-kill-p)
--------------------------------

Now, doing (setq-local buffer-confirm-kill t) will prevent accidental data
loss.

It’s been so useful that I wonder if something like this shouldn’t just be
part of Emacs itself.

I just had a new baby so I can’t promise to keep up with responses to this
thread, but I wanted to post a first draft of this now, both to get feedback
on the idea, and to enable anyone who might have the urge to run with the
idea. I’d love to turn it into a proper submission myself, but I might have to
wait until I’m sleeping through the night!




reply via email to

[Prev in Thread] Current Thread [Next in Thread]