2019-03-02 00:01:32 +00:00
|
|
|
;;; ol-docview.el --- Links to Docview mode buffers -*- lexical-binding: t; -*-
|
2009-11-29 16:21:04 +00:00
|
|
|
|
2024-01-02 01:47:10 +00:00
|
|
|
;; Copyright (C) 2009-2024 Free Software Foundation, Inc.
|
2009-11-29 16:21:04 +00:00
|
|
|
|
|
|
|
;; Author: Jan Böcker <jan.boecker at jboecker dot de>
|
2023-12-30 17:01:48 +00:00
|
|
|
;; Keywords: outlines, hypermedia, calendar, text
|
2021-09-26 07:44:29 +00:00
|
|
|
;; URL: https://orgmode.org
|
2009-11-29 16:21:04 +00:00
|
|
|
;;
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
;;
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 22:52:52 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2009-11-29 16:21:04 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; This file implements links to open files in doc-view-mode.
|
2016-08-23 20:13:56 +00:00
|
|
|
;; Org mode loads this module by default - if this is not what you want,
|
2009-11-29 16:21:04 +00:00
|
|
|
;; configure the variable `org-modules'.
|
|
|
|
|
|
|
|
;; The links take the form
|
|
|
|
;;
|
|
|
|
;; docview:<file path>::<page number>
|
|
|
|
;;
|
|
|
|
;; for example: [[docview:~/.elisp/org/doc/org.pdf::1][Org-Mode Manual]]
|
|
|
|
;;
|
|
|
|
;; Autocompletion for inserting links is supported; you will be
|
|
|
|
;; prompted for a file and a page number.
|
|
|
|
;;
|
|
|
|
;; If you use org-store-link in a doc-view mode buffer, the stored
|
|
|
|
;; link will point to the current page.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2022-08-04 13:53:05 +00:00
|
|
|
(require 'org-macs)
|
|
|
|
(org-assert-version)
|
2009-11-29 16:21:04 +00:00
|
|
|
|
2013-11-15 05:53:59 +00:00
|
|
|
(require 'doc-view)
|
2019-03-02 00:01:32 +00:00
|
|
|
(require 'ol)
|
2010-08-29 19:29:16 +00:00
|
|
|
|
2013-11-15 05:53:59 +00:00
|
|
|
(declare-function doc-view-goto-page "doc-view" (page))
|
|
|
|
(declare-function image-mode-window-get "image-mode" (prop &optional winprops))
|
2019-03-02 00:01:32 +00:00
|
|
|
(declare-function org-open-file "org" (path &optional in-emacs line search))
|
2009-12-11 07:49:44 +00:00
|
|
|
|
2016-07-05 14:26:28 +00:00
|
|
|
(org-link-set-parameters "docview"
|
|
|
|
:follow #'org-docview-open
|
|
|
|
:export #'org-docview-export
|
|
|
|
:store #'org-docview-store-link)
|
2009-11-29 16:21:04 +00:00
|
|
|
|
2023-10-09 13:33:28 +00:00
|
|
|
(defun org-docview-export (link description backend _info)
|
|
|
|
"Export a docview LINK with DESCRIPTION for BACKEND."
|
2019-02-27 19:22:51 +00:00
|
|
|
(let ((path (if (string-match "\\(.+\\)::.+" link) (match-string 1 link)
|
|
|
|
link))
|
|
|
|
(desc (or description link)))
|
2013-01-24 15:10:56 +00:00
|
|
|
(when (stringp path)
|
2019-02-27 19:22:51 +00:00
|
|
|
(setq path (expand-file-name path))
|
2013-01-24 15:10:56 +00:00
|
|
|
(cond
|
2023-10-09 13:33:28 +00:00
|
|
|
((eq backend 'html) (format "<a href=\"%s\">%s</a>" path desc))
|
|
|
|
((eq backend 'latex) (format "\\href{%s}{%s}" path desc))
|
2023-10-24 14:45:36 +00:00
|
|
|
((eq backend 'ascii) (format "[%s] (<%s>)" desc path))
|
2013-01-24 15:10:56 +00:00
|
|
|
(t path)))))
|
|
|
|
|
2020-02-17 10:28:50 +00:00
|
|
|
(defun org-docview-open (link _)
|
2023-10-09 13:33:18 +00:00
|
|
|
"Open docview: LINK."
|
2014-03-04 14:03:23 +00:00
|
|
|
(string-match "\\(.*?\\)\\(?:::\\([0-9]+\\)\\)?$" link)
|
|
|
|
(let ((path (match-string 1 link))
|
|
|
|
(page (and (match-beginning 2)
|
|
|
|
(string-to-number (match-string 2 link)))))
|
|
|
|
;; Let Org mode open the file (in-emacs = 1) to ensure
|
|
|
|
;; org-link-frame-setup is respected.
|
2022-05-04 15:13:58 +00:00
|
|
|
(if (file-exists-p path)
|
|
|
|
(org-open-file path 1)
|
|
|
|
(error "No such file: %s" path))
|
2014-03-04 14:03:23 +00:00
|
|
|
(when page (doc-view-goto-page page))))
|
2009-11-29 16:21:04 +00:00
|
|
|
|
2023-11-19 14:52:05 +00:00
|
|
|
(defun org-docview-store-link (&optional _interactive?)
|
2010-07-15 22:54:53 +00:00
|
|
|
"Store a link to a docview buffer."
|
2009-11-29 16:21:04 +00:00
|
|
|
(when (eq major-mode 'doc-view-mode)
|
|
|
|
;; This buffer is in doc-view-mode
|
|
|
|
(let* ((path buffer-file-name)
|
2010-08-29 19:29:16 +00:00
|
|
|
(page (image-mode-window-get 'page))
|
2015-12-20 21:15:41 +00:00
|
|
|
(link (concat "docview:" path "::" (number-to-string page))))
|
2018-11-26 23:04:41 +00:00
|
|
|
(org-link-store-props
|
2009-11-29 16:21:04 +00:00
|
|
|
:type "docview"
|
|
|
|
:link link
|
|
|
|
:description path))))
|
|
|
|
|
|
|
|
(defun org-docview-complete-link ()
|
2010-07-15 22:54:53 +00:00
|
|
|
"Use the existing file name completion for file.
|
|
|
|
Links to get the file name, then ask the user for the page number
|
|
|
|
and append it."
|
2018-11-26 23:04:41 +00:00
|
|
|
(concat (replace-regexp-in-string "^file:" "docview:" (org-link-complete-file))
|
2009-11-29 16:21:04 +00:00
|
|
|
"::"
|
|
|
|
(read-from-minibuffer "Page:" "1")))
|
|
|
|
|
2019-03-02 00:01:32 +00:00
|
|
|
(provide 'ol-docview)
|
2010-07-15 22:54:53 +00:00
|
|
|
|
2019-03-02 00:01:32 +00:00
|
|
|
;;; ol-docview.el ends here
|