;; jao-doc-view.el -- Remembering visited documents -*- lexical-binding: t; -*-
;; Copyright (c) 2013, 2015, 2017, 2018, 2019, 2021 Jose Antonio Ortega Ruiz
;; This file 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 file 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 .
;; Author: Jose Antonio Ortega Ruiz
;; Start date: Fri Feb 15, 2013 01:21
;;; Comentary:
;; Some utilities to keep track of visited documents and their structure.
;;; Code:
;;; Session
(require 'doc-view)
(defvar jao-doc-view-session-file "~/.emacs.d/cache/doc-view-session.eld")
(defun jao-doc-view--read-file (file)
(let ((buff (find-file-noselect file)))
(ignore-errors
(with-current-buffer buff
(goto-char (point-min)))
(read buff))))
(defun jao-doc-view--save-to-file (file value)
(with-current-buffer (find-file-noselect file)
(erase-buffer)
(insert (format "%S" value))
(save-buffer)))
(defun jao-doc-view-session (&optional file)
(let ((file (or file jao-doc-view-session-file)))
(jao-doc-view--read-file file)))
(defun jao-doc-view-save-session (&optional skip-current)
(interactive)
(let ((docs '())
(cb (when skip-current (current-buffer))))
(dolist (b (buffer-list))
(with-current-buffer b
(when (and (derived-mode-p 'doc-view-mode 'pdf-view-mode)
(not (equalp cb b)))
(add-to-list 'docs (buffer-file-name)))))
(when (> (length docs) 0)
(jao-doc-view--save-to-file jao-doc-view-session-file docs))
(message "%s docs saved" (length docs))))
(defun jao-doc-view--save-session-1 ()
(when (derived-mode-p 'pdf-view-mode 'doc-view-mode)
(jao-doc-view-save-session t)))
(defun jao-doc-view--save-session ()
(let ((inhibit-message t)
(message-log-max nil))
(jao-doc-view-save-session)
t))
(add-hook 'kill-emacs-query-functions #'jao-doc-view--save-session)
(add-hook 'kill-buffer-hook #'jao-doc-view--save-session-1)
(add-hook 'doc-view-mode-hook #'jao-doc-view--save-session)
(add-hook 'pdf-view-mode-hook #'jao-doc-view--save-session)
;;; PDF info
(defvar-local jao--pdf-outline nil)
(defmacro jao-doc-view--pdf-call (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 (&optional fname)
(let ((base (file-name-base (or fname (buffer-file-name)))))
(capitalize (replace-regexp-in-string "-" " " base))))
(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))
(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))
(when (and height (derived-mode-p 'pdf-view-mode))
(image-set-window-vscroll
(round (/ (* height (cdr (pdf-view-image-size))) (frame-char-height))))))
(defun jao-doc-view-pdf-outline ()
(if (derived-mode-p 'pdf-view-mode)
(pdf-info-outline)
(let ((outline nil)
(fn (shell-quote-argument (buffer-file-name))))
(with-temp-buffer
(insert (shell-command-to-string (format "mutool show %s outline" fn)))
(goto-char (point-min))
(while (re-search-forward ".+\\(\t+\\)\"\\(.+\\)\"\t#\\([0-9]+\\)," 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)
(when (not jao--pdf-outline)
(setq-local jao--pdf-outline (jao-doc-view-pdf-outline)))
(let ((page (or page (jao-doc-view-current-page)))
(outline jao--pdf-outline)
(cur-page 0)
(cur-title (jao-doc-view-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)))
;;; imenu
(defun jao-doc-view--enable-imenu ()
(setq-local imenu-create-index-function #'jao-doc-view--imenu-create-index)
(imenu-add-to-menubar "PDF outline"))
(defun jao-doc-view--imenu-create-index ()
(let (index)
(dolist (item (or jao--pdf-outline
(setq jao--pdf-outline (jao-doc-view-pdf-outline))))
(let-alist item
(let* ((lvl (make-string (max 0 (1- .level)) ?.))
(title (format "%s%s (%s)" lvl .title .page)))
(push `(,title 0 jao-doc-view--go ,item) index))))
(nreverse index)))
(defun jao-doc-view--go (&rest args)
(when-let (item (car (last args)))
(let-alist item (jao-doc-view-goto-page .page))))
(add-hook 'doc-view-mode-hook #'jao-doc-view--enable-imenu)
;;; Page trailing
(defvar-local jao-doc-view--trail-back ())
(defvar-local jao-doc-view--trail-fwd ())
(defun jao-doc-view--trail-push (dest-page)
(when-let (page (jao-doc-view-current-page))
(unless (eq (car jao-doc-view--trail-back) page)
(push page jao-doc-view--trail-back))))
(defun jao-doc-view-back ()
(interactive nil doc-view-mode)
(if-let (p (pop jao-doc-view--trail-back))
(progn (push (jao-doc-view-current-page) jao-doc-view--trail-fwd)
(jao-doc-view-goto-page p))
(message "No more back marks.")))
(defun jao-doc-view-forward ()
(interactive nil doc-view-mode)
(if-let (p (pop jao-doc-view--trail-fwd))
(progn (push (jao-doc-view-current-page) jao-doc-view--trail-back)
(jao-doc-view-goto-page p))
(message "No more forward marks.")))
(advice-add 'doc-view-goto-page :before #'jao-doc-view--trail-push)
(provide 'jao-doc-view)