1

I want the image tooltip code also make work for pdf format, thus I added the pdf keyword to the file extension list of the original code (http://kitchingroup.cheme.cmu.edu/blog/2016/03/21/Displaying-image-overlays-on-image-filenames-in-Emacs/#sec-1)). However it doesn't work with pdf. How can I make this work?

(defvar image-tooltip-re (concat  "\\(?3:'\\|\"\\)\\(?1:.*\\."
                                  (regexp-opt '("png" "PNG" "JPG" "jpeg"
                                                "jpg" "JPEG" "eps" "EPS" "pdf"))
                                  "\\)\\(?:\\3\\)")
  "Regexp to match image filenames in quotes")

(defun image-tooltip (window object position)
  (save-excursion
    (goto-char position)
    (let (beg end imgfile img s)
      (while (not (looking-at image-tooltip-re))
        (forward-char -1))
      (setq imgfile (match-string-no-properties 1))
      (when (file-exists-p imgfile)
        (setq img (create-image (expand-file-name imgfile)
                                'imagemagick nil :width 200))
        (propertize "Look in the minibuffer"
                    'display img)))))

(font-lock-add-keywords
 nil
 `((,image-tooltip-re
    0 '(face font-lock-keyword-face
             help-echo image-tooltip))))

(font-lock-fontify-buffer)
(setq x-gtk-use-system-tooltips nil)

"tikz-standalone.pdf"

I think the imagemagick backend should support it, for example I can do display file.pdf from command line.

  • If you have pdf-tools installed, you can use the function pdf-info-renderpage, but take of the file-or-data-p argument to create-image. – politza Sep 2 '16 at 13:24
  • @poliza: I don't see, how to add this to the code above. Perhaps you can add to the code as you think it in an answer? – student Sep 2 '16 at 20:25
  • So your not a student of programming ? – politza Sep 2 '16 at 22:30
  • No :-). I am just an emacs user. – student Sep 3 '16 at 8:01
  • (memq 'PDF (imagemagick-types)) returns t here. So it is supported, even though it takes an unusual amount of time until the tooltip is displayed (~5s). – politza Sep 3 '16 at 8:55
1

Imagemagick seems to be able to render PDF files, though apparently it is very slow.

Also, hovering the mouse over a filename calls the original image-tooltip about a dozen times, making things even worse. A solution could be to immediately put the image as a help-echo property, see below.

(defvar image-tooltip-regexp (concat  "\\(?3:'\\|\"\\)\\(?1:.*\\.\\(?2:"
                                      (regexp-opt '("png" "jpeg" "jpg" "eps" "pdf"))
                                      "\\)\\)\\(?:\\3\\)"))

(defvar image-tooltip-width 200)

(defun image-tooltip-fontify (&optional limit)
  (while (re-search-forward image-tooltip-regexp nil t)
    (let* ((imgfile (expand-file-name (match-string-no-properties 1)))
           (imgtype (intern (downcase (match-string-no-properties 2))))
           (data-p (and (eq imgtype 'pdf)
                        (require 'pdf-tools nil t)))
           (data-or-file (if data-p
                             (pdf-info-renderpage 1 image-tooltip-width imgfile)
                           imgfile)))
      (when (file-exists-p imgfile)
        (with-silent-modifications
          (put-text-property
           (match-beginning 0)
           (match-end 0)
           'help-echo
           (propertize " " 'display
                       (create-image
                        data-or-file
                        'imagemagick
                        data-p
                        :width image-tooltip-width))))))))

(font-lock-add-keywords nil
  `((image-tooltip-fontify)))

(font-lock-fontify-buffer)
(setq x-gtk-use-system-tooltips nil)
1

pdf is probably not a supported image type for create-image. You probably have to run a command to convert the pdf to a temporary png file (eg. convert some.pdf temp.png), and use the temporary png file in the create-image function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.