aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-nov.el
blob: d43a8f3040f11e0c520e7da9e86eab07383743a4 (plain) (blame)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
;;; my-nov.el -- Extensions for nov.el -*- lexical-binding: t -*-

;; Copyright (C) 2023 Free Software Foundation.

;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "28.2"))

;; This file is part of dotfiles.

;; dotfiles is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; dotfiles 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 Affero General
;; Public License for more details.

;; You should have received a copy of the GNU Affero General Public
;; License along with dotfiles.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Extensions for nov.el.

;;; Code:

(require 'nov)

;; override nov-render-title
;; this is because header line does not work with follow mode
(defun my-nov-render-title (dom)
  "Custom <title> rendering function for DOM.
Sets `header-line-format' to a combination of the EPUB title and
chapter title."
  (let ((title (cdr (assq 'title nov-metadata)))
        (chapter-title (car (esxml-node-children dom))))
    (when (not chapter-title)
      (setq chapter-title "No title"))
    ;; this shouldn't happen for properly authored EPUBs
    (when (not title)
      (setq title "No title"))
    ;; TODO: fix mode line update
    (setq mode-line-buffer-identification
          (format "%s: %s (%d%%)"
	                title chapter-title
                  (/ (* 100 (my-nov-word-position)) my-nov-total-word-count)
                  ))
    ))

(defun my-nov-render-span (dom)
  (unless (equal (dom-attr dom 'epub:type) "pagebreak")
    (shr-generic dom)))

(defun my-nov-find-file-with-ipath (file-name ipath)
  "Find epub file and goto IPATH.

Useful for recoll."
  (find-file file-name)
  (unless (derived-mode-p 'nov-mode) (nov-mode))
  (nov-goto-document (nov-find-document (lambda (p) (eq ipath (car p))))))

(defun my-nov-scroll-up (arg)
  "Scroll with `scroll-up' or visit next chapter if at bottom."
  (interactive "P")
  (if (>= (follow-window-end) (point-max))
      (nov-next-document)
    (follow-scroll-up arg)))

(defun my-nov-copy-buffer-file-with-staging ()
  (interactive)
  (unless (derived-mode-p 'nov-mode) (error "Not in nov mode"))
  (pcase-let* ((name
                (completing-read (format "Copy %s to: " nov-file-name)
                                 my-copy-file-targets
                                 nil t))
               (`(,dest ,staging) (alist-get name my-copy-file-targets
                                             nil nil #'equal)))
    (my-copy-file-with-staging
     nov-file-name dest staging)))

(defun my-nov-set-margins ()
  ;; Does not work as well as setq left- and right-margin-width
  ;; (set-window-margins nil 3 2)
  (setq left-margin-width 3)
  (setq right-margin-width 2)
  ;; Does not work as well as setq left- and right-fringe-width
  ;; (set-window-fringes nil 0 0)
  (setq left-fringe-width 0)
  (setq right-fringe-width 0)
  (visual-line-mode)
  )

(defvar-local my-nov-document-word-counts nil
  "Word count of each nov document.")

(defvar-local my-nov-total-word-count nil
  "Total word count of the epub.")

(defun my-nov-count-words ()
  (interactive)
  (unless my-nov-document-word-counts
    (message "Counting words...")
    (setq my-nov-document-word-counts
          (apply
           'vector
           (seq-map
            (lambda (doc)
              (with-temp-buffer
                (pcase-let ((`(,name . ,file) doc))
                  (insert-file-contents file)
                  (nov-render-html)
                  (cons name (count-words (point-min) (point-max))))))
            nov-documents)))
    (setq my-nov-total-word-count
          (seq-reduce
           (lambda (sum pair)
             (+ sum (cdr pair)))
           my-nov-document-word-counts
           0))
    (message "Counting words...done")))

(defun my-nov-stats ()
  (interactive)
  (message "%d words; %d standard pages"
           my-nov-total-word-count
           (ceiling (/ my-nov-total-word-count 300.0))))

;;; TODO: also show current percentage in the total book in the mode
;;; line
(defun my-nov-goto-nth-word (n)
  "Go to the nth word of the current epub."
  (my-nov-count-words)
  (setq nov-documents-index -1)
  (let ((found
         (seq-find
          (lambda (pair)
            (setq n (- n (cdr pair)))
            (setq nov-documents-index (1+ nov-documents-index))
            (<= n 0))
          my-nov-document-word-counts)))
    (nov-render-document)
    (if (> n 0)
        (end-of-buffer)
      (forward-word (+ n (cdr found)))))
  )

(defun my-nov-word-position ()
  "Where are we in terms of word position?

Return n, such that nth word of the epub is at the beginning of the
screen."
  (my-nov-count-words)
  (let ((result 0))
    (dotimes (i nov-documents-index)
      (setq result (+ result (cdr (aref my-nov-document-word-counts i)))))
    (save-excursion
      (move-to-window-line 0)
      (setq result (+ result (count-words (point-min) (point)))))))

(defun my-nov-skim-forward ()
  "Forward by 3-10% of the book."
  (interactive)
  (let ((pc (+ 3 (random 8))))
    (my-nov-goto-nth-word
     (+ (my-nov-word-position)
        (/ (* my-nov-total-word-count pc) 100)))
    (message "Skimmed forward by %d%% of the book" pc)))

(defun my-nov-skim-backward ()
  "Backward by 3-10% of the book."
  (interactive)
  (let ((pc (+ 3 (random 8))))
    (my-nov-goto-nth-word
     (max
      0
      (- (my-nov-word-position)
         (/ (* my-nov-total-word-count pc) 100))))
    (message "Skimmed backward by %d%% of the book" pc)))

(defun my-nov-goto-random-position ()
  "Goto a random position in the epub."
  (interactive)
  (my-nov-count-words)
  (let ((n (random my-nov-total-word-count)))
    (my-nov-goto-nth-word n)
    (message "Went to the %dth word (%d%% of the book)."
             n (/ (* n 100) my-nov-total-word-count))))

(provide 'my-nov)
;;; my-nov.el ends here