org.texi (Literal examples): Fix typo
[org-mode/org-tableheadings.git] / lisp / org-docview.el
blobd2db685543166453580dfdb08c71408f8ca65937
1 ;;; org-docview.el --- support for links to doc-view-mode buffers
3 ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 ;; Author: Jan Böcker <jan.boecker at jboecker dot de>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
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.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file implements links to open files in doc-view-mode.
28 ;; Org-mode loads this module by default - if this is not what you want,
29 ;; configure the variable `org-modules'.
31 ;; The links take the form
33 ;; docview:<file path>::<page number>
35 ;; for example: [[docview:~/.elisp/org/doc/org.pdf::1][Org-Mode Manual]]
37 ;; Autocompletion for inserting links is supported; you will be
38 ;; prompted for a file and a page number.
40 ;; If you use org-store-link in a doc-view mode buffer, the stored
41 ;; link will point to the current page.
43 ;;; Code:
46 (require 'org)
47 (require 'doc-view)
49 (declare-function doc-view-goto-page "doc-view" (page))
50 (declare-function image-mode-window-get "image-mode" (prop &optional winprops))
52 (org-add-link-type "docview" 'org-docview-open 'org-docview-export)
53 (add-hook 'org-store-link-functions 'org-docview-store-link)
55 (defun org-docview-export (link description format)
56 "Export a docview link from Org files."
57 (let* ((path (when (string-match "\\(.+\\)::.+" link)
58 (match-string 1 link)))
59 (desc (or description link)))
60 (when (stringp path)
61 (setq path (org-link-escape (expand-file-name path)))
62 (cond
63 ((eq format 'html) (format "<a href=\"%s\">%s</a>" path desc))
64 ((eq format 'latex) (format "\href{%s}{%s}" path desc))
65 ((eq format 'ascii) (format "%s (%s)" desc path))
66 (t path)))))
68 (defun org-docview-open (link)
69 (when (string-match "\\(.*\\)::\\([0-9]+\\)$" link)
70 (let* ((path (match-string 1 link))
71 (page (string-to-number (match-string 2 link))))
72 (org-open-file path 1) ;; let org-mode open the file (in-emacs = 1)
73 ;; to ensure org-link-frame-setup is respected
74 (doc-view-goto-page page)
75 )))
77 (defun org-docview-store-link ()
78 "Store a link to a docview buffer."
79 (when (eq major-mode 'doc-view-mode)
80 ;; This buffer is in doc-view-mode
81 (let* ((path buffer-file-name)
82 (page (image-mode-window-get 'page))
83 (link (concat "docview:" path "::" (number-to-string page)))
84 (description ""))
85 (org-store-link-props
86 :type "docview"
87 :link link
88 :description path))))
90 (defun org-docview-complete-link ()
91 "Use the existing file name completion for file.
92 Links to get the file name, then ask the user for the page number
93 and append it."
94 (concat (replace-regexp-in-string "^file:" "docview:" (org-file-complete-link))
95 "::"
96 (read-from-minibuffer "Page:" "1")))
99 (provide 'org-docview)
101 ;;; org-docview.el ends here