unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Manuel Giraud via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: contovob@tcd.ie, stefankangas@gmail.com, 61394@debbugs.gnu.org
Subject: bug#61394: 30.0.50; [PATCH] Image-dired thumb name based on content
Date: Thu, 27 Jul 2023 15:52:55 +0200	[thread overview]
Message-ID: <87fs59qx54.fsf@ledu-giraud.fr> (raw)
In-Reply-To: <83ila5n8co.fsf@gnu.org> (Eli Zaretskii's message of "Thu, 27 Jul 2023 10:04:23 +0300")

[-- Attachment #1: Type: text/plain, Size: 263 bytes --]

Eli Zaretskii <eliz@gnu.org> writes:

> Thanks for the reminder.  I think we should install this now.  So
> please rebase on the current master branch and repost, and I will
> install it then.

Thanks.  Here is the rebased version of the patch.
-- 
Manuel Giraud

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-New-option-image-dired-thumb-naming-bug-61394.patch --]
[-- Type: text/x-patch, Size: 8573 bytes --]

From 83002e2cda2c3c8c28b2e37837625709fc4ca233 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <manuel@ledu-giraud.fr>
Date: Sat, 25 Feb 2023 19:27:07 +0100
Subject: [PATCH] New option 'image-dired-thumb-naming' (bug#61394)

* lisp/image/image-dired.el (image-dired-thumb-naming): New user
option to control thumbnail name.

* lisp/image/image-dired-util.el (image-dired-thumb-name): Update
to use new user option and compute contents SHA-1 if needed.
(image-dired-contents-sha1): New function to compute the SHA-1 of the
first 4KiB of a file.
---
 etc/NEWS                       |  5 ++
 lisp/image/image-dired-util.el | 84 +++++++++++++++++++++-------------
 lisp/image/image-dired.el      | 26 ++++++++++-
 3 files changed, 80 insertions(+), 35 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index d0dab755212..a8e7d97df7e 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -567,6 +567,11 @@ Similarly to buffer restoration by Desktop, 'recentf-mode' checking
 of the accessibility of remote files can now time out if
 'remote-file-name-access-timeout' is set to a positive number.
 
+** Image Dired
+
+*** New user option 'image-dired-thumb-naming'.
+You can now configure how a thumbnail is named using this option.
+
 \f
 * New Modes and Packages in Emacs 30.1
 
diff --git a/lisp/image/image-dired-util.el b/lisp/image/image-dired-util.el
index a80b3afc0f3..3f6880fc807 100644
--- a/lisp/image/image-dired-util.el
+++ b/lisp/image/image-dired-util.el
@@ -30,6 +30,7 @@
 (eval-when-compile (require 'cl-lib))
 
 (defvar image-dired-dir)
+(defvar image-dired-thumb-naming)
 (defvar image-dired-thumbnail-storage)
 
 (defconst image-dired--thumbnail-standard-sizes
@@ -57,42 +58,59 @@ image-dired-dir
       (message "Thumbnail directory created: %s" image-dired-dir))
     image-dired-dir))
 
+(defun image-dired-contents-sha1 (filename)
+  "Compute the SHA-1 of the first 4KiB of FILENAME's contents."
+  (with-temp-buffer
+    (insert-file-contents-literally filename nil 0 4096)
+    (sha1 (current-buffer))))
+
 (defun image-dired-thumb-name (file)
   "Return absolute file name for thumbnail FILE.
-Depending on the value of `image-dired-thumbnail-storage', the
-file name of the thumbnail will vary:
-- For `use-image-dired-dir', make a SHA1-hash of the image file's
-  directory name and add that to make the thumbnail file name
-  unique.
-- For `per-directory' storage, just add a subdirectory.
-- For `standard' storage, produce the file name according to the
-  Thumbnail Managing Standard.  Among other things, an MD5-hash
-  of the image file's directory name will be added to the
-  filename.
-See also `image-dired-thumbnail-storage'."
+Depending on the value of `image-dired-thumbnail-storage' and
+`image-dired-thumb-naming', the file name of the thumbnail will
+vary:
+
+- If `image-dired-thumbnail-storage' is set to one of the value
+  of `image-dired--thumbnail-standard-sizes', produce the file
+  name according to the Thumbnail Managing Standard.  Among other
+  things, an MD5-hash of the image file's directory name will be
+  added to the file name.
+
+- Otherwise `image-dired-thumbnail-storage' is used to set the
+  directory where to store the thumbnail.  In this latter case
+  the name given to the thumbnail depends on the value of
+  `image-dired-thumb-naming'.
+
+See also `image-dired-thumbnail-storage' and
+`image-dired-thumb-naming'."
   (let ((file (expand-file-name file)))
-    (cond ((memq image-dired-thumbnail-storage
-                 image-dired--thumbnail-standard-sizes)
-           (let ((thumbdir (cl-case image-dired-thumbnail-storage
-                             (standard "thumbnails/normal")
-                             (standard-large "thumbnails/large")
-                             (standard-x-large "thumbnails/x-large")
-                             (standard-xx-large "thumbnails/xx-large"))))
-             (expand-file-name
-              ;; MD5 is mandated by the Thumbnail Managing Standard.
-              (concat (md5 (concat "file://" file)) ".png")
-              (expand-file-name thumbdir (xdg-cache-home)))))
-          ((or (eq 'image-dired image-dired-thumbnail-storage)
-               ;; Maintained for backwards compatibility:
-               (eq 'use-image-dired-dir image-dired-thumbnail-storage))
-           (expand-file-name (format "%s.jpg" (sha1 file))
-                             (image-dired-dir)))
-          ((eq 'per-directory image-dired-thumbnail-storage)
-           (expand-file-name (format "%s.thumb.jpg"
-                                     (file-name-nondirectory file))
-                             (expand-file-name
-                              ".image-dired"
-                              (file-name-directory file)))))))
+    (if (memq image-dired-thumbnail-storage
+              image-dired--thumbnail-standard-sizes)
+        (let ((thumbdir (cl-case image-dired-thumbnail-storage
+                          (standard "thumbnails/normal")
+                          (standard-large "thumbnails/large")
+                          (standard-x-large "thumbnails/x-large")
+                          (standard-xx-large "thumbnails/xx-large"))))
+          (expand-file-name
+           ;; MD5 and PNG is mandated by the Thumbnail Managing
+           ;; Standard.
+           (concat (md5 (concat "file://" file)) ".png")
+           (expand-file-name thumbdir (xdg-cache-home))))
+      (let ((name (if (eq 'sha1-contents image-dired-thumb-naming)
+                      (image-dired-contents-sha1 file)
+                    ;; Defaults to SHA-1 of file name
+                    (if (eq 'per-directory image-dired-thumbnail-storage)
+                        (sha1 (file-name-nondirectory file))
+                      (sha1 file)))))
+        (cond ((or (eq 'image-dired image-dired-thumbnail-storage)
+                   ;; Maintained for backwards compatibility:
+                   (eq 'use-image-dired-dir image-dired-thumbnail-storage))
+               (expand-file-name (format "%s.jpg" name) (image-dired-dir)))
+              ((eq 'per-directory image-dired-thumbnail-storage)
+               (expand-file-name (format "%s.jpg" name)
+                                 (expand-file-name
+                                  ".image-dired"
+                                  (file-name-directory file)))))))))
 
 (defvar image-dired-thumbnail-buffer "*image-dired*"
   "Image-Dired's thumbnail buffer.")
diff --git a/lisp/image/image-dired.el b/lisp/image/image-dired.el
index b13b3e08ce2..96a0c2ef9bc 100644
--- a/lisp/image/image-dired.el
+++ b/lisp/image/image-dired.el
@@ -162,8 +162,27 @@ image-dired-dir
 `image-dired-thumbnail-storage'."
   :type 'directory)
 
+(defcustom image-dired-thumb-naming 'sha1-filename
+  "How `image-dired' names thumbnail files.
+When set to `sha1-filename' the name of thumbnail is built by
+computing the SHA-1 of the full file name of the image.
+
+When set to `sha1-contents' the name of thumbnail is built by
+computing the SHA-1 of first 4KiB of the image contents (See
+`image-dired-contents-sha1').
+
+In both case, a \"jpg\" extension is appended to save as JPEG.
+
+The value of this option is ignored if Image-Dired is customized
+to use the Thumbnail Managing Standard.  See
+`image-dired-thumbnail-storage'."
+  :type '(choice :tag "How to name thumbnail files"
+                 (const :tag "SHA-1 of the image file name" sha1-filename)
+                 (const :tag "SHA-1 of the image contents" sha1-contents))
+  :version "30.1")
+
 (defcustom image-dired-thumbnail-storage 'image-dired
-  "How `image-dired' stores thumbnail files.
+  "Where `image-dired' stores thumbnail files.
 There are three ways that Image-Dired can store and generate
 thumbnails:
 
@@ -189,6 +208,9 @@ image-dired-thumbnail-storage
 
     Set this user option to `per-directory'.
 
+To control the naming of thumbnails for alternatives (2) and (3)
+above, customize the value of `image-dired-thumb-naming'.
+
 To control the default size of thumbnails for alternatives (2)
 and (3) above, customize the value of `image-dired-thumb-size'.
 
@@ -197,7 +219,7 @@ image-dired-thumbnail-storage
 
 For more information on the Thumbnail Managing Standard, see:
 https://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-latest.html"
-  :type '(choice :tag "How to store thumbnail files"
+  :type '(choice :tag "Where to store thumbnail files"
                  (const :tag "Use image-dired-dir" image-dired)
                  (const :tag "Thumbnail Managing Standard (normal 128x128)"
                         standard)
-- 
2.40.0


  reply	other threads:[~2023-07-27 13:52 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-09 19:06 bug#61394: 30.0.50; [PATCH] Image-dired thumb name based on content Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-10 15:13 ` Basil Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-10 18:46   ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-11  9:50     ` Eli Zaretskii
2023-02-11 12:30       ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-11 14:53         ` Eli Zaretskii
2023-02-11 22:33           ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-11 23:06           ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-12  3:02             ` Stefan Kangas
2023-02-12 21:53               ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-15 14:19                 ` Stefan Kangas
2023-02-15 15:35                   ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-19 14:06                     ` Stefan Kangas
2023-02-19 14:43                       ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-19 16:19                         ` Stefan Kangas
2023-02-20  9:20                           ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-25 18:45                           ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-26 19:18                             ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-27  7:04                               ` Eli Zaretskii
2023-07-27 13:52                                 ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-07-27 14:16                                   ` Eli Zaretskii
2023-07-27 21:30                                     ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-28  6:55                                       ` Eli Zaretskii
2023-07-28  9:33                                         ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-28 12:20                                           ` Eli Zaretskii
2023-07-28 16:00                                             ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-28 18:44                                               ` Eli Zaretskii
2023-07-29  9:51                                                 ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-29 10:34                                                   ` Eli Zaretskii
2023-07-29 16:50                                                     ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-03  8:43                                                       ` Eli Zaretskii
2023-07-29 10:47                                                   ` Michael Albinus
2023-07-31 15:53                                                   ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-01 17:05                                                     ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-02 11:42                                                       ` Eli Zaretskii
2023-08-02 18:00                                                         ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-02 18:16                                                           ` Eli Zaretskii
2023-08-03 11:10                                                             ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-03 11:38                                                               ` Eli Zaretskii
2023-08-03 16:51                                                                 ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-03 18:30                                                                   ` Eli Zaretskii
2023-08-04  7:44                                                                     ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-04 10:55                                                                       ` Eli Zaretskii
2023-08-04 13:37                                                                         ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-04 14:05                                                                           ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87fs59qx54.fsf@ledu-giraud.fr \
    --to=bug-gnu-emacs@gnu.org \
    --cc=61394@debbugs.gnu.org \
    --cc=contovob@tcd.ie \
    --cc=eliz@gnu.org \
    --cc=manuel@ledu-giraud.fr \
    --cc=stefankangas@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).