org-pdfview.el 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ;;; org-pdfview.el --- Support for links to documents in pdfview mode
  2. ;; Copyright (C) 2014 Markus Hauck
  3. ;; Author: Markus Hauck <markus1189@gmail.com>
  4. ;; Maintainer: Markus Hauck <markus1189@gmail.com>
  5. ;; Keywords: org, pdf-view, pdf-tools
  6. ;; Version: 0.1
  7. ;; Package-Requires: ((org "8.2.10") (pdf-tools "0.80"))
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; Add support for org links from pdfview buffers like docview.
  22. ;;
  23. ;; To enable this automatically, use:
  24. ;; (eval-after-load 'org '(require 'org-pdfview))
  25. ;; If you want, you can also configure the org-mode default open PDF file function.
  26. ;; (add-to-list 'org-file-apps '("\\.pdf\\'" . (lambda (file link) (org-pdfview-open link))))
  27. ;;; Code:
  28. (require 'org)
  29. (require 'pdf-tools)
  30. (require 'pdf-view)
  31. (if (fboundp 'org-link-set-parameters)
  32. (org-link-set-parameters "pdfview"
  33. :follow #'org-pdfview-open
  34. :complete #'org-pdfview-complete-link
  35. :store #'org-pdfview-store-link)
  36. (org-add-link-type "pdfview" 'org-pdfview-open)
  37. (add-hook 'org-store-link-functions 'org-pdfview-store-link))
  38. ;;;###autoload
  39. (defun org-pdfview-open (link)
  40. "Open LINK in pdf-view-mode."
  41. (cond ((string-match "\\(.*\\)::\\([0-9]*\\)\\+\\+\\([[0-9]\\.*[0-9]*\\)" link)
  42. (let* ((path (match-string 1 link))
  43. (page (string-to-number (match-string 2 link)))
  44. (height (string-to-number (match-string 3 link))))
  45. (org-open-file path 1)
  46. (pdf-view-goto-page page)
  47. (image-set-window-vscroll
  48. (round (* height (cdr (pdf-view-image-size)))))))
  49. ((string-match "\\(.*\\)::\\([0-9]+\\)$" link)
  50. (let* ((path (match-string 1 link))
  51. (page (string-to-number (match-string 2 link))))
  52. (org-open-file path 1)
  53. (pdf-view-goto-page page)))
  54. (t
  55. (org-open-file link 1))))
  56. ;; Redefine the links (for position)
  57. (defun org-pdfview-ask-pos ()
  58. "Ask for position in a pdfview buffer"
  59. (let*
  60. ((pdfpos
  61. (format
  62. "%0.2f"
  63. ;; Ratio of (1) current position (in pixels)
  64. ;; and (2) Size of PDF image (without
  65. ;; slicing)
  66. (/ (float (window-vscroll nil t))
  67. (cdr (pdf-view-image-size t)))))
  68. (pos
  69. (read-string
  70. "Position (between 0 and 1 or empty): " pdfpos)))
  71. (if
  72. ;; Check if position was empty
  73. (or (string= pos "")
  74. (string= pos "nil"))
  75. ;; Position was not provided (empty result)
  76. ""
  77. ;; Position was provided
  78. (concat "++" pos)
  79. )))
  80. ;;;###autoload
  81. (defun org-pdfview-store-link ()
  82. "Store a link to a pdfview buffer."
  83. (when (eq major-mode 'pdf-view-mode)
  84. ;; This buffer is in pdf-view-mode
  85. (let* ((path buffer-file-name)
  86. (page (pdf-view-current-page))
  87. (link (concat
  88. "pdfview:" path "::"
  89. (number-to-string page)
  90. (org-pdfview-ask-pos))))
  91. (org-link-store-props
  92. :type "pdfview"
  93. :link link
  94. :description path))))
  95. ;;;###autoload
  96. (defun org-pdfview-export (link description format)
  97. "Export the pdfview LINK with DESCRIPTION for FORMAT from Org files."
  98. (let* ((path (when (string-match "\\(.+\\)::.+" link)
  99. (match-string 1 link)))
  100. (desc (or description link)))
  101. (when (stringp path)
  102. (setq path (org-link-escape (expand-file-name path)))
  103. (cond
  104. ((eq format 'html) (format "<a href=\"%s\">%s</a>" path desc))
  105. ((eq format 'latex) (format "\\href{%s}{%s}" path desc))
  106. ((eq format 'ascii) (format "%s (%s)" desc path))
  107. (t path)))))
  108. (defun org-pdfview-complete-link (&optional arg)
  109. "Use the existing file name completion for file.
  110. Links to get the file name, then ask the user for the page number
  111. and append it."
  112. (concat (replace-regexp-in-string "^file:" "pdfview:" (org-file-complete-link arg))
  113. "::"
  114. (read-from-minibuffer "Page:" "1")))
  115. (provide 'org-pdfview)
  116. ;;; org-pdfview.el ends here