summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorjao <jao@gnu.org>2020-10-25 01:26:02 +0000
committerjao <jao@gnu.org>2020-10-25 01:26:02 +0000
commit450d8da4d8ae965a4a7d9903772403b68c376d8f (patch)
tree470a6baa17455300dcb8540454ab08fa4652ab5e
parent43f6cae85b24d0c986ab1ed45d399ef010ea1f4b (diff)
downloadelibs-450d8da4d8ae965a4a7d9903772403b68c376d8f.tar.gz
elibs-450d8da4d8ae965a4a7d9903772403b68c376d8f.tar.bz2
jao-org-links: extending doc: to deal with pdfs and page numbers
-rw-r--r--org/jao-org-links.el92
-rw-r--r--org/jao-org-pdfview.el98
-rw-r--r--sys/jao-devon.el17
3 files changed, 83 insertions, 124 deletions
diff --git a/org/jao-org-links.el b/org/jao-org-links.el
index bd3512e..b34bb06 100644
--- a/org/jao-org-links.el
+++ b/org/jao-org-links.el
@@ -1,44 +1,65 @@
(require 'jao-org-utils)
-(require 'jao-org-pdfview)
-(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)
+(defvar jao-org--sink-dir "./")
- (define-key org-mode-map "\C-cd" 'jao-org-insert-devon-link)
- (define-key org-mode-map "\C-cs" 'jao-org-insert-safari-link))
+(defun jao-org--pdf-p (file) (string-match-p ".*\\.pdf$" file))
-(defvar jao-org--sink-dir "./")
+(defun jao-org-links--open-pdf (link)
+ "Open LINK in pdf-view-mode."
+ (require 'pdf-tools)
+ (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 jao-org-follow-doc (link)
- (let ((dest-path (concat org-directory "/doc/" link)))
+(defun jao-org-links--follow-doc (link)
+ (let* ((full-link (concat org-directory "/doc/" link))
+ (dest-path (car (split-string full-link "::"))))
(when (not (file-exists-p dest-path))
(let* ((sink-file (expand-file-name link jao-org--sink-dir))
(real-file (if (file-exists-p sink-file) sink-file
(read-file-name "Import file: "
jao-org--sink-dir link link))))
(shell-command (format "mv %s %s" real-file dest-path))))
- (browse-url (format "file://%s" (expand-file-name dest-path)))))
+ (if (jao-org--pdf-p dest-path)
+ (jao-org-links--open-pdf full-link)
+ (browse-url (format "file://%s" (expand-file-name dest-path))))))
-(defun jao-org-complete-doc (&optional arg)
+(defun jao-org-links--complete-doc (&optional arg)
(let ((default-directory jao-org--sink-dir))
- (replace-regexp-in-string "^file:" "doc:" (org-file-complete-link arg))))
+ (let ((f (replace-regexp-in-string "^file:" "doc:"
+ (org-file-complete-link arg))))
+ (if (jao-org--pdf-p p)
+ (concat f "::" (read-from-minibuffer "Page: " "1"))
+ f))))
(defsubst jao-org--title->file (title)
(concat (mapconcat 'downcase (split-string title nil t) "-") ".pdf"))
+(defun jao-org--pdf-title ()
+ (let ((base (file-name-base (pdf-view-buffer-file-name))))
+ (capitalize (replace-regexp-in-string "-" " " base))))
+
+(defun jao-org-links--store-pdf-link ()
+ (when (eq major-mode 'pdf-view-mode)
+ (let ((path buffer-file-name)
+ (page (pdf-view-current-page)))
+ (org-store-link-props
+ :type "doc"
+ :link (format "doc:%s::%d" (file-name-nondirectory path) page)
+ :description (format "%s (p. %d)" (jao-org--pdf-title) page)))))
+
;;;###autoload
(defun jao-org-insert-doc (title)
(interactive "sDocument title: ")
@@ -47,8 +68,27 @@
;;;###autoload
(defun jao-org-links-setup (sink-dir)
(org-link-set-parameters "doc"
- :follow #'jao-org-follow-doc
- :complete #'jao-org-complete-doc)
+ :follow #'jao-org-links--follow-doc
+ :complete #'jao-org-links--complete-doc
+ :store #'jao-org-links--store-pdf-link)
(setq jao-org--sink-dir (file-name-as-directory sink-dir)))
+(defun jao-org--open-other (otp)
+ (let ((rxs ))))
+
+;;;###autoload
+(defun jao-org-pdf-goto-org (arg)
+ (interactive "P")
+ (when (jao-org--pdf-p buffer-file-name)
+ (let* ((file (replace-regexp-in-string "/doc/\\(.+\\)\\.pdf$"
+ "/notes/\\1.org"
+ buffer-file-name))
+ (new (not (file-exists-p file)))
+ (title (jao-org--pdf-title)))
+ (when (or arg new) (org-store-link nil t))
+ (find-file-other-window file)
+ (when new
+ (insert "#+title: " title "\n#+author:\n#+startup: latexpreview\n\n")
+ (org-insert-link)))))
+
(provide 'jao-org-links)
diff --git a/org/jao-org-pdfview.el b/org/jao-org-pdfview.el
deleted file mode 100644
index 2f38160..0000000
--- a/org/jao-org-pdfview.el
+++ /dev/null
@@ -1,98 +0,0 @@
-;;; org-pdfview.el --- Support for links to documents in pdfview mode
-
-;; Copyright (C) 2014 Markus Hauck
-
-;; Author: Markus Hauck <markus1189@gmail.com>
-;; Maintainer: Markus Hauck <markus1189@gmail.com>
-;; 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 <http://www.gnu.org/licenses/>.
-
-;;; 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 "<a href=\"%s\">%s</a>" 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
- :export #'org-pdfview-export)
-
-(provide 'jao-org-pdfview)
-;;; jao-org-pdfview.el ends here
diff --git a/sys/jao-devon.el b/sys/jao-devon.el
index 4d454ba..65e989f 100644
--- a/sys/jao-devon.el
+++ b/sys/jao-devon.el
@@ -55,4 +55,21 @@
(jao-devon-add-html-page title url html))
(w3m-view-source))))
+(with-eval-after-load "org"
+ (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))
+
(provide 'jao-devon)