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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
|
;;; org-drill.el --- Self-testing using spaced repetition -*- lexical-binding: t -*-
;;; Header:
;; Maintainer: Phillip Lord <phillip.lord@russet.org.uk>
;; Author: Paul Sexton <eeeickythump@gmail.com>
;; Version: 2.7.0
;; Package-Requires: ((emacs "25.3") (seq "2.14") (org "9.6") (persist "0.3"))
;; Keywords: games, outlines, multimedia
;; URL: https://github.com/cjennings/org-drill
;;
;; 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 'calendar)
(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)
(defgroup org-drill-display nil
"How cards, clozes, and the session are displayed."
:tag "Org-Drill Display"
:group 'org-drill)
(defgroup org-drill-algorithm nil
"Spaced-repetition scheduling algorithm and its parameters."
:tag "Org-Drill Algorithm"
:group 'org-drill)
(defgroup org-drill-session nil
"What gets drilled and how a session is bounded."
:tag "Org-Drill Session"
:group 'org-drill)
(defgroup org-drill-leech nil
"Handling of leeches: items that are failed repeatedly."
:tag "Org-Drill Leech"
:group 'org-drill)
(defgroup org-drill-statistics nil
"Persistent session log and the statistics dashboard.
The dashboard is opt-in via \\='M-x org-drill-statistics\\='; the session log
itself is updated automatically at the end of every completed (non-suspended)
drill session. See docs/design/stats-dashboard.org for the design rationale."
:tag "Org-Drill Statistics"
:group 'org-drill)
(defconst org-drill-version "2.7.0"
"Version of the org-drill package.
Keep this in sync with the Version header at the top of this file.")
;;;###autoload
(defun org-drill-version ()
"Report the installed org-drill version in the echo area.
Returns the version string so it is useful in non-interactive code too."
(interactive)
(message "org-drill %s" org-drill-version)
org-drill-version)
(defcustom org-drill-question-tag
"drill"
"Tag for topics which are review topics."
:group 'org-drill-session
:type 'string)
(defvar org-drill-leitner-tag "leitner"
"Tag marking entries reviewed via the Leitner box system.")
(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-session
: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-session
:type '(choice integer (const nil)))
(defcustom org-drill-on-timeout-action
'finish-current
"What to do with unfinished cards when `org-drill-maximum-duration' is reached.
- `finish-current' (default): keep the historical behavior. The
card in progress and any cards queued for re-drilling this session
(the \\='again\\=' queue) are still presented before the session ends.
- `discard-current': end the session as soon as the time limit is
reached, dropping the in-progress card and the again-queue. The
dropped cards are left untouched — they keep whatever scheduling
they already had and simply turn up again next session."
:group 'org-drill-session
:type '(choice (const finish-current) (const discard-current)))
(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-session
: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-algorithm
: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-algorithm
: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-leech
: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-leech
: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-display)
(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-display)
(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-display)
(defcustom org-drill-use-visible-cloze-face-p
nil
"Highlight cloze-deleted text."
:group 'org-drill-display
:type 'boolean)
(defcustom org-drill-auto-enable-mode t
"When non-nil, enable `org-drill-mode' automatically in Org buffers that
contain drill cards — headings tagged with `org-drill-question-tag' or
`org-drill-leitner-tag'. This scopes cloze fontification to buffers that
actually hold cards instead of installing it in every Org buffer."
:group 'org-drill-display
: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-display
:type 'boolean)
(defcustom org-drill-new-count-color
"royal blue"
"Foreground colour for remaining new items."
:group 'org-drill-display
: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-display
:type 'color)
(defcustom org-drill-failed-count-color
"red"
"Foreground colour for remaining failed items."
:group 'org-drill-display
:type 'color)
(defcustom org-drill-done-count-color
"sienna"
"Foreground colour for reviewed items."
:group 'org-drill-display
:type 'color)
(defcustom org-drill-left-cloze-delimiter
"["
"String used within org buffers to delimit cloze deletions."
:group 'org-drill-display
:type 'string)
(defcustom org-drill-right-cloze-delimiter
"]"
"String used within org buffers to delimit cloze deletions."
:group 'org-drill-display
: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))
(defvar org-drill-hint-separator "||"
"Delimiter in cloze expression for hints.")
(defun org-drill--compute-cloze-regexp ()
"Return a regexp that detects clozes.
The inner match is constrained to non-newline characters so a cloze
stays within one line. An older version used
`[[:cntrl:][:graph:][:space:]]' which silently included newline,
letting a stray `[' match all the way to a `]' several lines later
and bleeding the cloze face onto intervening org headings (upstream
issue #38)."
(concat "\\("
(regexp-quote org-drill-left-cloze-delimiter)
"[^\n]+?\\)\\(\\|"
(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.")
;; Keys pressed during a drill session to quit, edit the item, etc.
;; These are defcustoms so they can be rebound from customize-group.
(defcustom org-drill--quit-key ?q
"Character to quit the session."
:group 'org-drill-session
:type 'character)
(defcustom org-drill--edit-key ?e
"Character to suspend the session."
:group 'org-drill-session
:type 'character)
(defcustom org-drill--help-key ??
"Character to show help."
:group 'org-drill-session
:type 'character)
(defcustom org-drill--skip-key ?s
"Character to skip to the next item."
:group 'org-drill-session
:type 'character)
(defcustom org-drill--tags-key ?t
"Character to edit the tags."
:group 'org-drill-session
:type 'character)
(defcustom org-drill--undo-key ?u
"Character to undo the most recent rating during a session.
Pressing it at the rating prompt restores the previous card's
scheduling data and re-queues that card (see `org-drill-undo-last-rating')."
:group 'org-drill-session
:type 'character)
(defcustom org-drill-undo-limit
3
"How many recent ratings can be undone with `org-drill--undo-key'.
Each rating snapshots the card's scheduling state; only this many of the
most recent snapshots are kept."
:group 'org-drill-session
:type 'integer)
(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))
"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-session
:type '(alist :key-type (choice string (const nil))
:value-type function))
(defcustom org-drill-treat-headline-as-card-p nil
"When non-nil, treat a drill entry with an empty body as a valid card.
By default a drill entry whose body is empty is skipped during a
session unless its card type opts in via the DRILL-EMPTY-P slot of
`org-drill-card-type-alist'. When this is non-nil, an empty-bodied
entry is presented as a card with the heading itself as the question,
regardless of card type. This covers hierarchical-notes decks where
the heading is the prompt and the answer lives in child entries, and
decks where the heading alone is the card (upstream issues #30 and #41)."
:group 'org-drill-session
:type 'boolean)
(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-session
: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-session
: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-session
: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-session
:type 'boolean)
;; ADR 2026-05-27 — default scheduling algorithm (upstream issue #46).
;;
;; Context. The default for the past several years has been SM5. Issue
;; #46 (2022-07) asked whether SM2 or Simple8 would be better defaults
;; out of the box. The decision was deferred until #147 (the card-state
;; refactor) landed, which it now has.
;;
;; The three candidates as implemented here:
;;
;; - SM2: classic SuperMemo 2. Carries no algorithm-level state beyond
;; the per-card item-data. Simple, well-validated, no persistence-file
;; surface. The conservative default in many SR implementations.
;;
;; - SM5: SuperMemo 5 with a per-user optimal-factor matrix persisted
;; via `persist-defvar' (`org-drill-sm5-optimal-factor-matrix'). The
;; matrix really does adapt across sessions, so SM5's "learns over
;; time" claim materializes for each user. The downside is the
;; persistence-file dependency — upstream issue #45 surfaced "End of
;; file during parsing" raised from inside `persist', and we now wrap
;; the load in a `condition-case' that falls back to a fresh nil
;; matrix. A SM5 user whose persist file rots silently regresses to
;; learning-from-scratch every session.
;;
;; - Simple8: modernized SM-family scheduler. Learns per-card difficulty
;; from quality grades and failure count, no global matrix. Adjusts
;; intervals for early or late reviews (delta-days), a real-world
;; concern that SM2 and SM5 don't address. No persistence-file
;; dependency. The "boring modern" choice.
;;
;; Decision. Simple8. Per-card difficulty learning gives most of SM5's
;; adaptation value without the persist-file fragility, and delta-days
;; handling matches how people actually use spaced repetition (cards do
;; get reviewed late). SM5 stays available for users who want the
;; persisted optimal-factor matrix; SM2 stays available for users who
;; want the simplest possible scheduler.
;;
;; Consequences. New installs: Simple8 by default. Existing users with
;; `org-drill-spaced-repetition-algorithm' set in their config: no
;; change. Existing users on the previous default with no setting in
;; their config: their next session schedules under Simple8, which uses
;; a different per-card state shape than SM5. No state migration is
;; performed — Simple8 reads what it can from the existing item-data
;; (last-interval, repetitions, failures, total-repeats, meanq) and
;; carries on. SM5's optimal-factor matrix is preserved on disk and
;; available if the user switches back.
(defcustom org-drill-spaced-repetition-algorithm
'simple8
"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.
The default is Simple8; see the ADR comment above the defcustom in the
source for the rationale."
:group 'org-drill-algorithm
:type '(choice (const sm2) (const sm5) (const simple8)))
;; Wrap `persist-defvar' in `condition-case' so a corrupted persist
;; file (upstream issue #45 — "End of file during parsing" raised from
;; deep inside the persist package) doesn't prevent org-drill from
;; loading. Fall back to a fresh nil matrix when persist-load fails.
(condition-case err
(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.")
(error
(message
"org-drill: failed to load persisted SM5 matrix (%s); using fresh state"
err)
(defvar org-drill-sm5-optimal-factor-matrix nil
"Persistent matrix of optimal factors (fallback after load failure).")))
;; Statistics session log. Records one entry per completed drill session
;; so the stats dashboard (docs/design/stats-dashboard.org) has
;; a temporal axis to render trends against. Same persist-defvar +
;; condition-case pattern as the SM5 matrix above. See
;; `org-drill--session-log-quarantine' for the corrupt-file recovery
;; behavior — the dashboard's spec adds a rename-and-quarantine step on
;; top of the SM5 path's fresh-start fallback.
(cl-defstruct org-drill-session-record
"One completed drill session, persisted to `org-drill-session-log'.
Slots match the v0 stats-dashboard spec; see
docs/design/stats-dashboard.org for field semantics."
start-time ; float — `float-time' at session start
end-time ; float — `float-time' at session end
scope ; symbol or list — `org-drill-scope' at start
algorithm ; symbol — `org-drill-spaced-repetition-algorithm' at start
qualities ; vector of int — every 0-5 quality entered, in order
pass-percent ; int — (count qualities > failure-quality) / total * 100
new-count ; int — new entries at session end
mature-count ; int — young-mature + old-mature entries at session end
failed-count ; int — failed entries at session end
cram-mode) ; bool — cram-mode value at session start
(defun org-drill--session-log-quarantine ()
"Rename a corrupt `org-drill-session-log' persist file out of the way.
The file is renamed to a `.corrupt-YYYY-MM-DDTHHMMSS' sibling so the
next save doesn't overwrite a potentially recoverable file. The
timestamp includes seconds so a repeat corruption on the same day does
not silently overwrite the earlier quarantine. No-op if the file
can't be located or doesn't exist.
Locates the on-disk file via `persist--file-location', which is an
internal symbol in the `persist' package. The `fboundp' gate keeps a
future persist release that renames it from breaking
package load — at the cost of becoming a silent no-op on that release,
which would let the next save overwrite the corrupt file. Worth a
follow-up if persist changes its API."
(when (fboundp 'persist--file-location)
(let ((file (ignore-errors
(persist--file-location 'org-drill-session-log))))
(when (and file (file-exists-p file))
(let ((quarantine (format "%s.corrupt-%s"
file
(format-time-string "%Y-%m-%dT%H%M%S"))))
(ignore-errors (rename-file file quarantine t))
(lwarn 'org-drill :warning
"Corrupt session-log file moved to %s; starting fresh."
quarantine))))))
(condition-case err
(persist-defvar org-drill-session-log
nil
"List of `org-drill-session-record' values, newest first.
Updated at the end of every completed (non-suspended) drill session by
`org-drill-record-session'. Read by the stats dashboard
(`org-drill-statistics', when implemented) to render trend panels.")
(error
(message
"org-drill: failed to load persisted session log (%s); using fresh state"
err)
(ignore-errors (org-drill--session-log-quarantine))
(defvar org-drill-session-log nil
"Persistent session log (fallback after load failure).")))
(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-algorithm
: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-algorithm
: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-algorithm
: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-display
: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-session
: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-algorithm
: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-algorithm
: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-algorithm
:type 'float)
(defcustom org-drill-presentation-prompt-with-typing nil
"Non-nil indicates that answers should be given in a buffer."
:group 'org-drill-display
: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-display
:type 'boolean)
(defcustom org-drill-hide-modeline-during-session
t
"If non-nil, hide the modeline during drill sessions.
This provides a cleaner, more focused display for reading drill cards.
The modeline is automatically restored when the session ends."
:group 'org-drill-display
:type 'boolean)
(defcustom org-drill-text-size-during-session
20
"Font size (in points) to use during drill sessions.
Set to nil to use the default font size without scaling.
Typical values are 16-24 for comfortable reading."
:group 'org-drill-display
:type '(choice (const :tag "Use default size" nil)
(integer :tag "Font size in points")))
(defcustom org-drill-use-variable-pitch
nil
"If non-nil, use variable-pitch font during drill sessions.
This can make text more readable for long-form content."
:group 'org-drill-display
:type 'boolean)
(defvar org-drill--saved-modeline-format nil
"Saved modeline format before drill session started.")
(defvar org-drill--saved-text-scale nil
"Saved text scale level before drill session started.")
(defvar org-drill--saved-variable-pitch-mode nil
"Saved variable-pitch-mode state before drill session started.")
(defvar org-drill--saved-display-buffer nil
"Buffer in which the active drill session set up its display.
Captured at setup so restore can target the same buffer even if the
user has switched away by the time `org-drill--restore-display' runs.")
(defvar org-drill-display-answer-hook nil
"Hook called when `org-drill' answers are displayed.")
(defvar org-drill-before-session-hook nil
"Hook run before starting a drill session.
Called in the drill buffer after opening the file but before
presenting the first card. Useful for setting up display preferences
like fonts, text scale, or window layout.")
(defvar org-drill-after-session-hook nil
"Hook run after ending a drill session.
Called when the session ends normally, is quit by the user, or
encounters an error. Useful for restoring display preferences
or performing cleanup.")
(defclass org-drill-session ()
((qualities :initform nil)
(start-time
:initform 0.0
:documentation "Time at which the session started"
:type float)
(scope-at-start
:initform nil
:documentation
"Value of `org-drill-scope' captured at session start. Stored on the
stats-dashboard session record verbatim so a mid-session change to the
defcustom doesn't misrepresent what was actually drilled.")
(algorithm-at-start
:initform nil
:documentation
"Value of `org-drill-spaced-repetition-algorithm' captured at session
start. Same rationale as `scope-at-start'.")
(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)
(undo-stack
:initform nil
:documentation "Stack of pre-rating scheduling snapshots, most recent
first, used by `org-drill-undo-last-rating'. Capped at `org-drill-undo-limit'.")
(current-item
:initform nil
:documentation "Set to the marker for the item currently being tested.")
(cram-mode
:initform nil
:documentation "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"))
(defcustom org-drill-lapse-threshold-days
90
"Number of days overdue before an entry is considered lapsed.
When an entry is more than this many days overdue and
`org-drill--lapse-very-overdue-entries-p' is non-nil, the entry
is treated as lapsed and will be scheduled as a failure (quality 2)
even if answered correctly."
:group 'org-drill-algorithm
:type 'integer)
(defvar org-drill--lapse-very-overdue-entries-p nil
"If non-nil, entries more than `org-drill-lapse-threshold-days' overdue are 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-lapse-threshold-days '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-treat-headline-as-card-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)
;;;; Utilities ================================================================
(defmacro org-drill-pop-random (place)
"Remove an item randomly from PLACE."
(let ((index-var (cl-gensym)))
`(if (null ,place)
nil
(let ((,index-var (cl-random (length ,place))))
(prog1 (nth ,index-var ,place)
(setf ,place (append (cl-subseq ,place 0 ,index-var)
(cl-subseq ,place (1+ ,index-var)))))))))
(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 (org-time-stamp-format t 'no-bracket) 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))))
(defmacro org-drill-with-card-display (&rest body)
"Eval BODY in the standard card-display envelope.
Combines `with-hidden-comments', `with-hidden-cloze-hints', and
`with-hidden-cloze-text' — the wrap most card presenters open with."
(declare (debug t) (indent 0))
`(org-drill-with-hidden-comments
(org-drill-with-hidden-cloze-hints
(org-drill-with-hidden-cloze-text
,@body))))
(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-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)
"Return non-nil if entry that is scheduled DAYS-OVERDUE days 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
(> last-interval 0) ; prevent division by zero
;;(> 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 ((raw-value (org-entry-get (point) "DRILL_AVERAGE_QUALITY")))
(if raw-value
(string-to-number raw-value)
(or default nil))))
(defun org-drill-entry-last-interval (&optional default)
"Return the SM last interval for entry at point."
(let ((raw-value (org-entry-get (point) "DRILL_LAST_INTERVAL")))
(if raw-value
(string-to-number raw-value)
(or default 0))))
(defun org-drill-entry-repeats-since-fail (&optional default)
"Return the SM repeats since fail for entry at point."
(let ((raw-value (org-entry-get (point) "DRILL_REPEATS_SINCE_FAIL")))
(if raw-value
(string-to-number raw-value)
(or default 0))))
(defun org-drill-entry-total-repeats (&optional default)
"Return the SM total number of repeats for the entry at point."
(let ((raw-value (org-entry-get (point) "DRILL_TOTAL_REPEATS")))
(if raw-value
(string-to-number raw-value)
(or default 0))))
(defun org-drill-entry-ease (&optional default)
"Return the SM ease for the entry at point."
(let ((raw-value (org-entry-get (point) "DRILL_EASE")))
(if raw-value
(string-to-number raw-value)
default)))
(defun org-drill-random-dispersal-factor ()
"Return arandom 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--safe-read-learn-data (learn-str)
"Safely parse LEARN_DATA string, validating it contains only numbers.
Returns the parsed list or nil if invalid or unsafe."
(when (and learn-str (stringp learn-str))
(condition-case nil
(let ((data (car (read-from-string learn-str))))
;; Validate it's a list of at least 3 elements
(when (and (listp data)
(>= (length data) 3)
;; Validate first 3 elements are numbers
(numberp (nth 0 data))
(numberp (nth 1 data))
(numberp (nth 2 data)))
data))
(error nil))))
;;; Card-state struct ---------------------------------------------------------
;;
;; ADR (2026-05-27): the schedulers and the item-data round-trip used to pass
;; the same recall fields around as long positional lists, in three different
;; orderings — `get-item-data'/`store-item-data' as
;; (last-interval repetitions failures total-repeats meanq ease), the schedulers
;; taking (last-interval n ef quality failures meanq total-repeats ...), and the
;; schedulers returning (next-interval repetitions ef failures meanq
;; total-repeats ...). Three call sites re-ordered between the shapes by hand,
;; each re-order a latent positional bug, and a new scheduler had to match all
;; three layouts.
;;
;; `org-drill-card-state' bundles the shared stored fields under named slots so
;; the shape is explicit and the manual re-ordering goes away. `next-interval'
;; and `quality' stay outside the struct: the former is scheduling output, the
;; latter is the rating just entered — neither is persisted recall state.
;;
;; Migration is staged: this struct lands first (inert), then the item-data
;; round-trip adopts it, then each scheduler in turn.
(cl-defstruct org-drill-card-state
last-interval repetitions ease failures meanq total-repeats)
(defun org-drill-get-item-data ()
"Return an `org-drill-card-state' with the stored recall data for the item at point.
Slots:
- LAST-INTERVAL is the interval in days that was used to schedule the item's
current review date.
- REPETITIONS is the number of times the item has been successfully recalled
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.
- MEANQ 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."
(let ((learn-str (org-entry-get (point) "LEARN_DATA"))
(repeats (org-drill-entry-total-repeats :missing)))
(cond
(learn-str
(let ((learn-data (org-drill--safe-read-learn-data learn-str)))
(if learn-data
(make-org-drill-card-state
:last-interval (nth 0 learn-data)
:repetitions (nth 1 learn-data)
:failures (org-drill-entry-failure-count)
:total-repeats (nth 1 learn-data)
:meanq (org-drill-entry-last-quality)
:ease (nth 2 learn-data)) ; EF
;; If LEARN_DATA is invalid/unsafe, fall through to next case
(if (not (eql :missing repeats))
(make-org-drill-card-state
:last-interval (org-drill-entry-last-interval)
:repetitions (org-drill-entry-repeats-since-fail)
:failures (org-drill-entry-failure-count)
:total-repeats (org-drill-entry-total-repeats)
:meanq (org-drill-entry-average-quality)
:ease (org-drill-entry-ease))
;; Virgin item
(make-org-drill-card-state
:last-interval 0 :repetitions 0 :failures 0
:total-repeats 0 :meanq nil :ease nil)))))
((not (eql :missing repeats))
(make-org-drill-card-state
:last-interval (org-drill-entry-last-interval)
:repetitions (org-drill-entry-repeats-since-fail)
:failures (org-drill-entry-failure-count)
:total-repeats (org-drill-entry-total-repeats)
:meanq (org-drill-entry-average-quality)
:ease (org-drill-entry-ease)))
(t ; virgin item
(make-org-drill-card-state
:last-interval 0 :repetitions 0 :failures 0
:total-repeats 0 :meanq nil :ease nil)))))
(defun org-drill-store-item-data (state)
"Store the recall data in STATE, an `org-drill-card-state', into the item at point.
STATE's LAST-INTERVAL slot holds the interval to schedule going forward (the
caller passes the scheduler's next-interval there)."
(org-entry-delete (point) "LEARN_DATA")
(org-set-property "DRILL_LAST_INTERVAL"
(number-to-string
(org-drill-round-float (org-drill-card-state-last-interval state) 4)))
(org-set-property "DRILL_REPEATS_SINCE_FAIL"
(number-to-string (org-drill-card-state-repetitions state)))
(org-set-property "DRILL_TOTAL_REPEATS"
(number-to-string (org-drill-card-state-total-repeats state)))
(org-set-property "DRILL_FAILURE_COUNT"
(number-to-string (org-drill-card-state-failures state)))
(org-set-property "DRILL_AVERAGE_QUALITY"
(number-to-string
(org-drill-round-float (org-drill-card-state-meanq state) 3)))
(org-set-property "DRILL_EASE"
(number-to-string
(org-drill-round-float (org-drill-card-state-ease state) 3))))
;;; SM2 Algorithm =============================================================
(defun org-drill-determine-next-interval-sm2 (state quality)
"Return the SM2 schedule for STATE after a review of recall QUALITY (0-5).
STATE is an `org-drill-card-state' carrying LAST-INTERVAL, REPETITIONS,
EASE (the \\='easiness factor\\='), FAILURES, MEANQ and TOTAL-REPEATS.
QUALITY is the rating just entered.
Returns a list:
(INTERVAL REPEATS EF FAILURES MEAN TOTAL-REPEATS OFMATRIX), where:
- INTERVAL is the number of days until the item should next be reviewed
- REPEATS is incremented by 1.
- EF is modified based on the recall quality for the item.
- OF-MATRIX is not modified."
(let ((last-interval (org-drill-card-state-last-interval state))
(n (org-drill-card-state-repetitions state))
(ef (org-drill-card-state-ease state))
(failures (org-drill-card-state-failures state))
(meanq (org-drill-card-state-meanq state))
(total-repeats (org-drill-card-state-total-repeats state)))
(if (zerop n) (setq n 1))
(if (null ef) (setq ef 2.5))
(setq meanq (if meanq
(/ (+ quality (* meanq total-repeats 1.0))
(1+ total-repeats))
quality))
(cl-assert (> n 0))
(cl-assert (and (>= quality 0) (<= quality 5)))
(if (org-drill--quality-failed-p quality)
;; When an item is failed, its interval is reset to 0,
;; but its EF is unchanged
(list -1 1 ef (1+ failures) meanq (1+ total-repeats)
org-drill-sm5-optimal-factor-matrix)
;; else:
(let* ((next-ef (org-drill-modify-e-factor ef quality))
(interval
(cond
((<= n 1) 1)
((= n 2)
(cond
(org-drill-add-random-noise-to-intervals-p
(cl-case quality
(5 6)
(4 4)
(3 3)
(2 1)
(t -1)))
(t 6)))
(t (* last-interval next-ef)))))
(list (if org-drill-add-random-noise-to-intervals-p
(+ last-interval (* (- interval last-interval)
(org-drill-random-dispersal-factor)))
interval)
(1+ n)
next-ef
failures meanq (1+ total-repeats)
org-drill-sm5-optimal-factor-matrix)))))
;;; SM5 Algorithm =============================================================
(defun org-drill-modify-e-factor (ef quality)
"Return new e-factor given existing EF and QUALITY."
(if (< ef 1.3)
1.3
(+ ef (- 0.1 (* (- 5 quality) (+ 0.08 (* (- 5 quality) 0.02)))))))
(defun org-drill-modify-of (of q fraction)
"Return modify of."
(let ((temp (* of (+ 0.72 (* q 0.07)))))
(+ (* (- 1 fraction) of) (* fraction temp))))
(defun org-drill-set-optimal-factor (n ef of-matrix of)
"Set the optimal factor."
(let ((factors (assoc n of-matrix)))
(if factors
(let ((ef-of (assoc ef (cdr factors))))
(if ef-of
(setcdr ef-of of)
(push (cons ef of) (cdr factors))))
(push (cons n (list (cons ef of))) of-matrix)))
of-matrix)
(defun org-drill-initial-optimal-factor-sm5 (n ef)
"Return initial optimal factor."
(if (= 1 n)
org-drill-sm5-initial-interval
ef))
(defun org-drill-get-optimal-factor-sm5 (n ef of-matrix)
"Return optimal factor."
(let ((factors (assoc n of-matrix)))
(or (and factors
(let ((ef-of (assoc ef (cdr factors))))
(and ef-of (cdr ef-of))))
(org-drill-initial-optimal-factor-sm5 n ef))))
(defun org-drill-inter-repetition-interval-sm5 (last-interval n ef &optional of-matrix)
"Return repetition interval."
(let ((of (org-drill-get-optimal-factor-sm5 n ef (or of-matrix
org-drill-sm5-optimal-factor-matrix))))
(if (= 1 n)
of
(* of last-interval))))
(defun org-drill-determine-next-interval-sm5 (state quality of-matrix &optional delta-days)
"Return the SM5 schedule for STATE after a review of recall QUALITY (0-5).
STATE is an `org-drill-card-state'. OF-MATRIX is the optimal-factor
matrix. DELTA-DAYS, when set, adjusts the interval for an early or late
repetition."
(let ((last-interval (org-drill-card-state-last-interval state))
(n (org-drill-card-state-repetitions state))
(ef (org-drill-card-state-ease state))
(failures (org-drill-card-state-failures state))
(meanq (org-drill-card-state-meanq state))
(total-repeats (org-drill-card-state-total-repeats state)))
(if (zerop n) (setq n 1))
(if (null ef) (setq ef 2.5))
(cl-assert (> n 0))
(cl-assert (and (>= quality 0) (<= quality 5)))
(unless of-matrix
(setq of-matrix org-drill-sm5-optimal-factor-matrix))
(setq of-matrix (copy-tree of-matrix))
(setq meanq (if meanq
(/ (+ quality (* meanq total-repeats 1.0))
(1+ total-repeats))
quality))
(let ((next-ef (org-drill-modify-e-factor ef quality))
(old-ef ef)
(new-of (org-drill-modify-of (org-drill-get-optimal-factor-sm5 n ef of-matrix)
quality org-drill-learn-fraction))
(interval nil))
(when (and org-drill-adjust-intervals-for-early-and-late-repetitions-p
delta-days (cl-minusp delta-days))
(setq new-of (org-drill-early-interval-factor
(org-drill-get-optimal-factor-sm5 n ef of-matrix)
(org-drill-inter-repetition-interval-sm5
last-interval n ef of-matrix)
delta-days)))
(setq of-matrix
(org-drill-set-optimal-factor n next-ef of-matrix
(org-drill-round-float new-of 3))) ; round OF to 3 d.p.
(setq ef next-ef)
(cond
;; "Failed" -- reset repetitions to 0,
((org-drill--quality-failed-p quality)
(list -1 1 old-ef (1+ failures) meanq (1+ total-repeats)
of-matrix)) ; Not clear if OF matrix is supposed to be
; preserved
(t
(setq interval (org-drill-inter-repetition-interval-sm5
last-interval n ef of-matrix))
(if org-drill-add-random-noise-to-intervals-p
(setq interval (* interval (org-drill-random-dispersal-factor))))
(list interval
(1+ n)
ef
failures
meanq
(1+ total-repeats)
of-matrix))))))
;;; Simple8 Algorithm =========================================================
(defun org-drill-simple8-first-interval (failures)
"Arguments:
- FAILURES: integer >= 0. The total number of times the item has
been forgotten, ever.
Returns the optimal FIRST interval for an item which has previously been
forgotten on FAILURES occasions."
;; SM8 first-interval model. An item never forgotten gets ~2.4849 days;
;; each prior failure shrinks that by a factor of e^-0.057 (~5.5% per
;; failure). Both constants are the empirical fit from the SM8 algorithm
;; and are not meant to be tuned individually.
(* 2.4849 (exp (* -0.057 failures))))
(defun org-drill-simple8-interval-factor (ease repetition)
"Arguments:
- EASE: floating point number >= 1.2. Corresponds to `AF' in SM8 algorithm.
- REPETITION: the number of times the item has been tested.
1 is the first repetition (ie the second trial).
Returns:
The factor by which the last interval should be
multiplied to give the next interval. Corresponds to `RF' or `OF'."
;; 1.2 is the SM8 floor for the interval factor: it never drops below 1.2,
;; so intervals always grow. The amount above the floor decays as
;; learn-fraction raised to log2(repetition), pulling later repetitions
;; toward the floor.
(+ 1.2 (* (- ease 1.2) (expt org-drill-learn-fraction (log repetition 2)))))
(defun org-drill-simple8-quality->ease (quality)
"Return theease (`AF' in the SM8 algorithm) which corresponds
to a mean item quality of QUALITY."
;; Quality (0-5 mean recall score) maps to ease/AF through this 4th-degree
;; polynomial, a least-squares fit carried over from the SM8 algorithm.
;; The five coefficients are empirical and only meaningful together, so
;; treat them as one fitted curve rather than independent knobs.
(+ (* 0.0542 (expt quality 4))
(* -0.4848 (expt quality 3))
(* 1.4916 (expt quality 2))
(* -1.2403 quality)
1.4515))
(defun org-drill-determine-next-interval-simple8 (state quality &optional delta-days)
"Return the Simple8 schedule for STATE after a review of recall QUALITY (0-5).
STATE is an `org-drill-card-state'. DELTA-DAYS is how many days overdue
the item was when reviewed: 0 = on the scheduled day, +N = N days late,
-N = N days early.
Returns the new item data, as a list of 6 values:
- NEXT-INTERVAL
- REPEATS
- EASE
- FAILURES
- AVERAGE-QUALITY
- TOTAL-REPEATS.
See the documentation for `org-drill-get-item-data' for a description of these."
(let ((last-interval (org-drill-card-state-last-interval state))
(repeats (org-drill-card-state-repetitions state))
(failures (org-drill-card-state-failures state))
(meanq (org-drill-card-state-meanq state))
(totaln (org-drill-card-state-total-repeats state)))
(cl-assert (>= repeats 0))
(cl-assert (and (>= quality 0) (<= quality 5)))
(cl-assert (or (null meanq) (and (>= meanq 0) (<= meanq 5))))
(let ((next-interval nil))
(setf meanq (if meanq
(/ (+ quality (* meanq totaln 1.0)) (1+ totaln))
quality))
(cond
((org-drill--quality-failed-p quality)
(cl-incf failures)
(cl-incf totaln)
(setf repeats 0
next-interval -1))
((or (zerop repeats)
(zerop last-interval))
(setf next-interval (org-drill-simple8-first-interval failures))
(cl-incf repeats)
(cl-incf totaln))
(t
(let* ((use-n
(if (and
org-drill-adjust-intervals-for-early-and-late-repetitions-p
(numberp delta-days) (cl-plusp delta-days)
(cl-plusp last-interval))
(+ repeats (min 1 (/ delta-days last-interval 1.0)))
repeats))
(factor (org-drill-simple8-interval-factor
(org-drill-simple8-quality->ease meanq) use-n))
(next-int (* last-interval factor)))
(when (and org-drill-adjust-intervals-for-early-and-late-repetitions-p
(numberp delta-days) (cl-minusp delta-days))
;; The item was reviewed earlier than scheduled.
(setf factor (org-drill-early-interval-factor
factor next-int (abs delta-days))
next-int (* last-interval factor)))
(setf next-interval next-int)
(cl-incf repeats)
(cl-incf totaln))))
(list
(if (and org-drill-add-random-noise-to-intervals-p
(cl-plusp next-interval))
(* next-interval (org-drill-random-dispersal-factor))
next-interval)
repeats
(org-drill-simple8-quality->ease meanq)
failures
meanq
totaln
))))
;;; Essentially copied from `org-learn.el', but modified to
;;; optionally call the SM2 or simple8 functions.
(defun org-drill-smart-reschedule (quality &optional days-ahead)
"If DAYS-AHEAD is supplied it must be a positive integer. The
item will be scheduled exactly this many days into the future."
(let ((delta-days (- (time-to-days (current-time))
(time-to-days (or (org-get-scheduled-time (point))
(current-time)))))
(ofmatrix org-drill-sm5-optimal-factor-matrix)
;; Entries can have weights, 1 by default. Intervals are divided by the
;; item's weight, so an item with a weight of 2 will have all intervals
;; halved, meaning you will end up reviewing it twice as often.
;; Useful for entries which randomly present any of several facts.
(weight (org-entry-get (point) "DRILL_CARD_WEIGHT")))
(if (stringp weight)
(setq weight (string-to-number weight)))
(let ((state (org-drill-get-item-data)))
(cl-destructuring-bind (next-interval repetitions ease
failures meanq total-repeats
&optional new-ofmatrix)
(cl-case org-drill-spaced-repetition-algorithm
(sm5 (org-drill-determine-next-interval-sm5 state quality ofmatrix))
(sm2 (org-drill-determine-next-interval-sm2 state quality))
(simple8 (org-drill-determine-next-interval-simple8
state quality delta-days)))
(if (numberp days-ahead)
(setq next-interval days-ahead))
(if (and (null days-ahead)
(numberp weight) (cl-plusp weight)
(not (cl-minusp next-interval)))
(setq next-interval
(max 1.0 (+ (org-drill-card-state-last-interval state)
(/ (- next-interval
(org-drill-card-state-last-interval state))
weight)))))
(org-drill-store-item-data
(make-org-drill-card-state
:last-interval next-interval :repetitions repetitions :ease ease
:failures failures :meanq meanq :total-repeats total-repeats))
(if (eql 'sm5 org-drill-spaced-repetition-algorithm)
(setq org-drill-sm5-optimal-factor-matrix new-ofmatrix))
;; Pick the schedule based on days-ahead. The optional arg may be
;; nil (callers omit it to mean "use the algorithm-computed
;; next-interval"). The original cond compared `(= 0 days-ahead)'
;; before any type guard, which crashed on nil.
(cond
((and (numberp days-ahead) (= 0 days-ahead))
(org-schedule '(4)))
((and (numberp days-ahead) (cl-minusp days-ahead))
(org-schedule nil (current-time)))
(t
(let ((interval (if (numberp days-ahead) days-ahead next-interval)))
(org-schedule nil (time-add (current-time)
(days-to-time
(round interval)))))))))))
(defun org-drill-hypothetical-next-review-date (quality)
"Return aninteger representing the number of days into the future
that the current item would be scheduled, based on a recall quality
of QUALITY."
(let ((weight (org-entry-get (point) "DRILL_CARD_WEIGHT"))
(state (org-drill-get-item-data)))
(if (stringp weight)
(setq weight (string-to-number weight)))
(cl-destructuring-bind (next-interval _repetitions _ease
_failures _meanq _total-repeats
&optional _ofmatrix)
(cl-case org-drill-spaced-repetition-algorithm
(sm5 (org-drill-determine-next-interval-sm5
state quality org-drill-sm5-optimal-factor-matrix))
(sm2 (org-drill-determine-next-interval-sm2 state quality))
(simple8 (org-drill-determine-next-interval-simple8
state quality)))
(cond
((not (cl-plusp next-interval))
0)
((and (numberp weight) (cl-plusp weight))
(+ (org-drill-card-state-last-interval state)
(max 1.0 (/ (- next-interval
(org-drill-card-state-last-interval state))
weight))))
(t
next-interval)))))
(defun org-drill-hypothetical-next-review-dates ()
"Return hypothetical next review dates."
(let ((intervals nil))
(dotimes (quality 6)
(push (max (or (car intervals) 0)
(org-drill-hypothetical-next-review-date quality))
intervals))
(reverse intervals)))
(defun org-drill--read-key-sequence (prompt)
"Just like `read-key-sequence' but with input method turned off.
Uses `deactivate-input-method' / `activate-input-method' rather than
`set-input-method', because `(set-input-method nil)' clears
`default-input-method' as a side effect when no input method is
currently active (upstream issues #52 and #58)."
(let ((old-input-method current-input-method))
(unwind-protect
(progn
(when current-input-method
(deactivate-input-method))
(read-key-sequence prompt))
(when old-input-method
(activate-input-method old-input-method)))))
(defun org-drill--read-rating-key (typed-answer rating-help-block)
"Read a rating key (0-5) or control key for a drill prompt.
Loops on `read-key-sequence' until the user presses 0..5, the
configured quit key, the configured edit key, or C-g. Arrow / scroll
/ wheel events navigate the visible buffer. The configured tags key
runs `org-set-tags-command'. The configured help key flips the
prompt to include RATING-HELP-BLOCK above the standard prompt line.
Returns the chosen character.
TYPED-ANSWER, when non-nil, is rendered as a `Your answer: ...' line
above the prompt — used by the typed-answer flow to show the user
what they just typed.
RATING-HELP-BLOCK is the multi-line ratings explanation shown on
the help key. It's a plain string (no trailing newline), with the
caller supplying the wording appropriate to its scheduler (SM,
Leitner, etc.).
Shared by `org-drill-reschedule' and `org-drill-leitner-rebox'."
(let ((ch nil)
(input nil)
(typed-answer-statement (if typed-answer
(format "Your answer: %s\n" typed-answer)
""))
(key-prompt (format "(0-5, %c=help, %c=edit, %c=tags, %c=undo, %c=quit)"
org-drill--help-key
org-drill--edit-key
org-drill--tags-key
org-drill--undo-key
org-drill--quit-key)))
(save-excursion
(while (not (memq ch (list org-drill--quit-key
org-drill--edit-key
org-drill--undo-key
7 ; C-g
?0 ?1 ?2 ?3 ?4 ?5)))
(run-hooks 'org-drill-display-answer-hook)
(setq input (org-drill--read-key-sequence
(if (eq ch org-drill--help-key)
(format "%s\n\n%sHow well did you do? %s"
rating-help-block
typed-answer-statement
key-prompt)
(format "%sHow well did you do? %s"
typed-answer-statement key-prompt))))
(cond
((stringp input)
(setq ch (elt input 0)))
((and (vectorp input) (symbolp (elt input 0)))
(cl-case (elt input 0)
(up (ignore-errors (forward-line -1)))
(down (ignore-errors (forward-line 1)))
(left (ignore-errors (backward-char)))
(right (ignore-errors (forward-char)))
(prior (ignore-errors (scroll-down))) ; pgup
(next (ignore-errors (scroll-up))))) ; pgdn
((and (vectorp input) (listp (elt input 0))
(eventp (elt input 0)))
(cl-case (car (elt input 0))
(wheel-up (ignore-errors (mwheel-scroll (elt input 0))))
(wheel-down (ignore-errors (mwheel-scroll (elt input 0)))))))
(if (eql ch org-drill--tags-key)
(org-set-tags-command))))
ch))
(defun org-drill--snapshot-entry-data ()
"Capture the scheduling state of the entry at point for undo.
Returns (MARKER . DATA), where MARKER points at the entry heading and
DATA is an alist mapping each scheduling property (and the special
`scheduled' key) to its current value, or nil when unset. Restore it
with `org-drill--restore-entry-data'."
(cons (save-excursion (org-back-to-heading t) (point-marker))
(cons (cons 'scheduled (org-entry-get (point) "SCHEDULED"))
(mapcar (lambda (prop)
(cons prop (org-entry-get (point) prop)))
org-drill-scheduling-properties))))
(defun org-drill--restore-entry-data (snapshot)
"Restore the entry scheduling state captured in SNAPSHOT.
A property absent at snapshot time is deleted; the SCHEDULED line is put
back, or removed if there was none."
(org-with-point-at (car snapshot)
(dolist (cell (cdr snapshot))
(cond
((eq (car cell) 'scheduled)
(if (cdr cell)
(org-schedule nil (cdr cell))
(org-schedule '(4))))
((cdr cell)
(org-set-property (car cell) (cdr cell)))
(t
(org-delete-property (car cell)))))))
(defun org-drill--push-undo-snapshot (session)
"Snapshot the entry at point onto SESSION's undo stack, capped at
`org-drill-undo-limit'."
(push (org-drill--snapshot-entry-data) (oref session undo-stack))
(when (> (length (oref session undo-stack)) org-drill-undo-limit)
(setf (oref session undo-stack)
(cl-subseq (oref session undo-stack) 0 org-drill-undo-limit))))
(defun org-drill-undo-last-rating (session)
"Undo the most recent rating in SESSION.
Restore the card's pre-rating scheduling data, drop the recorded quality,
and re-queue the card so it is presented again. Does nothing when there
is nothing to undo."
(let ((snapshot (pop (oref session undo-stack))))
(if (null snapshot)
(message "Nothing to undo")
(org-drill--restore-entry-data snapshot)
(pop (oref session qualities))
(push (car snapshot) (oref session again-entries))
(message "Undid the last rating; that card will come around again"))))
(defun org-drill-reschedule (session)
"Return qualityrating (0-5), or nil if the user quit."
(let* ((next-review-dates (org-drill-hypothetical-next-review-dates))
(rating-help (format "0-2 Means you have forgotten the item.
3-5 Means you have remembered the item.
0 - Completely forgot.
1 - Even after seeing the answer, it still took a bit to sink in.
2 - After seeing the answer, you remembered it.
3 - It took you awhile, but you finally remembered. (+%s days)
4 - After a little bit of thought you remembered. (+%s days)
5 - You remembered the item really easily. (+%s days)"
(round (nth 3 next-review-dates))
(round (nth 4 next-review-dates))
(round (nth 5 next-review-dates)))))
(cl-block org-drill-reschedule
;; Loop so the undo key can take back the previous rating and then
;; return us to the prompt for the current card.
(while t
(let ((ch (org-drill--read-rating-key (oref session typed-answer)
rating-help)))
(cond
((eql ch org-drill--undo-key)
(org-drill-undo-last-rating session))
((and (>= ch ?0) (<= ch ?5))
(let ((quality (- ch ?0))
(failures (org-drill-entry-failure-count)))
(unless (oref session cram-mode)
;; Snapshot the pre-rating state so this rating can be undone.
(org-drill--push-undo-snapshot session)
(save-excursion
(let ((quality (if (org-drill--entry-lapsed-p session) 2 quality)))
(org-drill-smart-reschedule quality
(nth quality next-review-dates))))
(push quality (oref session qualities))
(cond
((org-drill--quality-failed-p quality)
(when org-drill-leech-failure-threshold
(if (> (1+ failures) org-drill-leech-failure-threshold)
(org-toggle-tag "leech" 'on))))
(t
(let ((scheduled-time (org-get-scheduled-time (point))))
(when scheduled-time
(message "Next review in %d days"
(- (time-to-days scheduled-time)
(time-to-days (current-time))))
(sit-for 0.5)))))
(org-set-property "DRILL_LAST_QUALITY" (format "%d" quality))
(org-set-property "DRILL_LAST_REVIEWED"
(org-drill-time-to-inactive-org-timestamp (current-time))))
(cl-return-from org-drill-reschedule quality)))
((eql ch org-drill--edit-key)
(cl-return-from org-drill-reschedule 'edit))
(t
(cl-return-from org-drill-reschedule nil))))))))
(defun org-drill-hide-subheadings-if (test)
"TEST is a function taking no arguments. TEST will be called for each
of the immediate subheadings of the current drill item, with the point
on the relevant subheading. TEST should return nil if the subheading is
to be revealed, non-nil if it is to be hidden.
Returns a list containing the position of each immediate subheading of
the current topic."
(let ((drill-entry-level (org-current-level))
(drill-sections nil))
(org-fold-show-subtree)
(save-excursion
(org-map-entries
(lambda ()
(when (and (not (outline-invisible-p))
(> (org-current-level) drill-entry-level))
(when (or (/= (org-current-level) (1+ drill-entry-level))
(funcall test))
(outline-hide-subtree))
(push (point) drill-sections)))
t 'tree))
(reverse drill-sections)))
(defun org-drill-hide-all-subheadings-except (heading-list)
"Hide all subheadings except HEADING-LIST."
(org-drill-hide-subheadings-if
(lambda () (let ((drill-heading (org-get-heading t)))
(not (member drill-heading heading-list))))))
(defun org-drill--quality-failed-p (quality)
"Non-nil when QUALITY counts as a failure for scheduling purposes.
A failure rating sends the card back to box 1 (Leitner) or resets
the interval (SM*). The threshold is `org-drill-failure-quality'."
(<= quality org-drill-failure-quality))
(defun org-drill--maybe-prepend-leech-warning (prompt)
"Prepend the leech warning to PROMPT when applicable.
The warning appears when `org-drill-leech-method' is `warn' and the
entry at point is a leech. Otherwise PROMPT is returned unchanged.
Used by the three prompt builders (mini-buffer prompt, in-buffer
prompt, prompt-for-string) to share the same preamble."
(if (and (eql 'warn org-drill-leech-method)
(org-drill-entry-leech-p))
(concat (propertize "!!! LEECH ITEM !!!
You seem to be having a lot of trouble memorising this item.
Consider reformulating the item to make it easier to remember.\n"
'face '(:foreground "red"))
prompt)
prompt))
(defcustom org-drill-show-outline-path-during-drill nil
"When non-nil, show the card's outline path in the drill prompt.
The ancestor headings are joined with \" > \" and bracketed, e.g.
\"[Spanish > Greetings]\", and prepended to the prompt so you can see
where the current card sits in the deck. Off by default."
:group 'org-drill-display
:type 'boolean)
(put 'org-drill-show-outline-path-during-drill 'safe-local-variable 'booleanp)
(defun org-drill--outline-path-string ()
"Return the outline path of the entry at point, bracketed for the prompt.
Joins the ancestor headings with \" > \" and wraps them in brackets with a
trailing space, e.g. \"[Spanish > Greetings] \". Returns the empty string
when the entry has no ancestors or point is not on a heading."
(let ((path (ignore-errors (org-get-outline-path))))
(if path
(concat "[" (mapconcat #'identity path " > ") "] ")
"")))
(defun org-drill--make-minibuffer-prompt (session prompt)
"Make a mini-buffer for the SESSION, with PROMPT."
(let ((status (cl-first (org-drill-entry-status session)))
(mature-entry-count (+ (length (oref session young-mature-entries))
(length (oref session old-mature-entries))
(length (oref session overdue-entries)))))
(format "%s %s %s %s %s %s"
(propertize
(char-to-string
(cond
((eql status :failed) ?F)
((oref session cram-mode) ?C)
(t
(cl-case status
(:new ?N) (:young ?Y) (:old ?o) (:overdue ?!)
(t ??)))))
'face `(:foreground
,(cl-case status
(:new org-drill-new-count-color)
((:young :old) org-drill-mature-count-color)
((:overdue :failed) org-drill-failed-count-color)
(t org-drill-done-count-color))))
(propertize
(number-to-string (length (oref session done-entries)))
'face `(:foreground ,org-drill-done-count-color)
'help-echo "The number of items you have reviewed this session.")
(propertize
(number-to-string (+ (length (oref session again-entries))
(length (oref session failed-entries))))
'face `(:foreground ,org-drill-failed-count-color)
'help-echo (concat "The number of items that you failed, "
"and need to review again."))
(propertize
(number-to-string mature-entry-count)
'face `(:foreground ,org-drill-mature-count-color)
'help-echo "The number of old items due for review.")
(propertize
(number-to-string (length (oref session new-entries)))
'face `(:foreground ,org-drill-new-count-color)
'help-echo (concat "The number of new items that you "
"have never reviewed."))
(if org-drill-show-outline-path-during-drill
(concat (org-drill--outline-path-string) prompt)
prompt))))
(defun org-drill-presentation-prompt (session &optional prompt)
"Create a card prompt with a timer and user-specified menu.
Arguments:
PROMPT: A string that overrides the standard prompt.
RETURNS: An alist of the form ((<char> . <symbol>)...) where
<char> is the character pressed and <symbol> is the
returned value, which will normally be either a symbol,
`t' or `nil'.
START-TIME: The time the card started to be displayed. This
defaults to (current-time), however, if the function
is called multiple times from one card then it might
be convenient to override this default.
"
(if org-drill-presentation-prompt-with-typing
(org-drill-presentation-prompt-in-buffer session prompt)
(org-drill-presentation-prompt-in-mini-buffer session prompt)))
(defun org-drill-presentation-prompt-in-mini-buffer (session &optional prompt)
"Prompt in the echo area for the user's response to the current card.
Show a running session timer, wait for a key, and handle the tags key by
editing tags and re-prompting. Return nil if the user quit, `edit' or
`skip' for those keys, and t for any other key (reveal the answer).
PROMPT overrides the standard \"Press key for answer\" prompt."
(let* ((item-start-time (current-time))
(input nil)
(ch nil)
(prompt
(or prompt
(format (concat "Press key for answer, "
"%c=edit, %c=tags, %c=skip, %c=quit.")
org-drill--edit-key
org-drill--tags-key
org-drill--skip-key
org-drill--quit-key)))
(full-prompt
(org-drill--make-minibuffer-prompt session prompt)))
(setq full-prompt (org-drill--maybe-prepend-leech-warning full-prompt))
(while (memq ch '(nil org-drill--tags-key))
(setq ch nil)
(while (not (input-pending-p))
(let ((elapsed (time-subtract (current-time) item-start-time)))
(message (concat (if (>= (time-to-seconds elapsed) (* 60 60))
"++:++ "
(format-time-string "%M:%S " elapsed))
full-prompt))
(sit-for 1)))
(setq input (org-drill--read-key-sequence nil))
(if (stringp input) (setq ch (elt input 0)))
(if (eql ch org-drill--tags-key)
(org-set-tags-command)))
(cond
((eql ch org-drill--quit-key) nil)
((eql ch org-drill--edit-key) 'edit)
((eql ch org-drill--skip-key) 'skip)
(t t))))
(defvar org-drill-presentation-timer nil
"Timer for buffer-entry of answers")
(defvar org-drill-presentation-timer-calls 0
"How many times the presentation timer has been called")
(defun org-drill-presentation-timer-cancel ()
"Cancel the presentation timer."
(when org-drill-presentation-timer
(cancel-timer org-drill-presentation-timer))
(setq org-drill-presentation-timer nil)
(setq org-drill-presentation-timer-calls 0))
(defun org-drill-presentation-minibuffer-timer-function
(item-start-time full-prompt)
"Return prompt for mini-buffer in `org-drill-response-mode'."
(let ((elapsed (time-subtract (current-time) item-start-time)))
(message (concat (if (>= (time-to-seconds elapsed) (* 60 60))
"++:++ "
(format-time-string "%M:%S " elapsed))
full-prompt)))
;; if we have done it this many times, we probably want to stop
(when (< 10 (cl-incf org-drill-presentation-timer-calls))
(org-drill-presentation-timer-cancel)))
(define-derived-mode org-drill-response-mode nil "Org-Drill")
(define-key org-drill-response-mode-map [return] 'org-drill-response-rtn)
(define-key org-drill-response-mode-map (kbd "C-c C-q") 'org-drill-response-quit)
(define-key org-drill-response-mode-map (kbd "C-c C-e") 'org-drill-response-edit)
(define-key org-drill-response-mode-map (kbd "C-c C-s") 'org-drill-response-skip)
(define-key org-drill-response-mode-map (kbd "C-c C-t") 'org-drill-response-tags)
(defun org-drill-response-complete ()
"Complete org-drill response mode."
(kill-buffer (current-buffer))
(exit-recursive-edit))
(defun org-drill-response-rtn ()
"Exit response mode with return value."
(interactive)
(let ((session org-drill-current-session))
(setf (oref session typed-answer) (buffer-string))
(oset session exit-kind t)
(org-drill-response-complete)))
(defun org-drill-response-quit ()
"Exit response mode with quit."
(interactive)
(oset org-drill-current-session exit-kind 'quit)
(org-drill-response-complete))
(defun org-drill-response-edit ()
"Exit response mode with edit."
(interactive)
(oset org-drill-current-session exit-kind 'edit)
(org-drill-response-complete))
(defun org-drill-response-skip ()
"Exit response mode with skip."
(interactive)
(oset org-drill-current-session exit-kind 'skip)
(org-drill-response-complete))
(defun org-drill-response-tags ()
"Exit response mode with tags."
(interactive)
(oset org-drill-current-session exit-kind 'tags)
(org-drill-response-complete))
(defun org-drill-response-get-buffer-create ()
"Create a response buffer.
Propagates the caller's input method into the new buffer, but only when
one is actually active — otherwise `(set-input-method nil)' would clear
`default-input-method' as a side effect (upstream issues #52, #58)."
(let ((local-current-input-method
current-input-method))
(with-current-buffer
(get-buffer-create "*Org-Drill*")
(erase-buffer)
(org-drill-response-mode)
(when local-current-input-method
(activate-input-method local-current-input-method))
(current-buffer))))
(defun org-drill-presentation-prompt-in-buffer (session &optional prompt)
"Display drill for SESSION with PROMPT."
(let* ((item-start-time (current-time))
(prompt
(or prompt
(format (concat "Type answer then return, "
"C-c C-e=edit, C-c C-t=tags, C-c C-s=skip, C-c C-q=quit."))))
(full-prompt
(org-drill--make-minibuffer-prompt session prompt)))
(setf (oref session drill-answer) nil)
(setq full-prompt (org-drill--maybe-prepend-leech-warning full-prompt))
(org-drill-presentation-timer-cancel)
(setq org-drill-presentation-timer
(run-with-idle-timer 1 t
#'org-drill-presentation-minibuffer-timer-function
item-start-time full-prompt)
org-drill-presentation-timer-calls 0)
(unwind-protect
(save-window-excursion
(let ((buf
(org-drill-response-get-buffer-create)))
(select-window
(display-buffer buf '(display-buffer-below-selected)))
;; Store the current session in a variable, so that it can
;; be picked up by the when we leave the buffer
(setq-local org-drill-current-session session)
(recursive-edit)
(oref session exit-kind)))
;; Always cancel timer, even if recursive-edit exits abnormally
(org-drill-presentation-timer-cancel))))
(cl-defun org-drill-presentation-prompt-for-string (session prompt)
"Create a card prompt with a timer and user-specified menu.
Arguments:
PROMPT: A string that overrides the standard prompt.
START-TIME: The time the card started to be displayed. This
defaults to (current-time), however, if the function
is called multiple times from one card then it might
be convenient to override this default.
"
(let* ((prompt
(or prompt
"Type your answer and press <Enter>: "))
(full-prompt
(org-drill--make-minibuffer-prompt session prompt)))
(setq full-prompt (org-drill--maybe-prepend-leech-warning full-prompt))
(setf (oref session drill-answer)
(read-string full-prompt nil nil nil t))))
(defun org-drill-pos-in-regexp (pos regexp &optional nlines)
"Return non-nil if POS is within REGEXP.
Normally only the current line is checked but NLINES can be checked instead.
If non-nil, returns (BEG . END) where beginning and end of the match are."
(save-excursion
(goto-char pos)
(org-in-regexp regexp nlines)))
(defun org-drill-hide-region (beg end &optional text)
"Hide the buffer region between BEG and END with an \\='invisible text\\='
visual overlay, or with the string TEXT if it is supplied."
(let ((ovl (make-overlay beg end)))
(overlay-put ovl 'category
'org-drill-hidden-text-overlay)
(overlay-put ovl 'priority 9999)
(when (stringp text)
(overlay-put ovl 'invisible nil)
(overlay-put ovl 'face 'default)
(overlay-put ovl 'display text))))
(defun org-drill-hide-heading-at-point (&optional text)
"Hide the heading at point."
(unless (org-at-heading-p)
(error "Point is not on a heading"))
(save-excursion
(let ((beg (point)))
(end-of-line)
(org-drill-hide-region beg (point) text))))
(defun org-drill-hide-comments ()
"Hide comments."
(save-excursion
(while (re-search-forward "^#.*$" nil t)
(org-drill-hide-region (match-beginning 0) (match-end 0)))))
(defun org-drill-hide-drawers ()
"Hide all drawers in the current entry, including PROPERTIES drawer.
This is more reliable than `org-cycle-hide-drawers' for drill display.
Drawer-end was previously captured as the value of `(point)' after
the search — always a number, so the subsequent `(when drawer-end)'
guard was dead. Capture the search result itself and gate on that,
otherwise a malformed drawer (no `:END:') ends up with a junk
overlay covering whatever range point happened to be at."
(save-excursion
(org-back-to-heading t)
(let ((end (save-excursion (org-end-of-subtree t t))))
(while (re-search-forward org-drawer-regexp end t)
(let* ((drawer-start (match-beginning 0))
(drawer-end (save-excursion
(when (re-search-forward
"^[ \t]*:END:[ \t]*$" end t)
(point)))))
(when drawer-end
(org-drill-hide-region drawer-start drawer-end)))))))
(defun org-drill--setup-display ()
"Set up display settings for drill session.
Saves current settings and applies drill-specific display preferences.
Records the current buffer so that `org-drill--restore-display' can
target it even if the user switches buffers mid-session."
(setq org-drill--saved-display-buffer (current-buffer))
;; Save current text scale and apply new size (face is global).
(when org-drill-text-size-during-session
(setq org-drill--saved-text-scale
(face-attribute 'default :height nil 'default))
(set-face-attribute 'default nil :height (* org-drill-text-size-during-session 10)))
;; Save and enable variable-pitch mode (buffer-local minor mode).
(when org-drill-use-variable-pitch
(setq org-drill--saved-variable-pitch-mode
(if (boundp 'variable-pitch-mode) variable-pitch-mode nil))
(variable-pitch-mode 1))
;; Save and hide modeline (buffer-local).
(when org-drill-hide-modeline-during-session
(setq org-drill--saved-modeline-format mode-line-format)
(setq-local mode-line-format nil)))
(defun org-drill--restore-display ()
"Restore display settings after drill session ends.
Buffer-local state (mode-line, variable-pitch-mode) is restored in
the buffer the session originated from — not the current buffer at
restore time, which may differ if the user switched away."
(let ((target (and (buffer-live-p org-drill--saved-display-buffer)
org-drill--saved-display-buffer)))
;; Restore text size (face is global — no buffer targeting needed).
(when org-drill--saved-text-scale
(set-face-attribute 'default nil :height org-drill--saved-text-scale)
(setq org-drill--saved-text-scale nil))
;; Restore variable-pitch-mode in the original buffer.
(when (and org-drill-use-variable-pitch
(not (eq org-drill--saved-variable-pitch-mode 'unbound)))
(when target
(with-current-buffer target
(variable-pitch-mode (if org-drill--saved-variable-pitch-mode 1 -1))))
(setq org-drill--saved-variable-pitch-mode nil))
;; Restore modeline in the original buffer.
(when org-drill--saved-modeline-format
(when target
(with-current-buffer target
(setq-local mode-line-format org-drill--saved-modeline-format)))
(setq org-drill--saved-modeline-format nil))
(setq org-drill--saved-display-buffer nil)))
(defun org-drill-unhide-text ()
"Unhide text."
(save-excursion
(dolist (ovl (overlays-in (point-min) (point-max)))
(when (eql 'org-drill-hidden-text-overlay (overlay-get ovl 'category))
(delete-overlay ovl)))))
(defun org-drill-hide-clozed-text ()
"Hide clozed text."
(save-excursion
(while (re-search-forward org-drill-cloze-regexp nil t)
;; Don't hide:
;; - org links, partly because they might contain inline
;; images which we want to keep visible.
;; - LaTeX math fragments
;; - the contents of SRC blocks
(unless (save-match-data
(or (org-drill-pos-in-regexp (match-beginning 0)
org-link-bracket-re 1)
(org-in-src-block-p)
(org-inside-LaTeX-fragment-p)))
(org-drill-hide-matched-cloze-text)))))
(defun org-drill-hide-matched-cloze-text ()
"Hide the current match with a \\='cloze\\=' visual overlay."
(let ((ovl (make-overlay (match-beginning 0) (match-end 0)))
(hint-sep-pos (string-match-p (regexp-quote org-drill-hint-separator)
(match-string 0))))
(overlay-put ovl 'category
'org-drill-cloze-overlay-defaults)
(overlay-put ovl 'priority 9999)
(if org-drill-cloze-length-matches-hidden-text-p
(overlay-put ovl 'display
(concat "[" (make-string (max 1 (- (length (match-string 0)) 2)) ?.) "]")))
(when (and hint-sep-pos
(> hint-sep-pos 1))
(let ((hint (substring-no-properties
(match-string 0)
(+ hint-sep-pos (length org-drill-hint-separator))
(1- (length (match-string 0))))))
(overlay-put
ovl 'display
;; If hint is like `X...' then display [X...]
;; otherwise display [...X]
(format "[%s%s%s]"
hint
(if (string-match-p (regexp-quote "...") hint) "" "...")
(if org-drill-cloze-length-matches-hidden-text-p
(make-string (max 0 (- (length (match-string 0))
(length hint)
(if (string-match-p (regexp-quote "...") hint) 0 3)
2))
?.)
"")))))))
(defun org-drill-hide-cloze-hints ()
"Hide cloze hints.
The cloze regex's hint group is `\\(\\|HINTSEP.+?\\)' — an empty
alternation — so it always participates in the match. Check for an
empty match (start = end) rather than nil match-beginning, otherwise
this function creates a zero-width overlay for every hint-less cloze."
(save-excursion
(while (re-search-forward org-drill-cloze-regexp nil t)
(unless (or (save-match-data
(org-drill-pos-in-regexp (match-beginning 0)
org-link-bracket-re 1))
;; Empty match → no hint present, nothing to hide.
(= (match-beginning 2) (match-end 2)))
(org-drill-hide-region (match-beginning 2) (match-end 2))))))
(defmacro org-drill-with-replaced-entry-text (text &rest body)
"During the execution of BODY, the entire text of the current entry is
concealed by an overlay that displays the string TEXT."
`(progn
(org-drill-replace-entry-text ,text)
(unwind-protect
(progn
,@body)
(org-drill-unreplace-entry-text))))
(defmacro org-drill-with-replaced-entry-text-multi (replacements &rest body)
"During the execution of BODY, the entire text of the current entry is
concealed by an overlay that displays the overlays in REPLACEMENTS."
`(progn
(org-drill-replace-entry-text ,replacements t)
(unwind-protect
(progn
,@body)
(org-drill-unreplace-entry-text))))
(defun org-drill-replace-entry-text (text &optional multi-p)
"Make an overlay that conceals the entire text of the item, not
including properties or the contents of subheadings. The overlay shows
the string TEXT.
If MULTI-P is non-nil, TEXT must be a list of values which are legal
for the `display' text property. The text of the item will be temporarily
replaced by all of these items, in the order in which they appear in
the list.
Note: does not actually alter the item."
(cond
((and multi-p
(listp text))
(org-drill-replace-entry-text-multi text))
(t
(let ((ovl (make-overlay (point-min)
(save-excursion
(outline-next-heading)
(point)))))
(overlay-put ovl 'priority 9999)
(overlay-put ovl 'category
'org-drill-replaced-text-overlay)
(overlay-put ovl 'display text)))))
(defun org-drill-unreplace-entry-text ()
"Unreplace entry text."
(save-excursion
(dolist (ovl (overlays-in (point-min) (point-max)))
(when (eql 'org-drill-replaced-text-overlay (overlay-get ovl 'category))
(delete-overlay ovl)))))
(defun org-drill-replace-entry-text-multi (replacements)
"Make overlays that conceal the entire text of the item, not
including properties or the contents of subheadings. The overlay shows
the string TEXT.
Note: does not actually alter the item."
(let ((ovl nil)
(p-min (point-min))
(p-max (save-excursion
(outline-next-heading)
(point))))
(cl-assert (>= (- p-max p-min) (length replacements)))
(dotimes (i (length replacements))
(setq ovl (make-overlay (+ p-min (* 2 i))
(if (= i (1- (length replacements)))
p-max
(+ p-min (* 2 i) 1))))
(overlay-put ovl 'priority 9999)
(overlay-put ovl 'category
'org-drill-replaced-text-overlay)
(overlay-put ovl 'display (nth i replacements)))))
(defmacro org-drill-with-replaced-entry-heading (heading &rest body)
"Display HEADING in place of current entry heading, and execute BODY."
`(progn
(org-drill-replace-entry-heading ,heading)
(unwind-protect
(progn
,@body)
(org-drill-unhide-text))))
(defun org-drill-replace-entry-heading (heading)
"Make an overlay that conceals the heading of the item. The overlay shows
the string TEXT.
Note: does not actually alter the item."
(org-drill-hide-heading-at-point heading))
(defun org-drill-unhide-clozed-text ()
"Show clozed text."
(save-excursion
(dolist (ovl (overlays-in (point-min) (point-max)))
(when (eql 'org-drill-cloze-overlay-defaults (overlay-get ovl 'category))
(delete-overlay ovl)))))
(defcustom org-drill-entry-text-max-lines
100
"Maximum number of lines of an entry's body text org-drill collects.
Used by `org-drill-get-entry-text', for example when echoing the next
Leitner item. Raise it for decks with very long card bodies."
:group 'org-drill-session
:type 'integer)
(defun org-drill-get-entry-text (&optional keep-properties-p)
"Return the text of the current entry."
(let ((text (org-agenda-get-some-entry-text
(point-marker) org-drill-entry-text-max-lines)))
(if keep-properties-p
text
(substring-no-properties text))))
(defun org-drill-entry-empty-p ()
"Return non-nil if the current entry is empty.
The search bound covers the whole subtree, so an entry whose answer
lives inside a child sub-heading is correctly reported as non-empty
\(upstream issue #13). `org-end-of-subtree' is used in place of
`outline-next-heading' because the latter lands on the first child
heading, which truncates the search range before the child's body."
(save-excursion
(org-back-to-heading t)
(let ((lim (save-excursion
(org-end-of-subtree t t) (point))))
(if (fboundp 'org-end-of-meta-data-and-drawers)
(org-end-of-meta-data-and-drawers) ; function removed Feb 2015
(org-end-of-meta-data t))
(or (>= (point) lim)
(null (re-search-forward "[[:graph:]]" lim t))))))
;;; Presentation functions ====================================================
;;
;; Each of these is called with point on topic heading. Each needs to show the
;; topic in the form of a 'question' or with some information 'hidden', as
;; appropriate for the card type. The user should then be prompted to press a
;; key. The function should then reveal either the 'answer' or the entire
;; topic, and should return t if the user chose to see the answer and rate their
;; recall, nil if they chose to quit.
(defun org-drill-present-simple-card (session)
"Present a simple card."
(org-drill-with-card-display
(org-drill-hide-all-subheadings-except nil)
(org-drill--show-latex-fragments) ; overlay all LaTeX fragments with images
(ignore-errors
(org-display-inline-images t))
(org-drill-hide-drawers)
(prog1 (org-drill-presentation-prompt session)
(org-drill-hide-subheadings-if 'org-drill-entry-p))))
(defun org-drill-present-default-answer (session reschedule-fn)
"Present a default answer.
SESSION is the current session.
RESCHEDULE-FN is the function to reschedule."
(prog1 (cond
((oref session drill-answer)
(org-drill-with-replaced-entry-text
(format "\nAnswer:\n\n %s\n" (oref session drill-answer))
(funcall reschedule-fn session)
))
(t
(org-drill-hide-subheadings-if 'org-drill-entry-p)
(org-drill-unhide-clozed-text)
(org-drill--show-latex-fragments)
(ignore-errors
(org-display-inline-images t))
(org-drill-hide-drawers)
;; LaTeX preview helpers require a window-system frame
;; (upstream issue #44). Skip on TTY.
(when (display-graphic-p)
(org-clear-latex-preview)
(save-excursion
(org-mark-subtree)
(let ((beg (region-beginning))
(end (region-end)))
(org--latex-preview-region beg end))
(deactivate-mark)))
(org-drill-with-hidden-cloze-hints
(funcall reschedule-fn session))))))
(defun org-drill-present-simple-card-with-typed-answer (session)
"Present a simple card with a typed answer."
(org-drill-with-card-display
(org-drill-hide-all-subheadings-except nil)
(org-drill--show-latex-fragments) ; overlay all LaTeX fragments with images
(ignore-errors
(org-display-inline-images t))
(org-drill-hide-drawers)
(prog1 (org-drill-presentation-prompt-for-string session nil)
(org-drill-hide-subheadings-if 'org-drill-entry-p))))
(defun org-drill--show-latex-fragments ()
"Show LaTeX fragments as inline images.
No-op on TTY frames — `org-latex-preview' requires a window system
and otherwise raises \"Window system frame should be used\" (upstream
issue #44, 2021). The TTY user just doesn't see preview images,
which is the right behavior."
(when (display-graphic-p)
(org-clear-latex-preview)
(org-latex-preview '(16))))
(defun org-drill-present-two-sided-card (session)
"Present a two-sided card for SESSION.
Hide every subheading, reveal one of the first two at random as the
question side, prompt, then re-hide on the way out."
(org-drill-with-card-display
(let ((drill-sections (org-drill-hide-all-subheadings-except nil)))
(when drill-sections
(save-excursion
(goto-char (nth (cl-random (min 2 (length drill-sections)))
drill-sections))
(org-fold-show-subtree)))
(org-drill--show-latex-fragments)
(ignore-errors
(org-display-inline-images t))
(org-drill-hide-drawers)
(prog1 (org-drill-presentation-prompt session)
(org-drill-hide-subheadings-if 'org-drill-entry-p)))))
(defun org-drill-present-multi-sided-card (session)
"Present a multi-sided card for SESSION.
Hide every subheading, reveal one at random as the question side,
prompt, then re-hide on the way out."
(org-drill-with-card-display
(let ((drill-sections (org-drill-hide-all-subheadings-except nil)))
(when drill-sections
(save-excursion
(goto-char (nth (cl-random (length drill-sections)) drill-sections))
(org-fold-show-subtree)))
(org-drill--show-latex-fragments)
(ignore-errors
(org-display-inline-images t))
(org-drill-hide-drawers)
(prog1 (org-drill-presentation-prompt session)
(org-drill-hide-subheadings-if 'org-drill-entry-p)))))
(defun org-drill--cloze-body-bounds ()
"Return (BODY-START . ITEM-END) for the drill item at point.
BODY-START is the position just after the property drawer or metadata,
and ITEM-END is the start of the next heading."
(let ((body-start (let ((prop-block (org-get-property-block)))
(if prop-block
(cdr prop-block)
(save-excursion
(org-back-to-heading t)
(if (fboundp 'org-end-of-meta-data-and-drawers)
(org-end-of-meta-data-and-drawers)
(org-end-of-meta-data t))
(point)))))
(item-end (save-excursion (outline-next-heading) (point))))
(cons body-start item-end)))
(defun org-drill--count-cloze-matches (body-start item-end)
"Count cloze regions between BODY-START and ITEM-END.
Matches inside an org link or a LaTeX fragment are skipped, so the count
matches the indices `org-drill--hide-cloze-by-index' will hide."
(let ((count 0))
(save-excursion
(goto-char body-start)
(while (re-search-forward org-drill-cloze-regexp item-end t)
(unless (save-match-data
(or (org-drill-pos-in-regexp (match-beginning 0)
org-link-bracket-re 1)
(org-inside-LaTeX-fragment-p)))
(cl-incf count))))
count))
(defun org-drill--hide-cloze-by-index (body-start item-end indices)
"Hide the cloze regions whose 1-based position is in INDICES.
Scans between BODY-START and ITEM-END with the same link/LaTeX skip as
`org-drill--count-cloze-matches', so the indices line up."
(save-excursion
(goto-char body-start)
(let ((cnt 0))
(while (re-search-forward org-drill-cloze-regexp item-end t)
(unless (save-match-data
(or (org-drill-pos-in-regexp (match-beginning 0)
org-link-bracket-re 1)
(org-inside-LaTeX-fragment-p)))
(cl-incf cnt)
(when (memq cnt indices)
(org-drill-hide-matched-cloze-text)))))))
(defun org-drill-present-multicloze-hide-n (session
number-to-hide
&optional
force-show-first
force-show-last
force-hide-first)
"Hides NUMBER-TO-HIDE pieces of text that are marked for cloze deletion,
chosen at random.
If NUMBER-TO-HIDE is negative, show only (ABS NUMBER-TO-HIDE) pieces,
hiding all the rest.
If FORCE-HIDE-FIRST is non-nil, force the first piece of text to be one of
the hidden items.
If FORCE-SHOW-FIRST is non-nil, never hide the first piece of text.
If FORCE-SHOW-LAST is non-nil, never hide the last piece of text.
If the number of text pieces in the item is less than
NUMBER-TO-HIDE, then all text pieces will be hidden (except the first or last
items if FORCE-SHOW-FIRST or FORCE-SHOW-LAST is non-nil)."
(when (and force-hide-first force-show-first)
(error "FORCE-HIDE-FIRST and FORCE-SHOW-FIRST are mutually exclusive"))
(org-drill-with-hidden-comments
(org-drill-with-hidden-cloze-hints
(org-drill-hide-all-subheadings-except nil)
(let* ((bounds (org-drill--cloze-body-bounds))
(body-start (car bounds))
(item-end (cdr bounds))
(match-count (org-drill--count-cloze-matches body-start item-end)))
(when (cl-minusp number-to-hide)
(setq number-to-hide (+ match-count number-to-hide)))
(when (cl-plusp match-count)
(let ((positions (org-drill-shuffle
(cl-loop for i from 1 to match-count collect i))))
(when force-hide-first
;; Force 1 into the list, and to the front of it.
(setq positions (cons 1 (remove 1 positions))))
(when force-show-first
(setq positions (remove 1 positions)))
(when force-show-last
(setq positions (remove match-count positions)))
(org-drill--hide-cloze-by-index
body-start item-end
(cl-subseq positions 0 (min number-to-hide (length positions)))))))
(org-drill--show-latex-fragments)
(ignore-errors
(org-display-inline-images t))
(org-drill-hide-drawers)
(prog1 (org-drill-presentation-prompt session)
(org-drill-hide-subheadings-if 'org-drill-entry-p)
(org-drill-unhide-clozed-text)))))
(defun org-drill-present-multicloze-hide-nth (session to-hide)
"Hide the TO-HIDE'th piece of clozed text. 1 is the first piece. If
TO-HIDE is negative, count backwards, so -1 means the last item, -2
the second to last, etc."
(org-drill-with-hidden-comments
(org-drill-with-hidden-cloze-hints
(org-drill-hide-all-subheadings-except nil)
(let* ((bounds (org-drill--cloze-body-bounds))
(body-start (car bounds))
(item-end (cdr bounds))
(match-count (org-drill--count-cloze-matches body-start item-end)))
(when (cl-minusp to-hide)
(setq to-hide (+ 1 to-hide match-count)))
(when (and (cl-plusp match-count) (<= to-hide match-count))
(org-drill--hide-cloze-by-index body-start item-end (list to-hide))))
(org-drill--show-latex-fragments)
(ignore-errors
(org-display-inline-images t))
(org-drill-hide-drawers)
(prog1 (org-drill-presentation-prompt session)
(org-drill-hide-subheadings-if 'org-drill-entry-p)
(org-drill-unhide-clozed-text)))))
(defun org-drill-present-multicloze-hide1 (session)
"Hides one of the pieces of text that are marked for cloze deletion,
chosen at random."
(org-drill-present-multicloze-hide-n session 1))
(defun org-drill-present-multicloze-hide2 (session)
"Hides two of the pieces of text that are marked for cloze deletion,
chosen at random."
(org-drill-present-multicloze-hide-n session 2))
(defun org-drill-present-multicloze-hide-first (session)
"Hides the first piece of text that is marked for cloze deletion."
(org-drill-present-multicloze-hide-nth session 1))
(defun org-drill-present-multicloze-hide-last (session)
"Hides the last piece of text that is marked for cloze deletion."
(org-drill-present-multicloze-hide-nth session -1))
(defun org-drill-present-multicloze-hide1-firstmore (session)
"Commonly, hides the FIRST piece of text that is marked for
cloze deletion. Uncommonly, hide one of the other pieces of text,
chosen at random.
The definitions of \\='commonly\\=' and \\='uncommonly\\=' are determined by
the value of `org-drill-cloze-text-weight'."
;; The 'firstmore' and 'lastmore' functions used to randomly choose whether
;; to hide the 'favoured' piece of text. However even when the chance of
;; hiding it was set quite high (80%), the outcome was too unpredictable over
;; the small number of repetitions where most learning takes place for each
;; item. In other words, the actual frequency during the first 10 repetitions
;; was often very different from 80%. Hence we use modulo instead.
(cond
((null org-drill-cloze-text-weight)
;; Behave as hide1cloze
(org-drill-present-multicloze-hide1 session))
((not (and (integerp org-drill-cloze-text-weight)
(cl-plusp org-drill-cloze-text-weight)))
(error "Illegal value for org-drill-cloze-text-weight: %S"
org-drill-cloze-text-weight))
((zerop (mod (1+ (org-drill-entry-total-repeats 0))
org-drill-cloze-text-weight))
;; Uncommonly, hide any item except the first
(org-drill-present-multicloze-hide-n session 1 t))
(t
;; Commonly, hide first item
(org-drill-present-multicloze-hide-first session))))
(defun org-drill-present-multicloze-show1-lastmore (session)
"Commonly, hides all pieces except the last. Uncommonly, shows
any random piece. The effect is similar to \\='show1cloze\\=' except
that the last item is much less likely to be the item that is
visible.
The definitions of \\='commonly\\=' and \\='uncommonly\\=' are determined by
the value of `org-drill-cloze-text-weight'."
(cond
((null org-drill-cloze-text-weight)
;; Behave as show1cloze
(org-drill-present-multicloze-show1 session))
((not (and (integerp org-drill-cloze-text-weight)
(cl-plusp org-drill-cloze-text-weight)))
(error "Illegal value for org-drill-cloze-text-weight: %S"
org-drill-cloze-text-weight))
((zerop (mod (1+ (org-drill-entry-total-repeats 0))
org-drill-cloze-text-weight))
;; Uncommonly, show any item except the last
(org-drill-present-multicloze-hide-n session -1 nil nil t))
(t
;; Commonly, show the LAST item
(org-drill-present-multicloze-hide-n session -1 nil t))))
(defun org-drill-present-multicloze-show1-firstless (session)
"Commonly, hides all pieces except one, where the shown piece
is guaranteed NOT to be the first piece. Uncommonly, shows any
random piece. The effect is similar to \\='show1cloze\\=' except that
the first item is much less likely to be the item that is
visible.
The definitions of \\='commonly\\=' and \\='uncommonly\\=' are determined by
the value of `org-drill-cloze-text-weight'."
(cond
((null org-drill-cloze-text-weight)
;; Behave as show1cloze
(org-drill-present-multicloze-show1 session))
((not (and (integerp org-drill-cloze-text-weight)
(cl-plusp org-drill-cloze-text-weight)))
(error "Illegal value for org-drill-cloze-text-weight: %S"
org-drill-cloze-text-weight))
((zerop (mod (1+ (org-drill-entry-total-repeats 0))
org-drill-cloze-text-weight))
;; Uncommonly, show the first item
(org-drill-present-multicloze-hide-n session -1 t))
(t
;; Commonly, show any item, except the first
(org-drill-present-multicloze-hide-n session -1 nil nil t))))
(defun org-drill-present-multicloze-show1 (session)
"Similar to `org-drill-present-multicloze-hide1', but hides all
the pieces of text that are marked for cloze deletion, except for one
piece which is chosen at random."
(org-drill-present-multicloze-hide-n session -1))
(defun org-drill-present-multicloze-show2 (session)
"Similar to `org-drill-present-multicloze-show1', but reveals two
pieces rather than one."
(org-drill-present-multicloze-hide-n session -2))
(defun org-drill-present-card-using-text (session question &optional answer)
"Present the string QUESTION as the only visible content of the card.
If ANSWER is supplied, set the session slot `drill-answer' to its value."
(if answer (setf (oref session drill-answer) answer))
(org-drill-with-hidden-comments
(org-drill-with-replaced-entry-text
(concat "\n" question)
(org-drill-hide-all-subheadings-except nil)
(org-drill-hide-drawers)
(ignore-errors
(org-display-inline-images t))
(prog1 (org-drill-presentation-prompt session)
(org-drill-hide-subheadings-if 'org-drill-entry-p)))))
(defun org-drill-entry (session)
"Present the current topic for interactive review, as in `org-drill'.
Review will occur regardless of whether the topic is due for review or whether
it meets the definition of a \\='review topic\\=' used by `org-drill'.
Returns a quality rating from 0 to 5, or nil if the user quit, or the symbol
EDIT if the user chose to exit the drill and edit the current item. Choosing
the latter option leaves the drill session suspended; it can be resumed
later using `org-drill-resume'.
See `org-drill' for more details."
(org-drill-entry-f session 'org-drill-reschedule))
(defun org-drill-card-tag-caller (item tag)
"Call the ITEM-th hook function registered for TAG.
Looks TAG up in `org-drill-card-tags-alist' and calls the function at
position ITEM in that entry. Does nothing if TAG has no entry."
(funcall
(or (nth item (assoc tag org-drill-card-tags-alist))
'ignore)))
(defun org-drill--resolve-presenter (card-type)
"Return (PRESENTATION-FN . ANSWER-FN) for CARD-TYPE.
Looks CARD-TYPE up in `org-drill-card-type-alist'. The mapped value is
either a bare presentation function or a list (PRESENTATION-FN ANSWER-FN
...). ANSWER-FN defaults to `org-drill-present-default-answer'.
PRESENTATION-FN is nil when CARD-TYPE is unrecognised."
(let ((entry (cdr (assoc card-type org-drill-card-type-alist))))
(if (listp entry)
(cons (cl-first entry)
(or (cl-second entry) 'org-drill-present-default-answer))
(cons entry 'org-drill-present-default-answer))))
(defun org-drill--classify-presentation-result (cont)
"Classify CONT, the value returned by a card's presentation function.
Returns `quit' (nil — the user quit), `edit', `skip', or `answer' (any
other non-nil value means reveal the answer)."
(cond
((not cont) 'quit)
((eql cont 'edit) 'edit)
((eql cont 'skip) 'skip)
(t 'answer)))
(defun org-drill-entry-f (session complete-func)
"Present the drill entry at point for SESSION and return its outcome.
Resolve the card's presenter from its DRILL_CARD_TYPE, run it, and
dispatch on the result: nil (quit), `edit', `skip', or otherwise reveal
the answer and call COMPLETE-FUNC (typically a reschedule function).
Returns the value the answer presenter returned, or `skip'/`edit'/nil."
(org-drill-goto-drill-entry-heading)
(let ((card-type (org-entry-get (point) "DRILL_CARD_TYPE" t)))
;; fontification functions in `outline-view-change-hook' (obsolete in Emacs 29.1)
;; can cause big slowdowns, so we no longer bind it since we require modern Emacs
(setf (oref session drill-answer) nil)
(org-save-outline-visibility t
(save-restriction
(org-narrow-to-subtree)
(org-fold-show-subtree)
(org-drill-hide-drawers)
(cl-destructuring-bind (presentation-fn . answer-fn)
(org-drill--resolve-presenter card-type)
(let* ((tags (org-get-tags))
(rtn
(if (null presentation-fn)
(progn
(message "%s:%d: Unrecognised card type '%s', skipping..."
(buffer-name) (point) card-type)
(sit-for 0.5)
'skip)
(mapc (apply-partially 'org-drill-card-tag-caller 1) tags)
(cl-case (org-drill--classify-presentation-result
(funcall presentation-fn session))
(quit (message "Quit") nil)
(edit 'edit)
(skip 'skip)
(answer
(save-excursion
(mapc (apply-partially 'org-drill-card-tag-caller 2) tags)
(funcall answer-fn session complete-func)))))))
(mapc (apply-partially 'org-drill-card-tag-caller 3) tags)
(cl-incf org-drill-cards-in-this-emacs)
rtn))))))
(defun org-drill-entries-pending-p (session)
"Return non-nil if SESSION still has entries left to drill.
True when an item is in progress or queued, unless the session's
item-count or duration limit has been reached.
When `org-drill-on-timeout-action' is `discard-current' and the
duration limit has been reached, the in-progress card and the
again-queue stop counting as pending, so the session ends now
rather than finishing them."
(unless (and (eq org-drill-on-timeout-action 'discard-current)
(org-drill-maximum-duration-reached-p session))
(or (oref session again-entries)
(oref session current-item)
(and (not (org-drill-maximum-item-count-reached-p session))
(not (org-drill-maximum-duration-reached-p session))
(or (oref session new-entries)
(oref session failed-entries)
(oref session young-mature-entries)
(oref session old-mature-entries)
(oref session overdue-entries)
(oref session again-entries))))))
(defun org-drill-pending-entry-count (session)
"Return the number of entries still queued in SESSION.
Counts the in-progress item plus every queue (new, failed, young,
old, overdue, again)."
(+ (if (markerp (oref session current-item)) 1 0)
(length (oref session new-entries))
(length (oref session failed-entries))
(length (oref session young-mature-entries))
(length (oref session old-mature-entries))
(length (oref session overdue-entries))
(length (oref session again-entries))))
(defun org-drill-maximum-duration-reached-p (session)
"Return trueif the current drill session has continued past its
maximum duration."
(and org-drill-maximum-duration
(not (oref session cram-mode))
(oref session start-time)
(> (- (float-time (current-time))
(oref session start-time))
(* org-drill-maximum-duration 60))))
(defun org-drill-maximum-item-count-reached-p (session)
"Return trueif the current drill session has reached the
maximum number of items."
(and org-drill-maximum-items-per-session
(not (oref session cram-mode))
(>= (if org-drill-item-count-includes-failed-items-p
(+ (length (oref session done-entries))
(length (oref session again-entries)))
(length (oref session done-entries)))
org-drill-maximum-items-per-session)))
(defun org-drill-pop-next-pending-entry (session)
"Remove and return the next entry to drill from SESSION.
Picks by priority: failed items first, then overdue, young-mature,
new and old-mature, and finally the again queue. Returns a marker, or
nil when nothing is pending."
(cl-block org-drill-pop-next-pending-entry
(let ((m nil)
(attempts 0)
(max-attempts 1000)) ; Safety limit to prevent infinite loop
(while (and (or (null m)
(not (org-drill-entry-p m)))
(< attempts max-attempts))
(setq
m
(cond
;; First priority is items we failed in a prior session.
((and (oref session failed-entries)
(not (org-drill-maximum-item-count-reached-p session))
(not (org-drill-maximum-duration-reached-p session)))
(org-drill-pop-random (oref session failed-entries)))
;; Next priority is overdue items.
((and (oref session overdue-entries)
(not (org-drill-maximum-item-count-reached-p session))
(not (org-drill-maximum-duration-reached-p session)))
;; We use `pop', not `pop-random', because we have already
;; sorted overdue items into a random order which takes
;; number of days overdue into account.
(pop (oref session overdue-entries)))
;; Next priority is 'young' items.
((and (oref session young-mature-entries)
(not (org-drill-maximum-item-count-reached-p session))
(not (org-drill-maximum-duration-reached-p session)))
(org-drill-pop-random (oref session young-mature-entries)))
;; Next priority is newly added items, and older entries.
;; We pool these into a single group.
((and (or (oref session new-entries)
(oref session old-mature-entries))
(not (org-drill-maximum-item-count-reached-p session))
(not (org-drill-maximum-duration-reached-p session)))
(cond
((< (cl-random (+ (length (oref session new-entries))
(length (oref session old-mature-entries))))
(length (oref session new-entries)))
(org-drill-pop-random (oref session new-entries)))
(t
(org-drill-pop-random (oref session old-mature-entries)))))
;; After all the above are done, last priority is items
;; that were failed earlier THIS SESSION.
((oref session again-entries)
(pop (oref session again-entries)))
(t ; nothing left -- return nil
(cl-return-from org-drill-pop-next-pending-entry nil))))
(cl-incf attempts))
;; If we exhausted attempts trying to find a valid entry, return nil
(if (>= attempts max-attempts)
nil
m))))
(defun org-drill--route-rating-result (session m result)
"Route RESULT (return value of `org-drill-entry') for marker M.
Updates SESSION queues based on what the user did and returns one of:
`quit' — user quit; caller should exit the loop (and stash :quit)
`edit' — user wants to edit; caller should exit (and stash marker)
`skip' — skip this item, continue the loop
`next' — rating was recorded into again- or done-entries, continue"
(cond
((null result)
(message "Quit")
(setf (oref session end-pos) :quit)
'quit)
((eql result 'edit)
(setf (oref session end-pos) (point-marker))
'edit)
((eql result 'skip)
(setf (oref session current-item) nil)
'skip)
(t
(cond
((org-drill--quality-failed-p result)
(when (oref session again-entries)
(setf (oref session again-entries)
(org-drill-shuffle (oref session again-entries))))
(org-drill-push-end m (oref session again-entries)))
(t
(push m (oref session done-entries))))
(setf (oref session current-item) nil)
'next)))
(defun org-drill--pick-next-marker (session resuming-p)
"Return the marker to drill next, and whether RESUMING-P stays true.
Returns a cons (M . RESUMING-P'). M is nil if no marker is available."
(cond
((or (not resuming-p)
(null (oref session current-item))
(not (org-drill-entry-p (oref session current-item))))
(cons (org-drill-pop-next-pending-entry session) resuming-p))
(t ; resuming a suspended session
(cons (oref session current-item) nil))))
(defun org-drill-entries (session &optional resuming-p)
"Return nil,t, or a list of markers representing entries that were
\\='failed\\=' and need to be presented again before the session ends.
RESUMING-P is true if we are resuming a suspended drill session."
(cl-block org-drill-entries
(while (org-drill-entries-pending-p session)
(cl-destructuring-bind (m . next-resuming-p)
(org-drill--pick-next-marker session resuming-p)
(setq resuming-p next-resuming-p)
(setf (oref session current-item) m)
(unless m
(error "Unexpectedly ran out of pending drill items"))
(save-excursion
(org-drill-goto-entry m)
(cond
((not (org-at-heading-p))
(error "Not at heading for entry %s" m))
((not (org-drill-entry-due-p session))
(message "Entry no longer due, skipping...")
(sit-for 0.3))
(t
(org-fold-show-entry)
(when (memq (org-drill--route-rating-result
session m (org-drill-entry session))
'(quit edit))
(cl-return-from org-drill-entries nil)))))))))
(defun org-drill--quality-percent (q qualities)
"Percentage of QUALITIES equal to Q, rounded to integer.
Returns 0 when QUALITIES is empty (instead of dividing by zero)."
(round (* 100 (cl-count q qualities)) (max 1 (length qualities))))
(defun org-drill--queue-tag (count label face-color)
"Build a propertized \"COUNT LABEL\" string for the final-report queue summary."
(propertize (format "%d %s" count label)
'face `(:foreground ,face-color)))
(defun org-drill--build-final-report-summary (session pass-percent qualities)
"Build the main `Session finished' message for SESSION.
PASS-PERCENT and QUALITIES come from the caller to avoid recomputing."
(format
"%d items reviewed. Session duration %s.
Recall of reviewed items:
Excellent (5): %3d%% | Near miss (2): %3d%%
Good (4): %3d%% | Failure (1): %3d%%
Hard (3): %3d%% | Abject failure (0): %3d%%
You successfully recalled %d%% of reviewed items (quality > %s)
%d/%d items still await review (%s, %s, %s, %s, %s).
Tomorrow, %d more items will become due for review.
Session finished. Press a key to continue..."
(length (oref session done-entries))
(format-seconds "%h:%.2m:%.2s"
(- (float-time (current-time))
(oref session start-time)))
(org-drill--quality-percent 5 qualities)
(org-drill--quality-percent 2 qualities)
(org-drill--quality-percent 4 qualities)
(org-drill--quality-percent 1 qualities)
(org-drill--quality-percent 3 qualities)
(org-drill--quality-percent 0 qualities)
pass-percent
org-drill-failure-quality
(org-drill-pending-entry-count session)
(+ (org-drill-pending-entry-count session)
(oref session dormant-entry-count))
(org-drill--queue-tag (+ (length (oref session failed-entries))
(length (oref session again-entries)))
"failed" org-drill-failed-count-color)
(org-drill--queue-tag (length (oref session overdue-entries))
"overdue" org-drill-failed-count-color)
(org-drill--queue-tag (length (oref session new-entries))
"new" org-drill-new-count-color)
(org-drill--queue-tag (length (oref session young-mature-entries))
"young" org-drill-mature-count-color)
(org-drill--queue-tag (length (oref session old-mature-entries))
"old" org-drill-mature-count-color)
(oref session due-tomorrow-count)))
(defun org-drill--build-low-pass-warning (session pass-percent)
"Build the WARNING prompt shown when PASS-PERCENT is below threshold."
(format
"%s
You failed %d%% of the items you reviewed during this session.
%d (%d%%) of all items scanned were overdue.
Are you keeping up with your items, and reviewing them
when they are scheduled? If so, you may want to consider
lowering the value of `org-drill-learn-fraction' slightly in
order to make items appear more frequently over time."
(propertize "WARNING!" 'face 'org-warning)
(- 100 pass-percent)
(oref session overdue-entry-count)
;; Divisor is wrapped in (max 1 ...) — both counts are zero in
;; degenerate scopes (cram with nothing collected, pure-failure
;; session on empty queues), and (/ ... 0) raised arith-error
;; before the warning could even render.
(round (* 100 (oref session overdue-entry-count))
(max 1 (+ (oref session dormant-entry-count)
(oref session due-entry-count))))))
(defun org-drill--compute-pass-percent (qualities)
"Return the pass percentage for QUALITIES — count above
`org-drill-failure-quality' over total, rounded. Empty QUALITIES yields 0
(via `(max 1 ...)' on the denominator). Single source of truth shared by
the end-of-session report and the dashboard session record."
(round (* 100 (cl-count-if (lambda (qual)
(> qual org-drill-failure-quality))
qualities))
(max 1 (length qualities))))
(defun org-drill-final-report (session)
"Display the end-of-session summary for SESSION.
Reports how many items were reviewed, the pass percentage, and the
new/mature/failed counts."
(let* ((qualities (oref session qualities))
(pass-percent (org-drill--compute-pass-percent qualities))
(prompt (org-drill--build-final-report-summary
session pass-percent qualities))
(max-mini-window-height 0.6))
(while (not (input-pending-p))
(message "%s" prompt)
(sit-for 0.5))
(read-char-exclusive)
(when (and qualities
(< pass-percent (- 100 org-drill-forgetting-index)))
(read-char-exclusive
(org-drill--build-low-pass-warning session pass-percent)))))
(defun org-drill-session-record-from-session (session start-time end-time)
"Build an `org-drill-session-record' from SESSION, START-TIME, END-TIME.
START-TIME and END-TIME are floats (e.g. from `float-time'). The
record's pass-percent comes from `org-drill--compute-pass-percent', the
same helper `org-drill-final-report' uses, so the two stay in lockstep.
Scope and algorithm are read from the session's start-of-session
captures (`scope-at-start' / `algorithm-at-start') so a mid-session
defcustom change doesn't misrepresent the recorded session."
(let ((qualities (oref session qualities)))
(make-org-drill-session-record
:start-time start-time
:end-time end-time
:scope (oref session scope-at-start)
:algorithm (oref session algorithm-at-start)
:qualities (apply #'vector qualities)
:pass-percent (org-drill--compute-pass-percent qualities)
:new-count (length (oref session new-entries))
:mature-count (+ (length (oref session young-mature-entries))
(length (oref session old-mature-entries)))
:failed-count (length (oref session failed-entries))
:cram-mode (oref session cram-mode))))
(defun org-drill-record-session (session start-time end-time)
"Append a record for SESSION to `org-drill-session-log' and persist.
START-TIME and END-TIME are floats. The new record lands at the head
of the log (newest first), then `persist-save' commits the log to disk."
(push (org-drill-session-record-from-session session start-time end-time)
org-drill-session-log)
(persist-save 'org-drill-session-log))
(defun org-drill-free-markers (session markers)
"MARKERS is a list of markers, all of which will be freed (set to
point nowhere). Alternatively, MARKERS can be \\='t\\=', in which case
all the markers used by Org-Drill will be freed."
(dolist (marker (if (eql t markers)
(append (oref session done-entries)
(oref session new-entries)
(oref session failed-entries)
(oref session again-entries)
(oref session overdue-entries)
(oref session young-mature-entries)
(oref session old-mature-entries))
markers))
(set-marker marker nil)))
;;; overdue-data is a list of entries, each entry has the form (POS DUE AGE)
;;; where POS is a marker pointing to the start of the entry, and
;;; DUE is a number indicating how many days ago the entry was due.
;;; AGE is the number of days elapsed since item creation (nil if unknown).
;;;
;;; Sort order:
;;; 1. Split by DUE: entries with DUE > org-drill-lapse-threshold-days
;;; are "lapsed", the rest are "not-lapsed". This matches the gate
;;; in `org-drill--entry-lapsed-p'.
;;; 2. Not-lapsed are sorted by DUE descending (most-overdue first).
;;; 3. Lapsed are appended after, sorted by AGE descending (oldest first).
(defun org-drill-order-overdue-entries (session)
"Order SESSION's overdue entries into the overdue queue.
Splits off entries lapsed past `org-drill-lapse-threshold-days' (when
`org-drill--lapse-very-overdue-entries-p' is set) and sorts the rest by
how overdue they are."
(let* ((lapsed-days (if org-drill--lapse-very-overdue-entries-p
org-drill-lapse-threshold-days
most-positive-fixnum))
(not-lapsed (cl-remove-if (lambda (entry) (> (or (cl-second entry) 0) lapsed-days))
(oref session overdue-data)))
(lapsed (cl-remove-if-not
(lambda (entry) (> (or (cl-second entry) 0)
lapsed-days))
(oref session overdue-data))))
(setf (oref session overdue-entries)
(mapcar 'cl-first
(append
(sort (org-drill-shuffle not-lapsed)
(lambda (entry other) (> (cl-second entry) (cl-second other))))
(sort lapsed
(lambda (entry other) (> (cl-third entry) (cl-third other)))))))))
(defun org-drill--entry-lapsed-p (session)
"Return non-nil if the entry at point is lapsed (very overdue) in SESSION.
Only when `org-drill--lapse-very-overdue-entries-p' is set and the entry
is more than `org-drill-lapse-threshold-days' overdue."
(and org-drill--lapse-very-overdue-entries-p
(> (or (org-drill-entry-days-overdue session) 0)
org-drill-lapse-threshold-days)))
(defun org-drill-entry-days-since-creation (session &optional use-last-interval-p)
"If USE-LAST-INTERVAL-P is non-nil, and DATE_ADDED is missing, use the
value of DRILL_LAST_INTERVAL instead (as the item's age must be at least
that many days)."
(let ((timestamp (org-entry-get (point) "DATE_ADDED")))
(cond
(timestamp
(condition-case nil
(- (org-time-stamp-to-now timestamp))
(error nil)))
(use-last-interval-p
(+ (or (org-drill-entry-days-overdue session) 0)
(string-to-number (or (org-entry-get (point) "DRILL_LAST_INTERVAL") "0"))))
(t nil))))
(defun org-drill--entry-empty-and-not-empty-friendly-p ()
"Non-nil when the entry has an empty body AND its card type doesn't
treat empty bodies as meaningful.
A card type that wants empty bodies is one whose entry in
`org-drill-card-type-alist' has a non-nil third element (the
DRILL-EMPTY-P slot). When `org-drill-treat-headline-as-card-p' is
non-nil, no empty entry is treated as skippable — the heading itself
is the card."
(and (not org-drill-treat-headline-as-card-p)
(org-drill-entry-empty-p)
(let* ((card-type (org-entry-get (point) "DRILL_CARD_TYPE" nil))
(card-def (cdr (assoc card-type org-drill-card-type-alist))))
(or (null card-type)
(not (cl-third card-def))))))
(defun org-drill--classify-status (session due last-int)
"Return the status keyword for the entry at point.
SESSION is needed for cram-mode and overdue-factor. DUE is the
days-overdue value already computed by `entry-days-overdue', and
LAST-INT is the entry's last interval (defaulted to 1)."
(cond
((not (org-drill-entry-p)) nil)
((org-drill--entry-empty-and-not-empty-friendly-p) nil)
((null due) :unscheduled) ; usually a skipped leech
((cl-minusp due) :future)
;; Mature entries that were failed last time are :failed regardless
;; of how young, old, or overdue they are. The 9999 default keeps
;; never-rated cards out of the failure bucket.
((org-drill--quality-failed-p (org-drill-entry-last-quality 9999)) :failed)
((org-drill-entry-new-p) :new)
;; Overdue overrides the young/old distinction.
((org-drill-entry-overdue-p session due last-int) :overdue)
((<= (org-drill-entry-last-interval 9999) org-drill-days-before-old) :young)
(t :old)))
(defun org-drill-entry-status (session)
"Return alist (STATUS DUE AGE) where DUE is the number of days overdue,
zero being due today, -1 being scheduled 1 day in the future.
AGE is the number of days elapsed since the item was created (nil if unknown).
STATUS is one of the following values:
- nil, if the item is not a drill entry, or has an empty body
- :unscheduled
- :future
- :new
- :failed
- :overdue
- :young
- :old
"
(save-excursion
(unless (org-at-heading-p)
(org-back-to-heading))
(let ((due (org-drill-entry-days-overdue session))
(age (org-drill-entry-days-since-creation session t))
(last-int (org-drill-entry-last-interval 1)))
(list (org-drill--classify-status session due last-int) due age))))
(defun org-drill-progress-message (collected scanned)
"Show a progress meter while scanning for due items.
COLLECTED is how many drill items have been gathered, SCANNED how many
entries have been examined. Updates only every 50 entries."
(when (zerop (% scanned 50))
(let* ((meter-width 40)
(current-meter-char (if (cl-oddp (floor scanned (* 50 meter-width))) ?| ?.))
(alternate-meter-char (if (eql current-meter-char ?.) ?| ?.)))
(message "Collecting due drill items:%4d %s%s"
collected
(make-string (% (ceiling scanned 50) meter-width)
alternate-meter-char)
(make-string (- meter-width (% (ceiling scanned 50) meter-width))
current-meter-char)))))
(defun org-drill-map-entry-function (session)
"Classify the drill entry at point and file it into SESSION's queues.
Called once per entry while scanning the drill scope, updating the
progress meter as it goes."
(org-drill-progress-message
(+ (length (oref session new-entries))
(length (oref session overdue-entries))
(length (oref session young-mature-entries))
(length (oref session old-mature-entries))
(length (oref session failed-entries)))
(cl-incf (oref session cnt)))
(when (org-drill-entry-p)
;; Per-entry processing is wrapped in `condition-case' so a single
;; bad entry doesn't kill the whole collection scan. Upstream
;; issue #53 reported that a new (no-ID) entry triggered a
;; "hash-table-p, nil" error that stopped the scan, leaving every
;; subsequent entry uncollected — the user had to re-run org-drill
;; once per item. Catching the error here lets the scan continue.
(condition-case err
(progn
(org-drill-id-get-create-with-warning session)
(cl-destructuring-bind (status due age)
(org-drill-entry-status session)
(cl-case status
(:unscheduled
(cl-incf (oref session dormant-entry-count)))
(:future
(cl-incf (oref session dormant-entry-count))
(if (eq -1 due)
(cl-incf (oref session due-tomorrow-count))))
(:new
(push (point-marker) (oref session new-entries)))
(:failed
(push (point-marker) (oref session failed-entries)))
(:young
(push (point-marker) (oref session young-mature-entries)))
(:overdue
(push (list (point-marker) due age) (oref session overdue-data)))
(:old
(push (point-marker) (oref session old-mature-entries))))))
(error
(message "org-drill: error processing entry at %s (%s); skipping"
(point) err)))))
(defun org-drill-id-get-create-with-warning (session)
"Return the entry's :ID:, creating one if absent.
The first time an ID has to be created in SESSION, warn the user that
the one-time ID-creation pass is slow, and flip the session's
warned-about-id-creation flag so the warning shows only once."
(when (and (not (oref session warned-about-id-creation))
(null (org-id-get)))
(message (concat "Creating unique IDs for items "
"(slow, but only happens once)"))
(sit-for 0.5)
(setf (oref session warned-about-id-creation) t))
(org-id-get-create))
;;;###autoload
(defun org-drill--prepare-fresh-session (session cram)
"Reset SESSION's queues and counters for a fresh (non-resume) run.
CRAM, if non-nil, starts the session in cram mode."
(org-drill-free-markers session t)
(setf (oref session cram-mode) cram
(oref session current-item) nil
(oref session done-entries) nil
(oref session dormant-entry-count) 0
(oref session due-entry-count) 0
(oref session due-tomorrow-count) 0
(oref session overdue-entry-count) 0
(oref session new-entries) nil
(oref session overdue-entries) nil
(oref session young-mature-entries) nil
(oref session old-mature-entries) nil
(oref session failed-entries) nil
(oref session again-entries) nil
(oref session undo-stack) nil
(oref session start-time) (float-time (current-time))
(oref session scope-at-start) org-drill-scope
(oref session algorithm-at-start) org-drill-spaced-repetition-algorithm))
(defun org-drill--collect-entries (session scope drill-match)
"Scan buffers in SCOPE for drill entries matching DRILL-MATCH.
Populates SESSION's queues via `org-drill-map-entry-function', then
sorts the overdue queue."
(let ((org-trust-scanner-tags t))
(org-drill-map-entries
(apply-partially #'org-drill-map-entry-function session)
scope drill-match)
(org-drill-order-overdue-entries session)
(setf (oref session overdue-entry-count)
(length (oref session overdue-entries)))))
(defun org-drill--queues-empty-p (session)
"Non-nil when SESSION has no current-item and every drill queue is empty."
(and (null (oref session current-item))
(null (oref session new-entries))
(null (oref session failed-entries))
(null (oref session overdue-entries))
(null (oref session young-mature-entries))
(null (oref session old-mature-entries))))
(defun org-drill--show-resume-hint ()
"Show the post-suspend `you can resume...' message."
(let ((keystr (org-drill-command-keybinding-to-string 'org-drill-resume)))
(message
"You can continue the drill session with the command `org-drill-resume'.%s"
(if keystr (format "\nYou can run this command by pressing %s." keystr)
""))))
(defun org-drill--show-end-message (session)
"Display the appropriate end-of-session message and side-effects.
If SESSION was suspended (end-pos is set), shows the resume hint.
Otherwise runs `org-drill-final-report', persists the SM5 matrix,
records the session for the stats dashboard, and optionally saves
buffers."
(cond
((oref session end-pos)
(when (markerp (oref session end-pos))
(org-drill-goto-entry (oref session end-pos))
(org-reveal)
(org-fold-show-entry))
(org-drill--show-resume-hint))
(t
(org-drill-final-report session)
(when (eql 'sm5 org-drill-spaced-repetition-algorithm)
(persist-save 'org-drill-sm5-optimal-factor-matrix))
;; Recording is protective of the end-of-session flow: a recorder
;; bug or a persist-save failure must not block the final-report
;; dismissal or the SM5 save above. Trap the error and surface it
;; via `message' so silent data loss leaves a forensic trail.
(condition-case err
(org-drill-record-session session
(oref session start-time)
(float-time (current-time)))
(error
(message "org-drill: failed to record session for the stats log (%s)"
err)))
(when org-drill-save-buffers-after-drill-sessions-p
(save-some-buffers))
(message "Drill session finished!")
(sit-for 1)
(message nil))))
;;;###autoload
(defun org-drill (&optional scope drill-match resume-p cram)
"Begin an interactive \\='drill session\\='. The user is asked to
review a series of topics (headers). Each topic is initially
presented as a \\='question\\=', often with part of the topic content
hidden. The user attempts to recall the hidden information or
answer the question, then presses a key to reveal the answer. The
user then rates his or her recall or performance on that
topic. This rating information is used to reschedule the topic
for future review.
Org-drill proceeds by:
- Finding all topics (headings) in SCOPE which have either been
used and rescheduled before, or which have a tag that matches
`org-drill-question-tag'.
- All matching topics which are either unscheduled, or are
scheduled for the current date or a date in the past, are
considered to be candidates for the drill session.
- If `org-drill-maximum-items-per-session' is set, a random
subset of these topics is presented. Otherwise, all of the
eligible topics will be presented.
SCOPE determines the scope in which to search for
questions. It accepts the same values as `org-drill-scope',
which see.
DRILL-MATCH, if supplied, is a string specifying a tags/property/
todo query. Only items matching the query will be considered.
It accepts the same values as `org-drill-match', which see.
If RESUME-P is non-nil, resume a suspended drill session rather
than starting a new one.
CRAM, if non-nil, will start a new session in cram mode. If
resuming a suspended session, this parameter is ignored."
(interactive)
(let ((session (if resume-p
org-drill-last-session
(setq org-drill-last-session (org-drill-session)))))
;; Clear stale `end-pos' on resume — see upstream issue #33.
(when resume-p
(setf (oref session end-pos) nil))
(unless resume-p
(org-drill--prepare-fresh-session session cram))
(unwind-protect
(save-excursion
(unless resume-p
(org-drill--collect-entries session scope drill-match))
(setf (oref session due-entry-count)
(org-drill-pending-entry-count session))
(unless resume-p
(org-drill--setup-display)
(run-hooks 'org-drill-before-session-hook))
(cond
((org-drill--queues-empty-p session)
(message "I did not find any pending drill items."))
(t
(org-drill-entries session resume-p)
(message "Drill session finished!")
(sit-for 1)
(message nil))))
;; Cleanup runs whether the body succeeded, errored, or was
;; suspended via cl-return-from in `org-drill-entries'.
(org-drill--restore-display)
(run-hooks 'org-drill-after-session-hook)
(org-drill-free-markers session (oref session done-entries))
(unless (oref session end-pos)
(setf (oref session cram-mode) nil)))
(org-drill--show-end-message session)))
;;;###autoload
(defun org-drill-cram (&optional scope drill-match)
"Run an interactive drill session in \\='cram mode\\='. In cram mode,
all drill items are considered to be due for review, unless they
have been reviewed within the last `org-drill-cram-hours'
hours."
(interactive)
(org-drill scope drill-match nil t))
;;;###autoload
(defun org-drill-cram-tree ()
"Run an interactive drill session in \\='cram mode\\=' using subtree at point.
See also, `org-drill-cram' and `org-drill-tree'."
(interactive)
(org-drill-cram 'tree))
;;;###autoload
(defun org-drill-tree ()
"Run an interactive drill session using drill items within the
subtree at point."
(interactive)
(org-drill 'tree))
;;;###autoload
(defun org-drill-directory ()
"Run an interactive drill session using drill items from all org
files in the same directory as the current file."
(interactive)
(org-drill 'directory))
;;;###autoload
(defun org-drill-again (&optional scope drill-match)
"Run a new drill session, but try to use leftover due items that
were not reviewed during the last session, rather than scanning for
unreviewed items. If there are no leftover items in memory, a full
scan will be performed."
(interactive)
(let ((session org-drill-last-session))
(unless session
(user-error "No previous drill session to continue — run `org-drill' first"))
(setf (oref session cram-mode) nil)
(cond
((cl-plusp (org-drill-pending-entry-count session))
(org-drill-free-markers session (oref session done-entries))
(if (markerp (oref session current-item))
(set-marker (oref session current-item) nil))
(setf (oref session start-time) (float-time (current-time))
(oref session scope-at-start) org-drill-scope
(oref session algorithm-at-start) org-drill-spaced-repetition-algorithm)
(setf (oref session done-entries) nil
(oref session current-item) nil)
(org-drill scope drill-match t))
(t
(org-drill scope drill-match)))))
;;;###autoload
(defun org-drill-resume ()
"Resume a suspended drill session. Sessions are suspended by
exiting them with the `edit' or `quit' options."
(interactive)
(let ((session org-drill-last-session))
(unless session
(user-error "No suspended drill session to resume — run `org-drill' first"))
(cond
((org-drill-entries-pending-p session)
(org-drill nil nil t))
((and (cl-plusp (org-drill-pending-entry-count session))
;; Current drill session is finished, but there are still
;; more items which need to be reviewed.
(y-or-n-p (format
"You have finished the drill session. However, %d items still
need reviewing. Start a new drill session? "
(org-drill-pending-entry-count session))))
(org-drill-again))
(t
(message "You have finished the drill session.")))))
;;;###autoload
(defun org-drill-relearn-item ()
"Make the current item due for revision, and set its last interval to 0.
Makes the item behave as if it has been failed, without actually recording a
failure. This command can be used to \\='reset\\=' repetitions for an item."
(interactive)
(org-drill-smart-reschedule 4 0))
(defun org-drill-strip-entry-data ()
"Remove all org-drill scheduling data from the entry at point.
Deletes every property in `org-drill-scheduling-properties' and clears
the entry's schedule."
(dolist (prop org-drill-scheduling-properties)
(org-delete-property prop))
(org-schedule '(4)))
;;;###autoload
(defun org-drill-strip-all-data (&optional scope)
"Delete scheduling data from every drill entry in scope. This
function may be useful if you want to give your collection of
entries to someone else. Scope defaults to the current buffer,
and is specified by the argument SCOPE, which accepts the same
values as `org-drill-scope'."
(interactive)
(when (yes-or-no-p
"Delete scheduling data from ALL items in scope: are you sure?")
(cond
((null scope)
;; Scope is the current buffer. This means we can use
;; `org-delete-property-globally', which is faster.
(dolist (prop org-drill-scheduling-properties)
(org-delete-property-globally prop))
(org-drill-map-entries (lambda () (org-schedule '(4))) scope))
(t
(org-drill-map-entries 'org-drill-strip-entry-data scope)))
(message "Done.")))
(defvar-local org-drill--installed-cloze-keywords nil
"The exact cloze font-lock keyword list installed by `org-drill-mode' in
this buffer, kept so the mode can remove precisely what it added.")
(define-minor-mode org-drill-mode
"Minor mode that highlights cloze deletions in the current buffer.
Enabling the mode adds cloze fontification to this buffer only, so the
highlighting does not leak into unrelated Org buffers. That scoping also
prevents Org priority cookies such as `[#A]' — which match the cloze
`[...]' pattern — from being fontified as clozes in non-drill buffers.
The mode controls only the persistent display of clozes in the source
buffer. Running drill sessions (`org-drill', `org-drill-cram', etc.) does
not depend on it.
Highlighting is applied only when `org-drill-use-visible-cloze-face-p' is
non-nil; otherwise the mode is a no-op for fontification."
:lighter " Drill"
:group 'org-drill
;; Recompute the buffer-local cloze regexp/keywords in case the cloze
;; delimiters were redefined locally.
(setq-local org-drill-cloze-regexp (org-drill--compute-cloze-regexp))
(setq-local org-drill-cloze-keywords (org-drill--compute-cloze-keywords))
;; Remove any previously-installed keywords first so repeated toggles
;; don't stack duplicates.
(when org-drill--installed-cloze-keywords
(font-lock-remove-keywords nil org-drill--installed-cloze-keywords)
(setq org-drill--installed-cloze-keywords nil))
(when (and org-drill-mode org-drill-use-visible-cloze-face-p)
(setq org-drill--installed-cloze-keywords org-drill-cloze-keywords)
(font-lock-add-keywords nil org-drill--installed-cloze-keywords 'append))
(when font-lock-mode
(save-restriction
(widen)
(font-lock-flush)
(font-lock-ensure))))
(defun org-drill-buffer-has-cards-p ()
"Return non-nil if the current buffer contains a drill card — a heading
tagged with `org-drill-question-tag' or `org-drill-leitner-tag'.
The tag may sit on the heading itself, on an ancestor heading (the
ancestor line carries the literal tag, so the per-heading scan still
matches), or be applied file-wide via `#+FILETAGS:'. The file-tag case
is checked separately because the tag never appears on a heading line
there — the upstream bug where filetag-only decks failed to auto-enable
`org-drill-mode'."
(save-excursion
(save-restriction
(widen)
(let ((case-fold-search t)
(tags (concat "\\(?:"
(regexp-quote org-drill-question-tag)
"\\|"
(regexp-quote org-drill-leitner-tag)
"\\)")))
(or
;; Per-heading tag (also catches inheritance from a tagged ancestor).
(progn
(goto-char (point-min))
(re-search-forward (concat "^\\*+ .*:" tags ":") nil t))
;; File-wide tag via #+FILETAGS:, in either the space-separated or
;; colon-delimited syntax. The [: \t] boundaries keep a value like
;; `drilldown' from matching `drill'.
(progn
(goto-char (point-min))
(re-search-forward
(concat "^#\\+FILETAGS:.*[: \t]" tags "\\(?:[: \t]\\|$\\)")
nil t)))))))
(defun org-drill-maybe-enable-mode ()
"Enable `org-drill-mode' when appropriate for the current buffer.
Intended for `org-mode-hook': turns the mode on when
`org-drill-auto-enable-mode' is non-nil and the buffer holds drill cards."
(when (and org-drill-auto-enable-mode
(derived-mode-p 'org-mode)
(org-drill-buffer-has-cards-p))
(org-drill-mode 1)))
(add-hook 'org-mode-hook #'org-drill-maybe-enable-mode)
;;; Synching card collections =================================================
(defvar org-drill-dest-id-table (make-hash-table :test 'equal))
(defun org-drill-copy-entry-to-other-buffer (dest &optional path)
"Copy the subtree at point to the buffer DEST. The copy will receive
the tag \\='imported\\='."
(cl-block org-drill-copy-entry-to-other-buffer
(save-excursion
(let ((m nil))
(cl-flet ((paste-tree-here (&optional level)
(org-paste-subtree level)
(org-drill-strip-entry-data)
(org-toggle-tag "imported" 'on)
(org-drill-map-entries
(lambda ()
(let ((id (org-id-get)))
(org-drill-strip-entry-data)
(unless (gethash id org-drill-dest-id-table)
(puthash id (point-marker)
org-drill-dest-id-table))))
'tree)))
(unless path
(setq path (org-get-outline-path)))
(org-copy-subtree)
(switch-to-buffer dest)
(setq m
(condition-case nil
(org-find-olp path t)
(error ; path does not exist in DEST
(cl-return-from org-drill-copy-entry-to-other-buffer
(cond
((cdr path)
(org-drill-copy-entry-to-other-buffer
dest (butlast path)))
(t
;; We've looked all the way up the path
;; Default to appending to the end of DEST
(goto-char (point-max))
(newline)
(paste-tree-here)))))))
(goto-char m)
(outline-next-heading)
(newline)
(forward-line -1)
(paste-tree-here (1+ (or (org-current-level) 0)))
)))))
(defun org-drill--build-dest-id-table (dest)
"Populate `org-drill-dest-id-table' with id→marker for every entry in DEST."
(clrhash org-drill-dest-id-table)
(with-current-buffer dest
(org-drill-map-entries
(lambda ()
(let ((this-id (org-id-get)))
(when this-id
(puthash this-id (point-marker) org-drill-dest-id-table))))
'file)))
(defun org-drill--copy-scheduling-to-marker (marker)
"Copy the current entry's scheduling state to MARKER's position.
The destination's previous data is stripped first. Skips zero-
total-repeats items (i.e., never-rated cards) — those have no
scheduling information worth migrating."
(let ((state (org-drill-get-item-data))
(last-reviewed (org-entry-get (point) "DRILL_LAST_REVIEWED"))
(last-quality (org-entry-get (point) "DRILL_LAST_QUALITY"))
(scheduled-time (org-get-scheduled-time (point))))
(with-current-buffer (marker-buffer marker)
(save-excursion
(goto-char marker)
(org-drill-strip-entry-data)
(unless (zerop (org-drill-card-state-total-repeats state))
(org-drill-store-item-data state)
(if last-quality
(org-set-property "DRILL_LAST_QUALITY" last-quality)
(org-delete-property "DRILL_LAST_QUALITY"))
(if last-reviewed
(org-set-property "DRILL_LAST_REVIEWED" last-reviewed)
(org-delete-property "DRILL_LAST_REVIEWED"))
(when scheduled-time
(org-schedule nil scheduled-time)))))))
(defun org-drill--migrate-from-source (src dest ignore-new-items-p)
"Walk SRC's entries, migrating scheduling data into matching DEST entries.
Removes matched IDs from `org-drill-dest-id-table' as it goes, so the
caller can clean up unmatched DEST entries afterwards. Entries with
no DEST match are copied into DEST as new items unless IGNORE-NEW-ITEMS-P."
(with-current-buffer src
(org-drill-map-entries
(lambda ()
(let ((id (org-id-get)))
(cond
((or (null id) (not (org-drill-entry-p))) nil)
((gethash id org-drill-dest-id-table)
(let ((marker (gethash id org-drill-dest-id-table)))
(org-drill--copy-scheduling-to-marker marker)
(remhash id org-drill-dest-id-table)
(set-marker marker nil)))
(t
;; Item in SRC has an ID but no match in DEST — new item.
(unless ignore-new-items-p
(org-drill-copy-entry-to-other-buffer dest))))))
'file)))
(defun org-drill--strip-unmatched-dest-entries ()
"Strip scheduling data from every entry left in `org-drill-dest-id-table'.
Anything still in the table after migration didn't have a matching
SRC entry — its scheduling info came from a different deck and the
deck owner is opting in to a clean migration."
(maphash (lambda (_id m)
(goto-char m)
(org-drill-strip-entry-data)
(set-marker m nil))
org-drill-dest-id-table))
;;;###autoload
(defun org-drill-merge-buffers (src &optional dest ignore-new-items-p)
"SRC and DEST are two org mode buffers containing drill items.
For each drill item in DEST that shares an ID with an item in SRC,
overwrite scheduling data in DEST with data taken from the item in SRC.
This is intended for use when two people are sharing a set of drill items,
one person has made some updates to the item set, and the other person
wants to migrate to the updated set without losing their scheduling data.
By default, any drill items in SRC which do not exist in DEST are
copied into DEST. We attempt to place the copied item in the
equivalent location in DEST to its location in SRC, by matching
the heading hierarchy. However if IGNORE-NEW-ITEMS-P is non-nil,
we simply ignore any items that do not exist in DEST, and do not
copy them across."
(interactive "bImport scheduling info from which buffer?")
(unless dest
(setq dest (current-buffer)))
(setq src (get-buffer src)
dest (get-buffer dest))
(when (yes-or-no-p
(format
(concat "About to overwrite all scheduling data for drill items in `%s' "
"with information taken from matching items in `%s'. Proceed? ")
(buffer-name dest) (buffer-name src)))
(org-drill--build-dest-id-table dest)
(org-drill--migrate-from-source src dest ignore-new-items-p)
(with-current-buffer dest
(org-drill--strip-unmatched-dest-entries))))
;;; Card types for learning languages =========================================
;;; `conjugate' card type =====================================================
;;; See spanish.org for usage
(defvar org-drill-verb-tense-alist
'(("present" "tomato")
("simple present" "tomato")
("present indicative" "tomato")
;; past tenses
("past" "purple")
("simple past" "purple")
("preterite" "purple")
("imperfect" "darkturquoise")
("present perfect" "royalblue")
;; future tenses
("future" "green")
;; moods (backgrounds).
("indicative" nil) ; default
("subjunctive" "medium blue")
("conditional" "grey30")
("negative imperative" "red4")
("positive imperative" "darkgreen")
)
"Alist where each entry has the form (TENSE COLOUR), where
TENSE is a string naming a tense in which verbs can be
conjugated, and COLOUR is a string specifying a foreground colour
which will be used by `org-drill-present-verb-conjugation' and
`org-drill-show-answer-verb-conjugation' to fontify the verb and
the name of the tense.")
(defun org-drill--read-property-string (raw)
"Return the first datum read from property value RAW, or nil if RAW is nil.
Some card properties are stored as quoted Lisp strings, so a single
`read' strips one layer: \"\\\"hablar\\\"\" becomes \"hablar\"."
(and raw (car (read-from-string raw))))
(defun org-drill--face-from-alist (key alist default)
"Return the colour mapped to KEY in ALIST, case-insensitively, or DEFAULT.
ALIST entries are (NAME COLOUR ...) as in `org-drill-verb-tense-alist' and
`org-drill-noun-gender-alist'."
(or (cl-second (assoc-string key alist t)) default))
(defun org-drill-get-verb-conjugation-info ()
"Auxiliary function used by `org-drill-present-verb-conjugation' and
`org-drill-show-answer-verb-conjugation'."
(let ((infinitive (org-entry-get (point) "VERB_INFINITIVE" t))
(inf-hint (org-entry-get (point) "VERB_INFINITIVE_HINT" t))
(translation (org-entry-get (point) "VERB_TRANSLATION" t))
(tense (org-entry-get (point) "VERB_TENSE" nil))
(mood (org-entry-get (point) "VERB_MOOD" nil))
(highlight-face nil))
(unless (and infinitive translation (or tense mood))
(error "Missing information for verb conjugation card (%s, %s, %s, %s) at %s"
infinitive translation tense mood (point)))
(setq tense (if tense (downcase (org-drill--read-property-string tense)))
mood (if mood (downcase (org-drill--read-property-string mood)))
infinitive (org-drill--read-property-string infinitive)
inf-hint (org-drill--read-property-string inf-hint)
translation (org-drill--read-property-string translation))
(setq highlight-face
(list :foreground
(org-drill--face-from-alist tense org-drill-verb-tense-alist "hotpink")
:background
(org-drill--face-from-alist mood org-drill-verb-tense-alist "black")))
(setq infinitive (propertize infinitive 'face highlight-face))
(setq translation (propertize translation 'face highlight-face))
(if tense (setq tense (propertize tense 'face highlight-face)))
(if mood (setq mood (propertize mood 'face highlight-face)))
(list infinitive inf-hint translation tense mood)))
(defun org-drill--format-tense-mood (tense mood)
"Return a human-readable label for a verb's TENSE and MOOD.
Either argument may be nil. Returns nil when both are nil."
(cond
((and tense mood)
(format "%s tense, %s mood" tense mood))
(tense
(format "%s tense" tense))
(mood
(format "%s mood" mood))))
(defun org-drill-present-verb-conjugation (session)
"Present a drill entry whose card type is \\='conjugate\\='."
(cl-destructuring-bind (infinitive inf-hint translation tense mood)
(org-drill-get-verb-conjugation-info)
(org-drill-present-card-using-text
session
(cond
((zerop (cl-random 2))
(format "\nTranslate the verb\n\n%s\n\nand conjugate for the %s.\n\n"
infinitive (org-drill--format-tense-mood tense mood)))
(t
(format "\nGive the verb that means\n\n%s %s\n
and conjugate for the %s.\n\n"
translation
(if inf-hint (format " [HINT: %s]" inf-hint) "")
(org-drill--format-tense-mood tense mood)))))))
(defun org-drill-show-answer-verb-conjugation (session reschedule-fn)
"Show the answer for a drill item whose card type is \\='conjugate\\='.
RESCHEDULE-FN must be a function that calls `org-drill-reschedule' and
returns its return value."
(cl-destructuring-bind (infinitive _inf-hint translation tense mood)
(org-drill-get-verb-conjugation-info)
(org-drill-with-replaced-entry-heading
(format "%s of %s ==> %s\n\n"
(capitalize (org-drill--format-tense-mood tense mood))
infinitive translation)
(org-drill-hide-drawers)
(funcall reschedule-fn session))))
;;; `decline_noun' card type ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar org-drill-noun-gender-alist
'(("masculine" "dodgerblue")
("masc" "dodgerblue")
("male" "dodgerblue")
("m" "dodgerblue")
("feminine" "orchid")
("fem" "orchid")
("female" "orchid")
("f" "orchid")
("neuter" "green")
("neutral" "green")
("neut" "green")
("n" "green")
))
(defun org-drill-get-noun-info ()
"Auxiliary function used by `org-drill-present-noun-declension' and
`org-drill-show-answer-noun-declension'."
(let ((noun (org-entry-get (point) "NOUN" t))
(noun-hint (org-entry-get (point) "NOUN_HINT" t))
(noun-root (org-entry-get (point) "NOUN_ROOT" t))
(noun-gender (org-entry-get (point) "NOUN_GENDER" t))
(translation (org-entry-get (point) "NOUN_TRANSLATION" t))
(highlight-face nil))
(unless (and noun translation)
(error "Missing information for `decline_noun' card (%s, %s, %s, %s) at %s"
noun translation noun-hint noun-root (point)))
(setq noun-root (org-drill--read-property-string noun-root)
noun (org-drill--read-property-string noun)
noun-gender (downcase (org-drill--read-property-string noun-gender))
noun-hint (org-drill--read-property-string noun-hint)
translation (org-drill--read-property-string translation))
(setq highlight-face
(list :foreground
(org-drill--face-from-alist noun-gender
org-drill-noun-gender-alist "red")))
(setq noun (propertize noun 'face highlight-face))
(setq translation (propertize translation 'face highlight-face))
(list noun noun-root noun-gender noun-hint translation)))
(defun org-drill-present-noun-declension (session)
"Present a drill entry whose card type is \\='decline_noun\\='."
(cl-destructuring-bind (noun _noun-root noun-gender noun-hint translation)
(org-drill-get-noun-info)
(let* ((props (org-entry-properties (point)))
(definite
(cond
((assoc "DECLINE_DEFINITE" props)
(propertize (if (org-entry-get (point) "DECLINE_DEFINITE")
"definite" "indefinite")
'face 'warning))
(t nil)))
(plural
(cond
((assoc "DECLINE_PLURAL" props)
(propertize (if (org-entry-get (point) "DECLINE_PLURAL")
"plural" "singular")
'face 'warning))
(t nil))))
(org-drill-present-card-using-text
session
(cond
((zerop (cl-random 2))
(format "\nTranslate the noun\n\n%s (%s)\n\nand list its declensions%s.\n\n"
noun noun-gender
(if (or plural definite)
(format " for the %s %s form" definite plural)
"")))
(t
(format "\nGive the noun that means\n\n%s %s\n
and list its declensions%s.\n\n"
translation
(if noun-hint (format " [HINT: %s]" noun-hint) "")
(if (or plural definite)
(format " for the %s %s form" definite plural)
""))))))))
(defun org-drill-show-answer-noun-declension (session reschedule-fn)
"Show the answer for a drill item whose card type is \\='decline_noun\\='.
RESCHEDULE-FN must be a function that calls `org-drill-reschedule' and
returns its return value."
(cl-destructuring-bind (noun _noun-root noun-gender _noun-hint translation)
(org-drill-get-noun-info)
(org-drill-with-replaced-entry-heading
(format "Declensions of %s (%s) ==> %s\n\n"
noun noun-gender translation)
(org-drill-hide-drawers)
(funcall reschedule-fn session))))
;;; `spanish_verb' card type ==================================================
;;; Not very interesting, but included to demonstrate how a presentation
;;; function can manipulate which subheading are hidden versus shown.
(defconst org-drill--spanish-verb-prompts
'(("Infinitive" . "Translate this Spanish verb, and conjugate it for the *present* tense.")
("English" . "For the *present* tense, conjugate the Spanish translation of this English verb.")
("Infinitive" . "Translate this Spanish verb, and conjugate it for the *past* tense.")
("English" . "For the *past* tense, conjugate the Spanish translation of this English verb.")
("Infinitive" . "Translate this Spanish verb, and conjugate it for the *future perfect* tense.")
("English" . "For the *future perfect* tense, conjugate the Spanish translation of this English verb."))
"Six (REVEAL . PROMPT) pairs for `org-drill-present-spanish-verb'.
Each card-presentation chooses one at random, hiding all subheadings
except REVEAL and showing PROMPT in the rating prompt.")
(defun org-drill-present-spanish-verb (session)
"Present a Spanish-verb conjugation card for SESSION.
Pick one of the six (reveal . prompt) pairs in
`org-drill--spanish-verb-prompts' at random, reveal that subheading, and
prompt with the matching question."
(org-drill-with-card-display
(let* ((choice (nth (cl-random (length org-drill--spanish-verb-prompts))
org-drill--spanish-verb-prompts))
(reveal (car choice))
(prompt (cdr choice)))
(org-drill-hide-all-subheadings-except (list reveal))
(org-drill-hide-drawers)
(prog1 (org-drill-presentation-prompt session prompt)
(org-drill-hide-subheadings-if 'org-drill-entry-p)))))
;; org-drill :explain: implementations
(defun org-drill-get-explain-text (&optional existing-text)
"Fetch the explaination texts for this entry.
Explaination text is found in parent entries with an :explain:
tag. If there are multiple parents entries with such a tag, all
of them are returned.
Returns a list of strings."
(save-excursion
(save-restriction
(widen)
(if (>= 1 (funcall outline-level))
existing-text
(outline-up-heading 1 t)
(if (org-drill-explain-entry-p t)
(org-drill-get-explain-text
(cons
(org-drill-get-entry-text)
existing-text))
existing-text)))))
(defvar org-drill-explain-overlay nil)
(defun org-drill-explain-entry-p (&optional no-inherit)
"Return non-nilif an entry is associated with explanation"
(member "explain" (org-get-tags nil no-inherit)))
(defun org-drill-end-of-entry-pos ()
"Return the buffer position at the end of the current entry's subtree."
(save-excursion
(org-end-of-subtree)
(point)))
(defun org-drill-explain-answer-presenter ()
"Append the entry's explanation text as an overlay after the subtree.
The explanation is gathered from `:explain:'-tagged ancestors and stored
in `org-drill-explain-overlay' so `org-drill-explain-cleaner' can remove it."
(save-excursion
(when org-drill-explain-overlay
(delete-overlay org-drill-explain-overlay))
(let* ((end (org-drill-end-of-entry-pos))
(ov (make-overlay
end end
(current-buffer))))
(overlay-put ov 'after-string
(format "\n\nExplanation:\n\n%s"
(mapconcat 'identity
(org-drill-get-explain-text) "\n\n")))
(setq org-drill-explain-overlay ov))))
(defun org-drill-explain-cleaner ()
"Delete the explanation overlay left by `org-drill-explain-answer-presenter'."
(when org-drill-explain-overlay
(delete-overlay org-drill-explain-overlay)))
;;; Leitner Learning
(defvar org-drill-leitner-boxed-entries nil
"All leitner entries that are currently in an active box.")
(defvar org-drill-leitner-unboxed-entries nil
"All leitner entries that are not in a box.")
(defvar org-drill-leitner-promote-to-drill-p t)
(defvar org-drill-leitner-completed 0
"The number of entries that have been completed this time.")
(defun org-drill-sm-or-leitner ()
"Resume or start a drill, choosing the Leitner or SM path automatically.
Reuse the last session if one is pending; otherwise scan and dispatch to
the Leitner or spaced-repetition flow as appropriate."
(interactive)
;; org-drill-again uses org-drill-pending-entry-count to decide
;; whether it needs to scan or not.
(let* ((session
(or org-drill-last-session (org-drill-session)))
(pending
(org-drill-pending-entry-count session)))
(unless (cl-plusp pending)
(org-drill-map-entries
(apply-partially 'org-drill-map-entry-function session)
nil nil))
;; if the overdue entries are not ones we have just created
(if (> (org-drill-pending-entry-count session) org-drill-leitner-completed)
;; we should have scanned previously if we need to
(progn
(message "Org Drill: Starting SM learning")
(sit-for 0.5)
(setq org-drill-last-session session)
(org-drill-again))
(message "Org Drill: Starting leitner learning")
(sit-for 0.5)
(org-drill-leitner session))))
(defun org-drill-leitner (&optional session)
"Perform Leitner learning"
(interactive)
(let ((org-drill-leitner-boxed-entries nil)
(org-drill-leitner-unboxed-entries nil)
(session (setq org-drill-last-session
(or session
(org-drill-session))))
(count 0))
(org-drill-all-leitner-capture)
;; make sure we have enough (or at least the maximum number we
;; can) of boxed entities
(when (<
(length org-drill-leitner-boxed-entries)
(- org-drill-maximum-items-per-session count))
(org-drill-leitner-start-box
(- org-drill-maximum-items-per-session
(length org-drill-leitner-boxed-entries)
count))
(setq org-drill-leitner-boxed-entries nil)
(setq org-drill-leitner-unboxed-entries nil)
(org-drill-all-leitner-capture))
(pcase
(catch 'user-exit
(seq-map
(lambda (loc)
(org-drill-goto-entry loc)
(let ((r (org-drill-leitner-entry session)))
;; short circuit if necessary
(unless (eq t r)
(throw 'user-exit (list r loc)))))
(org-drill-shuffle
(seq-take org-drill-leitner-boxed-entries
org-drill-maximum-items-per-session))))
(`(quit ,_) t)
(`(edit ,loc)
(org-drill-goto-entry loc)
(org-reveal)
(org-fold-show-entry))
(`,_
(message "Finished Leitner Learning: %s complete today, %s in process, %s to start"
org-drill-leitner-completed
(length org-drill-leitner-boxed-entries)
(length org-drill-leitner-unboxed-entries))))))
;; take from John Kitchen
(defun org-drill-swap (LIST el1 el2)
"in LIST swap indices EL1 and EL2 in place"
(let ((tmp (elt LIST el1)))
(setf (elt LIST el1) (elt LIST el2))
(setf (elt LIST el2) tmp)))
(defun org-drill-shuffle (LIST)
"Return a random permutation of the elements in LIST.
The shuffle runs over a temporary vector with a Fisher-Yates pass,
so the cost is linear in the length of LIST rather than quadratic
\(the previous version indexed a list with `elt' on every swap)."
(cl-check-type LIST list)
(let ((vec (vconcat LIST)))
(cl-loop for i from (1- (length vec)) downto 1
do (let ((j (random (1+ i))))
(cl-rotatef (aref vec i) (aref vec j))))
(append vec nil)))
(defun org-drill-leitner-start-box (number)
"Box some items for the first time."
(message "Starting %s new items" number)
(sit-for 0.25)
(seq-map
(lambda (loc)
(org-drill-goto-entry loc)
(message "New leitner entry: %s" (org-drill-get-entry-text))
(sit-for 0.5)
(org-set-property "DRILL_LEITNER_BOX" "1"))
(seq-take
(org-drill-shuffle (seq-copy org-drill-leitner-unboxed-entries))
number)))
(defun org-drill-map-leitner (func &optional scope)
"Return all entries marked with leitner tag."
(org-map-entries
func (concat "+" "leitner")
(org-drill-current-scope scope)))
(defun org-drill-all-leitner-capture (&optional scope)
"Capture all items marked with a leitner tag"
(let ((org-drill-question-tag org-drill-leitner-tag))
(org-drill-map-leitner
(apply-partially #'org-drill-map-leitner-capture (org-drill-session))
scope)
(setq org-drill-leitner-boxed-entries
(nreverse org-drill-leitner-boxed-entries)
org-drill-leitner-unboxed-entries
(nreverse org-drill-leitner-unboxed-entries))))
(defun org-drill-map-leitner-capture (session)
"Capture this entry if it is a valid leitner entry"
;; This bit is all rather shared with org-drill-map-entry-function
(org-drill-progress-message
(+ (length org-drill-leitner-unboxed-entries)
(length org-drill-leitner-boxed-entries))
(cl-incf (oref session cnt)))
(when (org-drill-entry-p)
(org-drill-id-get-create-with-warning session)
(let ((leitner-box (org-entry-get (point) "DRILL_LEITNER_BOX" nil)))
(cond
;; Entries we have not looked at yet
((null leitner-box)
(push (point-marker) org-drill-leitner-unboxed-entries))
;; Entries we have finished with
((> (string-to-number leitner-box) 5) nil)
((and
(>= (string-to-number leitner-box) 0)
(<= (string-to-number leitner-box) 5))
(push (point-marker)
org-drill-leitner-boxed-entries))))))
(defun org-drill-leitner-entry (session)
"Interactive drill for the current entry."
(let ((org-drill-question-tag org-drill-leitner-tag))
(org-drill-entry-f session #'org-drill-leitner-rebox)))
(defun org-drill-leitner-rebox (session)
"Return qualityrating (0-5), or nil if the user quit."
(let ((ch (org-drill--read-rating-key
(oref session typed-answer)
"0-2 Means you have forgotten the item.
3-5 Means you have remembered the item.
0 - Completely forgot. (Back to Zero)
1 - Even after seeing the answer, it still took a bit to sink in (Back to one)
2 - After seeing the answer, you remembered it (Remain in current Box)
3 - It took you awhile, but you finally remembered. (Forward One)
4 - After a little bit of thought you remembered. (Forward One)
5 - You remembered the item really easily. (Forward One)")))
(cond
((and (>= ch ?0) (<= ch ?5))
(let ((current-box
;; Default to 0 if the property isn't set — `org-entry-get'
;; returns nil and `string-to-number' would error on nil.
;; Box 0 is sensible: a downgrade stays at 0, a promotion
;; goes to 1.
(string-to-number
(or (org-entry-get (point) "DRILL_LEITNER_BOX" nil)
"0"))))
(cond
((or (= ch ?0))
(message "Refiled down to box: 1")
(org-set-property "DRILL_LEITNER_BOX" "1"))
((or (= ch ?1))
(let ((box
(format
"%s"
(if (eq current-box 1)
1
(- current-box 1)))))
(message "Refiled down to box: %s" box)
(sit-for 0.3)
(org-set-property
"DRILL_LEITNER_BOX" box)))
((= ch ?2)
;; neither promote nor demote
(message "Remaining in box: %s" current-box)
(sit-for 0.3))
((or (= ch ?3) (= ch ?4)(= ch ?5))
(org-drill-leitner-promote current-box)))
t))
((= ch org-drill--edit-key)
'edit)
((= ch org-drill--quit-key)
'quit)
(t nil))))
(defun org-drill-leitner-promote (current-box)
"Promote the current entry to drill or otherwise"
(when (eq current-box 5)
(org-toggle-tag "leitner" 'off)
(when org-drill-leitner-promote-to-drill-p
(org-toggle-tag "drill" 'on))
(cl-incf org-drill-leitner-completed))
(org-set-property
"DRILL_LEITNER_BOX"
(format
"%s"
(+ current-box 1)))
(message "Refiled to box: %s" (+ current-box 1))
(sit-for 0.3))
;;; Test functions
(defun org-drill-test-display ()
"Developer helper: present the entry at point as a drill card.
Temporarily tags the entry so it counts as a drill item, runs the
presenter through `org-drill-entry-f', then removes the tag again."
(interactive)
;; set tag to anything
(org-toggle-tag "zysygy")
(unwind-protect
(let ((org-drill-question-tag "zysygy"))
(org-drill-entry-f (org-drill-session)
#'org-drill-test-display-rescheduler))
(org-toggle-tag "zysygy")))
(defun org-drill-test-display-rescheduler (_session)
"Stand-in reschedule function for `org-drill-test-display'.
Runs the answer hook and waits for a keypress instead of actually
rescheduling, so the developer helper can show the answer side."
(run-hooks 'org-drill-display-answer-hook)
;; Normally, the rescheduler waits for input at this point
(read-key-sequence "Press anything to continue"))
(defun org-drill-leitner-vs-drill-entries ()
"Report how many entries in scope are Leitner items versus drill items.
A diagnostic command for decks that mix the two systems."
(interactive)
(let
((number-drill-entries 0)
(org-drill-leitner-unboxed-entries nil)
(org-drill-leitner-boxed-entries nil))
(org-drill-all-leitner-capture)
(org-drill-map-entries
(lambda ()
(setq number-drill-entries (+ 1 number-drill-entries)))
nil nil)
(message "There are %s drill entries\nThere are %s leitner entries\nA total of %s entries."
number-drill-entries
(+ (length org-drill-leitner-boxed-entries)
(length org-drill-leitner-unboxed-entries))
(+ number-drill-entries
(+ (length org-drill-leitner-boxed-entries)
(length org-drill-leitner-unboxed-entries))))))
;;; Statistics dashboard: step-2 aggregation and rendering layer.
;;
;; Aggregators, section renderers, and the dashboard UI shell built on
;; the step-1 session-record/session-log data layer above.
;;; Statistics dashboard: customization and shared primitives
;; The `org-drill-statistics' defgroup already exists in org-drill.el
;; (around line 87) as a sibling of `org-drill-session'. These
;; defcustoms attach to it; do not redefine the group.
(defcustom org-drill-statistics-trend-days 90
"Number of days the statistics dashboard trend section spans.
Daily and weekly aggregates are computed over the most recent window
of this many days, ending today. Weekly aggregates additionally cap at
the last 12 weeks."
:type 'integer
:group 'org-drill-statistics)
(defcustom org-drill-statistics-forecast-days 7
"Number of days the statistics dashboard forecast section spans.
Cards are bucketed into this many upcoming days by their SCHEDULED
day, starting today."
:type 'integer
:group 'org-drill-statistics)
(defcustom org-drill-statistics-attention-row-limit 10
"Maximum rows shown per needs-attention table on the dashboard.
Tables that would exceed this length are truncated and gain a
\"+N more\" footer naming the number of hidden rows."
:type 'integer
:group 'org-drill-statistics)
(defcustom org-drill-statistics-leech-quality-threshold 2.5
"Average-quality ceiling below which a card counts as a leech candidate.
A card is flagged when its DRILL_FAILURE_COUNT is at least
`org-drill-leech-failure-threshold' and its DRILL_AVERAGE_QUALITY is
strictly less than this value."
:type 'number
:group 'org-drill-statistics)
;;; Shared pure primitives for the aggregation helpers.
;;
;; These are small, side-effect-free building blocks reused by the
;; trend, forecast, distribution, and needs-attention aggregators. Day
;; numbers are absolute integer day counts (`time-to-days'), so they
;; can be compared and subtracted directly without timezone surprises.
(defun org-drill-statistics--today-day ()
"Return today's absolute day number as an integer.
This is `time-to-days' of the current time. It is factored out so
tests can redefine it to a fixed day without mocking the clock."
(time-to-days (current-time)))
(defun org-drill-statistics--record-day (record)
"Return the integer day number of RECORD's start time.
RECORD is an `org-drill-session-record'; its start-time slot is a float
as produced by `float-time'. The result is an absolute day number, the
same scale as `org-drill-statistics--today-day'."
(time-to-days
(seconds-to-time (org-drill-session-record-start-time record))))
(defun org-drill-statistics--filter-log (log &optional algorithm)
"Return the records in LOG whose algorithm matches ALGORITHM.
LOG is a list of `org-drill-session-record'. When ALGORITHM is nil,
return LOG unchanged (all algorithms). Otherwise keep only records
whose algorithm slot is `eq' to ALGORITHM. The original list is not
modified."
(if (null algorithm)
log
(cl-remove-if-not
(lambda (record)
(eq (org-drill-session-record-algorithm record) algorithm))
log)))
(defun org-drill-statistics--log-since (log cutoff-float)
"Return the records in LOG started at or after CUTOFF-FLOAT.
LOG is a list of `org-drill-session-record'. CUTOFF-FLOAT is a float
in the same units as `float-time' (and the records' start-time slot).
A record is kept when its start-time is greater than or equal to
CUTOFF-FLOAT. The original list is not modified."
(cl-remove-if-not
(lambda (record)
(>= (org-drill-session-record-start-time record) cutoff-float))
log))
(defcustom org-drill-statistics-distribution-bar-width 40
"Maximum width, in block characters, of a distribution histogram bar.
The quality with the highest count fills this many blocks; every other
quality's bar is scaled proportionally against it. A larger value makes
the histogram wider on the dashboard."
:type 'integer
:group 'org-drill-statistics)
(defun org-drill-statistics--render-distribution (histogram)
"Return the quality-distribution section as a string.
HISTOGRAM is a 6-element vector of integer counts for qualities 0..5, as
produced by `org-drill-statistics--quality-histogram': element I is the
number of times quality I was rated.
The result begins with the section subheading \"** Quality
Distribution\" and contains one row per quality 0..5. Each row shows the
quality number, a horizontal bar of repeated block characters, the
absolute count, and that quality's percentage of the total ratings. The
longest bar is `org-drill-statistics-distribution-bar-width' blocks wide
and the others scale against the largest count, so the bars stay
proportional. A quality with a zero count renders an empty bar but is
still listed, so all six rows are always present.
When the histogram is empty (every count zero, no ratings recorded), the
percentages are all 0 and a short note replaces the bars' meaning, but
the six rows are still emitted so the layout is stable. This function is
pure: it returns a string and performs no buffer or display side
effects."
(let* ((counts (append histogram nil))
(total (apply #'+ counts))
(max-count (apply #'max counts))
(width (max 1 org-drill-statistics-distribution-bar-width))
(block ?\x2588)
(lines nil))
(dotimes (q 6)
(let* ((count (nth q counts))
(percent (if (> total 0)
(round (* 100.0 (/ (float count) total)))
0))
(bar-len (if (> max-count 0)
(round (* (/ (float count) max-count) width))
0))
(bar (make-string bar-len block))
(padded (concat bar
(make-string (max 0 (- width (length bar)))
?\s))))
(push (format "%d %s %4d %3d%%"
q padded count percent)
lines)))
(concat "** Quality Distribution\n"
(if (> total 0)
(format "Total ratings: %d\n" total)
"No quality ratings recorded yet.\n")
(mapconcat #'identity (nreverse lines) "\n")
"\n")))
(defun org-drill-statistics--reviews-by-day (log &optional days)
"Return a vector of review COUNTS per day over the last DAYS.
LOG is a list of `org-drill-session-record'; order does not matter
here. DAYS defaults to `org-drill-statistics-trend-days'. A
non-positive DAYS is clamped to 1.
The result has one slot per day, oldest to newest, covering the window
that ends today and spans DAYS days inclusive: slot 0 is the oldest day
in the window, the final slot is today. A day's count is the sum of
\(length qualities) across every record whose start day falls in the
window. Records that fall outside the window, in the past or the
future, are ignored. Empty days are zero-filled. A record with a nil
qualities slot contributes zero.
Day numbers come from `org-drill-statistics--record-day' and
`org-drill-statistics--today-day', so the window aligns on absolute
calendar days and the helper stays testable by redefining those."
(let* ((days (max 1 (or days org-drill-statistics-trend-days)))
(today (org-drill-statistics--today-day))
(start-day (- today (1- days)))
(counts (make-vector days 0)))
(dolist (record log counts)
(let* ((day (org-drill-statistics--record-day record))
(idx (- day start-day)))
(when (and (>= idx 0) (< idx days))
(let ((qualities (org-drill-session-record-qualities record)))
(aset counts idx
(+ (aref counts idx)
(if qualities (length qualities) 0)))))))))
(defun org-drill-statistics--pass-rate-by-day (log &optional days)
"Return a vector of daily pass rates over the last DAYS, oldest first.
LOG is a list of `org-drill-session-record', newest first as stored in
`org-drill-session-log'. DAYS defaults to `org-drill-statistics-trend-days'.
The result is a vector of length DAYS. Element 0 is the oldest day in
the window and the final element is today, so the vector reads left to
right in chronological order, matching sparkline rendering.
Each element is an integer pass rate, 0 to 100, for that day, or nil
when no qualities were recorded that day. A day's pass rate is the
percentage of that day's qualities that are passes across every record
started that day. A quality is a pass when it is strictly greater than
`org-drill-failure-quality'. Records outside the window are ignored.
The function is pure with respect to LOG: it reads only the record
slots and the failure-quality threshold. Today is taken from
`org-drill-statistics--today-day', which tests may redefine to a fixed
day."
(let* ((days (or days org-drill-statistics-trend-days))
(days (max 1 days))
(today (org-drill-statistics--today-day))
(oldest (- today (1- days)))
(passes (make-vector days 0))
(totals (make-vector days 0))
(threshold org-drill-failure-quality))
(dolist (record log)
(let* ((day (org-drill-statistics--record-day record))
(idx (- day oldest)))
(when (and (>= idx 0) (< idx days))
(let ((qualities (org-drill-session-record-qualities record)))
(when qualities
(dotimes (qi (length qualities))
(let ((q (aref qualities qi)))
(aset totals idx (1+ (aref totals idx)))
(when (> q threshold)
(aset passes idx (1+ (aref passes idx)))))))))))
(let ((result (make-vector days nil)))
(dotimes (i days)
(let ((total (aref totals i)))
(when (> total 0)
(aset result i (round (* 100.0 (/ (float (aref passes i))
total)))))))
result)))
(defconst org-drill-statistics--sparkline-chars "▁▂▃▄▅▆▇█"
"Eight-level quadrant-block charset for sparklines, low to high.
Index 0 is the shortest block, index 7 the full block. Used by
`org-drill-statistics--sparkline' to map scaled values to glyphs.")
(defun org-drill-statistics--sparkline (numbers &optional max)
"Render NUMBERS as a quadrant-block sparkline string.
NUMBERS is a sequence (list or vector) of non-negative numbers, with
nil permitted for a missing data point. Each non-nil value is scaled
against MAX and mapped to one of eight block glyphs in
`org-drill-statistics--sparkline-chars', low to high. Each nil value
renders as a single space, preserving column alignment.
MAX is the value mapped to the tallest block. When MAX is nil it
defaults to the largest non-nil value in NUMBERS. A value at or above
MAX renders as the full block; intermediate values scale linearly.
Edge cases handled without error:
- Empty NUMBERS returns the empty string.
- All-nil NUMBERS returns a string of spaces, one per entry.
- A MAX of zero (all values zero, or an explicit zero MAX) renders
every non-nil value as the lowest block, since no value exceeds the
ceiling and division by zero is avoided.
Values are clamped to the inclusive 0..MAX range before scaling, so a
value above MAX still maps to the full block rather than overflowing
the charset."
(let* ((seq (append numbers nil))
(effective-max
(or max
(let ((non-nil (delq nil (copy-sequence seq))))
(if non-nil (apply #'max non-nil) 0))))
(top (1- (length org-drill-statistics--sparkline-chars))))
(mapconcat
(lambda (n)
(cond
((null n) " ")
((or (null effective-max) (<= effective-max 0))
(substring org-drill-statistics--sparkline-chars 0 1))
(t
(let* ((clamped (max 0 (min n effective-max)))
(index (round (* (/ (float clamped) effective-max) top))))
(substring org-drill-statistics--sparkline-chars
index (1+ index))))))
seq
"")))
;;; Weekly trend aggregation for the statistics dashboard.
(defun org-drill-statistics--week-start-day (day)
"Return the absolute day number of the Monday starting DAY's week.
DAY is an absolute day number as produced by `time-to-days'. Weeks
start on Monday, matching ISO week conventions. The result is itself an
absolute day number and is `<=' DAY. Pure integer arithmetic, so it is
deterministic across timezones."
(- day (mod (- day 1) 7)))
(defun org-drill-statistics--avg-duration-min (records)
"Return the mean session duration of RECORDS in minutes, as a float.
RECORDS is a list of `org-drill-session-record'. Each duration is
end-time minus start-time, divided by 60. Returns 0.0 when RECORDS is
empty."
(if (null records)
0.0
(/ (cl-reduce
(lambda (acc record)
(+ acc
(/ (- (org-drill-session-record-end-time record)
(org-drill-session-record-start-time record))
60.0)))
records
:initial-value 0.0)
(float (length records)))))
(defun org-drill-statistics--weekly-aggregates (log &optional weeks)
"Return per-week aggregate statistics for the most recent WEEKS.
LOG is a list of `org-drill-session-record' in any order. WEEKS
defaults to 12 and bounds how many weeks the result spans, ending with
the week that contains today. WEEKS must be a positive integer.
The result is a list of plists, oldest week first, one per week in the
window even when a week has no sessions. Each plist has these keys:
:week-start absolute day number of that week's Monday
:reviews total quality ratings entered across the week
:pass-percent pooled pass percentage (0 when the week has none)
:avg-duration-min mean session duration in minutes, as a float
Weeks start on Monday. REVIEWS counts every entry in each record's
qualities vector, so a week's reviews is the sum over its sessions.
PASS-PERCENT is recomputed from the week's pooled qualities via
`org-drill--compute-pass-percent', not averaged from the stored
per-session percentages, so multi-session weeks stay correctly weighted.
This function is pure: it reads no org entries and does not modify LOG."
(setq weeks (or weeks 12))
(unless (and (integerp weeks) (>= weeks 1))
(error "WEEKS must be a positive integer, got %s" weeks))
(let* ((this-week (org-drill-statistics--week-start-day
(org-drill-statistics--today-day)))
(oldest (- this-week (* 7 (1- weeks))))
(buckets (make-vector weeks nil)))
(dolist (record log)
(let* ((wk (org-drill-statistics--week-start-day
(org-drill-statistics--record-day record)))
(idx (/ (- wk oldest) 7)))
(when (and (>= idx 0) (< idx weeks)
(zerop (mod (- wk oldest) 7)))
(aset buckets idx (cons record (aref buckets idx))))))
(let ((result nil))
(dotimes (i weeks)
(let* ((idx (- weeks 1 i))
(records (aref buckets idx))
(week-start (+ oldest (* 7 idx)))
(qualities (apply #'vconcat
(mapcar #'org-drill-session-record-qualities
records))))
(push (list :week-start week-start
:reviews (length qualities)
:pass-percent
(org-drill--compute-pass-percent qualities)
:avg-duration-min
(org-drill-statistics--avg-duration-min records))
result)))
result)))
(defun org-drill-statistics--quality-histogram (log)
"Return a 6-element vector of quality counts across every record in LOG.
LOG is a list of `org-drill-session-record'. Element I of the result is
the total number of times quality I was entered across all records'
qualities vectors, for I in 0..5. Qualities outside the 0..5 range are
ignored defensively so a malformed record cannot corrupt the histogram.
A record whose qualities slot is nil contributes nothing. The original
log and its records are not modified."
(let ((histogram (make-vector 6 0)))
(dolist (record log)
(let ((qualities (org-drill-session-record-qualities record)))
(when qualities
(mapc
(lambda (quality)
(when (and (integerp quality) (>= quality 0) (<= quality 5))
(aset histogram quality (1+ (aref histogram quality)))))
(append qualities nil)))))
histogram))
;;; Statistics dashboard: card-population overview aggregator.
(defun org-drill-statistics--overview-counts (&optional scope)
"Return a card-population overview plist for the drill entries in SCOPE.
SCOPE is a value understood by `org-drill-current-scope' (a symbol such
as `file', `directory', `agenda', or a list of files); nil means the
current value of `org-drill-scope'. The plist has four keys:
:total every genuine drill card seen (any non-nil entry status).
:new cards never reviewed (status :new).
:mature cards with an established interval (status :young, :old, or
:overdue), counted together as the mature population.
:lapsed cards whose last review failed (status :failed).
The mapping is driven by `org-drill-entry-status', which classifies the
entry at point into one of nil, :unscheduled, :future, :new, :failed,
:overdue, :young, or :old. Cards classified :unscheduled or :future are
real drill cards, so they count toward :total, but they are neither new,
mature, nor lapsed, so they are absent from those three buckets. Entries
whose status is nil (non-drill headings and skippable empty cards) are
ignored entirely and do not count toward :total.
A fresh, non-cram `org-drill-session' is created for the classification
so the result reflects the cards' own scheduling state rather than any
in-progress drill session."
(let ((session (org-drill-session))
(total 0)
(new 0)
(mature 0)
(lapsed 0))
(org-drill-map-entries
(lambda ()
(let ((status (car (org-drill-entry-status session))))
(when status
(cl-incf total)
(cl-case status
(:new (cl-incf new))
((:young :old :overdue) (cl-incf mature))
(:failed (cl-incf lapsed))))))
scope)
(list :total total :new new :mature mature :lapsed lapsed)))
;;; Statistics dashboard: needs-attention selectors (aggregation 7/8)
;;
;; These three selectors return lists of (HEADING . POS) cons cells for
;; the dashboard's needs-attention section: leech candidates,
;; long-overdue cards, and forgotten-new cards. POS is the integer
;; buffer position of the entry's heading (`point' at the heading), so
;; the renderer can build a follow link.
;;
;; The org traversal is isolated in a single collector,
;; `org-drill-statistics--collect-attention-data', which gathers one
;; plist per drill entry. Every predicate and sort then operates on
;; that plain data, so the classification and ordering logic is unit
;; testable with a `with-temp-buffer' fixture and without touching the
;; clock. Day-based fields are stored as integers relative to a
;; supplied "today" day number, again so tests stay deterministic.
(cl-defstruct (org-drill-statistics--entry-attention-data
(:constructor org-drill-statistics--make-entry-attention-data)
(:copier nil))
"Per-entry data used by the needs-attention selectors.
HEADING is the cleaned outline heading string. POS is the integer
buffer position of the heading. FAILURE-COUNT is DRILL_FAILURE_COUNT as
an integer (0 when absent). AVG-QUALITY is DRILL_AVERAGE_QUALITY as a
float, or nil when absent. DAYS-SINCE-REVIEW is the integer day count
since DRILL_LAST_REVIEWED, or nil when never reviewed. DAYS-SINCE-ADDED
is the integer day count since DATE_ADDED, or nil when absent.
TOTAL-REPEATS is DRILL_TOTAL_REPEATS as an integer (0 when absent)."
heading pos failure-count avg-quality
days-since-review days-since-added total-repeats)
;; Short aliases for the long struct accessors, used by the sort
;; comparators below so the comparator lines stay readable.
(defalias 'org-drill-statistics--ad-avg
#'org-drill-statistics--entry-attention-data-avg-quality)
(defalias 'org-drill-statistics--ad-fails
#'org-drill-statistics--entry-attention-data-failure-count)
(defalias 'org-drill-statistics--ad-review
#'org-drill-statistics--entry-attention-data-days-since-review)
(defalias 'org-drill-statistics--ad-added
#'org-drill-statistics--entry-attention-data-days-since-added)
(defun org-drill-statistics--collect-attention-data (&optional scope today-day)
"Collect `org-drill-statistics--entry-attention-data' for each drill entry.
SCOPE is passed through to `org-drill-map-entries' to bound the
traversal, defaulting to `org-drill-scope'. TODAY-DAY is the absolute
day number treated as today, defaulting to
`org-drill-statistics--today-day'; it is factored out so callers and
tests can pin the reference day.
The point is on each drill entry's heading while its data is read, so
this must run inside an org buffer. Returns a list of structs, one per
drill entry, in document order."
(let ((today (or today-day (org-drill-statistics--today-day))))
(org-drill-map-entries
(lambda ()
(org-drill-statistics--entry-attention-data-at-point today))
scope)))
(defun org-drill-statistics--entry-attention-data-at-point (today-day)
"Read needs-attention data for the drill entry at point.
TODAY-DAY is the absolute day number treated as today. Returns an
`org-drill-statistics--entry-attention-data' struct. This reads the
DRILL_* and DATE_ADDED properties via `org-entry-get' and the heading
via `org-get-heading', so the point must be on a drill heading."
(let* ((pos (point))
(heading (org-get-heading t t t t))
(failure-raw (org-entry-get pos "DRILL_FAILURE_COUNT"))
(avg-raw (org-entry-get pos "DRILL_AVERAGE_QUALITY"))
(last-raw (org-entry-get pos "DRILL_LAST_REVIEWED"))
(added-raw (org-entry-get pos "DATE_ADDED"))
(repeats-raw (org-entry-get pos "DRILL_TOTAL_REPEATS")))
(org-drill-statistics--make-entry-attention-data
:heading (or heading "")
:pos pos
:failure-count (if failure-raw (string-to-number failure-raw) 0)
:avg-quality (and avg-raw (string-to-number avg-raw))
:days-since-review
(org-drill-statistics--days-since-org-timestamp last-raw today-day)
:days-since-added
(org-drill-statistics--days-since-org-timestamp added-raw today-day)
:total-repeats (if repeats-raw (string-to-number repeats-raw) 0))))
(defun org-drill-statistics--days-since-org-timestamp (timestamp today-day)
"Return integer days from org TIMESTAMP string to TODAY-DAY.
TIMESTAMP is an org timestamp string (active or inactive) or nil.
TODAY-DAY is an absolute day number as from `time-to-days'. Returns the
difference TODAY-DAY minus the timestamp's day, so a value reviewed
today yields 0 and an older review yields a positive count. Returns nil
when TIMESTAMP is nil or cannot be parsed."
(when (and timestamp (not (string-empty-p timestamp)))
(condition-case nil
(- today-day
(time-to-days
(apply #'encode-time (org-parse-time-string timestamp))))
(error nil))))
;;; Pure predicates and the row cap.
(defun org-drill-statistics--leech-candidate-p (data)
"Non-nil when DATA describes a leech candidate.
DATA is an `org-drill-statistics--entry-attention-data'. A card is a
leech candidate when its failure count is at least
`org-drill-leech-failure-threshold' and its average quality is
known and strictly below `org-drill-statistics-leech-quality-threshold'.
A card with no recorded average quality is not flagged, since the
quality criterion cannot be evaluated."
(let ((avg (org-drill-statistics--entry-attention-data-avg-quality data)))
(and (>= (org-drill-statistics--entry-attention-data-failure-count data)
org-drill-leech-failure-threshold)
avg
(< avg org-drill-statistics-leech-quality-threshold))))
(defun org-drill-statistics--long-overdue-p (data)
"Non-nil when DATA describes a long-overdue card.
DATA is an `org-drill-statistics--entry-attention-data'. A card is
long overdue when it has a recorded last-review date and that review is
more than `org-drill-lapse-threshold-days' days ago. A card that was
never reviewed has no overdue measure and is not flagged here."
(let ((days (org-drill-statistics--entry-attention-data-days-since-review
data)))
(and days (> days org-drill-lapse-threshold-days))))
(defun org-drill-statistics--forgotten-new-p (data)
"Non-nil when DATA describes a forgotten-new card.
DATA is an `org-drill-statistics--entry-attention-data'. A card is
forgotten-new when it was added at least 14 days ago and has never been
repeated, that is its total repeats are zero. A card with no recorded
add date is not flagged, since its age cannot be evaluated."
(let ((added (org-drill-statistics--entry-attention-data-days-since-added
data)))
(and added
(>= added 14)
(= (org-drill-statistics--entry-attention-data-total-repeats data)
0))))
(defun org-drill-statistics--cap-rows (rows)
"Cap ROWS at `org-drill-statistics-attention-row-limit' entries.
ROWS is a list. When it is no longer than the limit, return it
unchanged. Otherwise return its first LIMIT elements; the renderer is
responsible for the trailing \"+N more\" footer using the original
length. ROWS is not modified."
(let ((limit org-drill-statistics-attention-row-limit))
(if (<= (length rows) limit)
rows
(cl-subseq rows 0 limit))))
;;; The three public selectors.
;;
;; Each filters the collected per-entry data with its predicate, sorts
;; per the spec, maps to (HEADING . POS), and caps the row count.
(defun org-drill-statistics--leech-candidates (&optional scope)
"Return capped leech candidates as a list of (HEADING . POS).
SCOPE bounds the traversal, defaulting to `org-drill-scope'. Candidates
satisfy `org-drill-statistics--leech-candidate-p' and are sorted worst
first by ascending average quality, breaking ties by descending failure
count. The list is capped at `org-drill-statistics-attention-row-limit'
rows; the full count is available to the caller via a fresh scan if a
\"+N more\" footer is needed."
(let* ((data (cl-remove-if-not
#'org-drill-statistics--leech-candidate-p
(org-drill-statistics--collect-attention-data scope)))
(sorted
(sort data
(lambda (a b)
(let ((qa (org-drill-statistics--ad-avg a))
(qb (org-drill-statistics--ad-avg b)))
(if (= qa qb)
(> (org-drill-statistics--ad-fails a)
(org-drill-statistics--ad-fails b))
(< qa qb)))))))
(org-drill-statistics--cap-rows
(mapcar (lambda (d)
(cons (org-drill-statistics--entry-attention-data-heading d)
(org-drill-statistics--entry-attention-data-pos d)))
sorted))))
(defun org-drill-statistics--long-overdue (&optional scope)
"Return capped long-overdue cards as a list of (HEADING . POS).
SCOPE bounds the traversal, defaulting to `org-drill-scope'. Cards
satisfy `org-drill-statistics--long-overdue-p' and are sorted most
overdue first, by descending days since last review. The list is capped
at `org-drill-statistics-attention-row-limit' rows."
(let* ((data (cl-remove-if-not
#'org-drill-statistics--long-overdue-p
(org-drill-statistics--collect-attention-data scope)))
(sorted
(sort data
(lambda (a b)
(> (org-drill-statistics--ad-review a)
(org-drill-statistics--ad-review b))))))
(org-drill-statistics--cap-rows
(mapcar (lambda (d)
(cons (org-drill-statistics--entry-attention-data-heading d)
(org-drill-statistics--entry-attention-data-pos d)))
sorted))))
(defun org-drill-statistics--forgotten-new (&optional scope)
"Return capped forgotten-new cards as a list of (HEADING . POS).
SCOPE bounds the traversal, defaulting to `org-drill-scope'. Cards
satisfy `org-drill-statistics--forgotten-new-p' and are sorted oldest
first, by descending days since they were added. The list is capped at
`org-drill-statistics-attention-row-limit' rows."
(let* ((data (cl-remove-if-not
#'org-drill-statistics--forgotten-new-p
(org-drill-statistics--collect-attention-data scope)))
(sorted
(sort data
(lambda (a b)
(> (org-drill-statistics--ad-added a)
(org-drill-statistics--ad-added b))))))
(org-drill-statistics--cap-rows
(mapcar (lambda (d)
(cons (org-drill-statistics--entry-attention-data-heading d)
(org-drill-statistics--entry-attention-data-pos d)))
sorted))))
;;; Forecast aggregator (helper 8/8).
;;
;; The forecast answers: of the scheduled cards in scope, how many come
;; due on each of the next DAYS days, starting today? The org-reading
;; part (collecting SCHEDULED day numbers from entries in scope) is kept
;; separate from the pure bucketing math so the math is unit-testable
;; without an org buffer.
(defun org-drill-statistics--bucket-forecast-days
(scheduled-days today-day days)
"Bucket SCHEDULED-DAYS into a DAYS-long forecast vector.
SCHEDULED-DAYS is a list of absolute integer day numbers (the scale of
`time-to-days'), one per scheduled card. TODAY-DAY is today's absolute
day number. DAYS is the number of forecast buckets.
Return a list of DAYS integers. Element I is the number of entries in
SCHEDULED-DAYS whose day equals TODAY-DAY plus I, so index 0 counts
cards due today and the last index counts cards due TODAY-DAY plus DAYS
minus one. Cards scheduled in the past or beyond the window are not
counted. When DAYS is zero or negative the result is the empty list."
(let ((buckets (if (> days 0) (make-vector days 0) (vector))))
(when (> days 0)
(dolist (day scheduled-days)
(let ((offset (- day today-day)))
(when (and (>= offset 0) (< offset days))
(aset buckets offset (1+ (aref buckets offset)))))))
(append buckets nil)))
(defun org-drill-statistics--scheduled-days (&optional scope)
"Return the SCHEDULED day numbers of drill entries in SCOPE.
SCOPE accepts the same values as `org-drill-scope'; nil uses the
current value. Walk the drill entries in scope with
`org-drill-map-entries' and collect, for each entry that carries a
SCHEDULED time, its absolute integer day number (`time-to-days' of the
scheduled time). Entries without a SCHEDULED time are skipped. The
returned list is in entry-traversal order and may contain duplicate
days (several cards due the same day)."
(delq nil
(org-drill-map-entries
(lambda ()
(let ((scheduled (org-get-scheduled-time (point))))
(when scheduled
(time-to-days scheduled))))
scope)))
(defun org-drill-statistics--forecast (&optional scope days)
"Return the upcoming-due forecast for drill entries in SCOPE.
SCOPE accepts the same values as `org-drill-scope'; nil uses the
current value. DAYS is the number of forecast buckets and defaults to
`org-drill-statistics-forecast-days'.
The result is a list of DAYS integers. Element I counts the cards in
scope whose SCHEDULED date falls on today plus I, so index 0 is due
today and the last index is due DAYS minus one days out. Cards
scheduled in the past or beyond the window are not counted, and cards
with no SCHEDULED time are ignored."
(let ((days (or days org-drill-statistics-forecast-days)))
(org-drill-statistics--bucket-forecast-days
(org-drill-statistics--scheduled-days scope)
(org-drill-statistics--today-day)
days)))
;;; Statistics dashboard: needs-attention section renderer (render 4/5).
;;
;; `org-drill-statistics--render-attention' returns the section text as a
;; string so it is unit testable without a live dashboard buffer. It runs
;; a single org scan via `org-drill-statistics--collect-attention-data',
;; then filters, sorts, caps, and footers each of the three categories.
;; Doing one scan (rather than calling the three public selectors, which
;; each scan and cap) gives the pre-cap totals needed for the "+N more"
;; footers for free.
(defun org-drill-statistics--card-link (heading pos)
"Return an org bracket link to a drill card heading.
HEADING is the card's outline heading string, used as the link
description. POS is the integer buffer position of the heading, carried
in the link path so the dashboard's RET handler can jump to the card.
The path has the form \"org-drill-card:POS\". Any closing bracket in
HEADING is replaced so a literal \"]]\" cannot terminate the link early.
An empty or nil HEADING falls back to a position-based description."
(let* ((desc (if (and heading (not (string-empty-p heading)))
heading
(format "card at %d" pos)))
(safe (replace-regexp-in-string "]" "}" desc)))
(format "[[org-drill-card:%d][%s]]" pos safe)))
(defun org-drill-statistics--render-attention-table
(title rows total empty-note)
"Render one needs-attention subsection as an org string.
TITLE is the third-level heading text for the subsection. ROWS is a
list of (HEADING . POS) cons cells, already capped at
`org-drill-statistics-attention-row-limit' and already sorted. TOTAL is
the full count of matching cards before capping, used for the
\"+N more\" footer. EMPTY-NOTE is the line shown when ROWS is empty.
The returned string ends with a trailing newline. When ROWS is
non-empty it contains a single-column org table of card links, followed
by a \"+N more\" footer line naming the hidden count when TOTAL exceeds
the number of rows shown."
(let ((shown (length rows)))
(concat
(format "*** %s\n" title)
(if (null rows)
(format "%s\n" empty-note)
(concat
"| Card |\n"
"|------|\n"
(mapconcat
(lambda (row)
(format "| %s |"
(org-drill-statistics--card-link (car row) (cdr row))))
rows
"\n")
"\n"
(let ((hidden (- total shown)))
(if (> hidden 0)
(format "+%d more\n" hidden)
"")))))))
(defun org-drill-statistics--render-attention (&optional scope)
"Return the needs-attention dashboard section as an org string.
SCOPE bounds the drill traversal and accepts the same values as
`org-drill-scope'; nil uses the current value. This must run inside an
org buffer because it scans drill entries via
`org-drill-statistics--collect-attention-data'.
The section opens with a \"** Needs attention\" heading and contains
three subsections, each an org table of card links:
Leech candidates cards failing repeatedly with low average quality,
worst first.
Long overdue cards whose last review is well past the lapse
threshold, most overdue first.
Forgotten new cards added long ago and never repeated, oldest
first.
Each table is capped at `org-drill-statistics-attention-row-limit' rows;
a category with more matches than the cap gains a \"+N more\" footer
naming the hidden count. A category with no matches shows a short note
instead of a table. This function returns a string and does not print,
switch buffers, or move point beyond the traversal itself.
A single collection pass feeds all three categories, so the full
pre-cap totals are available for the footers without rescanning."
(let* ((data (org-drill-statistics--collect-attention-data scope))
(leech-all (cl-remove-if-not
#'org-drill-statistics--leech-candidate-p data))
(overdue-all (cl-remove-if-not
#'org-drill-statistics--long-overdue-p data))
(new-all (cl-remove-if-not
#'org-drill-statistics--forgotten-new-p data))
(leech-sorted
(sort (copy-sequence leech-all)
(lambda (a b)
(let ((qa (org-drill-statistics--ad-avg a))
(qb (org-drill-statistics--ad-avg b)))
(if (= qa qb)
(> (org-drill-statistics--ad-fails a)
(org-drill-statistics--ad-fails b))
(< qa qb))))))
(overdue-sorted
(sort (copy-sequence overdue-all)
(lambda (a b)
(> (org-drill-statistics--ad-review a)
(org-drill-statistics--ad-review b)))))
(new-sorted
(sort (copy-sequence new-all)
(lambda (a b)
(> (org-drill-statistics--ad-added a)
(org-drill-statistics--ad-added b)))))
(to-rows
(lambda (structs)
(mapcar
(lambda (d)
(cons (org-drill-statistics--entry-attention-data-heading d)
(org-drill-statistics--entry-attention-data-pos d)))
(org-drill-statistics--cap-rows structs)))))
(concat
"** Needs attention\n"
(org-drill-statistics--render-attention-table
"Leech candidates"
(funcall to-rows leech-sorted)
(length leech-all)
"No leech candidates.")
(org-drill-statistics--render-attention-table
"Long overdue"
(funcall to-rows overdue-sorted)
(length overdue-all)
"No long-overdue cards.")
(org-drill-statistics--render-attention-table
"Forgotten new"
(funcall to-rows new-sorted)
(length new-all)
"No forgotten-new cards."))))
;;; Statistics dashboard: overview section renderer (render 1/5).
;;
;; Pure string builder. It reads card-population counts via
;; `org-drill-statistics--overview-counts' (which traverses the org
;; entries in scope) and the newest session record from LOG, then
;; returns the section text. It never prints, switches buffers, or
;; mutates state, so it is testable with a plain string assertion.
(defun org-drill-statistics--format-last-session (record)
"Return the one-line \"Last session\" recap string for RECORD.
RECORD is an `org-drill-session-record', or nil when no session has
been logged. When nil, return a line stating there is no session yet.
Otherwise the line names the session date, its duration in minutes, the
number of cards reviewed (length of the qualities vector), and the
pass percentage. Pure: it reads only RECORD's slots."
(if (null record)
"Last session: none recorded yet."
(let* ((start (org-drill-session-record-start-time record))
(end (org-drill-session-record-end-time record))
(qualities (org-drill-session-record-qualities record))
(reviewed (if qualities (length qualities) 0))
(duration-min (max 0 (round (/ (- end start) 60.0))))
(pass (org-drill-session-record-pass-percent record))
(date (format-time-string "%Y-%m-%d" (seconds-to-time start))))
(format
"Last session: %s, %d min, %d card%s reviewed, %d%% pass."
date duration-min reviewed (if (= reviewed 1) "" "s") pass))))
(defun org-drill-statistics--render-overview (&optional scope log)
"Return the Overview section of the statistics dashboard as a string.
SCOPE is passed to `org-drill-statistics--overview-counts' to bound the
card-population traversal; nil uses the current `org-drill-scope'. LOG
is a list of `org-drill-session-record' newest first, defaulting to
`org-drill-session-log'; its head supplies the \"Last session\" recap.
The returned string is an org fragment: a \"** Overview\" subheading, a
4-column org table with a header row (Total cards, New, Mature, Lapsed)
and one data row from `org-drill-statistics--overview-counts', then a
blank line and the one-line last-session recap. This function is pure
with respect to its inputs apart from the org traversal that
`org-drill-statistics--overview-counts' performs over SCOPE; it does
not print, switch buffers, or modify any state."
(let* ((log (or log org-drill-session-log))
(counts (org-drill-statistics--overview-counts scope))
(total (plist-get counts :total))
(new (plist-get counts :new))
(mature (plist-get counts :mature))
(lapsed (plist-get counts :lapsed))
(recap (org-drill-statistics--format-last-session (car log))))
(concat
"** Overview\n"
"| Total cards | New | Mature | Lapsed |\n"
"|-------------+-----+--------+--------|\n"
(format "| %d | %d | %d | %d |\n" total new mature lapsed)
"\n"
recap "\n")))
;;; Statistics dashboard: trends render helper (render 2/5).
;;
;; `org-drill-statistics--render-trends' returns the Trends section as a
;; string: a subheading, two sparkline lines (reviews per day and pass
;; rate per day over the trend window), then an org table of the last 12
;; weekly aggregates. It is pure: it reads only LOG and the dashboard
;; defcustoms, calls the aggregation helpers, and returns a string. It
;; never prints, inserts, or switches buffers, so it stays unit testable.
(defun org-drill-statistics--format-week-start (day)
"Format absolute DAY number as a YYYY-MM-DD date string.
DAY is an absolute day number on the `time-to-days' scale, as stored in
the :week-start slot of `org-drill-statistics--weekly-aggregates'. The
result is the Gregorian calendar date of that day, zero-padded. Pure
integer-to-string conversion via `calendar-gregorian-from-absolute', so
it is timezone independent and deterministic."
(let ((gregorian (calendar-gregorian-from-absolute day)))
(format "%04d-%02d-%02d"
(nth 2 gregorian)
(nth 0 gregorian)
(nth 1 gregorian))))
(defun org-drill-statistics--render-weekly-table (weekly)
"Render WEEKLY aggregates as an org-mode table string.
WEEKLY is a list of plists as returned by
`org-drill-statistics--weekly-aggregates', oldest week first. The table
has a header row, an org separator row, and one body row per week with
columns: Week (the Monday date), Reviews, Pass %, and Avg min (mean
session duration in minutes, one decimal place). The returned string
ends with a trailing newline. Pure string assembly: no buffer or org
table alignment is performed, so columns are single-space padded inside
the pipes and org realigns them when the buffer renders."
(let ((rows
(mapcar
(lambda (week)
(format "| %s | %d | %d | %.1f |"
(org-drill-statistics--format-week-start
(plist-get week :week-start))
(plist-get week :reviews)
(plist-get week :pass-percent)
(plist-get week :avg-duration-min)))
weekly)))
(concat "| Week | Reviews | Pass % | Avg min |\n"
"|------+---------+--------+---------|\n"
(if rows
(concat (mapconcat #'identity rows "\n") "\n")
""))))
(defun org-drill-statistics--render-trends (log &optional algorithm)
"Return the Trends dashboard section for LOG as a string.
LOG is a list of `org-drill-session-record', newest first as stored in
`org-drill-session-log'. ALGORITHM, when non-nil, restricts the section
to records whose algorithm slot is `eq' to it, via
`org-drill-statistics--filter-log'; nil includes every algorithm.
The section is a string containing, in order:
- an \"* Trends\" org subheading,
- a reviews-per-day sparkline over the last
`org-drill-statistics-trend-days' days, labelled with the window,
- a pass-rate-per-day sparkline over the same window,
- an org table of the last 12 weekly aggregates (week-start date,
reviews, pass percentage, mean session duration in minutes).
The sparklines come from `org-drill-statistics--sparkline' fed by
`org-drill-statistics--reviews-by-day' and
`org-drill-statistics--pass-rate-by-day'; the pass-rate sparkline is
scaled against a fixed ceiling of 100 so the glyph heights read as
absolute percentages rather than relative to the window's own peak. The
table comes from `org-drill-statistics--weekly-aggregates'.
This function is pure: it reads only LOG and the dashboard defcustoms,
returns a string, and does not print, insert, or switch buffers."
(let* ((filtered (org-drill-statistics--filter-log log algorithm))
(days org-drill-statistics-trend-days)
(reviews (org-drill-statistics--reviews-by-day filtered days))
(pass-rates (org-drill-statistics--pass-rate-by-day filtered days))
(weekly (org-drill-statistics--weekly-aggregates filtered 12)))
(concat
"* Trends\n"
(format "Reviews/day (last %d): %s\n"
days
(org-drill-statistics--sparkline reviews))
(format "Pass rate/day (last %d): %s\n"
days
(org-drill-statistics--sparkline pass-rates 100))
"\n"
(org-drill-statistics--render-weekly-table weekly))))
;;; Statistics dashboard: forecast section renderer (render 5/5).
;;
;; Pure string builder for the dashboard's Forecast section. It calls
;; `org-drill-statistics--forecast' for the per-day counts and lays them
;; out as a one-line org table: a header row labelling each upcoming day
;; (Today, +1, +2, ...) and a counts row beneath it. The function does
;; no printing and switches no buffers, so it is unit testable by
;; asserting on the returned string.
(defun org-drill-statistics--render-forecast (&optional scope days)
"Return the Forecast dashboard section as an org-formatted string.
SCOPE accepts the same values as `org-drill-scope'; nil uses the current
value. DAYS is the number of forecast buckets and defaults to
`org-drill-statistics-forecast-days'. Reading SCOPE walks org entries,
so this must run inside an org buffer when SCOPE resolves to live cards.
The returned string starts with a \"** Forecast\" subheading, then a
one-line org table. The table's header row labels each upcoming day:
the first column is \"Today\", and each later column is \"+N\" for N days
out. The counts row beneath holds the number of cards due on each day,
taken from `org-drill-statistics--forecast'. A trailing newline ends
the section so sections concatenate cleanly.
When DAYS resolves to zero or fewer the forecast is empty; the section
then carries a short note in place of the table."
(let* ((counts (org-drill-statistics--forecast scope days))
(n (length counts)))
(if (zerop n)
(concat "** Forecast\n"
"No forecast window configured.\n")
(let* ((labels (cons "Today"
(mapcar (lambda (i) (format "+%d" i))
(number-sequence 1 (1- n)))))
(header (concat "| " (mapconcat #'identity labels " | ") " |"))
(values (concat "| "
(mapconcat (lambda (c) (format "%d" c))
counts " | ")
" |")))
(concat "** Forecast\n"
header "\n"
values "\n")))))
;;; Statistics dashboard: buffer-wide filter state and the UI shell.
;;
;; The dashboard is one read-only org-mode buffer assembled from a
;; filter header line plus the five `org-drill-statistics--render-*'
;; section strings. A single buffer-wide filter (scope, range,
;; algorithm) drives every section; the cycle commands rotate one filter
;; dimension and re-render in place.
;;
;; Integration seam with the render helpers: the renderers read the
;; three buffer-local filter variables below.
;; `org-drill-statistics--render-all'
;; binds the resolved log and scope/algorithm into those vars before
;; calling the renderers, so each renderer sees a consistent window. See
;; the integration notes for the exact contract each renderer is expected
;; to honor.
(declare-function org-drill-statistics--render-overview "org-drill")
(declare-function org-drill-statistics--render-trends "org-drill")
(declare-function org-drill-statistics--render-distribution "org-drill")
(declare-function org-drill-statistics--render-attention "org-drill")
(declare-function org-drill-statistics--render-forecast "org-drill")
(defconst org-drill-statistics--buffer-name "*Org Drill Statistics*"
"Name of the read-only buffer holding the statistics dashboard.")
(defvar org-drill-statistics-range-presets
'(("last 90d" . 90)
("last 30d" . 30)
("last 7d" . 7)
("all time" . nil))
"Range filter presets for the dashboard, in cycle order.
Each element is a cons (LABEL . DAYS). LABEL is the human string shown
in the header line. DAYS is the size of the trailing window in days, or
nil for the whole log. The first element is the default range and the
cycle wraps back to it after the last.")
(defvar-local org-drill-statistics--scope nil
"Buffer-local active scope filter for the dashboard.
A value understood by `org-drill-current-scope', or nil to use the
current `org-drill-scope'. Set by `org-drill-statistics' and rotated by
`org-drill-statistics-cycle-scope'. Read by the render helpers to bound
their org traversal.")
(defvar-local org-drill-statistics--range "last 90d"
"Buffer-local active range-filter label for the dashboard.
One of the LABEL strings in `org-drill-statistics-range-presets'. Set
by `org-drill-statistics' and rotated by
`org-drill-statistics-cycle-range'. The matching DAYS value bounds the
session log the trend and distribution sections aggregate over.")
(defvar-local org-drill-statistics--algorithm nil
"Buffer-local active algorithm filter for the dashboard.
A drill algorithm symbol to restrict the session log to, or nil for all
algorithms. Set by `org-drill-statistics' and rotated by
`org-drill-statistics-cycle-algorithm'. Read by the render helpers via
the filtered log they are handed.")
(defun org-drill-statistics--range-cutoff-float (label)
"Return the float-time cutoff for range LABEL, or nil for all time.
LABEL is a key in `org-drill-statistics-range-presets'. When its DAYS
value is non-nil, return the `float-time' of the instant DAYS days before
now, so records at or after the cutoff fall in the window. When DAYS is
nil, or LABEL is unknown, return nil meaning no lower bound."
(let ((days (cdr (assoc label org-drill-statistics-range-presets))))
(when days
(- (float-time) (* days 86400.0)))))
(defun org-drill-statistics--filtered-log (range-label algorithm)
"Return `org-drill-session-log' narrowed by RANGE-LABEL and ALGORITHM.
RANGE-LABEL is a key in `org-drill-statistics-range-presets'; its DAYS
value bounds the trailing window, and a nil DAYS keeps the whole log.
ALGORITHM is an algorithm symbol to keep, or nil for all algorithms.
The original log is not modified."
(let* ((by-algo (org-drill-statistics--filter-log
org-drill-session-log algorithm))
(cutoff (org-drill-statistics--range-cutoff-float range-label)))
(if cutoff
(org-drill-statistics--log-since by-algo cutoff)
by-algo)))
(defun org-drill-statistics--header-line (scope range algorithm)
"Return the dashboard filter header line for SCOPE, RANGE and ALGORITHM.
SCOPE is the active scope value, or nil for the current `org-drill-scope'.
RANGE is a range-preset label string. ALGORITHM is an algorithm symbol,
or nil for all algorithms. The result is a single line summarizing the
three active filters, matching the spec's header format."
(format "Scope: %s Range: %s Algorithm: %s"
(or scope org-drill-scope)
range
(or algorithm "all")))
(defun org-drill-statistics--render-all (scope range algorithm)
"Assemble the full dashboard body string for the active filters.
SCOPE, RANGE and ALGORITHM are the resolved filter values. This binds
the resolved filtered log and the scope/algorithm into the buffer-local
filter variables so each `org-drill-statistics--render-*' helper reads a
consistent window, then concatenates the header line and the five
section strings. The log and card scans run while these bindings are in
effect. Returns the assembled buffer contents as a string."
(let* ((org-drill-statistics--scope scope)
(org-drill-statistics--range range)
(org-drill-statistics--algorithm algorithm)
(log (org-drill-statistics--filtered-log range algorithm))
(hist (org-drill-statistics--quality-histogram log)))
(concat
(org-drill-statistics--header-line scope range algorithm)
"\n\n"
(org-drill-statistics--render-overview scope log)
"\n"
;; LOG is already algorithm-filtered, so pass nil to avoid
;; filtering a second time.
(org-drill-statistics--render-trends log nil)
"\n"
(org-drill-statistics--render-distribution hist)
"\n"
(org-drill-statistics--render-attention scope)
"\n"
(org-drill-statistics--render-forecast scope))))
(defvar org-drill-statistics-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "q") #'quit-window)
(define-key map (kbd "g") #'org-drill-statistics-refresh)
(define-key map (kbd "e") #'org-drill-statistics-export-csv)
(define-key map (kbd "s") #'org-drill-statistics-cycle-scope)
(define-key map (kbd "r") #'org-drill-statistics-cycle-range)
(define-key map (kbd "a") #'org-drill-statistics-cycle-algorithm)
(define-key map (kbd "RET") #'org-open-at-point)
map)
"Keymap for `org-drill-statistics-mode'.
Bindings: q bury, g refresh, e export CSV, s cycle scope, r cycle range,
a cycle algorithm, RET follow the card link at point. These avoid the
read-only org-mode bindings the dashboard relies on for link following
and navigation.")
(define-minor-mode org-drill-statistics-mode
"Minor mode active in the org-drill statistics dashboard buffer.
Installs `org-drill-statistics-mode-map' so q, g, e, s, r, a and RET
drive the dashboard. Enabled by `org-drill-statistics' after the buffer
is put in `org-mode'; not intended to be toggled by hand."
:lighter " OrgDrillStats"
:keymap org-drill-statistics-mode-map)
(defun org-drill-statistics--render (buffer scope range algorithm)
"Render the dashboard for SCOPE, RANGE, ALGORITHM into BUFFER.
BUFFER is the target buffer. Its read-only state is lifted for the
write, the assembled body replaces the contents, point returns to the
top, and the buffer is left read-only. The three filter values are
stored buffer-locally so refresh and the cycle commands can read and
rotate them. Returns BUFFER."
(with-current-buffer buffer
(let ((inhibit-read-only t)
(body (org-drill-statistics--render-all scope range algorithm)))
(erase-buffer)
(insert body)
(goto-char (point-min)))
(setq org-drill-statistics--scope scope
org-drill-statistics--range range
org-drill-statistics--algorithm algorithm)
(setq buffer-read-only t)
buffer))
;;;###autoload
(defun org-drill-statistics ()
"Open the org-drill statistics dashboard.
Builds the read-only org-mode buffer `*Org Drill Statistics*' from the
filter header and the five render sections, using the current
`org-drill-scope', the default range, and all algorithms, then switches
to it. Refresh and the s/r/a cycle commands re-render in place."
(interactive)
(let ((buffer (get-buffer-create org-drill-statistics--buffer-name))
(scope org-drill-scope)
(range (caar org-drill-statistics-range-presets))
(algorithm nil))
(with-current-buffer buffer
(org-mode)
(org-drill-statistics-mode 1))
(org-drill-statistics--render buffer scope range algorithm)
(switch-to-buffer buffer)
buffer))
(defun org-drill-statistics-refresh ()
"Re-render the dashboard in place, preserving the active filters.
Reads the buffer-local scope, range, and algorithm filters and rebuilds
every section against the current log and card state. Bound to g."
(interactive)
(org-drill-statistics--render
(current-buffer)
org-drill-statistics--scope
org-drill-statistics--range
org-drill-statistics--algorithm))
(defun org-drill-statistics-cycle-scope ()
"Cycle the dashboard scope filter and refresh.
Rotates through a small fixed set of common scopes, then re-renders.
Bound to s."
(interactive)
(let* ((scopes '(file directory agenda agenda-with-archives))
(current org-drill-statistics--scope)
(tail (cdr (member current scopes)))
(next (or (car tail) (car scopes))))
(setq org-drill-statistics--scope next)
(org-drill-statistics-refresh)
(message "Scope: %s" next)))
(defun org-drill-statistics-cycle-range ()
"Cycle the dashboard range filter and refresh.
Rotates through `org-drill-statistics-range-presets' in order, wrapping
after the last preset, then re-renders. Bound to r."
(interactive)
(let* ((labels (mapcar #'car org-drill-statistics-range-presets))
(current org-drill-statistics--range)
(tail (cdr (member current labels)))
(next (or (car tail) (car labels))))
(setq org-drill-statistics--range next)
(org-drill-statistics-refresh)
(message "Range: %s" next)))
(defun org-drill-statistics-cycle-algorithm ()
"Cycle the dashboard algorithm filter and refresh.
Rotates through every algorithm symbol seen in `org-drill-session-log'
plus an all-algorithms state (nil), then re-renders. When the log has
no records the filter stays at all-algorithms. Bound to a."
(interactive)
(let* ((algorithms
(delete-dups
(delq nil
(mapcar #'org-drill-session-record-algorithm
org-drill-session-log))))
;; nil (all) leads the cycle, then each known algorithm.
(states (cons nil algorithms))
(current org-drill-statistics--algorithm)
(tail (cdr (member current states)))
(next (if (member current states)
(car tail)
nil)))
(setq org-drill-statistics--algorithm next)
(org-drill-statistics-refresh)
(message "Algorithm: %s" (or next "all"))))
;;; CSV export for the statistics dashboard (step 3).
;;
;; The row builders are pure: they turn the session log and the scope's
;; drill entries into lists of field strings. `--write-csv' renders those
;; through `--csv-row' and writes a file. The command threads the active
;; dashboard filters (scope, range, algorithm) into all three views.
(defun org-drill-statistics--csv-quote (field)
"Return FIELD as a CSV field string, quoted when required (RFC 4180).
FIELD is coerced to a string with `format' when it is not already one.
It is wrapped in double quotes when it contains a comma, a double quote,
a carriage return, or a newline, and any embedded double quote is doubled."
(let ((s (if (stringp field) field (format "%s" field))))
(if (string-match-p "[\",\r\n]" s)
(concat "\"" (replace-regexp-in-string "\"" "\"\"" s) "\"")
s)))
(defun org-drill-statistics--csv-row (fields)
"Join FIELDS into one CSV record line, quoting each field as needed.
FIELDS is a list; each element passes through `org-drill-statistics--csv-quote'.
No trailing newline is added."
(mapconcat #'org-drill-statistics--csv-quote fields ","))
(defun org-drill-statistics--write-csv (path header rows)
"Write HEADER then ROWS as CSV to PATH.
HEADER is a list of column names. ROWS is a list of field-lists. Each is
rendered with `org-drill-statistics--csv-row' and terminated by a newline."
(with-temp-file path
(insert (org-drill-statistics--csv-row header) "\n")
(dolist (row rows)
(insert (org-drill-statistics--csv-row row) "\n"))))
(defconst org-drill-statistics--sessions-csv-header
'("start" "end" "scope" "algorithm" "qualities" "pass_percent"
"new" "mature" "failed" "cram")
"Column header for sessions.csv, tracking the session-record slots.")
(defun org-drill-statistics--session-row (record)
"Return RECORD as a list of CSV field strings for sessions.csv.
RECORD is an `org-drill-session-record'. Start and end times render as
local \"YYYY-MM-DD HH:MM:SS\"; the qualities vector renders space-joined."
(list
(format-time-string
"%Y-%m-%d %H:%M:%S"
(seconds-to-time (org-drill-session-record-start-time record)))
(format-time-string
"%Y-%m-%d %H:%M:%S"
(seconds-to-time (org-drill-session-record-end-time record)))
(format "%s" (org-drill-session-record-scope record))
(format "%s" (org-drill-session-record-algorithm record))
(mapconcat #'number-to-string
(append (or (org-drill-session-record-qualities record) []) nil)
" ")
(number-to-string (org-drill-session-record-pass-percent record))
(number-to-string (org-drill-session-record-new-count record))
(number-to-string (org-drill-session-record-mature-count record))
(number-to-string (org-drill-session-record-failed-count record))
(if (org-drill-session-record-cram-mode record) "t" "nil")))
(defun org-drill-statistics--sessions-rows (log)
"Return one CSV field-list per record in LOG, preserving LOG order."
(mapcar #'org-drill-statistics--session-row log))
(defconst org-drill-statistics--cards-csv-header
'("heading" "last_interval" "repeats_since_fail" "total_repeats"
"failure_count" "average_quality" "ease" "last_quality"
"last_reviewed" "status")
"Column header for cards.csv.")
(defun org-drill-statistics--card-row (session)
"Return the drill entry at point as a list of CSV fields for cards.csv.
SESSION is a transient `org-drill-session' used only to classify status.
A missing property renders as the empty string."
(list
(org-get-heading t t t t)
(or (org-entry-get (point) "DRILL_LAST_INTERVAL") "")
(or (org-entry-get (point) "DRILL_REPEATS_SINCE_FAIL") "")
(or (org-entry-get (point) "DRILL_TOTAL_REPEATS") "")
(or (org-entry-get (point) "DRILL_FAILURE_COUNT") "")
(or (org-entry-get (point) "DRILL_AVERAGE_QUALITY") "")
(or (org-entry-get (point) "DRILL_EASE") "")
(or (org-entry-get (point) "DRILL_LAST_QUALITY") "")
(or (org-entry-get (point) "DRILL_LAST_REVIEWED") "")
(format "%s" (or (car (org-drill-entry-status session)) ""))))
(defun org-drill-statistics--cards-rows (&optional scope)
"Return one CSV field-list per drill entry in SCOPE for cards.csv.
SCOPE is a value understood by `org-drill-current-scope'; nil uses the
current `org-drill-scope'. A fresh non-cram session classifies status."
(let ((session (org-drill-session))
(rows nil))
(org-drill-map-entries
(lambda () (push (org-drill-statistics--card-row session) rows))
scope)
(nreverse rows)))
(defconst org-drill-statistics--daily-csv-header
'("date" "reviews" "passes" "fails" "pass_percent" "duration_min")
"Column header for daily.csv.")
(defun org-drill-statistics--daily-rows (log &optional days)
"Return one CSV field-list per day over the last DAYS for daily.csv.
LOG is a list of `org-drill-session-record'. DAYS defaults to
`org-drill-statistics-trend-days'; a non-positive value is clamped to 1.
Rows are oldest-first, one per day, the final row being today. Each row
is (DATE REVIEWS PASSES FAILS PASS_PERCENT DURATION_MIN). A pass is a
quality above `org-drill-failure-quality'. Empty days report zeros."
(let* ((days (max 1 (or days org-drill-statistics-trend-days)))
(today (org-drill-statistics--today-day))
(start-day (- today (1- days)))
(reviews (make-vector days 0))
(passes (make-vector days 0))
(fails (make-vector days 0))
(duration (make-vector days 0.0)))
(dolist (record log)
(let* ((day (org-drill-statistics--record-day record))
(idx (- day start-day)))
(when (and (>= idx 0) (< idx days))
(let ((qs (org-drill-session-record-qualities record)))
(when qs
(mapc
(lambda (q)
(aset reviews idx (1+ (aref reviews idx)))
(if (> q org-drill-failure-quality)
(aset passes idx (1+ (aref passes idx)))
(aset fails idx (1+ (aref fails idx)))))
qs)))
(aset duration idx
(+ (aref duration idx)
(/ (- (org-drill-session-record-end-time record)
(org-drill-session-record-start-time record))
60.0))))))
(let ((rows nil))
(dotimes (i days)
(let* ((g (calendar-gregorian-from-absolute (+ start-day i)))
(date (format "%04d-%02d-%02d" (nth 2 g) (nth 0 g) (nth 1 g)))
(r (aref reviews i)))
(push (list date
(number-to-string r)
(number-to-string (aref passes i))
(number-to-string (aref fails i))
(number-to-string
(if (> r 0)
(round (* 100.0 (/ (float (aref passes i)) r)))
0))
(number-to-string (round (aref duration i))))
rows)))
(nreverse rows))))
;;;###autoload
(defun org-drill-statistics-export-csv (directory)
"Export the statistics views to CSV files in DIRECTORY.
Writes sessions.csv (one row per session record), cards.csv (one row per
drill entry in the active scope), and daily.csv (one row per day in the
active range). When called from the dashboard buffer the active
scope/range/algorithm filters apply; otherwise the buffer-local defaults
do. Interactively, prompts for DIRECTORY."
(interactive
(list (read-directory-name "Export statistics CSV to directory: ")))
(let* ((scope org-drill-statistics--scope)
(range (or org-drill-statistics--range
(caar org-drill-statistics-range-presets)))
(algorithm org-drill-statistics--algorithm)
(log (org-drill-statistics--filtered-log range algorithm))
(days (cdr (assoc range org-drill-statistics-range-presets))))
(make-directory directory t)
(org-drill-statistics--write-csv
(expand-file-name "sessions.csv" directory)
org-drill-statistics--sessions-csv-header
(org-drill-statistics--sessions-rows log))
(org-drill-statistics--write-csv
(expand-file-name "cards.csv" directory)
org-drill-statistics--cards-csv-header
(org-drill-statistics--cards-rows scope))
(org-drill-statistics--write-csv
(expand-file-name "daily.csv" directory)
org-drill-statistics--daily-csv-header
(org-drill-statistics--daily-rows log days))
(when (called-interactively-p 'interactive)
(message "Exported sessions.csv, cards.csv, daily.csv to %s" directory))
directory))
(provide 'org-drill)
;;; org-drill.el ends here
|