unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Variable for Confirming Killing a Buffer
@ 2021-12-02 21:06 Oliver Taylor
  2021-12-02 22:41 ` Stefan Kangas
  2021-12-03 16:58 ` Lars Ingebrigtsen
  0 siblings, 2 replies; 11+ messages in thread
From: Oliver Taylor @ 2021-12-02 21:06 UTC (permalink / raw)
  To: emacs-devel

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!




^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2021-12-03 22:33 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-02 21:06 Variable for Confirming Killing a Buffer Oliver Taylor
2021-12-02 22:41 ` Stefan Kangas
2021-12-02 23:17   ` Philip Kaludercic
2021-12-02 23:25   ` Oliver Taylor
2021-12-03 16:58 ` Lars Ingebrigtsen
2021-12-03 19:24   ` Michael Heerdegen
2021-12-03 21:35     ` Lars Ingebrigtsen
2021-12-03 22:27       ` Michael Heerdegen
2021-12-03 22:33         ` Lars Ingebrigtsen
2021-12-03 19:45   ` Oliver Taylor
2021-12-03 21:32     ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).