summaryrefslogtreecommitdiff
path: root/org-fc-review.el
blob: 64b72d1fcdc13110cb318e9a58b914cb50849e4e (about) (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
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

;;; Configuration

(defcustom org-fc-review-data-drawer "REVIEW_DATA"
  "Name of the drawer used to store review data."
  :type 'string
  :group 'org-fc)

;;; Session Management

(defclass org-fc-review-session ()
  ((current-item :initform nil)
   (ratings :initform nil)
   (cards :initform nil)))

(defun org-fc-session-cards-pending-p (session)
  (not (null (oref session cards))))

(defun org-fc-session-pop-next-card (session)
  (let ((card (pop (oref session cards))))
    (setf (oref session current-item) card)
    card))

(defun org-fc-session-add-rating (session rating)
  (push rating (oref session ratings)))

(defun org-fc-session-stats-string (session)
  (with-slots (ratings) session
    (let ((len (length ratings)))
      (if (plusp len)
          (format "%.2f again, %.2f hard, %.2f good, %.2f easy"
                  (/ (* 100.0 (count 'again ratings)) len)
                  (/ (* 100.0 (count 'hard ratings)) len)
                  (/ (* 100.0 (count 'good ratings)) len)
                  (/ (* 100.0 (count 'easy ratings)) len))
        "No ratings yet"))))

(defvar org-fc-review--current-session nil
  "Current review session.")

;;; Helper Functions

(defun org-fc-review-next-time (next-interval)
  "Generate an org-mode timestamp NEXT-INTERVAL days from now"
  (let ((seconds (* next-interval 60 60 24))
        (now (time-to-seconds)))
    (format-time-string
     org-fc-timestamp-format
     (seconds-to-time (+ now seconds))
     "UTC0")))

(defun org-fc-id-goto (id file)
  "File-scoped variant of `org-id-goto'."
  (let ((position (org-id-find-id-in-file id file)))
    (if position
        (goto-char (cdr position))
      (error "ID %s not found in %s" id file))))

;;; Reviewing Cards

(defun org-fc-review--context (context)
  (let* ((session (make-instance 'org-fc-review-session))
         (cards (org-fc-due-positions context)))
    (if org-fc-review--current-session
        (message "Flashcards are already being reviewed")
      (if (null cards)
          (message "No cards due right now")
        (progn
          (setq org-fc-review--current-session session)
          (setf (oref session cards) cards)
          (org-fc-review-next-card))))))

(defun org-fc-review-buffer ()
  (interactive)
  (org-fc-review--context 'buffer))

(defun org-fc-review-all ()
  (interactive)
  (org-fc-review--context 'all))

(defun org-fc-review-next-card ()
  "Review the next card of the current session"
  (if (org-fc-session-cards-pending-p org-fc-review--current-session)
      (let* ((card (org-fc-session-pop-next-card org-fc-review--current-session))
             (path (plist-get card :path))
             (id (plist-get card :id))
             (type (plist-get card :type))
             (position (plist-get card :position)))
        ;; TODO: org-id-goto already jumps to the file
        ;; Check if buffer was already open,
        ;; set flag in session (kill buffer?)
        (with-current-buffer (find-file path)
          (goto-char (point-min))
          (org-fc-id-goto id path)
          (org-fc-show-all)
          (org-fc-narrow-tree)
          (org-fc-hide-drawers)
          (org-fc-show-latex)
          (outline-hide-subtree)
          (funcall (org-fc-type-setup-fn type) position)))
    (progn
      (message "Review Done")
      (setq org-fc-review--current-session nil)
      (org-fc-show-all))))

(defhydra org-fc-review-rate-hydra ()
  "
%(length (oref org-fc-review--current-session cards)) cards remaining
%s(org-fc-session-stats-string org-fc-review--current-session)

"
  ("a" (org-fc-review-rate-card 'again) "Rate as again" :exit t)
  ("h" (org-fc-review-rate-card 'hard) "Rate as hard" :exit t)
  ("g" (org-fc-review-rate-card 'good) "Rate as good" :exit t)
  ("e" (org-fc-review-rate-card 'easy) "Rate as easy" :exit t)
  ("q" org-fc-review-quit "Quit" :exit t))

(defhydra org-fc-review-flip-hydra ()
  "
%(length (oref org-fc-review--current-session cards)) cards remaining
%s(org-fc-session-stats-string org-fc-review--current-session)

"
  ("RET" org-fc-review-flip "Flip" :exit t)
  ;; Neo-Layout ergonomics
  ("n" org-fc-review-flip "Flip" :exit t)
  ("q" org-fc-review-quit "Quit" :exit t))

(defmacro org-fc-review-with-current-item (var &rest body)
  "Helper macro for functions that work with the current item of
a review session."
  (declare (indent defun))
  `(if org-fc-review--current-session
      (-if-let (,var (oref org-fc-review--current-session current-item))
        (if (string= (plist-get ,var :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)
  (org-fc-review-with-current-item card
    (let ((type (plist-get card :type)))
      (funcall (org-fc-type-flip-fn type)))))

;; TODO: Remove -card suffix
(defun org-fc-review-rate-card (rating)
  "Rate the card at point if it has the same id as the current
  card of the review session."
  (interactive)
  (org-fc-review-with-current-item card
    (let ((path (plist-get card :path))
          (id (plist-get card :id))
          (position (plist-get card :position)))
      (org-fc-session-add-rating org-fc-review--current-session rating)
      (org-fc-review-update-data path id position rating)
      (save-buffer)
      ;; TODO: Conditional kill
      (kill-buffer)
      (org-fc-review-next-card))))

(defun org-fc-review-update-data (path id position rating)
  (save-excursion
    (org-fc-goto-entry-heading)
    (let* ((data (org-fc-get-review-data))
           (current (assoc position data #'string=)))
      (unless current
        (error "No review data found for this position"))
      (let ((ease (string-to-number (second current)))
            (box (string-to-number (third current)))
            (interval (string-to-number (fourth current))))
        (org-fc-review-history-add
         (list
          (org-fc-timestamp-now)
          path
          id
          position
          (format "%.2f" ease)
          (format "%d" box)
          (format "%.2f" interval)
          (symbol-name rating)))
        (destructuring-bind (next-ease next-box next-interval)
            (org-fc-sm2-next-parameters ease box interval rating)
          (setcdr
           current
           (list (format "%.2f" next-ease)
                 (number-to-string next-box)
                 (format "%.2f" next-interval)
                 (org-fc-review-next-time next-interval)))
          (org-fc-set-review-data data))))))

(defun org-fc-review-quit ()
  "Quit the review, remove all overlays from the buffer."
  (interactive)
  (setq org-fc-review--current-session nil)
  (org-fc-show-all))

;;; Writing Review History

(defun org-fc-review-history-add (elements)
  "Add ELEMENTS to the history csv file."
  (unless (and (boundp 'org-fc-demo-mode) org-fc-demo-mode)
    (append-to-file
     (concat
      (mapconcat #'identity elements "\t")
      "\n")
     nil
     org-fc-review-history-file)))

;;; Reading / Writing Review Data

;; Based on `org-log-beginning'
(defun org-fc-review-data-position (&optional create)
  "Return (BEGINNING . END) points of the review data drawer.
When optional argument CREATE is non-nil, the function creates a
drawer, if necessary.  Returned position ignores narrowing.

BEGINNING is the start of the first line inside the drawer,
END is the start of the line with :END: on it."
  (org-with-wide-buffer
   (org-end-of-meta-data)
   (let ((regexp (concat "^[ \t]*:" (regexp-quote org-fc-review-data-drawer) ":[ \t]*$"))
         (end (if (org-at-heading-p) (point)
                (save-excursion (outline-next-heading) (point))))
         (case-fold-search t))
     (catch 'exit
       ;; Try to find existing drawer.
       (while (re-search-forward regexp end t)
         (let ((element (org-element-at-point)))
           (when (eq (org-element-type element) 'drawer)
             (throw 'exit
                    (cons (org-element-property :contents-begin element)
                          (org-element-property :contents-end element))))))
       ;; No drawer found.  Create one, if permitted.
       (when create
         (unless (bolp) (insert "\n"))
         (let ((beg (point)))
           (insert ":" org-fc-review-data-drawer ":\n:END:\n")
           (org-indent-region beg (point)))
         (cons
          (line-beginning-position 0)
          (line-beginning-position 0)))))))


(defun org-fc-get-review-data ()
  (let ((position (org-fc-review-data-position nil)))
    (if position
        (save-excursion
          (goto-char (car position))
          (cddr (org-table-to-lisp))))))

(defun org-fc-set-review-data (data)
  (save-excursion
    (let ((position (org-fc-review-data-position t)))
      (kill-region (car position) (cdr position))
      (goto-char (car position))
      (insert "| position | ease | box | interval | due |\n")
      (insert "|-|-|-|-|-|\n")
      (loop for datum in data do
            (insert
             "| "
             (mapconcat (lambda (x) (format "%s" x)) datum " | ")
             " |\n"))
      (org-table-align))))

(defun org-fc-review-data-default (position)
  (list position org-fc-sm2-initial-ease 0 0
        (org-fc-timestamp-now)))

(defun org-fc-review-data-update (positions)
  "Update review data to POSITIONS.
If a doesn't exist already, it is initialized with default
values.  Entries in the table not contained in POSITIONS are
removed."
  (unless (and (boundp 'org-fc-demo-mode) org-fc-demo-mode)
      (let ((old-data (org-fc-get-review-data)))
        (org-fc-set-review-data
         (mapcar
          (lambda (pos)
            (or
             (assoc pos old-data #'string=)
             (org-fc-review-data-default pos)))
          positions)))))

;;; Exports

(provide 'org-fc-review)