]> git.eshelyaron.com Git - emacs.git/commitdiff
image-dired: Add support for PDF files (Bug#26432)
authorStefan Kangas <stefankangas@gmail.com>
Fri, 16 Sep 2022 19:54:29 +0000 (21:54 +0200)
committerStefan Kangas <stefankangas@gmail.com>
Fri, 16 Sep 2022 20:28:46 +0000 (22:28 +0200)
* lisp/image/image-dired.el
(image-dired--file-name-regexp): New function.
(image-dired-display-thumbnail-original-image): Clean up.
(image-dired-display-image): Support visiting PDF files.
* lisp/image/image-dired-dired.el
(image-dired-dired-toggle-marked-thumbs):
* lisp/image/image-dired.el (image-dired-get-thumbnail-image)
(image-dired-show-all-from-dir): Use above new function instead of
'image-file-name-regexp'.

etc/NEWS
lisp/image/image-dired-dired.el
lisp/image/image-dired.el

index df09533c0b97b28f047a3557f37821e8fd7196b7..97ec53ca7c1b83a669be28d20423e3582bac459f 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2052,6 +2052,11 @@ associated with Image-Dired.
 This replaces the message most navigation commands in the thumbnail
 buffer used to show at the bottom of the screen.
 
+---
+*** PDF support.
+Image-Dired now displays thumbnails for PDF files.  Type 'RET' on a
+PDF file in the thumbnail buffer to visit the corresponding PDF.
+
 ---
 *** Support GraphicsMagick command line tools.
 Support for the GraphicsMagick command line tool ("gm") has been
index c8d19028748a77b5e664177db9d0e4ac254744d1..e3aa3da421dcf9d7b1d4d7d49c595e4341848421 100644 (file)
@@ -69,7 +69,7 @@ previous -ARG, if ARG<0) files."
          thumb-file
          overlay)
      (when (and image-file
-                (string-match-p (image-file-name-regexp) image-file))
+                (string-match-p (image-dired--file-name-regexp) image-file))
        (setq thumb-file (image-dired-get-thumbnail-image image-file))
        ;; If image is not already added, then add it.
        (let ((thumb-ov (cl-loop for ov in (overlays-in (point) (1+ (point)))
index 442767dda1099a27dac23baf9b43efdfbf250476..449945b88d8d86ec2003c3c23aacdf84ee6f8542 100644 (file)
@@ -356,6 +356,11 @@ This affects the following commands:
 \f
 ;;; Util functions
 
+(defun image-dired--file-name-regexp ()
+  (let ((image-file-name-extensions
+         (append '("pdf") image-file-name-extensions)))
+    (image-file-name-regexp)))
+
 (defun image-dired-insert-image (file type relief margin)
   "Insert image FILE of image TYPE, using RELIEF and MARGIN, at point."
   (let ((i `(image :type ,type
@@ -366,7 +371,7 @@ This affects the following commands:
 
 (defun image-dired-get-thumbnail-image (file)
   "Return the image descriptor for a thumbnail of image file FILE."
-  (unless (string-match-p (image-file-name-regexp) file)
+  (unless (string-match-p (image-dired--file-name-regexp) file)
     (error "%s is not a valid image file" file))
   (let* ((thumb-file (image-dired-thumb-name file))
          (thumb-attr (file-attributes thumb-file)))
@@ -538,8 +543,8 @@ thumbnail buffer to be selected."
 ;;;###autoload
 (defun image-dired-show-all-from-dir (dir)
   "Make a thumbnail buffer for all images in DIR and display it.
-Any file matching `image-file-name-regexp' is considered an image
-file.
+Any file matching `image-dired--file-name-regexp' is considered an
+image file.
 
 If the number of image files in DIR exceeds
 `image-dired-show-all-from-dir-max-files', ask for confirmation
@@ -547,7 +552,7 @@ before creating the thumbnail buffer.  If that variable is nil,
 never ask for confirmation."
   (interactive "DImage-Dired: ")
   (dired dir)
-  (dired-mark-files-regexp (image-file-name-regexp))
+  (dired-mark-files-regexp (image-dired--file-name-regexp))
   (let ((files (dired-get-marked-files nil nil nil t)))
     (cond ((and (null (cdr files)))
            (message "No image files in directory"))
@@ -1091,10 +1096,9 @@ Ask user how many thumbnails should be displayed per row."
                        image-dired-external-viewer file)))))
 
 (defun image-dired-display-image (file &optional _ignored)
-  "Display image FILE in image buffer.
-Use this when you want to display the image, in a new window.
-The window will use `image-dired-display-image-mode' which is
-based on `image-mode'."
+  "Display image FILE in a the image buffer window.
+If it is an image, the window will use `image-dired-display-image-mode'
+which is based on `image-mode'."
   (declare (advertised-calling-convention (file) "29.1"))
   (setq file (expand-file-name file))
   (when (not (file-exists-p file))
@@ -1106,7 +1110,10 @@ based on `image-mode'."
     (when-let ((buf (find-file-noselect file nil t)))
       (pop-to-buffer buf)
       (rename-buffer image-dired-display-image-buffer)
-      (image-dired-display-image-mode)
+      (if (string-match (image-file-name-regexp) file)
+          (image-dired-display-image-mode)
+        ;; Support visiting PDF files.
+        (normal-mode))
       (select-window cur-win))))
 
 (defun image-dired-display-thumbnail-original-image (&optional arg)
@@ -1114,14 +1121,15 @@ based on `image-mode'."
 See documentation for `image-dired-display-image' for more information.
 With prefix argument ARG, display image in its original size."
   (interactive "P" image-dired-thumbnail-mode)
+  (unless (string-equal major-mode "image-dired-thumbnail-mode")
+    (user-error "Not in `image-dired-thumbnail-mode'"))
   (let ((file (image-dired-original-file-name)))
-    (if (not (string-equal major-mode "image-dired-thumbnail-mode"))
-        (message "Not in image-dired-thumbnail-mode")
-      (if (not (image-dired-image-at-point-p))
-          (message "No thumbnail at point")
-        (if (not file)
-            (message "No original file name found")
-          (image-dired-display-image file arg))))))
+    (cond ((not (image-dired-image-at-point-p))
+           (message "No thumbnail at point"))
+          ((not file)
+           (message "No original file name found"))
+          (t
+           (image-dired-display-image file arg)))))
 
 (defun image-dired-rotate-original-left ()
   "Rotate original image left (counter clockwise) 90 degrees.