Source

org-drill / org-drill.el

Full commit
   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
;;; org-drill.el - Self-testing using spaced repetition
;;;
;;; Author: Paul Sexton <eeeickythump@gmail.com>
;;; Version: 2.3
;;; Repository at http://bitbucket.org/eeeickythump/org-drill/
;;;
;;;
;;; Synopsis
;;; ========
;;;
;;; Uses the SuperMemo spaced repetition algorithms to conduct interactive
;;; "drill sessions", where the material to be remembered 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 drilled.
;;;
;;; 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.


(eval-when-compile (require 'cl))
(eval-when-compile (require 'hi-lock))
(require 'org)
(require 'org-id)
(require 'org-learn)


(defgroup org-drill nil
  "Options concerning interactive drill sessions in Org mode (org-drill)."
  :tag "Org-Drill"
  :group 'org-link)



(defcustom org-drill-question-tag
  "drill"
  "Tag which topics must possess in order to be identified as review topics
by `org-drill'."
  :group 'org-drill
  :type 'string)


(defcustom org-drill-maximum-items-per-session
  30
  "Each drill session will present at most this many topics for review.
Nil means unlimited."
  :group 'org-drill
  :type '(choice integer (const nil)))



(defcustom org-drill-maximum-duration
  20
  "Maximum duration of a drill session, in minutes.
Nil means unlimited."
  :group 'org-drill
  :type '(choice integer (const nil)))


(defcustom org-drill-failure-quality
  2
  "If the quality of recall for an item is this number or lower,
it is regarded as an unambiguous failure, and the repetition
interval for the card is reset to 0 days.  If the quality is higher
than this number, it is regarded as successfully recalled, but the
time interval to the next repetition will be lowered if the quality
was near to a fail.

By default this is 2, for SuperMemo-like behaviour. For
Mnemosyne-like behaviour, set it to 1.  Other values are not
really sensible."
  :group 'org-drill
  :type '(choice (const 2) (const 1)))


(defcustom org-drill-forgetting-index
  10
  "What percentage of items do you consider it is 'acceptable' to
forget each drill session? The default is 10%. A warning message
is displayed at the end of the session if the percentage forgotten
climbs above this number."
  :group 'org-drill
  :type 'integer)


(defcustom org-drill-leech-failure-threshold
  15
  "If an item is forgotten more than this many times, it is tagged
as a 'leech' item."
  :group 'org-drill
  :type '(choice integer (const nil)))


(defcustom org-drill-leech-method
  'skip
  "How should 'leech items' be handled during drill sessions?
Possible values:
- nil :: Leech items are treated the same as normal items.
- skip :: Leech items are not included in drill sessions.
- warn :: Leech items are still included in drill sessions,
  but a warning message is printed when each leech item is
  presented."
  :group 'org-drill
  :type '(choice (const 'warn) (const 'skip) (const nil)))


(defface org-drill-visible-cloze-face
  '((t (:foreground "darkseagreen")))
  "The face used to hide the contents of cloze phrases."
  :group 'org-drill)


(defface org-drill-visible-cloze-hint-face
  '((t (:foreground "dark slate blue")))
  "The face used to hide the contents of cloze phrases."
  :group 'org-drill)


(defface org-drill-hidden-cloze-face
  '((t (:foreground "deep sky blue" :background "blue")))
  "The face used to hide the contents of cloze phrases."
  :group 'org-drill)


(defcustom org-drill-use-visible-cloze-face-p
  nil
  "Use a special face to highlight cloze-deleted text in org mode
buffers?"
  :group 'org-drill
  :type 'boolean)


(defcustom org-drill-hide-item-headings-p
  nil
  "Conceal the contents of the main heading of each item during drill
sessions? You may want to enable this behaviour if item headings or tags
contain information that could 'give away' the answer."
  :group 'org-drill
  :type 'boolean)


(defcustom org-drill-new-count-color
  "royal blue"
  "Foreground colour used to display the count of remaining new items
during a drill session."
  :group 'org-drill
  :type 'color)

(defcustom org-drill-mature-count-color
  "green"
  "Foreground colour used to display the count of remaining mature items
during a drill session. Mature items are due for review, but are not new."
  :group 'org-drill
  :type 'color)

(defcustom org-drill-failed-count-color
  "red"
  "Foreground colour used to display the count of remaining failed items
during a drill session."
  :group 'org-drill
  :type 'color)

(defcustom org-drill-done-count-color
  "sienna"
  "Foreground colour used to display the count of reviewed items
during a drill session."
  :group 'org-drill
  :type 'color)


(setplist 'org-drill-cloze-overlay-defaults
          '(display "[...]"
                    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-cloze-regexp
  ;; ver 1   "[^][]\\(\\[[^][][^]]*\\]\\)"
  ;; ver 2   "\\(\\[.*?\\]\\|^[^[[:cntrl:]]*?\\]\\|\\[.*?$\\)"
  ;; ver 3!  "\\(\\[.*?\\]\\|\\[.*?[[:cntrl:]]+.*?\\]\\)"
  "\\(\\[[[:cntrl:][:graph:][:space:]]*?\\)\\(\\||.+?\\)\\(\\]\\)")


(defvar org-drill-cloze-keywords
  `((,org-drill-cloze-regexp
     (1 'org-drill-visible-cloze-face nil)
     (2 'org-drill-visible-cloze-hint-face t)
     (3 'org-drill-visible-cloze-face nil)
     )))


(defcustom org-drill-card-type-alist
  '((nil . org-drill-present-simple-card)
    ("simple" . org-drill-present-simple-card)
    ("twosided" . org-drill-present-two-sided-card)
    ("multisided" . org-drill-present-multi-sided-card)
    ("hide1cloze" . org-drill-present-multicloze-hide1)
    ("hide2cloze" . org-drill-present-multicloze-hide2)
    ("show1cloze" . org-drill-present-multicloze-show1)
    ("multicloze" . org-drill-present-multicloze-hide1)
    ("conjugate" org-drill-present-verb-conjugation
     org-drill-show-answer-verb-conjugation)
    ("spanish_verb" . org-drill-present-spanish-verb)
    ("translate_number" org-drill-present-translate-number
     org-drill-show-answer-translate-number))
  "Alist associating card types with presentation functions. Each entry in the
alist takes one of two forms:
1. (CARDTYPE . QUESTION-FN), where CARDTYPE is a string or nil (for default),
   and QUESTION-FN is a function which takes no arguments and returns a boolean
   value.
2. (CARDTYPE QUESTION-FN ANSWER-FN), where ANSWER-FN is a function that takes
   one argument -- the argument is a function that itself takes no arguments.
   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. (Its argument is a function that prompts the user and
   performs rescheduling)."
  :group 'org-drill
  :type '(alist :key-type (choice string (const nil)) :value-type function))


(defcustom org-drill-spaced-repetition-algorithm
  'sm5
  "Which SuperMemo spaced repetition algorithm to use for scheduling items.
Available choices are:
- SM2 :: the SM2 algorithm, used in SuperMemo 2.0
- SM5 :: the SM5 algorithm, used in SuperMemo 5.0
- Simple8 :: a modified version of the SM8 algorithm. SM8 is used in
  SuperMemo 98. The version implemented here is simplified in that while it
  'learns' the difficulty of each item using quality grades and number of
  failures, it does not modify the matrix of values that
  governs how fast the inter-repetition intervals increase. A method for
  adjusting intervals when items are reviewed early or late has been taken
  from SM11, a later version of the algorithm, and included in Simple8."
  :group 'org-drill
  :type '(choice (const 'sm2) (const 'sm5) (const 'simple8)))



(defcustom org-drill-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 (using the 'customize' facility) at the end of each
drill session.

Over time, values in the matrix will adapt to the individual user's
pace of learning."
  :group 'org-drill
  :type 'sexp)


(defcustom org-drill-add-random-noise-to-intervals-p
  nil
  "If true, the number of days until an item's next repetition
will vary slightly from the interval calculated by the SM2
algorithm. The variation is very small when the interval is
small, but scales up with the interval."
  :group 'org-drill
  :type 'boolean)


(defcustom org-drill-adjust-intervals-for-early-and-late-repetitions-p
  nil
  "If true, when the student successfully reviews an item 1 or more days
before or after the scheduled review date, this will affect that date of
the item's next scheduled review, according to the algorithm presented at
 [[http://www.supermemo.com/english/algsm11.htm#Advanced%20repetitions]].

Items that were reviewed early will have their next review date brought
forward. Those that were reviewed late will have their next review
date postponed further.

Note that this option currently has no effect if the SM2 algorithm
is used."
  :group 'org-drill
  :type 'boolean)


(defcustom org-drill-cram-hours
  12
  "When in cram mode, items are considered due for review if
they were reviewed at least this many hours ago."
  :group 'org-drill
  :type 'integer)


;;; NEW items have never been presented in a drill session before.
;;; MATURE items HAVE been presented at least once before.
;;; - YOUNG mature items were scheduled no more than
;;;   ORG-DRILL-DAYS-BEFORE-OLD days after their last
;;;   repetition. These items will have been learned 'recently' and will have a
;;;   low repetition count.
;;; - OLD mature items have intervals greater than
;;;   ORG-DRILL-DAYS-BEFORE-OLD.
;;; - OVERDUE items are past their scheduled review date by more than
;;;   LAST-INTERVAL * (ORG-DRILL-OVERDUE-INTERVAL-FACTOR - 1) days,
;;;   regardless of young/old status.


(defcustom org-drill-days-before-old
  10
  "When an item's inter-repetition interval rises above this value in days,
it is no longer considered a 'young' (recently learned) item."
  :group 'org-drill
  :type 'integer)


(defcustom org-drill-overdue-interval-factor
  1.2
  "An item is considered overdue if its scheduled review date is
more than (ORG-DRILL-OVERDUE-INTERVAL-FACTOR - 1) * LAST-INTERVAL
days in the past. For example, a value of 1.2 means an additional
20% of the last scheduled interval is allowed to elapse before
the item is overdue. A value of 1.0 means no extra time is
allowed at all - items are immediately considered overdue if
there is even one day's delay in reviewing them. This variable
should never be less than 1.0."
  :group 'org-drill
  :type 'float)


(defcustom org-drill-learn-fraction
  0.5
  "Fraction between 0 and 1 that governs how quickly the spaces
between successive repetitions increase, for all items. The
default value is 0.5. Higher values make spaces increase more
quickly with each successful repetition. You should only change
this in small increments (for example 0.05-0.1) as it has an
exponential effect on inter-repetition spacing."
  :group 'org-drill
  :type 'float)


(defvar *org-drill-session-qualities* nil)
(defvar *org-drill-start-time* 0)
(defvar *org-drill-new-entries* nil)
(defvar *org-drill-dormant-entry-count* 0)
(defvar *org-drill-due-entry-count* 0)
(defvar *org-drill-overdue-entry-count* 0)
(defvar *org-drill-due-tomorrow-count* 0)
(defvar *org-drill-current-entry-schedule-type* nil)
(defvar *org-drill-overdue-entries* nil
  "List of markers for items that are considered 'overdue', based on
the value of ORG-DRILL-OVERDUE-INTERVAL-FACTOR.")
(defvar *org-drill-young-mature-entries* nil
  "List of markers for mature entries whose last inter-repetition
interval was <= ORG-DRILL-DAYS-BEFORE-OLD days.")
(defvar *org-drill-old-mature-entries* nil
  "List of markers for mature entries whose last inter-repetition
interval was greater than ORG-DRILL-DAYS-BEFORE-OLD days.")
(defvar *org-drill-failed-entries* nil)
(defvar *org-drill-again-entries* nil)
(defvar *org-drill-done-entries* nil)
(defvar *org-drill-current-item* nil
  "Set to the marker for the item currently being tested.")
(defvar *org-drill-cram-mode* nil
  "Are we in 'cram mode', where all items are considered due
for review unless they were already reviewed in the recent past?")


;;; Make the above settings safe as file-local variables.


(put 'org-drill-question-tag 'safe-local-variable 'stringp)
(put 'org-drill-maximum-items-per-session 'safe-local-variable
     '(lambda (val) (or (integerp val) (null val))))
(put 'org-drill-maximum-duration 'safe-local-variable
     '(lambda (val) (or (integerp val) (null val))))
(put 'org-drill-failure-quality 'safe-local-variable 'integerp)
(put 'org-drill-forgetting-index 'safe-local-variable 'integerp)
(put 'org-drill-leech-failure-threshold 'safe-local-variable 'integerp)
(put 'org-drill-leech-method 'safe-local-variable
     '(lambda (val) (memq val '(nil skip warn))))
(put 'org-drill-use-visible-cloze-face-p 'safe-local-variable 'booleanp)
(put 'org-drill-hide-item-headings-p 'safe-local-variable 'booleanp)
(put 'org-drill-spaced-repetition-algorithm 'safe-local-variable
     '(lambda (val) (memq val '(simple8 sm5 sm2))))
(put 'org-drill-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)


;;;; Utilities ================================================================


(defun free-marker (m)
  (set-marker m nil))


(defmacro pop-random (place)
  (let ((idx (gensym)))
    `(if (null ,place)
         nil
       (let ((,idx (random* (length ,place))))
         (prog1 (nth ,idx ,place)
           (setq ,place (append (subseq ,place 0 ,idx)
                                (subseq ,place (1+ ,idx)))))))))


(defmacro push-end (val place)
  "Add VAL to the end of the sequence stored in PLACE. Return the new
value."
  `(setq ,place (append ,place (list ,val))))


(defun shuffle-list (list)
  "Randomly permute the elements of LIST (all permutations equally likely)."
  ;; Adapted from 'shuffle-vector' in cookie1.el
  (let ((i 0)
	j
	temp
	(len (length list)))
    (while (< i len)
      (setq j (+ i (random* (- len i))))
      (setq temp (nth i list))
      (setf (nth i list) (nth j list))
      (setf (nth j list) temp)
      (setq i (1+ i))))
  list)


(defun 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 time-to-inactive-org-timestamp (time)
  (format-time-string
   (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
   time))



(defmacro with-hidden-cloze-text (&rest body)
  `(progn
     (org-drill-hide-clozed-text)
     (unwind-protect
         (progn
           ,@body)
       (org-drill-unhide-clozed-text))))


(defmacro with-hidden-cloze-hints (&rest body)
  `(progn
     (org-drill-hide-cloze-hints)
     (unwind-protect
         (progn
           ,@body)
       (org-drill-unhide-text))))


(defmacro with-hidden-comments (&rest body)
  `(progn
     (if org-drill-hide-item-headings-p
         (org-drill-hide-heading-at-point))
     (org-drill-hide-comments)
     (unwind-protect
         (progn
           ,@body)
       (org-drill-unhide-text))))


(defun org-drill-days-since-last-review ()
  "Nil means a last review date has not yet been stored for
the item.
Zero means it was reviewed today.
A positive number means it was reviewed that many days ago.
A negative number means the date of last review is in the future --
this should never happen."
  (let ((datestr (org-entry-get (point) "DRILL_LAST_REVIEWED")))
    (when datestr
      (- (time-to-days (current-time))
         (time-to-days (apply 'encode-time
                              (org-parse-time-string datestr)))))))


(defun org-drill-hours-since-last-review ()
  "Like `org-drill-days-since-last-review', but return value is
in hours rather than days."
  (let ((datestr (org-entry-get (point) "DRILL_LAST_REVIEWED")))
    (when datestr
      (floor
       (/ (- (time-to-seconds (current-time))
             (time-to-seconds (apply 'encode-time
                                     (org-parse-time-string datestr))))
          (* 60 60))))))


(defun org-drill-entry-p (&optional marker)
  "Is MARKER, or the point, in a 'drill item'? This will return nil if
the point is inside a subheading of a drill item -- to handle that
situation use `org-part-of-drill-entry-p'."
  (save-excursion
    (when marker
      (org-drill-goto-entry marker))
    (member org-drill-question-tag (org-get-local-tags))))


(defun org-drill-goto-entry (marker)
  (switch-to-buffer (marker-buffer marker))
  (goto-char marker))


(defun org-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-at))))


(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-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-local-tags))))


;; (defun org-drill-entry-due-p ()
;;   (cond
;;    (*org-drill-cram-mode*
;;     (let ((hours (org-drill-hours-since-last-review)))
;;       (and (org-drill-entry-p)
;;            (or (null hours)
;;                (>= hours org-drill-cram-hours)))))
;;    (t
;;     (let ((item-time (org-get-scheduled-time (point))))
;;       (and (org-drill-entry-p)
;;            (or (not (eql 'skip org-drill-leech-method))
;;                (not (org-drill-entry-leech-p)))
;;            (or (null item-time)         ; not scheduled
;;                (not (minusp             ; scheduled for today/in past
;;                      (- (time-to-days (current-time))
;;                         (time-to-days item-time))))))))))


(defun org-drill-entry-days-overdue ()
  "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
   (*org-drill-cram-mode*
    (let ((hours (org-drill-hours-since-last-review)))
      (and (org-drill-entry-p)
           (or (null hours)
               (>= hours org-drill-cram-hours))
           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 (&optional days-overdue last-interval)
  "Returns true if entry that is scheduled DAYS-OVERDUE dasy in the past,
and whose last inter-repetition interval was LAST-INTERVAL, should be
considered 'overdue'. If the arguments are not given they are extracted
from the entry at point."
  (unless days-overdue
    (setq days-overdue (org-drill-entry-days-overdue)))
  (unless last-interval
    (setq last-interval (org-drill-entry-last-interval 1)))
  (and (numberp days-overdue)
       (> days-overdue 1)               ; enforce a sane minimum 'overdue' gap
       ;;(> due org-drill-days-before-overdue)
       (> (/ (+ days-overdue last-interval 1.0) last-interval)
          org-drill-overdue-interval-factor)))



(defun org-drill-entry-due-p ()
  (let ((due (org-drill-entry-days-overdue)))
    (and (not (null due))
         (not (minusp due)))))


(defun org-drill-entry-new-p ()
  (and (org-drill-entry-p)
       (let ((item-time (org-get-scheduled-time (point))))
         (null item-time))))


(defun org-drill-entry-last-quality (&optional default)
  (let ((quality (org-entry-get (point) "DRILL_LAST_QUALITY")))
    (if quality
        (string-to-number quality)
      default)))


(defun org-drill-entry-failure-count ()
  (let ((quality (org-entry-get (point) "DRILL_FAILURE_COUNT")))
    (if quality
        (string-to-number quality)
      0)))


(defun org-drill-entry-average-quality (&optional default)
  (let ((val (org-entry-get (point) "DRILL_AVERAGE_QUALITY")))
    (if val
        (string-to-number val)
      (or default nil))))

(defun org-drill-entry-last-interval (&optional default)
  (let ((val (org-entry-get (point) "DRILL_LAST_INTERVAL")))
    (if val
        (string-to-number val)
      (or default 0))))

(defun org-drill-entry-repeats-since-fail (&optional default)
  (let ((val (org-entry-get (point) "DRILL_REPEATS_SINCE_FAIL")))
    (if val
        (string-to-number val)
      (or default 0))))

(defun org-drill-entry-total-repeats (&optional default)
  (let ((val (org-entry-get (point) "DRILL_TOTAL_REPEATS")))
    (if val
        (string-to-number val)
      (or default 0))))

(defun org-drill-entry-ease (&optional default)
  (let ((val (org-entry-get (point) "DRILL_EASE")))
    (if val
        (string-to-number val)
      default)))


;;; From http://www.supermemo.com/english/ol/sm5.htm
(defun org-drill-random-dispersal-factor ()
  "Returns a random number between 0.5 and 1.5."
  (let ((a 0.047)
        (b 0.092)
        (p (- (random* 1.0) 0.5)))
    (flet ((sign (n)
                 (cond ((zerop n) 0)
                       ((plusp n) 1)
                       (t -1))))
      (/ (+ 100 (* (* (/ -1 b) (log (- 1 (* (/ b a ) (abs p)))))
                   (sign p)))
         100.0))))

(defun pseudonormal (mean variation)
  "Random numbers in a pseudo-normal distribution with mean MEAN, range
    MEAN-VARIATION to MEAN+VARIATION"
  (+  (random* variation)
      (random* variation)
      (- variation)
      mean))


(defun org-drill-early-interval-factor (optimal-factor
                                                optimal-interval
                                                days-ahead)
  "Arguments:
- OPTIMAL-FACTOR: interval-factor if the item had been tested
exactly when it was supposed to be.
- OPTIMAL-INTERVAL: interval for next repetition (days) if the item had been
tested exactly when it was supposed to be.
- DAYS-AHEAD: how many days ahead of time the item was reviewed.

Returns an adjusted optimal factor which should be used to
calculate the next interval, instead of the optimal factor found
in the matrix."
  (let ((delta-ofmax (* (1- optimal-factor)
                    (/ (+ optimal-interval
                          (* 0.6 optimal-interval) -1) (1- optimal-interval)))))
    (- optimal-factor
       (* delta-ofmax (/ days-ahead (+ days-ahead (* 0.6 optimal-interval)))))))


(defun org-drill-get-item-data ()
  "Returns a list of 6 items, containing all the stored recall
  data for the item at point:
- LAST-INTERVAL is the interval in days that was used to schedule the item's
  current review date.
- REPEATS is the number of items the item has been successfully recalled without
  without any failures. It is reset to 0 upon failure to recall the item.
- FAILURES is the total number of times the user has failed to recall the item.
- TOTAL-REPEATS includes both successful and unsuccessful repetitions.
- AVERAGE-QUALITY is the mean quality of recall of the item over
  all its repetitions, successful and unsuccessful.
- EASE is a number reflecting how easy the item is to learn. Higher is easier.
"
  (let ((learn-str (org-entry-get (point) "LEARN_DATA"))
        (repeats (org-drill-entry-total-repeats :missing)))
    (cond
     (learn-str
      (let ((learn-data (or (and learn-str
                                 (read learn-str))
                            (copy-list initial-repetition-state))))
        (list (nth 0 learn-data)        ; last interval
              (nth 1 learn-data)        ; repetitions
              (org-drill-entry-failure-count)
              (nth 1 learn-data)
              (org-drill-entry-last-quality)
              (nth 2 learn-data)        ; EF
              )))
     ((not (eql :missing repeats))
      (list (org-drill-entry-last-interval)
            (org-drill-entry-repeats-since-fail)
            (org-drill-entry-failure-count)
            (org-drill-entry-total-repeats)
            (org-drill-entry-average-quality)
            (org-drill-entry-ease)))
     (t  ; virgin item
      (list 0 0 0 0 nil nil)))))


(defun org-drill-store-item-data (last-interval repeats failures
                                                total-repeats meanq
                                                ease)
  "Stores the given data in the item at point."
  (org-entry-delete (point) "LEARN_DATA")
  (org-set-property "DRILL_LAST_INTERVAL"
                    (number-to-string (round-float last-interval 4)))
  (org-set-property "DRILL_REPEATS_SINCE_FAIL" (number-to-string repeats))
  (org-set-property "DRILL_TOTAL_REPEATS" (number-to-string total-repeats))
  (org-set-property "DRILL_FAILURE_COUNT" (number-to-string failures))
  (org-set-property "DRILL_AVERAGE_QUALITY"
                    (number-to-string (round-float meanq 3)))
  (org-set-property "DRILL_EASE"
                    (number-to-string (round-float ease 3))))



;;; SM2 Algorithm =============================================================


(defun determine-next-interval-sm2 (last-interval n ef quality
                                                  failures meanq total-repeats)
  "Arguments:
- LAST-INTERVAL -- the number of days since the item was last reviewed.
- REPEATS -- the number of times the item has been successfully reviewed
- EF -- the 'easiness factor'
- QUALITY -- 0 to 5

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."
  (assert (> n 0))
  (assert (and (>= quality 0) (<= quality 5)))
  (if (<= quality org-drill-failure-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-optimal-factor-matrix)
    ;; else:
    (let* ((next-ef (modify-e-factor ef quality))
           (interval
            (cond
             ((<= n 1) 1)
             ((= n 2)
              (cond
               (org-drill-add-random-noise-to-intervals-p
                (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-optimal-factor-matrix))))


;;; SM5 Algorithm =============================================================


(defun inter-repetition-interval-sm5 (last-interval n ef &optional of-matrix)
  (let ((of (get-optimal-factor n ef (or of-matrix
                                         org-drill-optimal-factor-matrix))))
    (if (= 1 n)
	of
      (* of last-interval))))


(defun determine-next-interval-sm5 (last-interval n ef quality
                                                  failures meanq total-repeats
                                                  of-matrix &optional delta-days)
  (if (zerop n) (setq n 1))
  (if (null ef) (setq ef 2.5))
  (assert (> n 0))
  (assert (and (>= quality 0) (<= quality 5)))
  (unless of-matrix
    (setq of-matrix org-drill-optimal-factor-matrix))
  (setq of-matrix (cl-copy-tree of-matrix))

  (setq meanq (if meanq
                  (/ (+ quality (* meanq total-repeats 1.0))
                     (1+ total-repeats))
                quality))

  (let ((next-ef (modify-e-factor ef quality))
        (old-ef ef)
        (new-of (modify-of (get-optimal-factor 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 (minusp delta-days))
      (setq new-of (org-drill-early-interval-factor
                    (get-optimal-factor n ef of-matrix)
                    (inter-repetition-interval-sm5
                     last-interval n ef of-matrix)
                    delta-days)))

    (setq of-matrix
          (set-optimal-factor n next-ef of-matrix
                              (round-float new-of 3)))     ; round OF to 3 d.p.

    (setq ef next-ef)

    (cond
     ;; "Failed" -- reset repetitions to 0,
     ((<= quality org-drill-failure-quality)
      (list -1 1 old-ef (1+ failures) meanq (1+ total-repeats)
            of-matrix))     ; Not clear if OF matrix is supposed to be
                            ; preserved
     ;; For a zero-based quality of 4 or 5, don't repeat
     ;; ((and (>= quality 4)
     ;;       (not org-learn-always-reschedule))
     ;;  (list 0 (1+ n) ef failures meanq
     ;;        (1+ total-repeats) of-matrix))     ; 0 interval = unschedule
     (t
      (setq interval (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."
  (* 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 (* (- ease 1.2) (expt org-drill-learn-fraction (log repetition 2)))))


(defun org-drill-simple8-quality->ease (quality)
  "Returns the ease (`AF' in the SM8 algorithm) which corresponds
to a mean item quality of QUALITY."
  (+ (* 0.0542 (expt quality 4))
     (* -0.4848 (expt quality 3))
     (* 1.4916 (expt quality 2))
     (* -1.2403 quality)
     1.4515))


(defun determine-next-interval-simple8 (last-interval repeats quality
                                                      failures meanq totaln
                                                      &optional delta-days)
  "Arguments:
- LAST-INTERVAL -- the number of days since the item was last reviewed.
- REPEATS -- the number of times the item has been successfully reviewed
- EASE -- the 'easiness factor'
- QUALITY -- 0 to 5
- DELTA-DAYS -- how many days overdue was the item when it was reviewed.
  0 = reviewed on the scheduled day. +N = N days overdue.
  -N = reviewed 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."
  (assert (>= repeats 0))
  (assert (and (>= quality 0) (<= quality 5)))
  (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
     ((<= quality org-drill-failure-quality)
      (incf failures)
      (setf repeats 0
            next-interval -1))
     ((or (zerop repeats)
          (zerop last-interval))
      (setf next-interval (org-drill-simple8-first-interval failures))
      (incf repeats)
      (incf totaln))
     (t
      (let* ((use-n
              (if (and
                   org-drill-adjust-intervals-for-early-and-late-repetitions-p
                   (numberp delta-days) (plusp delta-days)
                   (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) (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)
        (incf repeats)
        (incf totaln))))
    (list
     (if (and org-drill-add-random-noise-to-intervals-p
              (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-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 (read weight)))
    (destructuring-bind (last-interval repetitions failures
                                       total-repeats meanq ease)
        (org-drill-get-item-data)
      (destructuring-bind (next-interval repetitions ease
                                         failures meanq total-repeats
                                         &optional new-ofmatrix)
          (case org-drill-spaced-repetition-algorithm
            (sm5 (determine-next-interval-sm5 last-interval repetitions
                                              ease quality failures
                                              meanq total-repeats ofmatrix))
            (sm2 (determine-next-interval-sm2 last-interval repetitions
                                              ease quality failures
                                              meanq total-repeats))
            (simple8 (determine-next-interval-simple8 last-interval repetitions
                                                      quality failures meanq
                                                      total-repeats
                                                      delta-days)))
        (if (numberp days-ahead)
            (setq next-interval days-ahead))

        (org-drill-store-item-data next-interval repetitions failures
                                   total-repeats meanq ease)
        (if (and (null days-ahead)
                 (numberp weight) (plusp weight)
                 (not (minusp next-interval)))
            (setq next-interval (max 1.0 (/ next-interval weight))))

        (if (eql 'sm5 org-drill-spaced-repetition-algorithm)
            (setq org-drill-optimal-factor-matrix new-ofmatrix))

        (cond
         ((= 0 days-ahead)
          (org-schedule t))
         ((minusp days-ahead)
          (org-schedule nil (current-time)))
         (t
          (org-schedule nil (time-add (current-time)
                                      (days-to-time
                                       (round next-interval))))))))))


(defun org-drill-hypothetical-next-review-date (quality)
  "Returns an integer 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")))
    (destructuring-bind (last-interval repetitions failures
                                       total-repeats meanq ease)
        (org-drill-get-item-data)
      (if (stringp weight)
          (setq weight (read weight)))
      (destructuring-bind (next-interval repetitions ease
                                         failures meanq total-repeats
                                         &optional ofmatrix)
          (case org-drill-spaced-repetition-algorithm
            (sm5 (determine-next-interval-sm5 last-interval repetitions
                                              ease quality failures
                                              meanq total-repeats
                                              org-drill-optimal-factor-matrix))
            (sm2 (determine-next-interval-sm2 last-interval repetitions
                                              ease quality failures
                                              meanq total-repeats))
            (simple8 (determine-next-interval-simple8 last-interval repetitions
                                                      quality failures meanq
                                                      total-repeats)))
        (cond
         ((not (plusp next-interval))
          0)
         ((and (numberp weight) (plusp weight))
          (max 1.0 (/ next-interval weight)))
         (t
          next-interval))))))


(defun org-drill-hypothetical-next-review-dates ()
  (let ((intervals nil))
    (dotimes (q 6)
      (push (max (or (car intervals) 0)
                 (org-drill-hypothetical-next-review-date q))
            intervals))
    (reverse intervals)))


(defun org-drill-reschedule ()
  "Returns quality rating (0-5), or nil if the user quit."
  (let ((ch nil)
        (input nil)
        (next-review-dates (org-drill-hypothetical-next-review-dates)))
    (save-excursion
      (while (not (memq ch '(?q ?e ?0 ?1 ?2 ?3 ?4 ?5)))
        (setq input (read-key-sequence
                     (if (eq ch ??)
                         (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)

How well did you do? (0-5, ?=help, e=edit, t=tags, q=quit)"
                                 (round (nth 3 next-review-dates))
                                 (round (nth 4 next-review-dates))
                                 (round (nth 5 next-review-dates)))
                       "How well did you do? (0-5, ?=help, e=edit, q=quit)")))
        (cond
         ((stringp input)
          (setq ch (elt input 0)))
         ((and (vectorp input) (symbolp (elt input 0)))
          (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)))
          (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 ?t)
            (org-set-tags-command))))
    (cond
     ((and (>= ch ?0) (<= ch ?5))
      (let ((quality (- ch ?0))
            (failures (org-drill-entry-failure-count)))
        (save-excursion
          (org-drill-smart-reschedule quality
                                      (nth quality next-review-dates)))
        (push quality *org-drill-session-qualities*)
        (cond
         ((<= quality org-drill-failure-quality)
          (when org-drill-leech-failure-threshold
            ;;(setq failures (if failures (string-to-number failures) 0))
            ;; (org-set-property "DRILL_FAILURE_COUNT"
            ;;                   (format "%d" (1+ failures)))
            (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"
                          (time-to-inactive-org-timestamp (current-time)))
        quality))
     ((= ch ?e)
      'edit)
     (t
      nil))))


;; (defun org-drill-hide-all-subheadings-except (heading-list)
;;   "Returns a list containing the position of each immediate subheading of
;; the current topic."
;;   (let ((drill-entry-level (org-current-level))
;;         (drill-sections nil)
;;         (drill-heading nil))
;;     (org-show-subtree)
;;     (save-excursion
;;       (org-map-entries
;;        (lambda ()
;;          (when (and (not (outline-invisible-p))
;;                     (> (org-current-level) drill-entry-level))
;;            (setq drill-heading (org-get-heading t))
;;            (unless (and (= (org-current-level) (1+ drill-entry-level))
;;                         (member drill-heading heading-list))
;;              (hide-subtree))
;;            (push (point) drill-sections)))
;;        "" 'tree))
;;     (reverse drill-sections)))



(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-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))
             (hide-subtree))
           (push (point) drill-sections)))
       "" 'tree))
    (reverse drill-sections)))


(defun org-drill-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-presentation-prompt (&rest fmt-and-args)
  (let* ((item-start-time (current-time))
         (input nil)
         (ch nil)
         (last-second 0)
         (mature-entry-count (+ (length *org-drill-young-mature-entries*)
                                (length *org-drill-old-mature-entries*)
                                (length *org-drill-overdue-entries*)))
         (prompt
          (if fmt-and-args
              (apply 'format
                     (first fmt-and-args)
                     (rest fmt-and-args))
            (concat "Press key for answer, "
                    "e=edit, t=tags, s=skip, q=quit."))))
    (setq prompt
          (format "%s %s %s %s %s %s"
                  (propertize
                   (char-to-string
                    (case *org-drill-current-entry-schedule-type*
                      (new ?N) (young ?Y) (old ?o) (overdue ?!) (failed ?F) (t ??)))
                   'face `(:foreground
                           ,(case *org-drill-current-entry-schedule-type*
                              (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 *org-drill-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 *org-drill-again-entries*)
                                        (length *org-drill-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 *org-drill-new-entries*))
                   'face `(:foreground ,org-drill-new-count-color)
                   'help-echo (concat "The number of new items that you "
                                      "have never reviewed."))
                  prompt))
    (if (and (eql 'warn org-drill-leech-method)
             (org-drill-entry-leech-p))
        (setq prompt (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)))
    (while (memq ch '(nil ?t))
      (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))
                           prompt))
          (sit-for 1)))
      (setq input (read-key-sequence nil))
      (if (stringp input) (setq ch (elt input 0)))
      (if (eql ch ?t)
          (org-set-tags-command)))
    (case ch
      (?q nil)
      (?e 'edit)
      (?s 'skip)
      (otherwise t))))


(defun org-pos-in-regexp (pos regexp &optional nlines)
  (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)
    (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)
  (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 ()
  (save-excursion
    (while (re-search-forward "^#.*$" nil t)
      (org-drill-hide-region (match-beginning 0) (match-end 0)))))


(defun org-drill-unhide-text ()
  ;; This will also unhide the item's heading.
  (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 ()
  (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
      (unless (org-pos-in-regexp (match-beginning 0)
                                 org-bracket-link-regexp 1)
        (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))))
    (overlay-put ovl 'category
                 'org-drill-cloze-overlay-defaults)
    (when (find ?| (match-string 0))
      (let ((hint (substring-no-properties
                   (match-string 0)
                   (1+ (position ?| (match-string 0)))
                   (1- (length (match-string 0))))))
        (overlay-put
         ovl 'display
         ;; If hint is like `X...' then display [X...]
         ;; otherwise display [...X]
         (format (if (string-match-p "\\.\\.\\." hint) "[%s]" "[%s...]")
                 hint))))))


(defun org-drill-hide-cloze-hints ()
  (save-excursion
    (while (re-search-forward org-drill-cloze-regexp nil t)
      (unless (or (org-pos-in-regexp (match-beginning 0)
                                     org-bracket-link-regexp 1)
                  (null (match-beginning 2))) ; hint subexpression matched
        (org-drill-hide-region (match-beginning 2) (match-end 2))))))


(defmacro 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))))


(defun org-drill-replace-entry-text (text)
  "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.
Note: does not actually alter the item."
  (let ((ovl (make-overlay (point-min)
                           (save-excursion
                             (outline-next-heading)
                             (point)))))
    (overlay-put ovl 'category
                 'org-drill-replaced-text-overlay)
    (overlay-put ovl 'display text)))


(defun org-drill-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)))))


(defmacro with-replaced-entry-heading (heading &rest 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 ()
  (save-excursion
    (dolist (ovl (overlays-in (point-min) (point-max)))
      (when (eql 'org-drill-cloze-overlay-defaults (overlay-get ovl 'category))
        (delete-overlay ovl)))))


(defun org-drill-get-entry-text (&optional keep-properties-p)
  (let ((text (org-agenda-get-some-entry-text (point-marker) 100)))
    (if keep-properties-p
        text
      (substring-no-properties text))))


(defun org-drill-entry-empty-p ()
  (zerop (length (org-drill-get-entry-text))))



;;; 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 ()
  (with-hidden-comments
   (with-hidden-cloze-hints
    (with-hidden-cloze-text
     (org-drill-hide-all-subheadings-except nil)
     (org-display-inline-images t)
     (org-cycle-hide-drawers 'all)
     (prog1 (org-drill-presentation-prompt)
       (org-drill-hide-subheadings-if 'org-drill-entry-p))))))


(defun org-drill-present-default-answer (reschedule-fn)
  (org-drill-hide-subheadings-if 'org-drill-entry-p)
  (org-drill-unhide-clozed-text)
  (with-hidden-cloze-hints
   (funcall reschedule-fn)))


(defun org-drill-present-two-sided-card ()
  (with-hidden-comments
   (with-hidden-cloze-hints
    (with-hidden-cloze-text
     (let ((drill-sections (org-drill-hide-all-subheadings-except nil)))
       (when drill-sections
         (save-excursion
           (goto-char (nth (random* (min 2 (length drill-sections)))
                           drill-sections))
           (org-show-subtree)))
       (org-display-inline-images t)
       (org-cycle-hide-drawers 'all)
       (prog1 (org-drill-presentation-prompt)
         (org-drill-hide-subheadings-if 'org-drill-entry-p)))))))



(defun org-drill-present-multi-sided-card ()
  (with-hidden-comments
   (with-hidden-cloze-hints
    (with-hidden-cloze-text
     (let ((drill-sections (org-drill-hide-all-subheadings-except nil)))
       (when drill-sections
         (save-excursion
           (goto-char (nth (random* (length drill-sections)) drill-sections))
           (org-show-subtree)))
       (org-display-inline-images t)
       (org-cycle-hide-drawers 'all)
       (prog1 (org-drill-presentation-prompt)
         (org-drill-hide-subheadings-if 'org-drill-entry-p)))))))


(defun org-drill-present-multicloze-hide-n (number-to-hide)
  "Hides NUMBER-TO-HIDE pieces of text that are marked for cloze deletion,
chosen at random."
  (with-hidden-comments
   (with-hidden-cloze-hints
    (let ((item-end nil)
          (match-count 0)
          (body-start (or (cdr (org-get-property-block))
                          (point))))
      (org-drill-hide-all-subheadings-except nil)
      (save-excursion
        (outline-next-heading)
        (setq item-end (point)))
      (save-excursion
        (goto-char body-start)
        (while (re-search-forward org-drill-cloze-regexp item-end t)
          (incf match-count)))
      (when (plusp match-count)
        (let ((match-nums (subseq (shuffle-list (loop for i from 1 to match-count
                                                      collect i))
                                  0 number-to-hide)))
          (dolist (pos-to-hide match-nums)
            (save-excursion
              (goto-char body-start)
              (re-search-forward org-drill-cloze-regexp
                                 item-end t pos-to-hide)
              (org-drill-hide-matched-cloze-text)))))
      (org-display-inline-images t)
      (org-cycle-hide-drawers 'all)
      (prog1 (org-drill-presentation-prompt)
        (org-drill-hide-subheadings-if 'org-drill-entry-p)
        (org-drill-unhide-clozed-text))))))


(defun org-drill-present-multicloze-hide1 ()
  "Hides one of the pieces of text that are marked for cloze deletion,
chosen at random."
  (org-drill-present-multicloze-hide-n 1))


(defun org-drill-present-multicloze-hide2 ()
  "Hides two of the pieces of text that are marked for cloze deletion,
chosen at random."
  (org-drill-present-multicloze-hide-n 2))


;; (defun org-drill-present-multicloze-hide1 ()
;;   "Hides one of the pieces of text that are marked for cloze deletion,
;; chosen at random."
;;   (with-hidden-comments
;;    (let ((item-end nil)
;;          (match-count 0)
;;          (body-start (or (cdr (org-get-property-block))
;;                          (point))))
;;      (org-drill-hide-all-subheadings-except nil)
;;      (save-excursion
;;        (outline-next-heading)
;;        (setq item-end (point)))
;;      (save-excursion
;;        (goto-char body-start)
;;        (while (re-search-forward org-drill-cloze-regexp item-end t)
;;          (incf match-count)))
;;      (when (plusp match-count)
;;        (save-excursion
;;          (goto-char body-start)
;;          (re-search-forward org-drill-cloze-regexp
;;                             item-end t (1+ (random match-count)))
;;          (org-drill-hide-matched-cloze-text)))
;;      (org-display-inline-images t)
;;      (org-cycle-hide-drawers 'all)
;;      (prog1 (org-drill-presentation-prompt)
;;        (org-drill-hide-subheadings-if 'org-drill-entry-p)
;;        (org-drill-unhide-clozed-text)))))


(defun org-drill-present-multicloze-show1 ()
  "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."
  (with-hidden-comments
   (with-hidden-cloze-hints
    (let ((item-end nil)
          (match-count 0)
          (body-start (or (cdr (org-get-property-block))
                          (point))))
      (org-drill-hide-all-subheadings-except nil)
      (save-excursion
        (outline-next-heading)
        (setq item-end (point)))
      (save-excursion
        (goto-char body-start)
        (while (re-search-forward org-drill-cloze-regexp item-end t)
          (incf match-count)))
      (when (plusp match-count)
        (let ((match-to-hide (random* match-count)))
          (save-excursion
            (goto-char body-start)
            (dotimes (n match-count)
              (re-search-forward org-drill-cloze-regexp
                                 item-end t)
              (unless (= n match-to-hide)
                (org-drill-hide-matched-cloze-text))))))
      (org-display-inline-images t)
      (org-cycle-hide-drawers 'all)
      (prog1 (org-drill-presentation-prompt)
        (org-drill-hide-subheadings-if 'org-drill-entry-p)
        (org-drill-unhide-clozed-text))))))


(defun org-drill-present-card-using-text (question &optional answer)
  "Present the string QUESTION as the only visible content of the card."
  (with-hidden-comments
   (with-replaced-entry-text
    question
    (org-drill-hide-all-subheadings-except nil)
    (org-cycle-hide-drawers 'all)
    (prog1 (org-drill-presentation-prompt)
      (org-drill-hide-subheadings-if 'org-drill-entry-p)))))


;;; The following macro is necessary because `org-save-outline-visibility'
;;; currently discards the value returned by its body and returns a garbage
;;; value instead. (as at org mode v 7.5)

(defmacro org-drill-save-visibility (&rest body)
  "Store the current visibility state of the org buffer, and restore it
after executing BODY. Return the value of the last expression
in BODY."
  (let ((retval (gensym)))
    `(let ((,retval nil))
       (org-save-outline-visibility t
         (setq ,retval
               (progn
                 ,@body)))
       ,retval)))


(defun org-drill-entry ()
  "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."
  (interactive)
  (org-drill-goto-drill-entry-heading)
  ;;(unless (org-part-of-drill-entry-p)
  ;;  (error "Point is not inside a drill entry"))
  ;;(unless (org-at-heading-p)
  ;;  (org-back-to-heading))
  (let ((card-type (org-entry-get (point) "DRILL_CARD_TYPE"))
        (answer-fn 'org-drill-present-default-answer)
        (cont nil))
    (org-drill-save-visibility
     (save-restriction
       (org-narrow-to-subtree)
       (org-show-subtree)
       (org-cycle-hide-drawers 'all)

       (let ((presentation-fn (cdr (assoc card-type org-drill-card-type-alist))))
         (if (listp presentation-fn)
             (psetq answer-fn (or (second presentation-fn)
                                  'org-drill-present-default-answer)
                    presentation-fn (first presentation-fn)))
         (cond
          (presentation-fn
           (setq cont (funcall presentation-fn)))
          (t
           (error "Unknown card type: '%s'" card-type))))

       (cond
        ((not cont)
         (message "Quit")
         nil)
        ((eql cont 'edit)
         'edit)
        ((eql cont 'skip)
         'skip)
        (t
         (save-excursion
           (funcall answer-fn
                    (lambda () (org-drill-reschedule))))))))))


(defun org-drill-entries-pending-p ()
  (or *org-drill-again-entries*
      (and (not (org-drill-maximum-item-count-reached-p))
           (not (org-drill-maximum-duration-reached-p))
           (or *org-drill-new-entries*
               *org-drill-failed-entries*
               *org-drill-young-mature-entries*
               *org-drill-old-mature-entries*
               *org-drill-overdue-entries*
               *org-drill-again-entries*))))


(defun org-drill-pending-entry-count ()
  (+ (length *org-drill-new-entries*)
     (length *org-drill-failed-entries*)
     (length *org-drill-young-mature-entries*)
     (length *org-drill-old-mature-entries*)
     (length *org-drill-overdue-entries*)
     (length *org-drill-again-entries*)))


(defun org-drill-maximum-duration-reached-p ()
  "Returns true if the current drill session has continued past its
maximum duration."
  (and org-drill-maximum-duration
       *org-drill-start-time*
       (> (- (float-time (current-time)) *org-drill-start-time*)
          (* org-drill-maximum-duration 60))))


(defun org-drill-maximum-item-count-reached-p ()
  "Returns true if the current drill session has reached the
maximum number of items."
  (and org-drill-maximum-items-per-session
       (>= (length *org-drill-done-entries*)
           org-drill-maximum-items-per-session)))


(defun org-drill-pop-next-pending-entry ()
  (block org-drill-pop-next-pending-entry
    (let ((m nil))
      (while (or (null m)
                 (not (org-drill-entry-p m)))
        (setq
         m
         (cond
          ;; First priority is items we failed in a prior session.
          ((and *org-drill-failed-entries*
                (not (org-drill-maximum-item-count-reached-p))
                (not (org-drill-maximum-duration-reached-p)))
           (setq *org-drill-current-entry-schedule-type* 'failed)
           (pop-random *org-drill-failed-entries*))
          ;; Next priority is overdue items.
          ((and *org-drill-overdue-entries*
                (not (org-drill-maximum-item-count-reached-p))
                (not (org-drill-maximum-duration-reached-p)))
           ;; 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.
           (setq *org-drill-current-entry-schedule-type* 'overdue)
           (pop *org-drill-overdue-entries*))
          ;; Next priority is 'young' items.
          ((and *org-drill-young-mature-entries*
                (not (org-drill-maximum-item-count-reached-p))
                (not (org-drill-maximum-duration-reached-p)))
           (setq *org-drill-current-entry-schedule-type* 'young)
           (pop-random *org-drill-young-mature-entries*))
          ;; Next priority is newly added items, and older entries.
          ;; We pool these into a single group.
          ((and (or *org-drill-new-entries*
                    *org-drill-old-mature-entries*)
                (not (org-drill-maximum-item-count-reached-p))
                (not (org-drill-maximum-duration-reached-p)))
           (cond
            ((< (random* (+ (length *org-drill-new-entries*)
                            (length *org-drill-old-mature-entries*)))
                (length *org-drill-new-entries*))
             (setq *org-drill-current-entry-schedule-type* 'new)
             (pop-random *org-drill-new-entries*))
            (t
             (setq *org-drill-current-entry-schedule-type* 'old)
             (pop-random *org-drill-old-mature-entries*))))
          ;; After all the above are done, last priority is items
          ;; that were failed earlier THIS SESSION.
          (*org-drill-again-entries*
           (setq *org-drill-current-entry-schedule-type* 'failed)
           (pop *org-drill-again-entries*))
          (t                            ; nothing left -- return nil
           (return-from org-drill-pop-next-pending-entry nil)))))
      m)))


(defun org-drill-entries (&optional resuming-p)
  "Returns 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."
  (block org-drill-entries
    (while (org-drill-entries-pending-p)
      (let ((m (cond
                ((or (not resuming-p)
                     (null *org-drill-current-item*)
                     (not (org-drill-entry-p *org-drill-current-item*)))
                 (org-drill-pop-next-pending-entry))
                (t                      ; resuming a suspended session.
                 (setq resuming-p nil)
                 *org-drill-current-item*))))
        (setq *org-drill-current-item* m)
        (unless m
          (error "Unexpectedly ran out of pending drill items"))
        (save-excursion
          (org-drill-goto-entry m)
          (setq result (org-drill-entry))
          (cond
           ((null result)
            (message "Quit")
            (setq end-pos :quit)
            (return-from org-drill-entries nil))
           ((eql result 'edit)
            (setq end-pos (point-marker))
            (return-from org-drill-entries nil))
           ((eql result 'skip)
            nil)                        ; skip this item
           (t
            (cond
             ((<= result org-drill-failure-quality)
              (if *org-drill-again-entries*
                  (setq *org-drill-again-entries*
                        (shuffle-list *org-drill-again-entries*)))
              (push-end m *org-drill-again-entries*))
             (t
              (push m *org-drill-done-entries*))))))))))



(defun org-drill-final-report ()
  (let ((pass-percent
         (round (* 100 (count-if (lambda (qual)
                                   (> qual org-drill-failure-quality))
                                 *org-drill-session-qualities*))
                (max 1 (length *org-drill-session-qualities*))))
        (prompt nil))
    (setq prompt
          (format
           "%d items reviewed. Session duration %s.
%d/%d items awaiting review (%s, %s, %s, %s, %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)
Tomorrow, %d more items will become due for review.
Session finished. Press a key to continue..."
           (length *org-drill-done-entries*)
           (format-seconds "%h:%.2m:%.2s"
                           (- (float-time (current-time)) *org-drill-start-time*))
           (org-drill-pending-entry-count)
           (+ (org-drill-pending-entry-count)
              *org-drill-dormant-entry-count*)
           (propertize
            (format "%d failed"
                    (+ (length *org-drill-failed-entries*)
                       (length *org-drill-again-entries*)))
            'face `(:foreground ,org-drill-failed-count-color))
           (propertize
            (format "%d overdue"
                    (length *org-drill-overdue-entries*))
            'face `(:foreground ,org-drill-failed-count-color))
           (propertize
            (format "%d new"
                    (length *org-drill-new-entries*))
            'face `(:foreground ,org-drill-new-count-color))
           (propertize
            (format "%d young"
                    (length *org-drill-young-mature-entries*))
            'face `(:foreground ,org-drill-mature-count-color))
           (propertize
            (format "%d old"
                    (length *org-drill-old-mature-entries*))
            'face `(:foreground ,org-drill-mature-count-color))
           (round (* 100 (count 5 *org-drill-session-qualities*))
                  (max 1 (length *org-drill-session-qualities*)))
           (round (* 100 (count 2 *org-drill-session-qualities*))
                  (max 1 (length *org-drill-session-qualities*)))
           (round (* 100 (count 4 *org-drill-session-qualities*))
                  (max 1 (length *org-drill-session-qualities*)))
           (round (* 100 (count 1 *org-drill-session-qualities*))
                  (max 1 (length *org-drill-session-qualities*)))
           (round (* 100 (count 3 *org-drill-session-qualities*))
                  (max 1 (length *org-drill-session-qualities*)))
           (round (* 100 (count 0 *org-drill-session-qualities*))
                  (max 1 (length *org-drill-session-qualities*)))
           pass-percent
           org-drill-failure-quality
           *org-drill-due-tomorrow-count*
           ))

    (while (not (input-pending-p))
      (message "%s" prompt)
      (sit-for 0.5))
    (read-char-exclusive)

    (if (< pass-percent (- 100 org-drill-forgetting-index))
        (read-char-exclusive
         (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)
          *org-drill-overdue-entry-count*
          (round (* 100 *org-drill-overdue-entry-count*)
                 (+ *org-drill-dormant-entry-count*
                    *org-drill-due-entry-count*)))
         ))))



(defun org-drill-free-all-markers ()
  (dolist (m (append  *org-drill-done-entries*
                      *org-drill-new-entries*
                      *org-drill-failed-entries*
                      *org-drill-again-entries*
                      *org-drill-overdue-entries*
                      *org-drill-young-mature-entries*
                      *org-drill-old-mature-entries*))
    (free-marker m)))


(defun org-drill-order-overdue-entries (overdue-data)
  (setq *org-drill-overdue-entries*
        (mapcar 'car
                (sort (shuffle-list overdue-data)
                      (lambda (a b) (> (cdr a) (cdr b)))))))


(defun org-drill (&optional scope resume-p)
  "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 is passed to `org-map-entries', and can be any of:

nil     The current buffer, respecting the restriction if any.
        This is the default.
tree    The subtree started with the entry at point
file    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
 (file1 file2 ...)
        If this is a list, all files in the list will be scanned.

If RESUME-P is non-nil, resume a suspended drill session rather
than starting a new one."

  (interactive)
  (let ((end-pos nil)
        (overdue-data nil)
        (cnt 0))
    (block org-drill
      (unless resume-p
        (org-drill-free-all-markers)
        (setq *org-drill-current-item* nil
              *org-drill-done-entries* nil
              *org-drill-dormant-entry-count* 0
              *org-drill-due-entry-count* 0
              *org-drill-due-tomorrow-count* 0
              *org-drill-overdue-entry-count* 0
              *org-drill-new-entries* nil
              *org-drill-overdue-entries* nil
              *org-drill-young-mature-entries* nil
              *org-drill-old-mature-entries* nil
              *org-drill-failed-entries* nil
              *org-drill-again-entries* nil)
        (setq *org-drill-session-qualities* nil)
        (setq *org-drill-start-time* (float-time (current-time))))
      (setq *random-state* (make-random-state t)) ; reseed RNG
      (unwind-protect
          (save-excursion
            (unless resume-p
              (let ((org-trust-scanner-tags t)
                    (warned-about-id-creation nil))
                (org-map-entries
                 (lambda ()
                   (when (zerop (% (incf cnt) 50))
                     (message "Processing drill items: %4d%s"
                              (+ (length *org-drill-new-entries*)
                                 (length *org-drill-overdue-entries*)
                                 (length *org-drill-young-mature-entries*)
                                 (length *org-drill-old-mature-entries*)
                                 (length *org-drill-failed-entries*))
                              (make-string (ceiling cnt 50) ?.)))
                   (cond
                    ((not (org-drill-entry-p))
                     nil)               ; skip
                    (t
                     (when (and (not warned-about-id-creation)
                                (null (org-id-get)))
                       (message (concat "Creating unique IDs for items "
                                        "(slow, but only happens once)"))
                       (sit-for 0.5)
                       (setq warned-about-id-creation t))
                     (org-id-get-create) ; ensure drill entry has unique ID
                     (let ((due (org-drill-entry-days-overdue))
                           (last-int (org-drill-entry-last-interval 1)))
                       (cond
                        ((org-drill-entry-empty-p)
                         nil)           ; skip -- item body is empty
                        ((or (null due) ; unscheduled - usually a skipped leech
                             (minusp due)) ; scheduled in the future
                         (incf *org-drill-dormant-entry-count*)
                         (if (eq -1 due)
                             (incf *org-drill-due-tomorrow-count*)))
                        ((org-drill-entry-new-p)
                         (push (point-marker) *org-drill-new-entries*))
                        ((<= (org-drill-entry-last-quality 9999)
                             org-drill-failure-quality)
                         ;; Mature entries that were failed last time are
                         ;; FAILED, regardless of how young, old or overdue
                         ;; they are.
                         (push (point-marker) *org-drill-failed-entries*))
                        ((org-drill-entry-overdue-p due last-int)
                         ;; Overdue status overrides young versus old
                         ;; distinction.
                         ;; Store marker + due, for sorting of overdue entries
                         (push (cons (point-marker) due) overdue-data))
                        ((<= (org-drill-entry-last-interval 9999)
                             org-drill-days-before-old)
                         ;; Item is 'young'.
                         (push (point-marker)
                               *org-drill-young-mature-entries*))
                        (t
                         (push (point-marker)
                               *org-drill-old-mature-entries*)))))))
                 (concat "+" org-drill-question-tag) scope)
                ;; Order 'overdue' items so that the most overdue will tend to
                ;; come up for review first, while keeping exact order random
                (org-drill-order-overdue-entries overdue-data)
                (setq *org-drill-overdue-entry-count*
                      (length *org-drill-overdue-entries*))))
            (setq *org-drill-due-entry-count* (org-drill-pending-entry-count))
            (cond
             ((and (null *org-drill-new-entries*)
                   (null *org-drill-failed-entries*)
                   (null *org-drill-overdue-entries*)
                   (null *org-drill-young-mature-entries*)
                   (null *org-drill-old-mature-entries*))
              (message "I did not find any pending drill items."))
             (t
              (org-drill-entries resume-p)
              (message "Drill session finished!"))))
        (progn
          (unless end-pos
            (org-drill-free-all-markers)))))
    (cond
     (end-pos
      (when (markerp end-pos)
        (org-drill-goto-entry end-pos))
      (message
       "You can continue the drill session with `M-x org-drill-resume'."))
     (t
      (org-drill-final-report)
      (if (eql 'sm5 org-drill-spaced-repetition-algorithm)
          (org-drill-save-optimal-factor-matrix))
      ))))



(defun org-drill-save-optimal-factor-matrix ()
  (message "Saving optimal factor matrix...")
  (customize-save-variable 'org-drill-optimal-factor-matrix
                           org-drill-optimal-factor-matrix))


(defun org-drill-cram (&optional scope)
  "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)
  (let ((*org-drill-cram-mode* t))
    (org-drill scope)))


(defun org-drill-tree ()
  "Run an interactive drill session using drill items within the
subtree at point."
  (interactive)
  (org-drill 'tree))


(defun org-drill-resume ()
  "Resume a suspended drill session. Sessions are suspended by
exiting them with the `edit' option."
  (interactive)
  (org-drill nil t))


(defun org-drill-strip-entry-data ()
  (org-delete-property "LEARN_DATA")
  (org-delete-property "DRILL_LAST_INTERVAL")
  (org-delete-property "DRILL_REPEATS_SINCE_FAIL")
  (org-delete-property "DRILL_TOTAL_REPEATS")
  (org-delete-property "DRILL_FAILURE_COUNT")
  (org-delete-property "DRILL_AVERAGE_QUALITY")
  (org-delete-property "DRILL_EASE")
  (org-delete-property "DRILL_LAST_QUALITY")
  (org-delete-property "DRILL_LAST_REVIEWED")
  (org-schedule t))


(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'."
  (interactive)
  (when (yes-or-no-p
         "Delete scheduling data from ALL items in scope: are you sure?")
    (org-map-entries 'org-drill-strip-entry-data
                     "" scope)
    (message "Done.")))



(add-hook 'org-mode-hook
          (lambda ()
            (when org-drill-use-visible-cloze-face-p
              (font-lock-add-keywords 'org-mode
                                      org-drill-cloze-keywords
                                      nil))))


;;; 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'."
  (block org-drill-copy-entry-to-other-buffer
    (save-excursion
      (let ((src (current-buffer))
            (m nil))
        (flet ((paste-tree-here (&optional level)
                                (org-paste-subtree level)
                                (org-toggle-tag "imported" 'on)
                                (org-map-entries
                                 (lambda ()
                                   (let ((id (org-id-get)))
                                     (unless (gethash id *org-drill-dest-id-table*)
                                       (puthash id (point-marker)
                                                *org-drill-dest-id-table*))))
                                 (concat "+" org-drill-question-tag) 'tree)))
          (unless path
            (setq path (org-get-outline-path)))
          (switch-to-buffer dest)
          (setq m
                (condition-case nil
                    (org-find-olp path t)
                  (error                ; path does not exist in DEST
                   (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)
          (org-forward-same-level)
          (newline)
          (forward-line -1)
          (paste-tree-here (1+ (or (org-current-level) 0)))
          )))))



(defun org-drill-merge-buffers (src &optional dest)
  "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."
  ;; In future could look at what to do if we find an item in SRC whose ID
  ;; is not present in DEST -- copy the whole item to DEST?
  ;; org-copy-subtree --> org-paste-subtree
  ;; could try to put it "near" the closest marker
  (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)))
    ;; Compile list of all IDs in the destination buffer.
    (clrhash *org-drill-dest-id-table*)
    (with-current-buffer dest
      (org-map-entries
       (lambda ()
         (let ((this-id (org-id-get)))
           (when this-id
             (puthash this-id (point-marker) *org-drill-dest-id-table*))))
       (concat "+" org-drill-question-tag)))
    ;; Look through all entries in source buffer.
    (with-current-buffer src
      (org-map-entries
       (lambda ()
         (let ((id (org-id-get))
               (last-quality nil) (last-reviewed nil)
               (scheduled-time nil))
           (cond
            ((or (null id)
                 (not (org-drill-entry-p)))
             nil)
            ((gethash id *org-drill-dest-id-table*)
             ;; This entry matches an entry in dest. Retrieve all its
             ;; scheduling data, then go to the matching location in dest
             ;; and write the data.
             (let ((marker (gethash id *org-drill-dest-id-table*)))
               (destructuring-bind (last-interval repetitions failures
                                                  total-repeats meanq ease)
                   (org-drill-get-item-data)
                 (setq 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)))
                 (save-excursion
                   ;; go to matching entry in destination buffer
                   (switch-to-buffer (marker-buffer marker))
                   (goto-char marker)
                   (org-drill-strip-entry-data)
                   (unless (zerop total-repeats)
                     (org-drill-store-item-data last-interval repetitions failures
                                                total-repeats meanq ease)
                     (org-set-property "LAST_QUALITY" last-quality)
                     (org-set-property "LAST_REVIEWED" last-reviewed)
                     (if scheduled-time
                         (org-schedule nil scheduled-time)))))
               (free-marker marker)))
            (t
             ;; item in SRC has ID, but no matching ID in DEST.
             ;; It must be a new item that does not exist in DEST.
             ;; Copy the entire item to the *end* of DEST.
             (org-drill-copy-entry-to-other-buffer dest)))))
       ;; (org-copy-subtree)
       ;; (save-excursion
       ;;   (with-current-buffer dest
       ;;     (goto-char (point-max))
       ;;     (newline)
       ;;     (org-paste-subtree)
       ;;     ;; Check if item has any child drill items. If it does,
       ;;     ;; store their IDs in the hashtable, to signify that they
       ;;     ;; now exist in DEST.
       ;;     (org-map-entries
       ;;      (lambda ()
       ;;        (let ((id (org-id-get)))
       ;;          (unless (gethash id *org-drill-dest-id-table*)
       ;;            (puthash id (point-marker) *org-drill-dest-id-table*))))
       ;;      (concat "+" org-drill-question-tag) 'tree)
       ;;     ))))))
       (concat "+" org-drill-question-tag)))))



;;; Card types for learning languages =========================================

;;; Get spell-number.el from:
;;; http://www.emacswiki.org/emacs/spell-number.el
(autoload 'spelln-integer-in-words "spell-number")


;;; `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"))
  "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-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))
        (translation (org-entry-get (point) "VERB_TRANSLATION" t))
        (tense (org-entry-get (point) "VERB_TENSE" nil))
        (highlight-face nil))
    (unless (and infinitive translation tense)
      (error "Missing information for verb conjugation card (%s, %s, %s) at %s"
             infinitive translation tense (point)))
    (setq tense (downcase (car (read-from-string tense)))
          infinitive (car (read-from-string infinitive))
          translation (car (read-from-string translation)))
    (setq highlight-face
          (list :foreground
                (or (second (assoc-string tense org-drill-verb-tense-alist t))
                    "red")))
    (setq infinitive (propertize infinitive 'face highlight-face))
    (setq translation (propertize translation 'face highlight-face))
    (setq tense (propertize tense 'face highlight-face))
    (list infinitive translation tense)))


(defun org-drill-present-verb-conjugation ()
  "Present a drill entry whose card type is 'conjugate'."
  (destructuring-bind (infinitive translation tense)
      (org-drill-get-verb-conjugation-info)
    (org-drill-present-card-using-text
     (cond
      ((zerop (random* 2))
       (format "\nTranslate the verb\n\n%s\n\nand conjugate for the %s tense.\n\n"
               infinitive tense))
      (t
       (format "\nGive the verb that means\n\n%s\n\nand conjugate for the %s tense.\n\n"
               translation tense))))))


(defun org-drill-show-answer-verb-conjugation (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."
  (destructuring-bind (infinitive translation tense)
      (org-drill-get-verb-conjugation-info)
    (with-replaced-entry-heading
     (format "%s tense of %s ==> %s\n\n"
             (capitalize tense)
             infinitive translation)
     (funcall reschedule-fn))))


;;; `translate_number' card type ==============================================
;;; See spanish.org for usage

(defvar *drilled-number* 0)
(defvar *drilled-number-direction* 'to-english)

(defun org-drill-present-translate-number ()
  (let ((num-min (read (org-entry-get (point) "DRILL_NUMBER_MIN")))
        (num-max (read (org-entry-get (point) "DRILL_NUMBER_MAX")))
        (language (read (org-entry-get (point) "DRILL_LANGUAGE" t)))
        (highlight-face 'font-lock-warning-face))
    (cond
     ((not (fboundp 'spelln-integer-in-words))
      (message "`spell-number.el' not loaded, skipping 'translate_number' card...")
      (sit-for 0.5)
      'skip)
     ((not (and (numberp num-min) (numberp num-max) language))
      (error "Missing language or minimum or maximum numbers for number card"))
     (t
      (if (> num-min num-max)
          (psetf num-min num-max
                 num-max num-min))
      (setq *drilled-number*
            (+ num-min (random* (abs (1+ (- num-max num-min))))))
      (setq *drilled-number-direction*
            (if (zerop (random* 2)) 'from-english 'to-english))
      (org-drill-present-card-using-text
       (if (eql 'to-english *drilled-number-direction*)
           (format "\nTranslate into English:\n\n%s\n"
                   (let ((spelln-language language))
                     (propertize
                      (spelln-integer-in-words *drilled-number*)
                      'face highlight-face)))
         (format "\nTranslate into %s:\n\n%s\n"
                 (capitalize (format "%s" language))
                 (let ((spelln-language 'english-gb))
                   (propertize
                    (spelln-integer-in-words *drilled-number*)
                    'face highlight-face)))))))))


(defun org-drill-show-answer-translate-number (reschedule-fn)
  (let* ((language (read (org-entry-get (point) "DRILL_LANGUAGE" t)))
         (highlight-face 'font-lock-warning-face)
         (non-english
          (let ((spelln-language language))
            (propertize (spelln-integer-in-words *drilled-number*)
                        'face highlight-face)))
         (english
          (let ((spelln-language 'english-gb))
            (propertize (spelln-integer-in-words *drilled-number*)
                        'face 'highlight-face))))
    (with-replaced-entry-text
     (cond
      ((eql 'to-english *drilled-number-direction*)
       (format "\nThe English translation of %s is:\n\n%s\n"
               non-english english))
      (t
       (format "\nThe %s translation of %s is:\n\n%s\n"
               (capitalize (format "%s" language))
               english non-english)))
     (funcall reschedule-fn))))


;;; `spanish_verb' card type ==================================================
;;; Not very interesting, but included to demonstrate how a presentation
;;; function can manipulate which subheading are hidden versus shown.


(defun org-drill-present-spanish-verb ()
  (let ((prompt nil)
        (reveal-headings nil))
    (with-hidden-comments
     (with-hidden-cloze-hints
      (with-hidden-cloze-text
       (case (random* 6)
         (0
          (org-drill-hide-all-subheadings-except '("Infinitive"))
          (setq prompt
                (concat "Translate this Spanish verb, and conjugate it "
                        "for the *present* tense.")
                reveal-headings '("English" "Present Tense" "Notes")))
         (1
          (org-drill-hide-all-subheadings-except '("English"))
          (setq prompt (concat "For the *present* tense, conjugate the "
                               "Spanish translation of this English verb.")
                reveal-headings '("Infinitive" "Present Tense" "Notes")))
         (2
          (org-drill-hide-all-subheadings-except '("Infinitive"))
          (setq prompt (concat "Translate this Spanish verb, and "
                               "conjugate it for the *past* tense.")
                reveal-headings '("English" "Past Tense" "Notes")))
         (3
          (org-drill-hide-all-subheadings-except '("English"))
          (setq prompt (concat "For the *past* tense, conjugate the "
                               "Spanish translation of this English verb.")
                reveal-headings '("Infinitive" "Past Tense" "Notes")))
         (4
          (org-drill-hide-all-subheadings-except '("Infinitive"))
          (setq prompt (concat "Translate this Spanish verb, and "
                               "conjugate it for the *future perfect* tense.")
                reveal-headings '("English" "Future Perfect Tense" "Notes")))
         (5
          (org-drill-hide-all-subheadings-except '("English"))
          (setq prompt (concat "For the *future perfect* tense, conjugate the "
                               "Spanish translation of this English verb.")
                reveal-headings '("Infinitive" "Future Perfect Tense" "Notes"))))
       (org-cycle-hide-drawers 'all)
       (prog1 (org-drill-presentation-prompt)
         (org-drill-hide-subheadings-if 'org-drill-entry-p)))))))


(provide 'org-drill)