From 49f1af6333b90a700c52fd4845583c0506d14795 Mon Sep 17 00:00:00 2001 From: Manuel Giraud Date: Fri, 29 Dec 2023 11:48:21 +0100 Subject: [PATCH] Add some user control in image-mode caching Bug#68006 * lisp/image-mode.el (image-mode-eager-cache-flush): New custom. (image-toggle-display-image): Add a contents hash in image spec when flushing cache lazily. * lisp/image/image-dired-util.el (image-dired-thumb-name): * lisp/image.el (image-contents-sha1): Move 'image-dired-contents-sha1' to 'image-contents-sha1' in toplevel image library. --- lisp/image-mode.el | 20 ++++++++++++++++++-- lisp/image.el | 11 ++++++++++- lisp/image/image-dired-util.el | 8 +------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lisp/image-mode.el b/lisp/image-mode.el index d5ca6348c92..68be3faaae7 100644 --- a/lisp/image-mode.el +++ b/lisp/image-mode.el @@ -93,6 +93,16 @@ image-auto-resize-on-window-resize :version "27.1" :group 'image) +(defcustom image-mode-eager-cache-flush t + "Non-nil means an image is flushed from the cache before being +read again. When set to nil, an image spec is not removed from +the cache so it will be displayed faster the second time. + +See `image-cache-eviction-delay'." + :type 'boolean + :version "30.1" + :group 'image) + (defvar-local image-transform-resize nil "The image resize operation. Non-nil to resize the image upon first display. @@ -954,8 +964,14 @@ image-toggle-display-image (plist-put (cdr image) :width (plist-get (cdr image) :max-width))))) - ;; Discard any stale image data before looking it up again. - (image-flush image) + (if image-mode-eager-cache-flush + ;; Discard any stale image data before looking it up again. + (image-flush image) + ;; Add a content based hash into image spec to be sure that the + ;; cache is updated should the on disk image change. + (when (and filename (file-exists-p filename)) + (setq image (append image (list :hash (image-contents-sha1 filename)))))) + (setq image (append image (image-transform-properties image))) (setq props `(display ,image diff --git a/lisp/image.el b/lisp/image.el index e20fbcf4c98..0f1c74d9250 100644 --- a/lisp/image.el +++ b/lisp/image.el @@ -401,7 +401,16 @@ image-type-from-file-name ;; If nothing seems to be supported, return first type that matched. (or first (setq first type)))))))) - ;;;###autoload +;;;###autoload +(defun image-contents-sha1 (filename) + "Compute the SHA-1 of the first 4KiB of FILENAME's contents. +The first 4KiB does not represent the whole file but was chosen +because it is fast enough for interactive usage." + (with-temp-buffer + (insert-file-contents-literally filename nil 0 4096) + (sha1 (current-buffer)))) + +;;;###autoload (defun image-supported-file-p (file) "Say whether Emacs has native support for displaying TYPE. The value is a symbol specifying the image type, or nil if type diff --git a/lisp/image/image-dired-util.el b/lisp/image/image-dired-util.el index e17cc6c919f..e5f526fec57 100644 --- a/lisp/image/image-dired-util.el +++ b/lisp/image/image-dired-util.el @@ -59,12 +59,6 @@ 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' and @@ -99,7 +93,7 @@ image-dired-thumb-name (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) + (image-contents-sha1 file) ;; Defaults to SHA-1 of file name (sha1 file)))) (cond ((or (eq 'image-dired image-dired-thumbnail-storage) -- 2.43.0