Subject: [PATCH] Allow temp buffer cleanup in ediff-current-file
From: "Paul D. Nelson" <[email-masked]>
To: [email-masked]
Date: Thu, 27 Mar 2025 22:51:57 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

When ediff-current-file compares a modified buffer with its saved
version, it creates a temporary "FILE=..." buffer to hold the saved
contents.  Currently, this buffer persists after the Ediff session
quits, requiring manual cleanup later.

In my experience, this buffer serves no further purpose once the
comparison is done, and just clutters the buffer list.  On the other
hand, I could imagine someone using it as a way to retain a snapshot of
the saved file state.

The attached patch introduces a user option that, when non-nil, causes
the "FILE=..." buffer to be killed automatically after quitting Ediff.

The default is nil, so that the patch does not modify existing behavior.

Such automatic cleanup does not seem achievable via existing Ediff
hooks.

Any feedback would be welcome.

Thanks, best,

Paul

From c0a6429b5672f43114d7d67e268f31998723071d Mon Sep 17 00:00:00 2001
From: Paul Nelson <[email-masked]>
Date: Thu, 27 Mar 2025 22:00:08 +0100
Subject: [PATCH] Allow temp buffer cleanup in ediff-current-file

* lisp/vc/ediff.el
(ediff-current-file-cleanup-temporary-buffer): New custom option
that controls whether to kill the temporary buffer created by
ediff-current-file.
(ediff-current-file): Use it.
---
 etc/NEWS         |  5 +++++
 lisp/vc/ediff.el | 15 ++++++++++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index 1bd2fd6d486..7fbd6f830af 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1199,6 +1199,11 @@ is non-nil, this suffix is fontified using 'font-lock-type-face'.
 
 ** Ediff
 
++++
+*** New user option 'ediff-current-file-cleanup-temporary-buffer'.
+This option controls whether to kill the 'FILE=...' buffer created by
+'ediff-current-file' when the Ediff session exits.
+
 +++
 *** Ediff's copy commands now apply to all changes with 'C-u' prefix.
 The Ediff copy commands, bound to 'a', 'b', 'ab', etc., now copy all
diff --git a/lisp/vc/ediff.el b/lisp/vc/ediff.el
index 36580c73d6b..401c6e2c119 100644
--- a/lisp/vc/ediff.el
+++ b/lisp/vc/ediff.el
@@ -359,6 +359,12 @@ ediff-files-internal
 ;;;###autoload
 (defalias 'ediff #'ediff-files)
 
+(defcustom ediff-current-file-cleanup-temporary-buffer nil
+  "If non-nil, kill the 'FILE=...' buffer created by `ediff-current-file'.
+If this option is non-nil, the buffer is killed automatically when the
+Ediff session ends.  Otherwise, the buffer is preserved."
+  :type 'boolean)
+
 ;;;###autoload
 (defun ediff-current-file ()
   "Start ediff between current buffer and its file on disk.
@@ -395,7 +401,14 @@ ediff-current-file
       (insert-file-contents file-name)
       ;; Assume same modes:
       (funcall current-major))
-    (ediff-buffers revert-buf (current-buffer))))
+    (ediff-buffers revert-buf (current-buffer)
+                   (when ediff-current-file-cleanup-temporary-buffer
+                     (list (lambda ()
+                             (add-hook 'ediff-quit-hook
+                                       (lambda ()
+                                         (when (buffer-live-p revert-buf)
+                                           (kill-buffer revert-buf)))
+                                       nil t)))))))

 ;;;###autoload
-- 
2.39.3 (Apple Git-145)