-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-drill.el
3863 lines (3522 loc) · 155 KB
/
org-drill.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; org-drill.el --- Self-testing using spaced repetition -*- lexical-binding: t -*-
;;; Header:
;; Maintainer: Phillip Lord <[email protected]>
;; Author: Paul Sexton <[email protected]>
;; Version: 2.7.0
;; Package-Requires: ((emacs "25.3") (seq "2.14") (org "9.3") (persist "0.3"))
;; Keywords: games, outlines, multimedia
;; URL: https://gitlab.com/phillord/org-drill/issues
;;
;; This file is not part of GNU Emacs.
;;
;; Copyright (C) 2018-2019 Phillip Lord
;; Copyright (C) 2010-2015 Paul Sexton
;;
;;
;; 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:
;;
;; Within an Org mode outline or outlines, headings and associated content are
;; treated as "flashcards". Spaced repetition algorithms are used to conduct
;; interactive "drill sessions", where a selection of these flashcards is
;; presented to the student in random order. The student rates his or her
;; recall of each item, and this information is used to schedule the item for
;; later revision.
;;
;; Each drill session can be restricted to topics in the current buffer
;; (default), one or several files, all agenda files, or a subtree. A single
;; topic can also be tested.
;;
;; Different "card types" can be defined, which present their information to
;; the student in different ways.
;;
;; See the file README.org for more detailed documentation.
;;; Code:
(require 'cl-lib)
(require 'eieio)
(require 'org)
(require 'org-agenda)
(require 'org-id)
(require 'persist)
(require 'seq)
(defgroup org-drill nil
"Options concerning interactive drill sessions in Org mode (org-drill)."
:tag "Org-Drill"
:group 'org-link)
(defcustom org-drill-question-tag
"drill"
"Tag for topics which are review topics."
:group 'org-drill
:type 'string)
(defcustom org-drill-maximum-items-per-session
30
"Each drill session will present at most this many topics for review.
Nil means unlimited."
:group 'org-drill
:type '(choice integer (const nil)))
(defcustom org-drill-maximum-duration
20
"Maximum duration of a drill session, in minutes.
Nil means unlimited."
:group 'org-drill
:type '(choice integer (const nil)))
(defcustom org-drill-item-count-includes-failed-items-p
nil
"If non-nil, count failed items in overall count.
If nil (default), only successful items count towards this
total."
:group 'org-drill
:type 'boolean)
(defcustom org-drill-failure-quality
2
"Lower bound for an recall to be marked as failure.
If the quality of recall for an item is this number or lower,
it is regarded as an unambiguous failure, and the repetition
interval for the card is reset to 0 days. If the quality is higher
than this number, it is regarded as successfully recalled, but the
time interval to the next repetition will be lowered if the quality
was near to a fail.
By default this is 2, for SuperMemo-like behaviour. For
Mnemosyne-like behaviour, set it to 1. Other values are not
really sensible."
:group 'org-drill
:type '(choice (const 2) (const 1)))
(defcustom org-drill-forgetting-index
10
"The maximum percentage of items that can be forgotten before a warning.
What percentage of items do you consider it is 'acceptable' to
forget each drill session? The default is 10%. A warning message
is displayed at the end of the session if the percentage forgotten
climbs above this number."
:group 'org-drill
:type 'integer)
(defcustom org-drill-leech-failure-threshold
15
"Threshold before a item is defined as a leech.
If an item is forgotten more than this many times, it is tagged
as a 'leech' item."
:group 'org-drill
:type '(choice integer (const nil)))
(defcustom org-drill-leech-method
'skip
"How should 'leech items' be handled during drill sessions?
Possible values:
- nil :: Leech items are treated the same as normal items.
- skip :: Leech items are not included in drill sessions.
- warn :: Leech items are still included in drill sessions,
but a warning message is printed when each leech item is
presented."
:group 'org-drill
:type '(choice (const warn) (const skip) (const nil)))
(defface org-drill-visible-cloze-face
'((t (:foreground "darkseagreen")))
"The face used to hide the contents of cloze phrases."
:group 'org-drill)
(defface org-drill-visible-cloze-hint-face
'((t (:foreground "dark slate blue")))
"The face used to hide the contents of cloze phrases."
:group 'org-drill)
(defface org-drill-hidden-cloze-face
'((t (:foreground "deep sky blue" :background "blue")))
"The face used to hide the contents of cloze phrases."
:group 'org-drill)
(defcustom org-drill-use-visible-cloze-face-p
nil
"Highlight cloze-deleted text."
:group 'org-drill
:type 'boolean)
(defcustom org-drill-hide-item-headings-p
nil
"If non-nil, conceal headings during a drill session.
You may want to enable this behaviour if item headings or tags
contain information that could 'give away' the answer."
:group 'org-drill
:type 'boolean)
(defcustom org-drill-new-count-color
"royal blue"
"Foreground colour for remaining new items."
:group 'org-drill
:type 'color)
(defcustom org-drill-mature-count-color
"green"
"Foreground colour for remaining mature items.
Mature items are due for review, but are not new."
:group 'org-drill
:type 'color)
(defcustom org-drill-failed-count-color
"red"
"Foreground colour for remaining failed items."
:group 'org-drill
:type 'color)
(defcustom org-drill-done-count-color
"sienna"
"Foreground colour for reviewed items."
:group 'org-drill
:type 'color)
(defcustom org-drill-left-cloze-delimiter
"["
"String used within org buffers to delimit cloze deletions."
:group 'org-drill
:type 'string)
(defcustom org-drill-right-cloze-delimiter
"]"
"String used within org buffers to delimit cloze deletions."
:group 'org-drill
:type 'string)
(setplist 'org-drill-cloze-overlay-defaults
`(display ,(format "%s...%s"
org-drill-left-cloze-delimiter
org-drill-right-cloze-delimiter)
face org-drill-hidden-cloze-face
window t))
(setplist 'org-drill-hidden-text-overlay
'(invisible t))
(setplist 'org-drill-replaced-text-overlay
'(display "Replaced text"
face default
window t))
(add-hook 'org-font-lock-set-keywords-hook 'org-drill-add-cloze-fontification)
(defvar org-drill-hint-separator "||"
"Delimiter in cloze expression for hints.")
(defun org-drill--compute-cloze-regexp ()
"Return a regexp that detects clozes."
(concat "\\("
(regexp-quote org-drill-left-cloze-delimiter)
"[[:cntrl:][:graph:][:space:]]+?\\)\\(\\|"
(regexp-quote org-drill-hint-separator)
".+?\\)\\("
(regexp-quote org-drill-right-cloze-delimiter)
"\\)"))
(defun org-drill--compute-cloze-keywords ()
"Return a fontification spec that detects cloze keywords."
(list (list (org-drill--compute-cloze-regexp)
(cl-copy-list '(1 'org-drill-visible-cloze-face nil))
(cl-copy-list '(2 'org-drill-visible-cloze-hint-face t))
(cl-copy-list '(3 'org-drill-visible-cloze-face nil)))))
(defvar-local org-drill-cloze-regexp
(org-drill--compute-cloze-regexp)
"Regexp that detects cloze.
This is buffer-local variable.")
(defvar-local org-drill-cloze-keywords
(org-drill--compute-cloze-keywords)
"Fontification form for cloze.
This is a buffer-local variable.")
;; Variables defining what keys can be pressed during drill sessions to quit the
;; session, edit the item, etc.
(defvar org-drill--quit-key ?q
"Character to quit the session.")
(defvar org-drill--edit-key ?e
"Character to suspend the session.")
(defvar org-drill--help-key ??
"Character to show help.")
(defvar org-drill--skip-key ?s
"Character to skip to the next item.")
(defvar org-drill--tags-key ?t
"Character to edit the tags.")
(defcustom org-drill-card-type-alist
'((nil org-drill-present-simple-card)
("simple" org-drill-present-simple-card)
("simpletyped" org-drill-present-simple-card-with-typed-answer)
("twosided" org-drill-present-two-sided-card nil t)
("multisided" org-drill-present-multi-sided-card nil t)
("hide1cloze" org-drill-present-multicloze-hide1)
("hide2cloze" org-drill-present-multicloze-hide2)
("show1cloze" org-drill-present-multicloze-show1)
("show2cloze" org-drill-present-multicloze-show2)
("multicloze" org-drill-present-multicloze-hide1)
("hidefirst" org-drill-present-multicloze-hide-first)
("hidelast" org-drill-present-multicloze-hide-last)
("hide1_firstmore" org-drill-present-multicloze-hide1-firstmore)
("show1_lastmore" org-drill-present-multicloze-show1-lastmore)
("show1_firstless" org-drill-present-multicloze-show1-firstless)
("conjugate"
org-drill-present-verb-conjugation
org-drill-show-answer-verb-conjugation)
("decline_noun"
org-drill-present-noun-declension
org-drill-show-answer-noun-declension)
("spanish_verb" org-drill-present-spanish-verb)
("translate_number" org-drill-present-translate-number))
"Alist associating card types with presentation functions.
Each entry in the alist takes the form:
;;; (CARDTYPE QUESTION-FN [ANSWER-FN DRILL-EMPTY-P])
Where CARDTYPE is a string or nil (for default), and QUESTION-FN
is a function which takes no arguments and returns a boolean
value.
When supplied, ANSWER-FN is a function that takes one argument --
that argument is a function of no arguments, which when called,
prompts the user to rate their recall and performs rescheduling
of the drill item. ANSWER-FN is called with the point on the
active item's heading, just prior to displaying the item's
'answer'. It can therefore be used to modify the appearance of
the answer. ANSWER-FN must call its argument before returning.
When supplied, DRILL-EMPTY-P is a boolean value, default nil.
When non-nil, cards of this type will be presented during tests
even if their bodies are empty."
:group 'org-drill
:type '(alist :key-type (choice string (const nil))
:value-type function))
(defcustom org-drill-card-tags-alist
'(("explain" nil org-drill-explain-answer-presenter
org-drill-explain-cleaner))
"Alist associating tags with presentation functions.
The alist is of the form (TAG QUESTION-PRESENTER ANSWER-PRESENTER CLEANER).
When a card with the relevant TAG is tested, QUESTION-PRESENTER
will be called when the card is displayed to the user,
ANSWER-PRESENTER will be called with point in the entry when the
answer is displayed to the user and CLEANER will be called when
the answer is accepted. In all cases, point will be in the card
in question when the function is called. All values may be nil in
which case no function will be called."
:group 'org-drill
:type '(alist :key-type (choice string (const nil))
:value-type function))
(defcustom org-drill-scope
'file
"The scope to search for drill items in a session.
This can be any of:
file The current buffer, respecting the restriction if any.
This is the default.
tree The subtree started with the entry at point
file-no-restriction The current buffer, without restriction
file-with-archives The current buffer, and any archives associated with it.
agenda All agenda files
agenda-with-archives All agenda files with any archive files associated
with them.
directory All files with the extension '.org' in the same
directory as the current file (includes the current
file if it is an .org file.)
(FILE1 FILE2 ...) If this is a list, all files in the list will be scanned."
;; Note -- meanings differ slightly from the argument to org-map-entries:
;; 'file' means current file/buffer, respecting any restriction
;; 'file-no-restriction' means current file/buffer, ignoring restrictions
;; 'directory' means all *.org files in current directory
:group 'org-drill
:type '(choice (const :tag "The current buffer, respecting the restriction if any." file)
(const :tag "The subtree started with the entry at point" tree)
(const :tag "The current buffer, without restriction" file-no-restriction)
(const :tag "The current buffer, and any archives associated with it." file-with-archives)
(const :tag "All agenda files" agenda)
(const :tag "All agenda files with any archive files associated with them." agenda-with-archives)
(const :tag "All files with the extension '.org' in the same directory as the current file (includes the current file if it is an .org file.)" directory)
(repeat :tag "List of files to scan for drill items." file)))
(defcustom org-drill-match
nil
"If non-nil, a string specifying a tags/property/TODO query.
During drill sessions, only items that match this query will be
considered."
:group 'org-drill
:type '(choice (const nil) string))
(defcustom org-drill-save-buffers-after-drill-sessions-p
t
"If non-nil, prompt to save all modified buffers when a session ends."
:group 'org-drill
:type 'boolean)
(defcustom org-drill-spaced-repetition-algorithm
'sm5
"Which SuperMemo spaced repetition algorithm to use for scheduling items.
Available choices are:
- SM2 :: the SM2 algorithm, used in SuperMemo 2.0
- SM5 :: the SM5 algorithm, used in SuperMemo 5.0
- Simple8 :: a modified version of the SM8 algorithm. SM8 is used in
SuperMemo 98. The version implemented here is simplified in that while it
'learns' the difficulty of each item using quality grades and number of
failures, it does not modify the matrix of values that
governs how fast the inter-repetition intervals increase. A method for
adjusting intervals when items are reviewed early or late has been taken
from SM11, a later version of the algorithm, and included in Simple8."
:group 'org-drill
:type '(choice (const sm2) (const sm5) (const simple8)))
(persist-defvar org-drill-sm5-optimal-factor-matrix
nil
"DO NOT CHANGE THE VALUE OF THIS VARIABLE.
Persistent matrix of optimal factors, used by the SuperMemo SM5
algorithm. The matrix is saved at the end of each drill session.
Over time, values in the matrix will adapt to the individual user's
pace of learning.")
(defcustom org-drill-sm5-initial-interval
4.0
"In the SM5 algorithm, the initial interval after the first
successful presentation of an item is always 4 days. If you wish to change
this, you can do so here."
:group 'org-drill
:type 'float)
(defcustom org-drill-add-random-noise-to-intervals-p
nil
"If true, the number of days until an item's next repetition
will vary slightly from the interval calculated by the SM2
algorithm. The variation is very small when the interval is
small, but scales up with the interval."
:group 'org-drill
:type 'boolean)
(defcustom org-drill-adjust-intervals-for-early-and-late-repetitions-p
nil
"If true, when the student successfully reviews an item 1 or more days
before or after the scheduled review date, this will affect that date of
the item's next scheduled review, according to the algorithm presented at
[[http://www.supermemo.com/english/algsm11.htm#Advanced%20repetitions]].
Items that were reviewed early will have their next review date brought
forward. Those that were reviewed late will have their next review
date postponed further.
Note that this option currently has no effect if the SM2 algorithm
is used."
:group 'org-drill
:type 'boolean)
(defcustom org-drill-cloze-text-weight
4
"For card types 'hide1_firstmore', 'show1_lastmore' and 'show1_firstless',
this number determines how often the 'less favoured' situation
should arise. It will occur 1 in every N trials, where N is the
value of the variable.
For example, with the hide1_firstmore card type, the first piece
of clozed text should be hidden more often than the other
pieces. If this variable is set to 4 (default), the first item
will only be shown 25% of the time (1 in 4 trials). Similarly for
show1_lastmore, the last item will be shown 75% of the time, and
for show1_firstless, the first item would only be shown 25% of the
time.
If the value of this variable is NIL, then weighting is disabled, and
all weighted card types are treated as their unweighted equivalents."
:group 'org-drill
:type '(choice integer (const nil)))
(defcustom org-drill-cram-hours
12
"When in cram mode, items are considered due for review if
they were reviewed at least this many hours ago."
:group 'org-drill
:type 'integer)
;;; NEW items have never been presented in a drill session before.
;;; MATURE items HAVE been presented at least once before.
;;; - YOUNG mature items were scheduled no more than
;;; ORG-DRILL-DAYS-BEFORE-OLD days after their last
;;; repetition. These items will have been learned 'recently' and will have a
;;; low repetition count.
;;; - OLD mature items have intervals greater than
;;; ORG-DRILL-DAYS-BEFORE-OLD.
;;; - OVERDUE items are past their scheduled review date by more than
;;; LAST-INTERVAL * (ORG-DRILL-OVERDUE-INTERVAL-FACTOR - 1) days,
;;; regardless of young/old status.
(defcustom org-drill-days-before-old
10
"When an item's inter-repetition interval rises above this value in days,
it is no longer considered a 'young' (recently learned) item."
:group 'org-drill
:type 'integer)
(defcustom org-drill-overdue-interval-factor
1.2
"An item is considered overdue if its scheduled review date is
more than (ORG-DRILL-OVERDUE-INTERVAL-FACTOR - 1) * LAST-INTERVAL
days in the past. For example, a value of 1.2 means an additional
20% of the last scheduled interval is allowed to elapse before
the item is overdue. A value of 1.0 means no extra time is
allowed at all - items are immediately considered overdue if
there is even one day's delay in reviewing them. This variable
should never be less than 1.0."
:group 'org-drill
:type 'float)
(defcustom org-drill-learn-fraction
0.5
"Fraction between 0 and 1 that governs how quickly the spaces
between successive repetitions increase, for all items. The
default value is 0.5. Higher values make spaces increase more
quickly with each successful repetition. You should only change
this in small increments (for example 0.05-0.1) as it has an
exponential effect on inter-repetition spacing."
:group 'org-drill
:type 'float)
(defcustom org-drill-presentation-prompt-with-typing nil
"Non-nil indicates that answers should be given in a buffer."
:group 'org-drill
:type 'boolean)
(defcustom org-drill-cloze-length-matches-hidden-text-p
nil
"If non-nil, when concealing cloze deletions, force the length of
the ellipsis to match the length of the missing text. This may be useful
to preserve the formatting in a displayed table, for example."
:group 'org-drill
:type 'boolean)
(defvar org-drill-display-answer-hook nil
"Hook called when `org-drill' answers are displayed.")
(defclass org-drill-session ()
((qualities :initform nil)
(start-time
:initform 0.0
:documentation "Time at which the session started"
:type float)
(new-entries :initform nil)
(dormant-entry-count :initform 0)
(due-entry-count :initform 0)
(overdue-entry-count :initform 0)
(due-tomorrow-count :initform 0)
(overdue-entries
:initform nil
:documentation
"List of markers for items that are
considered 'overdue', based on the value of
ORG-DRILL-OVERDUE-INTERVAL-FACTOR.")
(young-mature-entries
:initform nil
:documentation "List of markers for mature entries whose last inter-repetition
interval was <= ORG-DRILL-DAYS-BEFORE-OLD days.")
(old-mature-entries
:initform nil
:documentation "List of markers for mature entries whose last inter-repetition
interval was greater than ORG-DRILL-DAYS-BEFORE-OLD days.")
(failed-entries :initform nil)
(again-entries :initform nil)
(done-entries :initform nil)
(current-item
:initform nil
:documentation "Set to the marker for the item currently being tested.")
(cram-mode
:initform nil
:documementation "Are we in 'cram mode', where all items are considered due
for review unless they were already reviewed in the recent past?")
(warned-about-id-creation
:initform nil
:documentation "Have we warned the user about ID creation this session?")
(overdue-data :initform nil)
(cnt :initform 0)
(exit-kind
:initform nil
:documentation "Return value from typed answers which use recursive edit.")
(typed-answer
:initform nil
:documentation "The last answer typed by the user.")
(drill-answer
:initform nil
:documentation "The correct answer when an item is being
presented. If this variable is non-nil, the default presentation
function will show its value instead of the default behaviour of
revealing the contents of the drilled item.
This variable is useful for card types that compute their answers
-- for example, a card type that asks the student to translate a
random number to another language.")
(end-pos :initform nil))
:documentation "An org-drill session object carries data about
the current state of a particular org-drill session." )
(defvar org-drill-current-session nil
"If non-nil, the current session.
The current session is an `org-drill-session' object.")
(defvar org-drill-last-session nil
"If non-nil, the last session.
This can be used to resume the last session.")
(defvar org-drill-cards-in-this-emacs 0
"The total number of cards displayed in this Emacs invocation.
This variable is not functionally important, but is used for
debugging.")
(defvar org-drill-scheduling-properties
'("LEARN_DATA" "DRILL_LAST_INTERVAL" "DRILL_REPEATS_SINCE_FAIL"
"DRILL_TOTAL_REPEATS" "DRILL_FAILURE_COUNT" "DRILL_AVERAGE_QUALITY"
"DRILL_EASE" "DRILL_LAST_QUALITY" "DRILL_LAST_REVIEWED"))
(defvar org-drill--lapse-very-overdue-entries-p nil
"If non-nil, entries more than 90 days overdue are regarded as 'lapsed'.
This means that when the item is eventually re-tested it will be
treated as 'failed' (quality 2) for rescheduling purposes,
regardless of whether the test was successful.")
;;; Make the above settings safe as file-local variables.
(put 'org-drill-question-tag 'safe-local-variable 'stringp)
(put 'org-drill-maximum-items-per-session 'safe-local-variable
'(lambda (val) (or (integerp val) (null val))))
(put 'org-drill-maximum-duration 'safe-local-variable
'(lambda (val) (or (integerp val) (null val))))
(put 'org-drill-failure-quality 'safe-local-variable 'integerp)
(put 'org-drill-forgetting-index 'safe-local-variable 'integerp)
(put 'org-drill-leech-failure-threshold 'safe-local-variable 'integerp)
(put 'org-drill-leech-method 'safe-local-variable
'(lambda (val) (memq val '(nil skip warn))))
(put 'org-drill-use-visible-cloze-face-p 'safe-local-variable 'booleanp)
(put 'org-drill-hide-item-headings-p 'safe-local-variable 'booleanp)
(put 'org-drill-spaced-repetition-algorithm 'safe-local-variable
'(lambda (val) (memq val '(simple8 sm5 sm2))))
(put 'org-drill-sm5-initial-interval 'safe-local-variable 'floatp)
(put 'org-drill-add-random-noise-to-intervals-p 'safe-local-variable 'booleanp)
(put 'org-drill-adjust-intervals-for-early-and-late-repetitions-p
'safe-local-variable 'booleanp)
(put 'org-drill-cram-hours 'safe-local-variable 'integerp)
(put 'org-drill-learn-fraction 'safe-local-variable 'floatp)
(put 'org-drill-days-before-old 'safe-local-variable 'integerp)
(put 'org-drill-overdue-interval-factor 'safe-local-variable 'floatp)
(put 'org-drill-scope 'safe-local-variable
'(lambda (val) (or (symbolp val) (listp val))))
(put 'org-drill-match 'safe-local-variable
'(lambda (val) (or (stringp val) (null val))))
(put 'org-drill-save-buffers-after-drill-sessions-p 'safe-local-variable 'booleanp)
(put 'org-drill-cloze-text-weight 'safe-local-variable
'(lambda (val) (or (null val) (integerp val))))
(put 'org-drill-left-cloze-delimiter 'safe-local-variable 'stringp)
(put 'org-drill-right-cloze-delimiter 'safe-local-variable 'stringp)
;;; Org compatability hacks
(when (version< org-version "9.2")
(advice-add 'org-get-tags :around #'org-drill-get-tags-advice))
(defun org-drill-get-tags-advice (orig-fun &rest args)
;; the two arg call obsoletes get-local-tags
(if (= 2 (length args))
;; and we don't want any byte compile errors
(if (fboundp 'org-get-local-tags) (org-get-local-tags))
;; the non-arg version doesn't return inherited tags, but
;; get-tags-at does.
(org-get-tags-at)))
(when (= 8 (car (version-to-list org-version)))
;; Shut up package-lint
(defalias 'org-drill-defun 'defun)
(org-drill-defun org-toggle-latex-fragment (&rest args)
(apply 'org-preview-latex-fragment args)))
;;;; Utilities ================================================================
(defmacro org-drill-pop-random (place)
"Remove an item randomly from PLACE."
(let ((idx (cl-gensym)))
`(if (null ,place)
nil
(let ((,idx (cl-random (length ,place))))
(prog1 (nth ,idx ,place)
(setf ,place (append (cl-subseq ,place 0 ,idx)
(cl-subseq ,place (1+ ,idx)))))))))
(defmacro org-drill-push-end (val place)
"Add VAL to the end of the sequence stored in PLACE. Return the new
value."
`(setf ,place (append ,place (list ,val))))
(defun org-drill-round-float (floatnum fix)
"Round the floating point number FLOATNUM to FIX decimal places.
Example: (round-float 3.56755765 3) -> 3.568"
(let ((n (expt 10 fix)))
(/ (float (round (* floatnum n))) n)))
(defun org-drill-command-keybinding-to-string (cmd)
"Return a human-readable description of the key/keys to which the command
CMD is bound, or nil if it is not bound to a key."
(let ((key (where-is-internal cmd overriding-local-map t)))
(if key (key-description key))))
(defun org-drill-time-to-inactive-org-timestamp (time)
"Convert TIME into org-mode timestamp."
(format-time-string
(concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
time))
(defun org-drill-map-entries (func &optional scope drill-match &rest skip)
"Like `org-map-entries', but only drill entries are processed."
(let ((org-drill-match (or drill-match org-drill-match)))
(apply 'org-map-entries func
(concat "+" org-drill-question-tag
(if (and (stringp org-drill-match)
(not (member (elt org-drill-match 0) '(?+ ?- ?|))))
"+" "")
(or org-drill-match ""))
(org-drill-current-scope scope)
skip)))
(defun org-drill-current-scope (scope)
"Translate SCOPE into an scope suitable for `org-map-entries'.
If scope is NIL, then use `org-drill-scope'.
Returns scope as defined by `org-map-entries'"
(let ((scope (or scope org-drill-scope)))
(cl-case scope
(file nil)
(file-no-restriction 'file)
(directory
(directory-files
(file-name-directory (buffer-file-name))
t "^[^.].*\\.org$"))
(t scope))))
(defmacro org-drill-with-hidden-cloze-text (&rest body)
"Eval BODY with clozed text hidden."
(declare (debug t))
`(progn
(org-drill-hide-clozed-text)
(unwind-protect
(progn
,@body)
(org-drill-unhide-clozed-text))))
(defmacro org-drill-with-hidden-cloze-hints (&rest body)
"Eval BODY with cloze hints hidden."
(declare (debug t))
`(progn
(org-drill-hide-cloze-hints)
(unwind-protect
(progn
,@body)
(org-drill-unhide-text))))
(defmacro org-drill-with-hidden-comments (&rest body)
"Eval BODY with comments hidden."
(declare (debug t))
`(progn
(if org-drill-hide-item-headings-p
(org-drill-hide-heading-at-point))
(org-drill-hide-comments)
(unwind-protect
(progn
,@body)
(org-drill-unhide-text))))
(defun org-drill-days-since-last-review ()
"Nil means a last review date has not yet been stored for
the item.
Zero means it was reviewed today.
A positive number means it was reviewed that many days ago.
A negative number means the date of last review is in the future --
this should never happen."
(let ((datestr (org-entry-get (point) "DRILL_LAST_REVIEWED")))
(when datestr
(- (time-to-days (current-time))
(time-to-days (apply 'encode-time
(org-parse-time-string datestr)))))))
(defun org-drill-hours-since-last-review ()
"Like `org-drill-days-since-last-review', but return value is
in hours rather than days."
(let ((datestr (org-entry-get (point) "DRILL_LAST_REVIEWED")))
(when datestr
(floor
(/ (- (time-to-seconds (current-time))
(time-to-seconds (apply 'encode-time
(org-parse-time-string datestr))))
(* 60 60))))))
(defun org-drill-entry-p (&optional marker)
"Is MARKER, or the point, in a 'drill item'? This will return nil if
the point is inside a subheading of a drill item -- to handle that
situation use `org-part-of-drill-entry-p'."
(save-excursion
(when marker
(org-drill-goto-entry marker))
(member org-drill-question-tag (org-get-tags nil t))))
(defun org-drill-goto-entry (marker)
"Switch to the buffer and position of MARKER."
(switch-to-buffer (marker-buffer marker))
(goto-char marker))
(defun org-drill-part-of-drill-entry-p ()
"Is the current entry either the main heading of a 'drill item',
or a subheading within a drill item?"
(or (org-drill-entry-p)
;; Does this heading INHERIT the drill tag
(member org-drill-question-tag (org-get-tags))))
(defun org-drill-goto-drill-entry-heading ()
"Move the point to the heading which holds the :drill: tag for this
drill entry."
(unless (org-at-heading-p)
(org-back-to-heading))
(unless (org-drill-part-of-drill-entry-p)
(error "Point is not inside a drill entry"))
(while (not (org-drill-entry-p))
(unless (org-up-heading-safe)
(error "Cannot find a parent heading that is marked as a drill entry"))))
(defun org-drill-entry-leech-p ()
"Is the current entry a 'leech item'?"
(and (org-drill-entry-p)
(member "leech" (org-get-tags nil t))))
;; (defun org-drill-entry-due-p ()
;; (cond
;; (*org-drill-cram-mode*
;; (let ((hours (org-drill-hours-since-last-review)))
;; (and (org-drill-entry-p)
;; (or (null hours)
;; (>= hours org-drill-cram-hours)))))
;; (t
;; (let ((item-time (org-get-scheduled-time (point))))
;; (and (org-drill-entry-p)
;; (or (not (eql 'skip org-drill-leech-method))
;; (not (org-drill-entry-leech-p)))
;; (or (null item-time) ; not scheduled
;; (not (cl-minusp ; scheduled for today/in past
;; (- (time-to-days (current-time))
;; (time-to-days item-time))))))))))
(defun org-drill-entry-days-overdue (session)
"Returns:
- NIL if the item is not to be regarded as scheduled for review at all.
This is the case if it is not a drill item, or if it is a leech item
that we wish to skip, or if we are in cram mode and have already reviewed
the item within the last few hours.
- 0 if the item is new, or if it scheduled for review today.
- A negative integer - item is scheduled that many days in the future.
- A positive integer - item is scheduled that many days in the past."
(cond
((oref session cram-mode)
(let ((hours (org-drill-hours-since-last-review)))
(and (org-drill-entry-p)
(or (null hours)
(>= hours org-drill-cram-hours))
0)))
(t
(let ((item-time (org-get-scheduled-time (point))))
(cond
((or (not (org-drill-entry-p))
(and (eql 'skip org-drill-leech-method)
(org-drill-entry-leech-p)))
nil)
((null item-time) ; not scheduled -> due now
0)
(t
(- (time-to-days (current-time))
(time-to-days item-time))))))))
(defun org-drill-entry-overdue-p (session &optional days-overdue last-interval)
"Returns true if entry that is scheduled DAYS-OVERDUE dasy in the past,
and whose last inter-repetition interval was LAST-INTERVAL, should be
considered 'overdue'. If the arguments are not given they are extracted
from the entry at point."
(unless days-overdue
(setq days-overdue (org-drill-entry-days-overdue session)))
(unless last-interval
(setq last-interval (org-drill-entry-last-interval 1)))
(and (numberp days-overdue)
(> days-overdue 1) ; enforce a sane minimum 'overdue' gap
;;(> due org-drill-days-before-overdue)
(> (/ (+ days-overdue last-interval 1.0) last-interval)
org-drill-overdue-interval-factor)))
(defun org-drill-entry-due-p (session)
"Return non-nil if the entry at point is overdue.
The SESSION can affect the definition of overdue."
(let ((due (org-drill-entry-days-overdue session)))
(and (not (null due))
(not (cl-minusp due)))))
(defun org-drill-entry-new-p ()
"Return non-nil if the entry at point is new."
(and (org-drill-entry-p)
(let ((item-time (org-get-scheduled-time (point))))
(null item-time))))
(defun org-drill-entry-last-quality (&optional default)
"Return the SM quality score for entry at point, or DEFAULT."
(let ((quality (org-entry-get (point) "DRILL_LAST_QUALITY")))
(if quality
(string-to-number quality)
default)))
(defun org-drill-entry-failure-count ()
"Return the SM failure count for entry at point."
(let ((quality (org-entry-get (point) "DRILL_FAILURE_COUNT")))
(if quality
(string-to-number quality)
0)))
(defun org-drill-entry-average-quality (&optional default)
"Return the SM average quality for entry at point."
(let ((val (org-entry-get (point) "DRILL_AVERAGE_QUALITY")))
(if val
(string-to-number val)
(or default nil))))
(defun org-drill-entry-last-interval (&optional default)
"Return the SM last interval for entry at point."
(let ((val (org-entry-get (point) "DRILL_LAST_INTERVAL")))
(if val
(string-to-number val)
(or default 0))))
(defun org-drill-entry-repeats-since-fail (&optional default)
"Return the SM repeats since fail for entry at point."
(let ((val (org-entry-get (point) "DRILL_REPEATS_SINCE_FAIL")))
(if val
(string-to-number val)
(or default 0))))
(defun org-drill-entry-total-repeats (&optional default)
"Return the SM total number of repeats for the entry at point."
(let ((val (org-entry-get (point) "DRILL_TOTAL_REPEATS")))
(if val
(string-to-number val)
(or default 0))))
(defun org-drill-entry-ease (&optional default)
"Return the SM ease for the entry at point."
(let ((val (org-entry-get (point) "DRILL_EASE")))
(if val
(string-to-number val)
default)))
(defun org-drill-random-dispersal-factor ()
"Returns a random number between 0.5 and 1.5.
This returns a strange random number distribution. See
http://www.supermemo.com/english/ol/sm5.htm for details."
(let ((a 0.047)
(b 0.092)
(p (- (cl-random 1.0) 0.5)))
(cl-flet ((sign (n)
(cond ((zerop n) 0)
((cl-plusp n) 1)
(t -1))))
(/ (+ 100 (* (* (/ -1 b) (log (- 1 (* (/ b a ) (abs p)))))
(sign p)))
100.0))))
(defun org-drill-early-interval-factor (optimal-factor
optimal-interval
days-ahead)
"Arguments:
- OPTIMAL-FACTOR: interval-factor if the item had been tested
exactly when it was supposed to be.
- OPTIMAL-INTERVAL: interval for next repetition (days) if the item had been
tested exactly when it was supposed to be.
- DAYS-AHEAD: how many days ahead of time the item was reviewed.
Returns an adjusted optimal factor which should be used to
calculate the next interval, instead of the optimal factor found
in the matrix."
(let ((delta-ofmax (* (1- optimal-factor)
(/ (+ optimal-interval
(* 0.6 optimal-interval) -1) (1- optimal-interval)))))
(- optimal-factor
(* delta-ofmax (/ days-ahead (+ days-ahead (* 0.6 optimal-interval)))))))
(defun org-drill-get-item-data ()
"Returns a list of 6 items, containing all the stored recall
data for the item at point:
- LAST-INTERVAL is the interval in days that was used to schedule the item's
current review date.
- REPEATS is the number of items the item has been successfully recalled without
without any failures. It is reset to 0 upon failure to recall the item.
- FAILURES is the total number of times the user has failed to recall the item.
- TOTAL-REPEATS includes both successful and unsuccessful repetitions.
- AVERAGE-QUALITY is the mean quality of recall of the item over
all its repetitions, successful and unsuccessful.
- EASE is a number reflecting how easy the item is to learn. Higher is easier.
"