ol-docview.el 3.5 KB

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