jao-pdf.el 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ;;; jao-pdf.el --- utilities for pdf files -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2022 jao
  3. ;; Author: jao <mail@jao.io>
  4. ;; Keywords: docs
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; Some niceties for PDFs:
  17. ;;
  18. ;; - Using mutools, we can extract the outline of PDFs, and tell back the
  19. ;; section title of a given page.
  20. ;; - Interoperability with zathura.
  21. (require 'jao-doc-session)
  22. ;;; PDF info
  23. (declare-function 'pdf-info-outline "pdf-info")
  24. (defvar-local jao-pdf--outline nil)
  25. (defun jao-pdf-is-pdf-file (file)
  26. "Simply checks the FILE extension."
  27. (string-match-p ".*\\.pdf$" file))
  28. (defun jao-pdf-title-to-file-name (title)
  29. "Convert a title, possibly with embedded spaces, to a PDF filename."
  30. (concat (mapconcat 'downcase (split-string title nil t) "-") ".pdf"))
  31. (defun jao-pdf-title (&optional fname)
  32. (if (or fname (not (derived-mode-p 'doc-view-mode 'pdf-view-mode)))
  33. (let ((base (file-name-base (or fname (buffer-file-name)))))
  34. (capitalize (replace-regexp-in-string "-" " " base)))
  35. (or (jao-pdf-section-title)
  36. (when buffer-file-name (jao-pdf-title buffer-file-name)))))
  37. (defvar-local jao-pdf--outline nil)
  38. (defun jao-pdf-section-title (&optional page file-name)
  39. (when (not jao-pdf--outline)
  40. (setq-local jao-pdf--outline (doc-view--pdf-outline file-name)))
  41. (let ((page (or page
  42. (and (derived-mode-p 'doc-view-mode) (doc-view-current-page))
  43. (and (derived-mode-p 'pdf-view) (pdf-view-current-page))))
  44. (outline jao-pdf--outline)
  45. (cur-page 0)
  46. (cur-title (jao-pdf-title (or file-name buffer-file-name "title"))))
  47. (while (and (car outline) page (< cur-page page))
  48. (setq cur-page (cdr (assoc 'page (car outline))))
  49. (when (<= cur-page page)
  50. (setq cur-title (cdr (assoc 'title (car outline)))))
  51. (setq outline (cdr outline)))
  52. (replace-regexp-in-string "[[:blank:]]+" " " cur-title)))
  53. ;;; zathura interop
  54. (defun jao-pdf-zathura-open-cmd (file page &optional suffix)
  55. (let ((page (if page (format "-P %s" page) "")))
  56. (format "zathura %s %s %s" file page (or suffix ""))))
  57. (defun jao-pdf-zathura-title-rx (file)
  58. (concat (file-name-nondirectory file) " \\[.+\\]"))
  59. ;; e.g. "~/org/doc/write-yourself-a-scheme-in-48-hours.pdf [96 (96/138)]"
  60. (defun jao-pdf-zathura-file-info (title)
  61. (when (string-match "\\(.+\\) \\[\\(.+\\) (\\([0-9]+\\)/\\([0-9]+\\))\\]"
  62. title)
  63. (list (expand-file-name (match-string 1 title))
  64. (string-to-number (match-string 3 title))
  65. (string-to-number (match-string 4 title))
  66. (match-string 2 title))))
  67. (defun jao-pdf--zathura-link (info)
  68. (when-let* ((file (car info))
  69. (page (cadr info))
  70. (no (or (car (last info)) page))
  71. (fn (file-name-nondirectory file))
  72. (lnk (format "doc:%s::%s" fn page))
  73. (desc (format "%s (p. %s)" (jao-pdf-section-title page file) no)))
  74. (org-make-link-string lnk desc)))
  75. (defun jao-pdf-zathura-org-link (title)
  76. (jao-pdf--zathura-link (jao-pdf-zathura-file-info title)))
  77. (provide 'jao-pdf)
  78. ;;; jao-pdf.el ends here