ol-docview.el (3808B)
1 ;;; ol-docview.el --- Links to Docview mode buffers -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2009-2024 Free Software Foundation, Inc. 4 5 ;; Author: Jan Böcker <jan.boecker at jboecker dot de> 6 ;; Keywords: outlines, hypermedia, calendar, text 7 ;; URL: https://orgmode.org 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 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. 20 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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 24 ;; 25 ;;; Commentary: 26 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'. 30 31 ;; The links take the form 32 ;; 33 ;; docview:<file path>::<page number> 34 ;; 35 ;; for example: [[docview:~/.elisp/org/doc/org.pdf::1][Org-Mode Manual]] 36 ;; 37 ;; Autocompletion for inserting links is supported; you will be 38 ;; prompted for a file and a page number. 39 ;; 40 ;; If you use org-store-link in a doc-view mode buffer, the stored 41 ;; link will point to the current page. 42 43 ;;; Code: 44 45 (require 'org-macs) 46 (org-assert-version) 47 48 (require 'doc-view) 49 (require 'ol) 50 51 (declare-function doc-view-goto-page "doc-view" (page)) 52 (declare-function image-mode-window-get "image-mode" (prop &optional winprops)) 53 (declare-function org-open-file "org" (path &optional in-emacs line search)) 54 55 (org-link-set-parameters "docview" 56 :follow #'org-docview-open 57 :export #'org-docview-export 58 :store #'org-docview-store-link) 59 60 (defun org-docview-export (link description backend _info) 61 "Export a docview LINK with DESCRIPTION for BACKEND." 62 (let ((path (if (string-match "\\(.+\\)::.+" link) (match-string 1 link) 63 link)) 64 (desc (or description link))) 65 (when (stringp path) 66 (setq path (expand-file-name path)) 67 (cond 68 ((eq backend 'html) (format "<a href=\"%s\">%s</a>" path desc)) 69 ((eq backend 'latex) (format "\\href{%s}{%s}" path desc)) 70 ((eq backend 'ascii) (format "[%s] (<%s>)" desc path)) 71 (t path))))) 72 73 (defun org-docview-open (link _) 74 "Open docview: LINK." 75 (string-match "\\(.*?\\)\\(?:::\\([0-9]+\\)\\)?$" link) 76 (let ((path (match-string 1 link)) 77 (page (and (match-beginning 2) 78 (string-to-number (match-string 2 link))))) 79 ;; Let Org mode open the file (in-emacs = 1) to ensure 80 ;; org-link-frame-setup is respected. 81 (if (file-exists-p path) 82 (org-open-file path 1) 83 (error "No such file: %s" path)) 84 (when page (doc-view-goto-page page)))) 85 86 (defun org-docview-store-link (&optional _interactive?) 87 "Store a link to a docview buffer." 88 (when (eq major-mode 'doc-view-mode) 89 ;; This buffer is in doc-view-mode 90 (let* ((path buffer-file-name) 91 (page (image-mode-window-get 'page)) 92 (link (concat "docview:" path "::" (number-to-string page)))) 93 (org-link-store-props 94 :type "docview" 95 :link link 96 :description path)))) 97 98 (defun org-docview-complete-link () 99 "Use the existing file name completion for file. 100 Links to get the file name, then ask the user for the page number 101 and append it." 102 (concat (replace-regexp-in-string "^file:" "docview:" (org-link-complete-file)) 103 "::" 104 (read-from-minibuffer "Page:" "1"))) 105 106 (provide 'ol-docview) 107 108 ;;; ol-docview.el ends here