ol-docview.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ;;; ol-docview.el --- Links to Docview mode buffers -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. ;; Author: Jan Böcker <jan.boecker at jboecker dot de>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; URL: https://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-macs)
  38. (org-assert-version)
  39. (require 'doc-view)
  40. (require 'ol)
  41. (declare-function doc-view-goto-page "doc-view" (page))
  42. (declare-function image-mode-window-get "image-mode" (prop &optional winprops))
  43. (declare-function org-open-file "org" (path &optional in-emacs line search))
  44. (org-link-set-parameters "docview"
  45. :follow #'org-docview-open
  46. :export #'org-docview-export
  47. :store #'org-docview-store-link)
  48. (defun org-docview-export (link description format)
  49. "Export a docview link from Org files."
  50. (let ((path (if (string-match "\\(.+\\)::.+" link) (match-string 1 link)
  51. link))
  52. (desc (or description link)))
  53. (when (stringp path)
  54. (setq path (expand-file-name path))
  55. (cond
  56. ((eq format 'html) (format "<a href=\"%s\">%s</a>" path desc))
  57. ((eq format 'latex) (format "\\href{%s}{%s}" path desc))
  58. ((eq format 'ascii) (format "%s (%s)" desc path))
  59. (t path)))))
  60. (defun org-docview-open (link _)
  61. (string-match "\\(.*?\\)\\(?:::\\([0-9]+\\)\\)?$" link)
  62. (let ((path (match-string 1 link))
  63. (page (and (match-beginning 2)
  64. (string-to-number (match-string 2 link)))))
  65. ;; Let Org mode open the file (in-emacs = 1) to ensure
  66. ;; org-link-frame-setup is respected.
  67. (if (file-exists-p path)
  68. (org-open-file path 1)
  69. (error "No such file: %s" path))
  70. (when page (doc-view-goto-page page))))
  71. (defun org-docview-store-link ()
  72. "Store a link to a docview buffer."
  73. (when (eq major-mode 'doc-view-mode)
  74. ;; This buffer is in doc-view-mode
  75. (let* ((path buffer-file-name)
  76. (page (image-mode-window-get 'page))
  77. (link (concat "docview:" path "::" (number-to-string page))))
  78. (org-link-store-props
  79. :type "docview"
  80. :link link
  81. :description path))))
  82. (defun org-docview-complete-link ()
  83. "Use the existing file name completion for file.
  84. Links to get the file name, then ask the user for the page number
  85. and append it."
  86. (concat (replace-regexp-in-string "^file:" "docview:" (org-link-complete-file))
  87. "::"
  88. (read-from-minibuffer "Page:" "1")))
  89. (provide 'ol-docview)
  90. ;;; ol-docview.el ends here