Org link and pdf-tools

  |   Source

By default, Org pdf link uses doc-view.el to open pdf. So if you move focus over the link docview:papers/last.pdf::NNN in a org file and run M-x org-open-at-point, API doc-view-goto-page is called.

These days pdf-tools is very popular. If pdf-tools is installed and enabled, API call of doc-view-goto-page will fail.

Below code fixes this problem. It will automatically call correct API with or without pdf-tools.

(defun my-org-docview-open-hack (orig-func &rest args)
  (let* ((link (car args)) path page)
    (string-match "\\(.*?\\)\\(?:::\\([0-9]+\\)\\)?$" link)
    (setq path (match-string 1 link))
    (setq page (and (match-beginning 2)
                    (string-to-number (match-string 2 link))))
    (org-open-file path 1)
    (when page
      (cond
       ((eq major-mode 'pdf-view-mode)
        (pdf-view-goto-page page))
       (t
        (doc-view-goto-page page))))))
(advice-add 'org-docview-open :around #'my-org-docview-open-hack)
Comments powered by Disqus