Subject: bug#61394: 30.0.50; [PATCH] Image-dired thumb name based on content

From: Manuel Giraud <[email-masked]>
Date: Fri, 04 Aug 2023 09:44:41 +0200

Eli Zaretskii <[email-masked]> writes:

>> From: Manuel Giraud <[email-masked]>
>> Cc: [email-masked],  [email-masked],  [email-masked]
>> Date: Thu, 03 Aug 2023 18:51:28 +0200
>> 
>> Eli Zaretskii <[email-masked]> writes:
>> 
>> >> +(defun image-dired-update-thumbnail-at-point ()
>> >> +  "Update the thumbnail at point if the original image file has been
>> >> +modified.  Take care of uncaching and removing the old thumbnail
>> >> +file."
>> >
>> > The first line of the doc string should be a single complete sentence.
>> > (Just move the "modified" part to the first line, I think there's
>> > enough space there.)
>> 
>> I don't understand.  "Update the thumbnail at point." does not seem
>> sufficient because the fact that the original image might be modified is
>> the main point of this function.
>
> What I had in mind is this:
>
>  (defun image-dired-update-thumbnail-at-point ()
>    "Update the thumbnail at point if the original image file has been modified.
>  This function uncaches and removes the thumbnail file under the old name."
>
> OK?

Ok.  Sorry I misread your reply ("move" not "remove").  BTW, I'm using
'M-q' on docstring and my fill-column is set to 70 (the default I
guess): is there a better way?

>> What about:
>> 
>> "When 'image-dired-thumb-naming' is set to 'sha1-contents', 'thumb' and
>> 'old-thumb' could be different file names.  Update the thumbnail then."
>
> Much better, thanks.

Ok, so here is a new patch.
>From b67f344f148dbabf8f2c0099aa9002af4570c5dd Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Tue, 1 Aug 2023 18:56:33 +0200
Subject: [PATCH] Fix thumbnail update when thumb name is based on image
 content

* lisp/image/image-dired-util.el
(image-dired-update-thumbnail-at-point): New function to update
thumbnail when original image contents changed.

* lisp/image/image-dired-external.el
(image-dired-rotate-original): Use it.

* lisp/image/image-dired.el (image-dired-display-thumbs): Fix
spacing while here.
---
 lisp/image/image-dired-external.el |  3 ++-
 lisp/image/image-dired-util.el     | 17 +++++++++++++++++
 lisp/image/image-dired.el          |  2 +-
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/lisp/image/image-dired-external.el b/lisp/image/image-dired-external.el
index 9f35e17a7e6..77352c25a3b 100644
--- a/lisp/image/image-dired-external.el
+++ b/lisp/image/image-dired-external.el
@@ -405,7 +405,8 @@ image-dired-rotate-original
                 (not image-dired-rotate-original-ask-before-overwrite))
             (progn
               (copy-file image-dired-temp-rotate-image-file file t)
-              (image-dired-refresh-thumb))
+              (image-dired-refresh-thumb)
+              (image-dired-update-thumbnail-at-point))
           (image-dired-display-image file))))))
 
 
diff --git a/lisp/image/image-dired-util.el b/lisp/image/image-dired-util.el
index 70911bce45a..53a5e274175 100644
--- a/lisp/image/image-dired-util.el
+++ b/lisp/image/image-dired-util.el
@@ -190,6 +190,23 @@ image-dired-image-at-point-p
   "Return non-nil if there is an `image-dired' thumbnail at point."
   (get-text-property (point) 'image-dired-thumbnail))
 
+(defun image-dired-update-thumbnail-at-point ()
+  "Update the thumbnail at point if the original image file has been modified.
+This function uncaches and removes the thumbnail file under the old name."
+  (when (image-dired-image-at-point-p)
+    (let* ((file (image-dired-original-file-name))
+           (thumb (expand-file-name (image-dired-thumb-name file)))
+           (image (get-text-property (point) 'display)))
+      (when image
+        (let ((old-thumb (plist-get (cdr image) :file)))
+          ;; When 'image-dired-thumb-naming' is set to
+          ;; 'sha1-contents', 'thumb' and 'old-thumb' could be
+          ;; different file names.  Update the thumbnail then.
+          (unless (string= thumb old-thumb)
+            (setf (plist-get (cdr image) :file) thumb)
+            (clear-image-cache old-thumb)
+            (delete-file old-thumb)))))))
+
 (defun image-dired-window-width-pixels (window)
   "Calculate WINDOW width in pixels."
   (declare (obsolete window-body-width "29.1"))
diff --git a/lisp/image/image-dired.el b/lisp/image/image-dired.el
index 98596510ec1..33beb5b3e49 100644
--- a/lisp/image/image-dired.el
+++ b/lisp/image/image-dired.el
@@ -590,7 +590,7 @@ image-dired-display-thumbs
 `image-dired-previous-line-and-display' where we do not want the
 thumbnail buffer to be selected."
   (interactive "P" nil dired-mode)
-  (setq image-dired--generate-thumbs-start  (current-time))
+  (setq image-dired--generate-thumbs-start (current-time))
   (let ((buf (image-dired-create-thumbnail-buffer))
         files dired-buf)
     (if arg
-- 
2.40.0

-- 
Manuel Giraud