Subject: Re: bug#61734: 30.0.50; [PATCH] Defaults to zero for image-dired--number-of-thumbnails

From: Manuel Giraud <[email-masked]>
Date: Fri, 24 Feb 2023 10:35:38 +0100

Hi,

Here is another version that fixes all the "out of sync" counter that I
have… but maybe it start to grow outside the scope of this bug report.
>From 078cff71abc7125558ed492e894aa7d1b487d9bd Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Thu, 23 Feb 2023 16:42:48 +0100
Subject: [PATCH] Fixes on image-dired thumbnails counter (bug#61734)

* lisp/image/image-dired.el (image-dired--number-of-thumbnails):
Defaults to zero to avoid wrong type error down the line.
(image-dired-delete-char): Update thumbnails counter upon deletion.
(image-dired-insert-thumbnail, image-dired--update-header-line,
image-dired-display-thumbs): Remove 'image-number text-property and
rely on the (point).
---
 lisp/image/image-dired.el | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/lisp/image/image-dired.el b/lisp/image/image-dired.el
index 8e2a75a418f..c33a977e5dc 100644
--- a/lisp/image/image-dired.el
+++ b/lisp/image/image-dired.el
@@ -424,11 +424,10 @@ image-dired--get-create-thumbnail-file
                          (file-name-nondirectory thumb-file)))
     thumb-file))
 
-(defun image-dired-insert-thumbnail ( file original-file-name
-                           associated-dired-buffer image-number)
+(defun image-dired-insert-thumbnail (file original-file-name
+                                          associated-dired-buffer)
   "Insert thumbnail image FILE.
-Add text properties ORIGINAL-FILE-NAME, ASSOCIATED-DIRED-BUFFER
-and IMAGE-NUMBER."
+Add text properties ORIGINAL-FILE-NAME, ASSOCIATED-DIRED-BUFFER."
   (let (beg end)
     (setq beg (point))
     (image-dired-insert-image
@@ -452,7 +451,6 @@ image-dired-insert-thumbnail
            'keymap nil
            'original-file-name original-file-name
            'associated-dired-buffer associated-dired-buffer
-           'image-number image-number
            'tags (image-dired-list-tags original-file-name)
            'mouse-face 'highlight
            'comment (image-dired-get-comment original-file-name)))))
@@ -546,7 +544,7 @@ image-dired--line-up-with-method
         (t
          (image-dired-line-up-dynamic))))
 
-(defvar-local image-dired--number-of-thumbnails nil)
+(defvar-local image-dired--number-of-thumbnails 0)
 
 ;;;###autoload
 (defun image-dired-display-thumbs (&optional arg append do-not-pop)
@@ -587,8 +585,8 @@ image-dired-display-thumbs
         (dolist (file files)
           (when (string-match-p (image-dired--file-name-regexp) file)
             (image-dired-insert-thumbnail
-             (image-dired--get-create-thumbnail-file file) file dired-buf
-             (cl-incf image-dired--number-of-thumbnails)))))
+             (image-dired--get-create-thumbnail-file file) file dired-buf)
+            (cl-incf image-dired--number-of-thumbnails))))
       (if (> image-dired--number-of-thumbnails 0)
           (if do-not-pop
               (display-buffer buf)
@@ -789,7 +787,7 @@ image-dired--update-header-line
     (let ((file-name (image-dired-original-file-name))
           (dired-buf (buffer-name (image-dired-associated-dired-buffer)))
           (image-count (format "%s/%s"
-                               (get-text-property (point) 'image-number)
+                               (1+ (/ (point) 2)) ;; Why this magical formula?
                                image-dired--number-of-thumbnails))
           (props (string-join (get-text-property (point) 'tags) ", "))
           (comment (get-text-property (point) 'comment))
@@ -1127,10 +1125,12 @@ image-dired-delete-char
   "Remove current thumbnail from thumbnail buffer and line up."
   (interactive nil image-dired-thumbnail-mode)
   (let ((inhibit-read-only t))
-    (delete-char 1))
+    (delete-char 1)
+    (cl-decf image-dired--number-of-thumbnails))
   (let ((pos (point)))
     (image-dired--line-up-with-method)
-    (goto-char pos)))
+    (goto-char pos)
+    (image-dired--update-header-line)))
 
 (defun image-dired-line-up ()
   "Line up thumbnails according to `image-dired-thumbs-per-row'.
-- 
2.39.1

-- 
Manuel Giraud