Subject: bug#26432: Acknowledgement (image-dired: Adding support for PDF thumbnails.)

From: Keith David Bershatsky <[email-masked]>
Date: Mon, 10 Apr 2017 21:01:50 -0700

In my own setup, I added the ability to detect the image size of the thumbnails and compare it to the current value of the image-dired width/height variables so that a new thumbnail will be created if the width/height of a stored cache thumbnail is different the current call.  For example, multiple thumbnails might be 100 pixels, but perhaps I want to see an 800 thumbnail of just the selected file.  This modification enables me to change the thumbnail image programmatically:

(defun image-dimensions (filename)
  (let* ((convert-program image-dired-cmd-create-thumbnail-program)
         (raw-dimensions
           (shell-command-to-string
             (concat convert-program " " "\"" filename "\"" " -ping -format \"%w x %h\" info:")))
         (list-dimensions
           (delete "x" (split-string raw-dimensions))))
    (cons
      (string-to-number (car list-dimensions))
      (string-to-number (cadr list-dimensions)))))

;;; ALTERNATIVE VERSION:

(defun image-dired-display-thumbs (&optional arg append do-not-pop)
  "Display thumbnails of all marked files, in `image-dired-thumbnail-buffer'.
If a thumbnail image does not exist for a file, it is created on the
fly.  With prefix argument ARG, display only thumbnail for file at
point (this is useful if you have marked some files but want to show
another one).

Recommended usage is to split the current frame horizontally so that
you have the dired buffer in the left window and the
`image-dired-thumbnail-buffer' buffer in the right window.

With optional argument APPEND, append thumbnail to thumbnail buffer
instead of erasing it first.

Optional argument DO-NOT-POP controls if `pop-to-buffer' should be
used or not.  If non-nil, use `display-buffer' instead of
`pop-to-buffer'.  This is used from functions like
`image-dired-next-line-and-display' and
`image-dired-previous-line-and-display' where we do not want the
thumbnail buffer to be selected."
  (interactive "P")
  (let ((buf (image-dired-create-thumbnail-buffer))
        thumb-name files dired-buf)
    (if arg
        (setq files (list (dired-get-filename)))
      (setq files (dired-get-marked-files)))
    (setq dired-buf (current-buffer))
    (with-current-buffer buf
      (let ((inhibit-read-only t))
        (if (not append)
            (erase-buffer)
          (goto-char (point-max)))
        (mapc
         (lambda (curr-file)
           (cond
             ((string-match (image-file-name-regexp) curr-file)
               (let* ((thumb-name (image-dired-thumb-name curr-file))
                      (thumbnail-dimensions
                        (when (file-exists-p thumb-name)
                          (image-dimensions thumb-name))))
                 (if (and
                        ;;; The goal is to move on to `image-dired-create-thumb' IF
                        ;;; the thumbnail exists and is the wrong size, or it does not exist.
                        (or (and (file-exists-p thumb-name)
                                 (not (or (= image-dired-thumb-width
                                             (car thumbnail-dimensions))
                                          (= image-dired-thumb-height
                                             (cdr thumbnail-dimensions))))
                                 (progn
                                   (clear-image-cache)
                                   'create-new-image))
                            (not (file-exists-p thumb-name)))
                        (not (= 0 (image-dired-create-thumb curr-file thumb-name))))
                   (message "Thumb could not be created for file %s" curr-file)
                   (image-dired-insert-thumbnail thumb-name curr-file dired-buf))))
             ((string-match "\\.\\(PDF\\)\\'" curr-file)
               (let* ((absolute-basename (file-name-sans-extension curr-file))
                      (png-filename (concat absolute-basename ".png"))
                      (pdf-first-page-filename (concat curr-file "[0]"))
                      (thumb-name (image-dired-thumb-name png-filename))
                      (thumbnail-dimensions
                        (when (file-exists-p thumb-name)
                          (image-dimensions thumb-name))))
                 (if (and
                        ;;; The goal is to move on to `image-dired-create-thumb' IF
                        ;;; the thumbnail exists and is the wrong size, or it does not exist.
                        (or (and (file-exists-p thumb-name)
                                 (not (or (= image-dired-thumb-width
                                             (car thumbnail-dimensions))
                                          (= image-dired-thumb-height
                                             (cdr thumbnail-dimensions))))
                                 (progn
                                   (clear-image-cache)
                                   'create-new-image))
                            (not (file-exists-p thumb-name)))
                        (not (= 0 (image-dired-create-thumb pdf-first-page-filename thumb-name))))
                     (message "Thumb could not be created for file %s" pdf-first-page-filename)
                   (image-dired-insert-thumbnail thumb-name pdf-first-page-filename dired-buf))))
             (t
               (message "%s does not match `image-file-name-regexp'" curr-file))))
         files))
      (cond ((eq 'dynamic image-dired-line-up-method)
             (image-dired-line-up-dynamic))
            ((eq 'fixed image-dired-line-up-method)
             (image-dired-line-up))
            ((eq 'interactive image-dired-line-up-method)
             (image-dired-line-up-interactive))
            ((eq 'none image-dired-line-up-method)
             nil)
            (t
             (image-dired-line-up-dynamic))))
    (if do-not-pop
        (display-buffer image-dired-thumbnail-buffer)
      (pop-to-buffer image-dired-thumbnail-buffer))))