Subject: Re: bug#77323: [PATCH] Allow temp buffer cleanup in ediff-current-file

From: "Paul D. Nelson" <[email-masked]>
Date: Sat, 29 Mar 2025 09:29:33 +0100

> - Have you thought about checking
> `ediff-current-file-cleanup-temporary-buffer' when quitting Ediff,
> instead of when entering Ediff (with other words: in the lambda you add
> to `ediff-quit-hook')?  I think this would fit better with the expected
> behavior.  A user might toggle the new option when the Ediff session is
> already running.

My reasons for "checking before" (basically to allow let-binding of the
variable) were less compelling and specific than what you describe, so I
agree that "checking after" is more natural and have updated the patch
accordingly.

>
> - Is this the only Ediff command that leaves such temporary buffers
> behind?

The other Ediff commands that create buffers are:

- ediff-regions-* and ediff-windows-*.  These create indirect buffers
  that are cleaned up when Ediff quits (via ediff-temp-indirect-buffer).

- ediff-patch-buffer.  This creates a buffer *_patched with the result
  of applying a patch to buffer *.  The persistence of this buffer is
  intentional - it allows the user to inspect or save the result.

- ediff-patch-file.  This applies a patch to a file, creating a backup
  (e.g., ediff.el.orig), and leaves buffers visiting both files.

- vc-ediff and vc-version-ediff.  These create buffers like
  ediff.el.~c0a6429b5672f43114d7d67e268f31998723071d~, which can be
  created by other VC commands (like log-view-find-revision) and are
  reused across calls.

In each case, the buffers either visit files, are cleaned up, or are
explicitly intended for further use by the client or other commands.

There is also the option ediff-keep-variants, for which nil means to
ask to remove any unmodified A/B/C buffers when quitting Ediff.

>From 7279b700cd9e95f7c1a6c3c193a1438358233496 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 | 16 +++++++++++++++-
 2 files changed, 20 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..f7e3be2384f 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,15 @@ 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)
+     (list (lambda ()
+             (add-hook 'ediff-quit-hook
+                       (lambda ()
+                         (when (and ediff-current-file-cleanup-temporary-buffer
+                                    (buffer-live-p revert-buf))
+                           (kill-buffer revert-buf)))
+                       nil t))))))

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