summaryrefslogtreecommitdiff
path: root/pdf.el
blob: dac65eee876d0e6aa5bba328866bc0a74d7364ec (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
96
97
98
99
100
;;; Dependency
(use-package "tablist" 'tablist)

(use-package "pdf-tools/lisp" 'pdf-view)

;; I want to integrate with the global register list.
(setq pdf-view-use-dedicated-register nil)

;; NOTE: Fix a bug

;;;###autoload
(defun durand-pdf-view-position-to-register (register)
  "Store current PDF position in register REGISTER.

See also `point-to-register'.

Modifed by Durand 2021-07-27 10:42:02.828266."
  (interactive
   (list (pdf-view-with-register-alist
           (register-read-with-preview "Position to register: "))))
  (let ((p (assq register (cond (pdf-view-use-dedicated-register
                                 pdf-view-register-alist)
                                (register-alist)))))
    (cond
     (p (setcdr p (pdf-view-registerv-make)))
     (pdf-view-use-dedicated-register
      (setq pdf-view-register-alist
            (cons (cons register (pdf-view-registerv-make))
                  pdf-view-register-alist)))
     ((setq register-alist
            (cons (cons register (pdf-view-registerv-make))
                  register-alist))))))

(advice-add 'pdf-view-position-to-register :override
            'durand-pdf-view-position-to-register)

;;;###autoload
(defun pdf-view-down-half-page-or-previous-page ()
  "Scroll down half page or go to the previous page."
  (interactive)
  (pdf-view-scroll-down-or-previous-page
   (floor (window-body-height) 2)))

;;;###autoload
(defun pdf-view-up-half-page-or-next-page ()
  "Scroll down half page or go to the previous page."
  (interactive)
  (pdf-view-scroll-up-or-next-page
   (floor (window-body-height) 2)))

;; If this information is shown in the mode line, then it will take a
;; century to display a PDF page, probably because it uses such
;; functions as `image-next-line', thus I bind a key to show this
;; information when I want to acquire this information.

;;;###autoload
(defun pdf-view-page-position ()
  (interactive)
  "Show the current position in the page."
  (let* ((edges (pdf-util-image-displayed-edges nil t))
         (percent-floats (pdf-util-scale-pixel-to-relative
                          (cons (car edges) (cadr edges)) nil t))
         (bottom-p
          (cond ((= (window-vscroll nil pdf-view-have-image-mode-pixel-vscroll)
                    (image-next-line 1)))
                ;; The return value of `image-previous-line' is not
                ;; specified in its documentation string, but if
                ;; `image-previous-line' returns a non-nil value, then
                ;; this will return nil; if it returns nil, then this
                ;; still returns nil.
                ((image-previous-line 1) nil)))
         (str
          (cond
           ((and bottom-p (= (cdr percent-floats) 0)) "All")
           ((= (cdr percent-floats) 0) "Top")
           (bottom-p "Bot")
           ((format "%d%%%%" (* 100 (cdr percent-floats)))))))
    (message str)))

(use-package "pdf-tools/lisp" 'pdf-history)
(use-package "pdf-tools/lisp" 'pdf-links)
(use-package "pdf-tools/lisp" 'pdf-outline)
(use-package "pdf-tools/lisp" 'pdf-sync)
(use-package "pdf-tools/lisp" 'pdf-occur)
(use-package "pdf-tools/lisp" 'pdf-annot)
(use-package "pdf-tools/lisp" 'pdf-tools)

(define-key pdf-view-mode-map (vector ?j) #'pdf-view-next-line-or-next-page)
(define-key pdf-view-mode-map (vector ?k) #'pdf-view-previous-line-or-previous-page)
(define-key pdf-view-mode-map (vector ?y) #'pdf-view-previous-line-or-previous-page)
(define-key pdf-view-mode-map (vector ?d) #'pdf-view-up-half-page-or-next-page)
(define-key pdf-view-mode-map (vector ?u) #'pdf-view-down-half-page-or-previous-page)
(define-key pdf-view-mode-map (vector 'tab) #'pdf-view-page-position)
(define-key pdf-view-mode-map (vector ?\C-<) #'image-scroll-left)
(define-key pdf-view-mode-map (vector ?\C->) #'image-scroll-right)
(define-key pdf-view-mode-map (vector ?>) #'image-eob)
(define-key pdf-view-mode-map (vector ?<) #'image-bob)
(define-key pdf-history-minor-mode-map (vector ?l) nil)

(pdf-tools-install)