;; Emacs 29.1付属のimage-dired-dired.elより;;;###autoload
(defunimage-dired-dired-toggle-marked-thumbs (&optional arg)
"Toggle thumbnails in front of marked file names in the Dired buffer.If no file is marked, toggle display of thumbnail on the current file's line.ARG, if non-nil (interactively, the prefix argument), specifies the fileswhose thumbnail display to toggle instead of the marked files: if ARG is aninteger, use the next ARG (or previous -ARG, if ARG<0) files; any othervalue of ARG means toggle thumbnail display of the current line's file."
(interactive"P" dired-mode)
(setq image-dired--generate-thumbs-start (current-time))
(dired-map-over-marks
(let ((image-pos (dired-move-to-filename))
(image-file (dired-get-filename nil t))
thumb-file
overlay)
(when (and image-file
(string-match-p (image-dired--file-name-regexp) image-file))
(setq thumb-file (create-image
(image-dired--get-create-thumbnail-file image-file)))
;; If image is not already added, then add it.
(let ((thumb-ov (cl-loop for ov in (overlays-in (point) (1+ (point)))
if (overlay-get ov 'thumb-file) return ov)))
(if thumb-ov
(delete-overlay thumb-ov)
(put-image thumb-file image-pos)
(setq overlay
(cl-loop for ov in (overlays-in (point) (1+ (point)))
if (overlay-get ov 'put-image) return ov))
(overlay-put overlay 'image-file image-file)
(overlay-put overlay 'thumb-file thumb-file))
;; ★ここに追加したい
)))
;; Show or hide thumbnail on ARG next files.
arg)
(add-hook 'dired-after-readin-hook
'image-dired-dired-after-readin-hook nil t))
(defunmy-image-dired-dired-toggle-marked-thumbs (&optional arg)
;; Derived from `image-dired-dired-toggle-marked-thumbs'"Toggle thumbnails in front of file names in the Dired buffer.If no marked file could be found, insert or hide thumbnails on thecurrent line. ARG, if non-nil, specifies the files to use insteadof the marked files. If ARG is an integer, use the next ARG (orprevious -ARG, if ARG<0) files."
(interactive"P")
(dired-map-over-marks
(my-image-dired-dired-set-thumb-visibility 'toggle) ;;ファイル毎の処理を分離
arg ; Show or hide image on ARG next files.
'show-progress) ; Update dired display after each image is updated.
(add-hook 'dired-after-readin-hook
'image-dired-dired-after-readin-hook nil t))
;; ★2023-08-17追加
(defvarmy-image-dired-dired-change-thumb-hook nil
"diredバッファ内のサムネイルの表示状態が変化したら呼び出されるフックです。呼ばれるときの引数は(THUMBNAIL-OVERLAY ORIGINAL-IMAGE-FILENAMEIMAGE-POSITION)です。現在のバッファは変化したdiredバッファです。")
(defunmy-image-dired-dired-set-thumb-visibility (visibility)
;; Derived from `image-dired-dired-toggle-marked-thumbs'
(let ((image-pos (dired-move-to-filename))
(image-file (dired-get-filename nil t)))
(when (and image-file
(string-match-p (image-file-name-regexp) image-file))
(let* ((thumb-file
;; Emacs 28まで;;(image-dired-get-thumbnail-image image-file);; Emacs 29から
(create-image
(image-dired--get-create-thumbnail-file image-file)))
(thumb-ov (cl-loop for ov in (overlays-in (point) (1+ (point)))
if (overlay-get ov 'thumb-file) return ov)))
;; 他から使うためにトグル以外もできるようにした
(if thumb-ov
(when (memq visibility '(nil toggle))
(delete-overlay thumb-ov)
;; ★2023-08-17追加 : 表示→非表示
(run-hook-with-args 'my-image-dired-dired-change-thumb-hook
nil image-file image-pos))
(when (memq visibility '(t toggle))
;; ★2023-08-17修正 : 非表示→表示
(let ((new-thumb-ov
;; 独自の関数を呼ぶ
(my-image-dired-dired-create-thumbnail-overlay
image-pos image-file thumb-file)))
(run-hook-with-args 'my-image-dired-dired-change-thumb-hook
new-thumb-ov image-file image-pos))))))))