-
Notifications
You must be signed in to change notification settings - Fork 2
/
pdf-view-pagemark.el
144 lines (118 loc) · 4.79 KB
/
pdf-view-pagemark.el
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
;;; pdf-view-pagemark.el --- Add indicator in pdfview mode to show the page remaining
;; Copyright (c) 2023 Kimi Ma <[email protected]>
;; Author: Kimi Ma <[email protected]>
;; URL: https://github.com/kimim/pdf-view-pagemark
;; Keywords: multimedia convenience
;; Version: 0.1
;; Package-Requires: ((pdf-tools "0.90") (posframe "1.4.2") (emacs "26.0"))
;; This file is NOT part of GNU Emacs.
;;; License:
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Because Emacs cannot show continuous pages at the same time, it is
;; difficult to keep track of the remaining text in current page, this
;; minor mode add a posframe indicator to tell you the line from
;; scroll up.
;;
;; To enable, add the following:
;;
;; (use-package pdf-view-pagemark
;; :custom (pdf-view-pagemark-timeout 2)
;; :hook (pdf-view-mode . pdf-view-pagemark-mode))
;;; Code:
(require 'pdf-view)
(require 'posframe)
(defcustom pdf-view-pagemark-buffer "*pdf-view-pagemark*"
"Buffer name to show pagemark."
:group 'pdf-view
:type 'string)
(defcustom pdf-view-pagemark-timeout 3
"Timeout to show pagemark."
:group 'pdf-view
:type 'integer)
(defcustom pdf-view-pagemark-alpha 40
"Trasparency level of pagemark posframe."
:group 'pdf-view
:type 'integer)
(defface pdf-view-pagemark-color
nil
"Background color of pagemark posframe."
:group 'pdf-view)
;;;###autoload
(define-minor-mode pdf-view-pagemark-mode
"Automatically show pagemark indicator."
:global nil
(if (not pdf-view-pagemark-mode)
(progn
(advice-remove 'image-scroll-up 'pdf-view-pagemark-indicate)
(posframe-delete pdf-view-pagemark-buffer))
(pdf-view-pagemark)))
(defun pdf-view-pagemark ()
"Enable pagemark."
(when (derived-mode-p 'pdf-view-mode)
;; This buffer is in pdf-view-mode
(advice-add 'image-scroll-up :before 'pdf-view-pagemark-indicate)))
(defun pdf-view-pagemark-image-height ()
"Get pdf height."
(let ((image (image-get-display-property)))
(ceiling (cdr (image-display-size image t)))))
(defun pdf-view-pagemark-image-width ()
"Get pdf width."
(let ((image (image-get-display-property)))
(ceiling (car (image-display-size image t)))))
(defun pdf-view-pagemark-win-height ()
"Get window height."
(let ((edges (window-edges nil t t)))
(- (nth 3 edges) (nth 1 edges))))
(defun pdf-view-pagemark-win-width ()
"Get window width."
(let ((edges (window-edges nil t t)))
(- (nth 2 edges) (nth 0 edges))))
(defun pdf-view-pagemark-rem-height ()
"Calculate remaining height of pdf page."
(- (pdf-view-pagemark-image-height) (window-vscroll nil t)
(pdf-view-pagemark-win-height)))
(defun pdf-view-pagemark-position ()
"Calculate indicator position."
(- (pdf-view-pagemark-win-height)
(pdf-view-pagemark-rem-height)))
(defun pdf-view-pagemark-window-left-edge ()
"Calculate x position of left window edge."
(* (car (window-edges)) (frame-char-width)))
(defun pdf-view-pagemark-left-margin ()
"Calculate pagemark left margin position, 0 if negative margin."
(max 0
(/ (- (pdf-view-pagemark-win-width)
(pdf-view-pagemark-image-width))
2)))
(defun pdf-view-pagemark-indicate (&optional _n)
"Show indicator for remaining pdf page."
(let* ((left-indent (+ (pdf-view-pagemark-window-left-edge)
(pdf-view-pagemark-left-margin)))
(rem-height (pdf-view-pagemark-rem-height))
(len (min (/ (pdf-view-pagemark-win-width) (frame-char-width))
(/ (pdf-view-pagemark-image-width) (frame-char-width))))
(bg (or (face-background 'pdf-view-pagemark-color)
(face-background 'highlight))))
(if (and (< 0 rem-height)
(< rem-height (pdf-view-pagemark-win-height)))
(set-frame-parameter
(posframe-show pdf-view-pagemark-buffer
:string (make-string len ?-)
:foreground-color bg
:background-color bg
:position `(,left-indent . ,(pdf-view-pagemark-position))
:timeout pdf-view-pagemark-timeout)
'alpha pdf-view-pagemark-alpha)
(posframe-hide pdf-view-pagemark-buffer))))
(provide 'pdf-view-pagemark)
;;; pdf-view-pagemark.el ends here