Implement an org-link type for pdf-tools

This commit is contained in:
Gerard Vermeulen 2022-02-03 19:10:05 +01:00
parent 23be15a8ee
commit 2a60ff0e98
1 changed files with 58 additions and 8 deletions

View File

@ -1217,12 +1217,17 @@ and =org=.
:CUSTOM_ID: sec:making-org-hyperlink-types
:END:
The code in listing [[lst:define-org-link-types]] defines =org-link= types for
backwards compatibility with [[https://github.com/jkitchin/org-ref][org-ref]].
Listing [[lst:org-ref-like-org-link-types]] defines =org-link= types for backwards
compatibility with [[https://github.com/jkitchin/org-ref][org-ref]]. Listing [[lst:define-org-pdfview-link-type]] uses ideas
from the definition of =docview-org-link= and =doi-org-link= types to define an
=pdfview-org-link= type for use with [[https://github.com/vedang/pdf-tools][pdf-tools]]. Finally, listing
[[lst:emacs-lisp-setup-patch-ol-info]] patches the function =org-info-export= and
the constant ~org-info-other-documents~, to export the =info-org-link= types in
this document to =html= and LaTeX correctly.
#+caption[Define =org-link= types]:
#+caption: Define =org-link= types.
#+name: lst:define-org-link-types
#+caption[Define =org-link= types for backwards compatibility with =org-ref=]:
#+caption: Define =org-link= types for backwards compatibility with =org-ref=.
#+name: lst:org-ref-like-org-link-types
#+begin_src emacs-lisp
(with-eval-after-load 'ol
(org-link-set-parameters
@ -1257,9 +1262,54 @@ backwards compatibility with [[https://github.com/jkitchin/org-ref][org-ref]].
(_ path)))))
#+end_src
Listing [[lst:emacs-lisp-setup-patch-ol-info]] patches the function =org-info-export=
and the constant ~org-info-other-documents~, to export the [[info:org#External Links][info: links (info)]] in
this document to =html= and LaTeX correctly.
#+caption[Define an =org-link= type for =pdftools=]:
#+caption: Define an =org-link= type for =pdftools=.
#+name: lst:define-org-pdfview-link-type
#+begin_src emacs-lisp
(with-eval-after-load 'ol
(autoload 'pdf-view-goto-page "pdf-view" nil t)
(org-link-set-parameters "pdfview"
:follow #'org-pdfview-open
:export #'org-pdfview-export
:store #'org-pdfview-store-link)
(defun org-pdfview-export (link description backend _)
"Export a \"pdfview\" type link."
(let ((path (if (string-match "\\(.+\\)::.+" link)
(match-string 1 link)
link))
(desc (or description link)))
(when (stringp path)
(setq path (expand-file-name path))
(pcase backend
(`html (format "<a href=\"%s\">%s</a>" path desc))
(`latex (format "\\href{%s}{%s}" path desc))
(`ascii (format "%s (%s)" desc path))
(_ path)))))
(defun org-pdfview-open (link _)
"Open a \"pdfview\" type link."
(string-match "\\(.*?\\)\\(?:::\\([0-9]+\\)\\)?$" link)
(let ((path (match-string 1 link))
(page (and (match-beginning 2)
(string-to-number (match-string 2 link)))))
;; Let Org mode open the file (in-emacs = 1) to ensure
;; org-link-frame-setup is respected.
(org-open-file path 1)
(when page (pdf-view-goto-page page))))
(defun org-pdfview-store-link ()
"Store a \"pdfview\" type link."
(when (eq major-mode 'pdf-view-mode)
;; This buffer is in pdf-view-mode
(let* ((path buffer-file-name)
(page (pdf-view-current-page))
(link (concat "pdftools:" path "::" (number-to-string page))))
(org-link-store-props
:type "pdftools"
:link link
:description path)))))
#+end_src
#+caption[Patch =ol-info=]:
#+caption: Patch =ol-info=.