org-docview.el 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ;;; org-docview.el --- support for links to doc-view-mode buffers
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Jan Böcker <jan.boecker at jboecker dot de>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 6.34trans
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file implements links to open files in doc-view-mode.
  25. ;; Org-mode loads this module by default - if this is not what you want,
  26. ;; configure the variable `org-modules'.
  27. ;; The links take the form
  28. ;;
  29. ;; docview:<file path>::<page number>
  30. ;;
  31. ;; for example: [[docview:~/.elisp/org/doc/org.pdf::1][Org-Mode Manual]]
  32. ;;
  33. ;; Autocompletion for inserting links is supported; you will be
  34. ;; prompted for a file and a page number.
  35. ;;
  36. ;; If you use org-store-link in a doc-view mode buffer, the stored
  37. ;; link will point to the current page.
  38. ;;; Code:
  39. (require 'org)
  40. (declare-function doc-view-goto-page "doc-view" (page))
  41. (declare-function doc-view-current-page "doc-view" (&optional win))
  42. (org-add-link-type "docview" 'org-docview-open)
  43. (add-hook 'org-store-link-functions 'org-docview-store-link)
  44. (defun org-docview-open (link)
  45. (when (string-match "\\(.*\\)::\\([0-9]+\\)$" link)
  46. (let* ((path (match-string 1 link))
  47. (page (string-to-number (match-string 2 link))))
  48. (org-open-file path 1) ;; let org-mode open the file (in-emacs = 1)
  49. ;; to ensure org-link-frame-setup is respected
  50. (doc-view-goto-page page)
  51. )))
  52. (defun org-docview-store-link ()
  53. "Store a link to a docview buffer"
  54. (when (eq major-mode 'doc-view-mode)
  55. ;; This buffer is in doc-view-mode
  56. (let* ((path buffer-file-name)
  57. (page (doc-view-current-page))
  58. (link (concat "docview:" path "::" (number-to-string page)))
  59. (description ""))
  60. (org-store-link-props
  61. :type "docview"
  62. :link link
  63. :description path))))
  64. (defun org-docview-complete-link ()
  65. "Use the existing file name completion for file: links to get the file name,
  66. then ask the user for the page number and append it."
  67. (concat (replace-regexp-in-string "^file:" "docview:" (org-file-complete-link))
  68. "::"
  69. (read-from-minibuffer "Page:" "1")))
  70. (provide 'org-docview)