org-docview.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ;;; org-docview.el --- Support for links to doc-view-mode buffers -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2018 Free Software Foundation, Inc.
  3. ;; Author: Jan Böcker <jan.boecker at jboecker dot de>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs 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. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file implements links to open files in doc-view-mode.
  23. ;; Org mode loads this module by default - if this is not what you want,
  24. ;; configure the variable `org-modules'.
  25. ;; The links take the form
  26. ;;
  27. ;; docview:<file path>::<page number>
  28. ;;
  29. ;; for example: [[docview:~/.elisp/org/doc/org.pdf::1][Org-Mode Manual]]
  30. ;;
  31. ;; Autocompletion for inserting links is supported; you will be
  32. ;; prompted for a file and a page number.
  33. ;;
  34. ;; If you use org-store-link in a doc-view mode buffer, the stored
  35. ;; link will point to the current page.
  36. ;;; Code:
  37. (require 'org)
  38. (require 'doc-view)
  39. (declare-function doc-view-goto-page "doc-view" (page))
  40. (declare-function image-mode-window-get "image-mode" (prop &optional winprops))
  41. (org-link-set-parameters "docview"
  42. :follow #'org-docview-open
  43. :export #'org-docview-export
  44. :store #'org-docview-store-link)
  45. (defun org-docview-export (link description format)
  46. "Export a docview link from Org files."
  47. (let* ((path (if (string-match "\\(.+\\)::.+" link) (match-string 1 link)
  48. link))
  49. (desc (or description link)))
  50. (when (stringp path)
  51. (setq path (org-link-escape (expand-file-name path)))
  52. (cond
  53. ((eq format 'html) (format "<a href=\"%s\">%s</a>" path desc))
  54. ((eq format 'latex) (format "\\href{%s}{%s}" path desc))
  55. ((eq format 'ascii) (format "%s (%s)" desc path))
  56. (t path)))))
  57. (defun org-docview-open (link)
  58. (string-match "\\(.*?\\)\\(?:::\\([0-9]+\\)\\)?$" link)
  59. (let ((path (match-string 1 link))
  60. (page (and (match-beginning 2)
  61. (string-to-number (match-string 2 link)))))
  62. ;; Let Org mode open the file (in-emacs = 1) to ensure
  63. ;; org-link-frame-setup is respected.
  64. (org-open-file path 1)
  65. (when page (doc-view-goto-page page))))
  66. (defun org-docview-store-link ()
  67. "Store a link to a docview buffer."
  68. (when (eq major-mode 'doc-view-mode)
  69. ;; This buffer is in doc-view-mode
  70. (let* ((path buffer-file-name)
  71. (page (image-mode-window-get 'page))
  72. (link (concat "docview:" path "::" (number-to-string page))))
  73. (org-store-link-props
  74. :type "docview"
  75. :link link
  76. :description path))))
  77. (defun org-docview-complete-link ()
  78. "Use the existing file name completion for file.
  79. Links to get the file name, then ask the user for the page number
  80. and append it."
  81. (concat (replace-regexp-in-string "^file:" "docview:" (org-file-complete-link))
  82. "::"
  83. (read-from-minibuffer "Page:" "1")))
  84. (provide 'org-docview)
  85. ;;; org-docview.el ends here