summaryrefslogtreecommitdiff
path: root/pdf-tools-annotation-list.el
blob: b3ec4d8b53774c925fc5c567a2447b0bf876a5bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
;;; pdf-tools-annotation-list.el --- List content of all pdf annotations  -*- lexical-binding: t; -*-

;; Copyright (C) 2022  Ferdinand Pieper

;; Author: Ferdinand Pieper <mail@pie.tf>
;; Keywords: pdf-tools, annotations

;; 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:

;; Provides annotation notes and also extracts text from highlighted regions.

;;; Code:

(require 'pdf-tools)
(require 'cl-lib)

(defconst pdf-tools-annotation-list-ignore-types
  (list 'link)
  "List of annotation types to ignore.")

(defun pdf-tools-annotation-list-get-region (coords)
  "Attempt to get 4-entry region \(LEFT TOP RIGHT BOTTOM\) from several COORDS.
We need this to import annotations and to get marked-up text, because
annotations are referenced by its coords, but functions for these tasks
need region."
  (let ((left0 (nth 0 (car coords)))
        (top0 (nth 1 (car coords)))
        (bottom0 (nth 3 (car coords)))
        (top1 (nth 1 (car (last coords))))
        (right1 (nth 2 (car (last coords))))
        (bottom1 (nth 3 (car (last coords))))
        (n (safe-length coords)))
    ;; we try to guess the line height to move
    ;; the region away from the boundary and
    ;; avoid double lines
    (list left0
          (+ top0 (/ (- bottom0 top0) 3))
          right1
          (- bottom1 (/ (- bottom1 top1) 3)))))

(defun pdf-tools-annotation-list-create-org-pdftools-link (filename page edges id)
  "Returns a formatted org-pdftools compatible link."
  (format "[[%s:%s::%s++%s][%s]]" org-pdftools-link-prefix filename page (nth 1 edges) id))

(defun pdf-tools-annotation-list-create-list ()
  "Return list of all annotations"
  (let* ((annots (sort (pdf-annot-getannots) 'pdf-annot-compare-annotations))
         (extracted-annots (mapcar
     (lambda (annot) ;; traverse all annotations
       (let* ((page (pdf-annot-get annot 'page))
              (has-markup-edges (pdf-annot-get annot 'markup-edges))
              (edges (if has-markup-edges
                         (car (pdf-annot-get annot 'markup-edges))
                       (pdf-annot-get annot 'edges)))
              (contents (pdf-annot-get annot 'contents))
              (id (symbol-name (pdf-annot-get-id annot)))
              (type (symbol-name (pdf-annot-get-type annot)))
              (filename (buffer-name))
              (entry (list
                      :file filename
                      :page page
                      :link (pdf-tools-annotation-list-create-org-pdftools-link
                             filename page edges id)
                      :id id
                      :type type
                      :contents contents
                      :text (when has-markup-edges
                              (pdf-info-gettext
                               page
                               (pdf-tools-annotation-list-get-region
                                (pdf-annot-get annot 'markup-edges)))))))
         entry
         ))
     (cl-remove-if
      (lambda (annot) (member (pdf-annot-get-type annot) pdf-tools-annotation-list-ignore-types))
      annots)
     )))
    extracted-annots))


(provide 'pdf-tools-annotation-list)
;;; pdf-tools-annotation-list.el ends here