From aaf39c38784198353b61bf6086f48a689b199f0d Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Fri, 16 Sep 2022 21:54:29 +0200 Subject: [PATCH] image-dired: Add support for PDF files (Bug#26432) * 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 | 5 +++++ lisp/image/image-dired-dired.el | 2 +- lisp/image/image-dired.el | 40 ++++++++++++++++++++------------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index df09533c0b9..97ec53ca7c1 100644 --- 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 diff --git a/lisp/image/image-dired-dired.el b/lisp/image/image-dired-dired.el index c8d19028748..e3aa3da421d 100644 --- a/lisp/image/image-dired-dired.el +++ b/lisp/image/image-dired-dired.el @@ -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))) diff --git a/lisp/image/image-dired.el b/lisp/image/image-dired.el index 442767dda10..449945b88d8 100644 --- a/lisp/image/image-dired.el +++ b/lisp/image/image-dired.el @@ -356,6 +356,11 @@ This affects the following commands: ;;; 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. -- 2.39.2