Subject: [PATCH] Flush preview images when leaving previews visible
From: "Paul D. Nelson" <[email-masked]>
To: [email-masked]
Date: Thu, 29 May 2025 10:58:30 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi all,

This patch fixes a rare bug with the setting
preview-leave-open-previews-visible that I added last year.

"Recall" that with the default value nil for that setting, when the user
moves the point into a previewed region, the TeX code becomes visible
and a TeX icon is shown in place of the preview graphic.  On the other
hand, when the setting is t, the TeX icon is replaced by the old preview
image, which thus remains visible.  After editing the TeX code, the user
can then regenerate the preview image, either manually via the preview-*
commands or automatically via the ELPA package preview-auto.

Some users told me that the setting did not work correctly in shared
drives, such as Dropbox and OneDrive.  The symptom was that the image
would not update correctly after editing in the manner described above.
I was never able to reproduce the issue myself, but eventually stumbled
upon [1], which led to the attached patch, a variant of which yielded
positive feedback from two of the users who had noted the issue.

The patch is conservative in that it only affects users of the setting
preview-leave-open-previews-visible.  Any feedback welcome.

[1]
https://lists.gnu.org/archive/html/bug-gnu-emacs/2022-12/msg00681.html

Thanks, best,

Paul

From 34cc04d34ed6d1bd8b0d4df6f9759efb2792eab7 Mon Sep 17 00:00:00 2001
From: Paul Nelson <[email-masked]>
Date: Tue, 27 May 2025 13:50:22 +0200
Subject: [PATCH] Flush preview images when leaving previews visible

* preview.el (preview-replace-active-icon): When
preview-leave-open-previews-visible is t, flush the current
image spec before mutating it in place.  Without this flush,
Emacs might still show the cached previous image (see Emacs
bug#59902 for related discussion).
---
 preview.el | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/preview.el b/preview.el
index 7f1027dc..cbb05a1f 100644
--- a/preview.el
+++ b/preview.el
@@ -1205,8 +1205,11 @@ is located."
   (cons 'image (cdr icon)))
 
 (defsubst preview-replace-active-icon (ov replacement)
-  "Replace the active Icon in OV by REPLACEMENT, another icon."
   (let ((img (overlay-get ov 'preview-image)))
+    (when (and preview-leave-open-previews-visible
+               (consp img))
+      ;; No "TeX icon" has been shown, so we flush manually.
+      (image-flush (car img) t))
     (setcdr (car img) (cdar replacement))
     (setcdr img (cdr replacement))))
 
-- 
2.39.3 (Apple Git-145)