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