summaryrefslogtreecommitdiffhomepage
path: root/lib/doc
diff options
context:
space:
mode:
authorjao <jao@gnu.org>2022-08-29 02:39:22 +0100
committerjao <jao@gnu.org>2022-08-29 02:39:22 +0100
commit8b8f6252bf71648f39b8af8c98ea2b1c399ef9d5 (patch)
tree78ca86f8b10360df7434838e992dc0218722ed22 /lib/doc
parent60773cad28b36360a4704121833e8d99c585b536 (diff)
downloadelibs-8b8f6252bf71648f39b8af8c98ea2b1c399ef9d5.tar.gz
elibs-8b8f6252bf71648f39b8af8c98ea2b1c399ef9d5.tar.bz2
new lib: jao-pdf
Diffstat (limited to 'lib/doc')
-rw-r--r--lib/doc/jao-doc-view.el58
-rw-r--r--lib/doc/jao-org-links.el15
-rw-r--r--lib/doc/jao-pdf.el113
3 files changed, 127 insertions, 59 deletions
diff --git a/lib/doc/jao-doc-view.el b/lib/doc/jao-doc-view.el
index d68cae9..a55d477 100644
--- a/lib/doc/jao-doc-view.el
+++ b/lib/doc/jao-doc-view.el
@@ -19,70 +19,24 @@
;; Start date: Fri Feb 15, 2013 01:21
(require 'doc-view)
+(require 'jao-pdf)
-;;; PDF info
+;;; Utilities
-(defvar-local jao--pdf-outline nil)
-
-(defmacro jao-doc-view--pdf-call (a b &rest args)
+(defmacro jao-doc-view--funcall (a b &rest args)
`(cond ((derived-mode-p 'pdf-view-mode) (,a ,@args))
((derived-mode-p 'doc-view-mode) (,b ,@args))))
-(defun jao-doc-view-is-pdf (file) (string-match-p ".*\\.pdf$" file))
-
-(defun jao-doc-view-title->file (title)
- (concat (mapconcat 'downcase (split-string title nil t) "-") ".pdf"))
-
(defun jao-doc-view-current-page ()
- (jao-doc-view--pdf-call pdf-view-current-page doc-view-current-page))
+ (jao-doc-view--funcall pdf-view-current-page doc-view-current-page))
(defun jao-doc-view-goto-page (page &optional height)
(when page
- (jao-doc-view--pdf-call pdf-view-goto-page doc-view-goto-page page))
+ (jao-doc-view--funcall pdf-view-goto-page doc-view-goto-page page))
(when (and height (derived-mode-p 'pdf-view-mode))
(image-set-window-vscroll
(round (/ (* height (cdr (pdf-view-image-size))) (frame-char-height))))))
-(defvar jao-doc-view--outline-rx
- ".+\\(\t+\\)\"\\(.+\\)\"\t#\\(?:page=\\)?\\([0-9]+\\)")
-
-(defun jao-doc-view-pdf-outline (&optional file-name)
- (if (derived-mode-p 'pdf-view-mode)
- (pdf-info-outline)
- (let* ((outline nil)
- (fn (or file-name (buffer-file-name) jao-doc-view--imenu-file))
- (fn (shell-quote-argument (expand-file-name fn))))
- (with-temp-buffer
- (insert (shell-command-to-string (format "mutool show %s outline" fn)))
- (goto-char (point-min))
- (while (re-search-forward jao-doc-view--outline-rx nil t)
- (push `((level . ,(length (match-string 1)))
- (title . ,(match-string 2))
- (page . ,(string-to-number (match-string 3))))
- outline)))
- (nreverse outline))))
-
-(defun jao-doc-view-section-title (&optional page file-name)
- (when (not jao--pdf-outline)
- (setq-local jao--pdf-outline (jao-doc-view-pdf-outline file-name)))
- (let ((page (or page (jao-doc-view-current-page)))
- (outline jao--pdf-outline)
- (cur-page 0)
- (cur-title (jao-doc-view-title (or file-name buffer-file-name "title"))))
- (while (and (car outline) (< cur-page page))
- (setq cur-page (cdr (assoc 'page (car outline))))
- (when (<= cur-page page)
- (setq cur-title (cdr (assoc 'title (car outline)))))
- (setq outline (cdr outline)))
- (replace-regexp-in-string "[[:blank:]]+" " " cur-title)))
-
-(defun jao-doc-view-title (&optional fname)
- (if (or fname (not (derived-mode-p 'doc-view-mode 'pdf-view-mode)))
- (let ((base (file-name-base (or fname (buffer-file-name)))))
- (capitalize (replace-regexp-in-string "-" " " base)))
- (or (jao-doc-view-section-title)
- (when buffer-file-name (jao-doc-view-title buffer-file-name)))))
-
;;; imenu
(defvar-local jao-doc-view--imenu-file nil)
(defvar-local jao-doc-view--goer 'jao-doc-view-goto-page)
@@ -97,7 +51,7 @@
(let (index)
(dolist (item (or jao--pdf-outline
(setq jao--pdf-outline
- (jao-doc-view-pdf-outline jao-doc-view--imenu-file))))
+ (jao-pdf-outline jao-doc-view--imenu-file))))
(let-alist item
(let* ((lvl (make-string (max 0 (1- .level)) ?.))
(title (format "%s%s (%s)" lvl .title .page)))
diff --git a/lib/doc/jao-org-links.el b/lib/doc/jao-org-links.el
index f1ba0be..c1995dd 100644
--- a/lib/doc/jao-org-links.el
+++ b/lib/doc/jao-org-links.el
@@ -4,6 +4,7 @@
(require 'jao-org-notes)
(require 'jao-doc-view)
+(require 'jao-pdf)
(declare pdf-info-outline "pdf-info")
@@ -39,7 +40,7 @@
(read-file-name "Import file: "
jao-org--sink-dir link link))))
(rename-file real-file dest-path)))
- (if (jao-doc-view-is-pdf dest-path)
+ (if (jao-pdf-is-pdf-file dest-path)
(jao-org-links--open-pdf full-link)
(browse-url (format "file://%s" (expand-file-name dest-path))))))
@@ -47,7 +48,7 @@
(let ((default-directory jao-org--sink-dir))
(let ((f (replace-regexp-in-string "^file:" "doc:"
(org-file-complete-link arg))))
- (if (jao-doc-view-is-pdf f)
+ (if (jao-pdf-is-pdf-file f)
(let ((page (read-from-minibuffer "Page: " "")))
(if (> (string-to-number page) 0)
(concat f "::" (read-from-minibuffer "Page: " ""))
@@ -63,7 +64,7 @@
(when (derived-mode-p 'pdf-view-mode 'doc-view-mode)
(jao-org-links-store-pdf-link buffer-file-name
(jao-doc-view-current-page)
- (jao-doc-view-section-title)))))
+ (jao-pdf-section-title)))))
;;;###autoload
(defun jao-org-links-store-pdf-link (path page title)
@@ -75,7 +76,7 @@
;;;###autoload
(defun jao-org-insert-doc (title)
(interactive "sDocument title: ")
- (insert (format "[[doc:%s][%s]]" (jao-doc-view-title->file title) title)))
+ (insert (format "[[doc:%s][%s]]" (jao-pdf-title-to-file-name title) title)))
;;;###autoload
(defun jao-org-org-to-pdf-file ()
@@ -97,7 +98,7 @@
;;;###autoload
(defun jao-org-insert-doc-skeleton (&optional title)
- (insert "#+title: " (or title (jao-doc-view-title (buffer-file-name)))
+ (insert "#+title: " (or title (jao-pdf-title (buffer-file-name)))
"\n#+author:\n#+filetags: ")
(jao-org-notes-insert-tags)
(insert "\n#+startup: latexpreview\n\n"))
@@ -105,10 +106,10 @@
;;;###autoload
(defun jao-org-pdf-goto-org (arg)
(interactive "P")
- (when (jao-doc-view-is-pdf buffer-file-name)
+ (when (jao-pdf-is-pdf-file buffer-file-name)
(let* ((file (jao-org-pdf-to-org-file))
(new (not (file-exists-p file)))
- (title (jao-doc-view-title)))
+ (title (jao-pdf-title)))
(when (or arg new) (org-store-link nil t))
(find-file-other-window file)
(when new
diff --git a/lib/doc/jao-pdf.el b/lib/doc/jao-pdf.el
new file mode 100644
index 0000000..62dfc08
--- /dev/null
+++ b/lib/doc/jao-pdf.el
@@ -0,0 +1,113 @@
+;;; jao-pdf.el --- utilities for pdf files -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2022 jao
+
+;; Author: jao <mail@jao.io>
+;; Keywords: docs
+
+;; 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 <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Some niceties for PDFs:
+;;
+;; - Using mutools, we can extract the outline of PDFs, and tell back the
+;; section title of a given page.
+;; - Interoperability with zathura.
+
+;;; PDF info
+
+(declare-function 'pdf-info-outline "pdf-info")
+
+(defvar-local jao-pdf--outline nil)
+
+(defun jao-pdf-is-pdf-file (file)
+ "Simply checks the FILE extension."
+ (string-match-p ".*\\.pdf$" file))
+
+(defun jao-pdf-title-to-file-name (title)
+ "Convert a title, possibly with embedded spaces, to a PDF filename."
+ (concat (mapconcat 'downcase (split-string title nil t) "-") ".pdf"))
+
+(defvar jao-pdf--outline-rx
+ ".+\\(\t+\\)\"\\(.+\\)\"\t#\\(?:page=\\)?\\([0-9]+\\)")
+
+(defun jao-pdf-outline (file-name)
+ "Return an alist describing the given FILE-NAME (or current if nil).
+The result is cached as a local buffer variable."
+ (if (derived-mode-p 'pdf-view-mode)
+ (pdf-info-outline)
+ (let* ((outline nil)
+ (fn (or file-name (buffer-file-name)))
+ (fn (shell-quote-argument (expand-file-name fn))))
+ (with-temp-buffer
+ (insert (shell-command-to-string (format "mutool show %s outline" fn)))
+ (goto-char (point-min))
+ (while (re-search-forward jao-pdf--outline-rx nil t)
+ (push `((level . ,(length (match-string 1)))
+ (title . ,(match-string 2))
+ (page . ,(string-to-number (match-string 3))))
+ outline)))
+ (nreverse outline))))
+
+(defun jao-pdf-title (&optional fname)
+ (if (or fname (not (derived-mode-p 'doc-view-mode 'pdf-view-mode)))
+ (let ((base (file-name-base (or fname (buffer-file-name)))))
+ (capitalize (replace-regexp-in-string "-" " " base)))
+ (or (jao-pdf-section-title)
+ (when buffer-file-name (jao-pdf-title buffer-file-name)))))
+
+(defun jao-pdf-section-title (&optional page file-name)
+ (when (not jao-pdf--outline)
+ (setq-local jao-pdf--outline (jao-pdf-outline file-name)))
+ (let ((page (or page (jao-pdf-current-page)))
+ (outline jao-pdf--outline)
+ (cur-page 0)
+ (cur-title (jao-pdf-title (or file-name buffer-file-name "title"))))
+ (while (and (car outline) (< cur-page page))
+ (setq cur-page (cdr (assoc 'page (car outline))))
+ (when (<= cur-page page)
+ (setq cur-title (cdr (assoc 'title (car outline)))))
+ (setq outline (cdr outline)))
+ (replace-regexp-in-string "[[:blank:]]+" " " cur-title)))
+
+;;; zathura interop
+(defun jao-pdf-zathura-file-info (title)
+ (when (string-match "\\(.+\\) \\[\\(.+\\) (\\([0-9]+\\)/\\([0-9]+\\))\\]"
+ title)
+ (list (expand-file-name (match-string 1 title))
+ (string-to-number (match-string 3 title))
+ (string-to-number (match-string 4 title))
+ (match-string 2 title))))
+
+(defun jao-pdf-goto-zathura-org (title &optional no-ask)
+ (when-let* ((info (jao-pdf-zathura-file-info title))
+ (pdf-file (car info))
+ (file (jao-org-pdf-to-org-file pdf-file))
+ (page (cadr info))
+ (pageno (or (car (last info)) page)))
+ (ignore-errors (jao-afio--goto-docs))
+ (let ((exists (file-exists-p file)))
+ (find-file file)
+ (unless exists (jao-org-insert-doc-skeleton))
+ (let* ((fn (file-name-nondirectory pdf-file))
+ (desc (jao-pdf-section-title page pdf-file))
+ (lnk (format "[[doc:%s::%d][%s]]" fn page desc)))
+ (if (or (not exists) (and (not no-ask) (y-or-n-p "Insert link?")))
+ (insert lnk "\n")
+ (kill-new lnk)
+ (message "Link to %s (%s) killed" file page))))))
+
+(provide 'jao-pdf)
+;;; jao-pdf.el ends here