From 313a6938101c868c5633fa82e6acea87486277dd Mon Sep 17 00:00:00 2001 From: jao Date: Wed, 8 Jul 2020 16:51:29 +0100 Subject: org-pdfview and related cleanups --- org/jao-org-links.el | 35 +++++++++--------- org/jao-org-pdfview.el | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 org/jao-org-pdfview.el (limited to 'org') diff --git a/org/jao-org-links.el b/org/jao-org-links.el index 4f8d606..fcb0825 100644 --- a/org/jao-org-links.el +++ b/org/jao-org-links.el @@ -1,9 +1,25 @@ (require 'jao-org-utils) -(require 'jao-devon) +(require 'jao-org-pdfview) -(autoload 'jao-as-safari-doc "jao-applescript.el") +(jao-when-darwin + (require 'jao-devon) + + (autoload 'jao-as-safari-doc "jao-applescript.el") + + (defun jao-org-insert-devon-link () + (interactive) + (insert (jao-devon-selection))) + + (defun jao-org-insert-safari-link () + (interactive) + (let ((ln (jao-as-safari-doc))) + (when ln (jao-org-insert-link (car ln) (cdr ln))))) + + (org-add-link-type "x-devonthink-item" 'jao-devon-open 'identity) + + (define-key org-mode-map "\C-cd" 'jao-org-insert-devon-link) + (define-key org-mode-map "\C-cs" 'jao-org-insert-safari-link)) -;; doc links (defvar jao-org--sink-dir "./") (defun jao-org-follow-doc (link) @@ -19,13 +35,6 @@ (shell-command (format "mv %s %s" real-file dest-path)))) (browse-url (format "file://%s" (expand-file-name dest-path))))) -;; devon links -(org-add-link-type "x-devonthink-item" 'jao-devon-open 'identity) - -(defun jao-org-insert-devon-link () - (interactive) - (insert (jao-devon-selection))) - (defsubst jao-org--title->file (title) (concat (mapconcat 'downcase (split-string title nil t) "-") ".pdf")) @@ -37,10 +46,4 @@ (org-add-link-type "doc" 'jao-org-follow-doc 'identity) (setq jao-org--sink-dir (file-name-as-directory sink-dir))) -(defun jao-org-insert-safari-link () - (interactive) - (let ((ln (jao-as-safari-doc))) - (when ln (jao-org-insert-link (car ln) (cdr ln))))) - - (provide 'jao-org-links) diff --git a/org/jao-org-pdfview.el b/org/jao-org-pdfview.el new file mode 100644 index 0000000..6c8fb60 --- /dev/null +++ b/org/jao-org-pdfview.el @@ -0,0 +1,97 @@ +;;; org-pdfview.el --- Support for links to documents in pdfview mode + +;; Copyright (C) 2014 Markus Hauck + +;; Author: Markus Hauck +;; Maintainer: Markus Hauck +;; Keywords: org, pdf-view, pdf-tools +;; Version: 0.1 +;; Package-Requires: ((org "8.2.10") (pdf-tools "0.80")) + +;; This program 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. +;; +;; This program 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 +;; along with this program. If not, see . + +;;; Commentary: +;; Add support for org links from pdfview buffers like docview. +;; +;; To enable this automatically, use: +;; (eval-after-load 'org '(require 'org-pdfview)) + +;; If you want, you can also configure the org-mode default open PDF file function. +;; (add-to-list 'org-file-apps '("\\.pdf\\'" . (lambda (file link) (org-pdfview-open link)))) + +;;; Code: +(require 'org) +(require 'pdf-tools) +(require 'pdf-view) + +(defun org-pdfview-open (link) + "Open LINK in pdf-view-mode." + (cond ((string-match "\\(.*\\)::\\([0-9]*\\)\\+\\+\\([[0-9]\\.*[0-9]*\\)" link) + (let* ((path (match-string 1 link)) + (page (string-to-number (match-string 2 link))) + (height (string-to-number (match-string 3 link)))) + (org-open-file path 1) + (pdf-view-goto-page page) + (image-set-window-vscroll + (round (/ (* height (cdr (pdf-view-image-size))) (frame-char-height)))))) + ((string-match "\\(.*\\)::\\([0-9]+\\)$" link) + (let* ((path (match-string 1 link)) + (page (string-to-number (match-string 2 link)))) + (org-open-file path 1) + (pdf-view-goto-page page))) + (t + (org-open-file link 1)))) + +(defun org-pdfview-store-link () + "Store a link to a pdfview buffer." + (when (eq major-mode 'pdf-view-mode) + ;; This buffer is in pdf-view-mode + (let* ((path buffer-file-name) + (page (pdf-view-current-page)) + (link (concat "pdfview:" path "::" (number-to-string page)))) + (org-store-link-props + :type "pdfview" + :link link + :description path)))) + +(defun org-pdfview-export (link description format) + "Export the pdfview LINK with DESCRIPTION for FORMAT from Org files." + (let* ((path (when (string-match "\\(.+\\)::.+" link) + (match-string 1 link))) + (desc (or description link))) + (when (stringp path) + (setq path (org-link-escape (expand-file-name path))) + (cond + ((eq format 'html) (format "%s" path desc)) + ((eq format 'latex) (format "\\href{%s}{%s}" path desc)) + ((eq format 'ascii) (format "%s (%s)" desc path)) + (t path))))) + +(defun org-pdfview-complete-link (&optional arg) + "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." + (concat (replace-regexp-in-string "^file:" "pdfview:" + (org-file-complete-link arg)) + "::" + (read-from-minibuffer "Page:" "1"))) + + +(org-link-set-parameters "pdfview" + :follow #'org-pdfview-open + :complete #'org-pdfview-complete-link + :store #'org-pdfview-store-link) + +(provide 'jao-org-pdfview) +;;; jao-org-pdfview.el ends here -- cgit v1.2.3