-
Notifications
You must be signed in to change notification settings - Fork 2
/
finito-buffer.el
440 lines (361 loc) Β· 15.7 KB
/
finito-buffer.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
;;; finito-buffer.el --- Buffer utilities for finito -*- lexical-binding: t -*-
;; Copyright (C) 2021 Laurence Warne
;; Author: Laurence Warne
;; Local variables:
;; package-lint-main-file: "finito.el"
;; end:
;; 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:
;; This file contains utilities for working with libro finito buffers
;;; Code:
(require 'cl-lib)
(require 'dash)
(require 'eieio)
(require 'iso8601)
(require 'org)
(require 's)
(require 'savehist)
(require 'finito-core)
;;; Faces
(defface finito-author-name
'((t :foreground "aquamarine"
:weight bold
:underline t))
"Face for author names."
:group 'finito)
(defface finito-book-descriptions
'((t :slant oblique
:inherit default))
"Face for book descriptions."
:group 'finito)
(defface finito-book-reviews
'((t :foreground "grey"
:inherit default))
"Face for book reviews."
:group 'finito)
(defface finito-book-review-header
'((t :foreground "grey"
:weight bold
:underline t
:inherit default))
"Face for book review headers."
:group 'finito)
(defface finito-rating
'((t :foreground "gold"
:weight bold))
"Face for ratings."
:group 'finito)
(defface finito-currently-reading
'((t :foreground "sky blue"
:italic t))
"Face for currently reading message."
:group 'finito)
(defface finito-last-read
'((t :foreground "light green"
:weight bold))
"Face for last read message."
:group 'finito)
(defface finito-summary-read
'((t :foreground "purple"
:weight bold))
"Face for the number of read books in a finito summary buffer."
:group 'finito)
(defface finito-summary-added
'((t :foreground "sky blue"
:weight bold))
"Face for the number of added books in a finito summary buffer."
:group 'finito)
(defface finito-summary-average-rating
'((t :foreground "gold"))
"Face for the average rating in a summary buffer."
:group 'finito)
(defface finito-review
'((t :foreground "grey"))
"Face for the content of a user book review."
:group 'finito)
;;; Custom variables
(defcustom finito-use-image-uris
nil
"If non-nil, insert raw image uris instead of downloading image thumbnails.
This option will disable the image cache. It's useful if for example you are
using `org-display-remote-inline-images'. In this case org would handle
display of images even if they are remote uris.
See also URL `https://github.com/LaurenceWarne/finito.el/issues/2' for more
information on using `org-display-remote-inline-images' with finito."
:group 'finito
:type 'boolean)
(defcustom finito-summary-show-recommended
t
"If non-nil, output a list of recommended sources for book searching."
:group 'finito
:type 'boolean)
(defcustom finito-show-descriptions-default
t
"If non-nil, write descriptions when displaying books in a finito buffer.
This can be overridden locally on a buffer by buffer basis via the
\"d\" key, or alternatively via the `finito--show-descriptions' local variable."
:group 'finito
:type 'boolean)
(defcustom finito-show-reviews-default
nil
"If non-nil, write reviews when displaying books in a finito buffer.
This can be overridden locally on a buffer by buffer basis via the
\"v\" key, or alternatively via the `finito--show-reviews' local variable."
:group 'finito
:type 'boolean)
;;; Constants
(defconst finito--summary-recommended-text
"** Looking for books?
- If you're interested in science fiction or fantasy check out the [[https://en.wikipedia.org/wiki/Hugo_Award_for_Best_Novel][Hugo Awards]] and the [[https://en.wikipedia.org/wiki/Nebula_Award_for_Best_Novel][Nebula Awards]]
- For general fiction there is the [[https://en.wikipedia.org/wiki/Pulitzer_Prize_for_Fiction][Pulitzer Prize]]
- You can ask for recommendations on [[https://www.reddit.com/r/books/][r/books]]
- Also check out [[https://openlibrary.org/][openlibrary.org]]")
;;; Internal variables
(defvar finito-show-description-alist nil)
(cl-pushnew 'finito-show-description-alist savehist-additional-variables)
(defvar finito-show-review-alist nil)
(cl-pushnew 'finito-show-review-alist savehist-additional-variables)
;;; Buffer local variables
(defvar-local finito--ewoc
nil
"The ewoc used to generate the text in finito buffers.
Its nodes should be alists of the form returned by
`finito--create-book-alist'.")
(defvar-local finito--collection
nil
"The name of the current collection.")
(defvar-local finito--collection-books-current-offset
nil
"Books offset for the current collection (for pagination).")
(defvar-local finito--collection-total-books
nil
"The total number of books in the current collection.")
(defvar-local finito--show-descriptions
nil
"An override that can be used to show/hide descriptions in all finito buffers.")
(defvar-local finito--show-reviews
nil
"An override that can be used to show/hide user reviews in all finito buffers.")
;;; Classes for working with buffers
(defclass finito-book-writer ()
((insert-order :initarg :books-offset
:initform '(title image author rating started-reading last-read description review)
:type list
:custom '(repeat symbol)
:documentation "The order of which to insert books attributes"))
"A class for writing book information to a buffer.")
(cl-defmethod finito-insert-book ((writer finito-book-writer) book-alist)
"Write BOOK-ALIST into the current buffer using WRITER.
BOOK-ALIST is an alist of the format returned by `finito--create-book-alist'"
(let-alist book-alist
(--each (oref writer insert-order)
(cond
((eq it 'title) (finito-insert-title writer .title))
((eq it 'image) (finito-insert-image
writer
(if finito-use-image-uris
.img-uri
.image-file-name)))
((eq it 'author) (finito-insert-author writer .authors))
((eq it 'rating) (when .rating (finito-insert-rating writer .rating)))
((eq it 'started-reading) (when .started-reading
(finito-insert-started-reading writer .started-reading)))
((eq it 'last-read) (when .last-read
(finito-insert-last-read writer .last-read)))
((eq it 'description) (finito-insert-description writer .description))
((eq it 'review) (finito-insert-review writer .review))))))
(cl-defmethod finito-insert-title ((_ finito-book-writer) title)
"Insert TITLE into the current buffer."
(insert "** " title "\n\n"))
(cl-defmethod finito-insert-image ((_ finito-book-writer) image)
"Insert IMAGE (an image file name) into the current buffer."
(insert "[[" image "]] "))
(cl-defmethod finito-insert-author ((_ finito-book-writer) authors)
"Insert AUTHORS into the current buffer."
(let ((authors-str (s-join ", " authors)))
(insert authors-str "\n\n")
(overlay-put (make-overlay (- (point) 2) (- (point) (length authors-str) 2))
'face
'finito-author-name)))
(cl-defmethod finito-insert-rating ((_ finito-book-writer) rating)
"Insert RATING into the current buffer."
(let ((rating-str (make-string (min rating 100) ?β
)))
(insert rating-str "\n")
(overlay-put (make-overlay (1- (point)) (- (point) (length rating-str) 1))
'face
'finito-rating)))
(cl-defmethod finito-insert-started-reading
((_ finito-book-writer) _started-reading)
"Insert STARTED-READING into the current buffer."
(let ((currently-reading-str "β In Progress β"))
(insert currently-reading-str "\n")
(overlay-put (make-overlay (1- (point)) (- (point)
(length currently-reading-str) 1))
'face
'finito-currently-reading)))
(cl-defmethod finito-insert-last-read
((_ finito-book-writer) last-read)
"Insert LAST-READ into the current buffer."
(let* ((last-read-str (concat "Last Read: " last-read)))
(insert last-read-str "\n")
(overlay-put (make-overlay (1- (point)) (- (point)
(length last-read-str) 1))
'face
'finito-last-read)))
(cl-defmethod finito-insert-description ((_ finito-book-writer) description)
"Insert DESCRIPTION into the current buffer."
(when (or (and (bound-and-true-p finito--collection)
(alist-get finito--collection
finito-show-description-alist
finito-show-descriptions-default
nil
'equal))
(bound-and-true-p finito--show-descriptions))
(insert description "\n")
(overlay-put (make-overlay (- (point) 2) (- (point) (length description) 2))
'face
'finito-book-descriptions)))
(cl-defmethod finito-insert-review ((_ finito-book-writer) review)
"Insert REVIEW into the current buffer."
(when (or (and (bound-and-true-p finito--collection)
(alist-get finito--collection
finito-show-review-alist
finito-show-reviews-default
nil
'equal))
(bound-and-true-p finito--show-reviews))
(when (and review (not (s-blank-p review)))
(let ((review-header "Review:"))
(insert review-header)
(overlay-put (make-overlay (point) (- (point) (length review-header)))
'face
'finito-book-review-header)
(insert " " review "\n")
(overlay-put (make-overlay (- (point) 1) (- (point) (length review) 1))
'face
'finito-book-reviews)))))
(cl-defmethod finito-use-pagination ((_ finito-book-writer))
"Return non-nil if pagination should be used alongside this writer."
t)
(defclass finito-minimal-book-writer (finito-book-writer)
((insert-order :initform '(title last-read author rating started-reading)))
"A class for writing book information to a buffer.")
(cl-defmethod finito-insert-title ((_ finito-minimal-book-writer) title)
"Insert TITLE into the current buffer."
(insert "** " title " "))
(cl-defmethod finito-insert-author ((_ finito-minimal-book-writer) authors)
"Insert AUTHORS into the current buffer."
(let ((authors-str (concat (s-join ", " authors))))
(insert authors-str " ")
(overlay-put (make-overlay (1- (point)) (- (point) (length authors-str) 1))
'face
'finito-author-name)))
(cl-defmethod finito-insert-rating ((_ finito-minimal-book-writer) rating)
"Insert RATING into the current buffer."
(let ((rating-str (format "%sβ
" rating)))
(insert rating-str " ")
(overlay-put (make-overlay (1- (point)) (- (point) (length rating-str) 1))
'face
'finito-rating)))
(cl-defmethod finito-insert-started-reading
((_ finito-minimal-book-writer) _started-reading)
"Insert STARTED-READING into the current buffer."
(let ((currently-reading-str "In Progress"))
(insert currently-reading-str)
(overlay-put (make-overlay (point) (- (point) (length currently-reading-str)))
'face
'finito-currently-reading)))
(cl-defmethod finito-insert-last-read ((_ finito-minimal-book-writer) _last-read)
"Insert LAST-READ into the current buffer."
(insert "π "))
(cl-defmethod finito-use-pagination ((_ finito-minimal-book-writer))
"Return non-nil if pagination should be used alongside this writer."
nil)
(defcustom finito-detailed-writer-instance
(finito-book-writer)
"Like `finito-writer-instance', but used only when minimal mode is inactive."
:group 'finito
:type 'object)
(defcustom finito-minimal-writer-instance
(finito-minimal-book-writer)
"Like `finito-writer-instance', but used only when minimal mode is active."
:group 'finito
:type 'object)
(defcustom finito-writer-instance
finito-detailed-writer-instance
"`finito-book-writer' instance used to write books in finito buffers."
:group 'finito
:type 'object)
(defclass finito-buffer-info ()
((title :initarg :title
:type string
:custom string
:documentation "The title of the finito buffer.")
(buf-name :initarg :buf-name
:type string
:custom string
:documentation "The name of the finito buffer.")
(buf-name-unique :initarg :buf-name-unique
:type boolean
:custom boolean
:documentation
"If non-nil delete buffers of the same name.")
(mode :initarg :mode
:type function
:custom function
:documentation "The mode the finito buffer should use."))
"A class for holding information about a finito buffer.")
(cl-defmethod finito-init-buffer ((buffer-info finito-buffer-info))
"Initialise the current buffer according to the properties of BUFFER-INFO."
(funcall (oref buffer-info mode)))
(cl-defmethod finito-title-string ((buffer-info finito-buffer-info))
"Get the title string using the properties of BUFFER-INFO."
(format "* %s\n" (oref buffer-info title)))
(defclass finito-collection-buffer-info (finito-buffer-info)
((books-offset :initarg :books-offset
:type integer
:custom integer
:documentation "The current books offset of the collection")
(total-books :initarg :total-books
:type integer
:custom integer
:documentation "The total number of books in the current collection"))
"A class for holding information about a finito collection buffer.")
(cl-defmethod finito-init-buffer ((buffer-info finito-collection-buffer-info))
"Initialise the current buffer according to the properties of BUFFER-INFO."
(cl-call-next-method)
(setq finito--collection (oref buffer-info title)
finito--collection-books-current-offset (oref buffer-info books-offset)
finito--collection-total-books (oref buffer-info total-books)))
(cl-defmethod finito-title-string ((buffer-info finito-collection-buffer-info))
"Get the title string using the properties of BUFFER-INFO."
(let ((base (concat "* " (oref buffer-info title) "\n")))
(if (finito--use-pagination)
(let* ((books-offset (oref buffer-info books-offset))
(total-books (oref buffer-info total-books))
(can-go-next (< (+ books-offset finito-collection-books-limit) total-books))
(can-go-previous (>= (- books-offset finito-collection-books-limit) 0)))
(concat base "\n" (format "Showing ~%s~-~%s~ of =%s= books"
(min (1+ books-offset) total-books)
(min total-books (+ books-offset finito-collection-books-limit))
total-books)
(and can-go-previous " ~P~: Previous page")
(and can-go-previous can-go-next ",")
(and can-go-next " ~N~: Next page")
"\n"))
base)))
;;; Misc functions
(defun finito--use-pagination ()
"Return non-nil if pagination should be used."
(finito-use-pagination finito-writer-instance))
(provide 'finito-buffer)
;;; finito-buffer.el ends here