-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathorg-fc-review.el
452 lines (384 loc) · 13.7 KB
/
org-fc-review.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
441
442
443
444
445
446
447
448
449
450
451
452
;;; org-fc-review.el --- Review mode for org-fc -*- lexical-binding: t; -*-
;; Copyright (C) 2020-2024 Leon Rische
;; Author: Leon Rische <[email protected]>
;; 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:
;;
;; During review, due cards are presented one after another and the
;; user is asked to rate each card.
;;
;; Cards are reviewed by
;; 1. opening the file they are in
;; 2. calling the setup function for the card type
;; 3. switch to review-flip-mode
;; 4. calling the flip function for the card type
;; 5. switch to review-rate-mode
;; 6. updating the review data based on the rating
;;
;;; Code:
(require 'eieio)
(require 'org-fc-core)
(require 'org-fc-review-data)
(require 'org-fc-scheduler)
;;; Hooks
(defcustom org-fc-before-setup-hook '()
"Functions run before a card is set up for review."
:type 'hook
:group 'org-fc)
(defcustom org-fc-after-setup-hook '()
"Functions run after a card is set up for review."
:type 'hook
:group 'org-fc)
(defcustom org-fc-after-flip-hook '()
"Functions run after a card is flipped during review."
:type 'hook
:group 'org-fc)
(defcustom org-fc-before-review-hook '()
"Functions run when a review session is started."
:type 'hook
:group 'org-fc)
(defcustom org-fc-after-review-hook '()
"Functions run when a review session ends / is quit."
:type 'hook
:group 'org-fc)
(defcustom org-fc-review-hide-title-in-header-line nil
"Whether or not to hide the file title in review header line.
Hide title for individual cards by adding the :notitle: tag."
:type 'boolean
:group 'org-fc)
;;; Variables
(defvar org-fc-review--session nil
"Current review session.")
(defvar org-fc-review--timestamp nil
"Time the last card was flipped.
Used to calculate the time needed for reviewing a card.")
(defvar org-fc-reviewing-existing-buffer nil
"Track if the current buffer was open before the review.")
(make-variable-buffer-local 'org-fc-reviewing-existing-buffer)
;;; Main Review Functions
;;;###autoload
(defun org-fc-review (context)
"Start a review session for all cards in CONTEXT.
Called interactively, prompt for the context.
Valid contexts:
- 'all, all cards in `org-fc-directories'
- 'buffer, all cards in the current buffer
- a list of paths"
(interactive (list (org-fc-select-context)))
(if org-fc-review--session
(when (yes-or-no-p "Flashcards are already being reviewed. Resume? ")
(org-fc-review-resume))
(let* ((index (org-fc-index context))
(cards (org-fc-index-filter-due index))
(order
(or
(plist-get context :order)
(if org-fc-shuffle-positions 'shuffled 'ordered)))
(scheduler
(cl-case order
(ordered (org-fc-scheduler))
(shuffled (org-fc-scheduler-shuffled))
(t (error "Unknown review order %s" order)))))
(if (null cards)
(message "No cards due right now")
(progn
(org-fc-scheduler-init scheduler cards)
(setq org-fc-review--session
(org-fc-make-review-session scheduler))
(run-hooks 'org-fc-before-review-hook)
(org-fc-review-next-card))))))
(defun org-fc-review-resume ()
"Resume review session, if it was paused."
(interactive)
(if org-fc-review--session
(progn
(org-fc-review-edit-mode -1)
(org-fc-review-next-card 'resuming))
(message "No session to resume to")))
;;;###autoload
(defun org-fc-review-buffer ()
"Review due cards in the current buffer."
(interactive)
(org-fc-review org-fc-context-buffer))
;;;###autoload
(defun org-fc-review-all ()
"Review all due cards."
(interactive)
(org-fc-review org-fc-context-all))
(cl-defmethod org-fc-review-item ((position org-fc-position) resuming)
"Review logic for a POSITION of a card."
(let* ((card (oref position card))
(path (oref (oref card file) path))
(id (oref card id))
(type (oref card type))
(name (oref position name))
(buffer (find-buffer-visiting path)))
(with-current-buffer (find-file path)
(unless resuming
;; If buffer was already open, don't kill it after rating the card
(if buffer
(setq-local org-fc-reviewing-existing-buffer t)
(setq-local org-fc-reviewing-existing-buffer nil))
(org-fc-set-header-line))
(org-fc-id-goto id path)
(org-fc-indent)
;; Make sure the headline the card is in is expanded
(org-reveal)
(org-fc-narrow)
(org-fc-hide-keyword-times)
(org-fc-hide-drawers)
(org-fc-show-latex)
(org-display-inline-images)
(run-hooks 'org-fc-before-setup-hook)
(setq org-fc-review--timestamp (time-to-seconds (current-time)))
(let ((step (funcall (org-fc-type-setup-fn type) name)))
(run-hooks 'org-fc-after-setup-hook)
;; If the card has a no-noop flip function,
;; skip to rate-mode
(let ((flip-fn (org-fc-type-flip-fn type)))
(if (or
(eq step 'rate)
(null flip-fn)
(eq flip-fn #'org-fc-noop))
(org-fc-review-rate-mode 1)
(org-fc-review-flip-mode 1)))))))
(defun org-fc-review-next-card (&optional resuming)
"Review the next card of the current session.
If RESUMING is non-nil, some parts of the buffer setup are skipped."
(if-let ((pos
(org-fc-scheduler-next-position
(oref org-fc-review--session scheduler))))
(condition-case err
(progn
(setf (oref org-fc-review--session current-item) pos)
(org-fc-review-item pos resuming))
(error
(org-fc-review-quit)
(signal (car err) (cdr err))))
(message "Review Done")
(org-fc-review-quit)))
(defmacro org-fc-review-with-current-item (var &rest body)
"Evaluate BODY with the current card bound to VAR.
Before evaluating BODY, check if the heading at point has the
same ID as the current card in the session."
(declare (indent defun))
`(when org-fc-review--session
(if-let ((,var (oref org-fc-review--session current-item)))
(if (string= (oref (oref ,var card) id) (org-id-get))
(progn ,@body)
(message "Flashcard ID mismatch"))
(message "No flashcard review is in progress"))))
(defun org-fc-review-flip ()
"Flip the current flashcard."
(interactive)
(condition-case err
(org-fc-review-with-current-item pos
(let ((type (oref (oref pos card) type)))
(funcall (org-fc-type-flip-fn type))
(run-hooks 'org-fc-after-flip-hook)
(org-fc-review-rate-mode)))
(error
(org-fc-review-quit)
(signal (car err) (cdr err)))))
(defun org-fc-review-rate (rating)
"Rate the card at point with RATING."
(interactive)
(condition-case err
(org-fc-review-with-current-item pos
(let* ((now (time-to-seconds (current-time)))
(delta (- now org-fc-review--timestamp)))
(org-fc-review-update-data pos rating delta)
(when (and (eq rating 'again) org-fc-append-failed-cards)
(org-fc-scheduler-push-position
(oref org-fc-review--session scheduler)
pos))
(save-buffer)
(if org-fc-reviewing-existing-buffer
(org-fc-review-reset)
(kill-buffer))
(org-fc-review-next-card)))
(error
(org-fc-review-quit)
(signal (car err) (cdr err)))))
(defun org-fc-review-rate-again ()
"Rate the card at point with `again'."
(interactive)
(org-fc-review-rate 'again))
(defun org-fc-review-rate-hard ()
"Rate the card at point with `hard'."
(interactive)
(org-fc-review-rate 'hard))
(defun org-fc-review-rate-good ()
"Rate the card at point with `good'."
(interactive)
(org-fc-review-rate 'good))
(defun org-fc-review-rate-easy ()
"Rate the card at point with `easy'."
(interactive)
(org-fc-review-rate 'easy))
(defun org-fc-review-skip-card ()
"Skip card and proceed to next."
(interactive)
(org-fc-review-reset)
(org-fc-review-next-card))
(defun org-fc-review-suspend-card ()
"Suspend card and proceed to next."
(interactive)
(org-fc-suspend-card)
(org-fc-review-with-current-item pos
(org-fc-scheduler-remove-siblings
(oref org-fc-review--session scheduler) pos))
(org-fc-review-reset)
(org-fc-review-next-card))
(defun org-fc-review-update-data (position rating delta)
"Use the card's spacing algorithm to update the review data of a
POSITION.
RATING is a review rating and DELTA the time in seconds between
showing and rating the card."
(org-fc-with-point-at-entry
(let ((algo (oref (oref position card) algo)))
(org-fc-algo-update-review-data algo position rating delta))))
(defun org-fc-review-reset ()
"Reset the buffer to its state before the review."
(org-fc-review-rate-mode -1)
(org-fc-review-flip-mode -1)
(org-fc-review-edit-mode -1)
(org-fc-reset-header-line)
(org-fc-remove-overlays)
(widen))
;;;###autoload
(defun org-fc-review-quit ()
"Quit the review, remove all overlays from the buffer."
(interactive)
(org-fc-review-reset)
(run-hooks 'org-fc-after-review-hook)
(setq org-fc-review--session nil))
;;;###autoload
(defun org-fc-review-edit ()
"Edit current flashcard.
Pauses the review, unnarrows the buffer and activates
`org-fc-edit-mode'."
(interactive)
(widen)
(org-fc-remove-overlays)
;; Queue the current flashcard so it's reviewed a second time
(org-fc-scheduler-push-position
(oref org-fc-review--session scheduler)
(oref org-fc-review--session current-item))
(setf (oref org-fc-review--session paused) t)
(setf (oref org-fc-review--session current-item) nil)
(org-fc-review-edit-mode 1))
;;; Sessions
(defclass org-fc-review-session ()
((current-item :initform nil)
(paused :initform nil :initarg :paused)
(scheduler
:initform (org-fc-scheduler)
:initarg :scheduler)))
(defun org-fc-make-review-session (scheduler)
"Create a new review session with SCHEDULER."
(org-fc-review-session
:scheduler scheduler))
;;; Header Line
(defvar org-fc-original-header-line-format nil
"`header-line-format' before it was set by org-fc.")
(defun org-fc-set-header-line ()
"Set the header-line for review."
(let* ((remaining (1+ (length
(oref
(oref org-fc-review--session scheduler)
positions))))
(current (oref org-fc-review--session current-item))
(title
(unless (or org-fc-review-hide-title-in-header-line
(member "notitle" (plist-get current :tags)))
(plist-get current :filetitle))))
(setq org-fc-original-header-line-format header-line-format)
(setq-local
header-line-format
`((org-fc-review-flip-mode "Flip")
(org-fc-review-rate-mode "Rate")
(org-fc-review-edit-mode "Edit")
,(format " (%d) " remaining)
,title))))
(defun org-fc-reset-header-line ()
"Reset the header-line to its original value."
(setq-local header-line-format org-fc-original-header-line-format))
;;; Modes
(defvar org-fc-review-flip-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'org-fc-review-flip)
(define-key map (kbd "q") 'org-fc-review-quit)
(define-key map (kbd "p") 'org-fc-review-edit)
(define-key map (kbd "s") 'org-fc-review-suspend-card)
map)
"Keymap for `org-fc-flip-mode'.")
(define-minor-mode org-fc-review-flip-mode
"Minor mode for flipping flashcards.
\\{org-fc-review-flip-mode-map}"
:init-value nil
:lighter " fc-flip"
:keymap org-fc-review-flip-mode-map
:group 'org-fc
(when org-fc-review-flip-mode
;; Make sure only one of the modes is active at a time
(org-fc-review-rate-mode -1)
;; Make sure we're in org mode and there is an active review session
(unless (and (derived-mode-p 'org-mode) org-fc-review--session)
(org-fc-review-flip-mode -1))))
;;;;; Rate Mode
(defvar org-fc-review-rate-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "a") 'org-fc-review-rate-again)
(define-key map (kbd "h") 'org-fc-review-rate-hard)
(define-key map (kbd "g") 'org-fc-review-rate-good)
(define-key map (kbd "e") 'org-fc-review-rate-easy)
(define-key map (kbd "s") 'org-fc-review-suspend-card)
(define-key map (kbd "p") 'org-fc-review-edit)
(define-key map (kbd "q") 'org-fc-review-quit)
map)
"Keymap for `org-fc-rate-mode'.")
(define-minor-mode org-fc-review-rate-mode
"Minor mode for rating flashcards.
\\{org-fc-review-rate-mode-map}"
:init-value nil
:lighter " fc-rate"
:keymap org-fc-review-rate-mode-map
:group 'org-fc
(when org-fc-review-rate-mode
;; Make sure only one of the modes is active at a time
(org-fc-review-flip-mode -1)
;; Make sure we're in org mode and there is an active review session
(unless (and (derived-mode-p 'org-mode) org-fc-review--session)
(org-fc-review-rate-mode -1))))
(defvar org-fc-review-edit-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") 'org-fc-review-resume)
(define-key map (kbd "C-c C-k") 'org-fc-review-quit)
map)
"Keymap for `org-fc-edit-mode'.")
(define-minor-mode org-fc-review-edit-mode
"Minor mode for editing flashcards.
\\{org-fc-review-edit-mode-map}"
:init-value nil
:lighter " fc-edit"
:keymap org-fc-review-edit-mode-map
:group 'org-fc
(when org-fc-review-edit-mode
(org-fc-review-flip-mode -1)
(org-fc-review-rate-mode -1)
;; Make sure we're in org mode and there is an active review session
(unless (and (derived-mode-p 'org-mode) org-fc-review--session)
(org-fc-review-edit-mode -1))))
;;; Footer
(provide 'org-fc-review)
;;; org-fc-review.el ends here