org-drill.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. ;;; org-drill.el - Self-testing with org-learn
  2. ;;;
  3. ;;; Author: Paul Sexton <eeeickythump@gmail.com>
  4. ;;; Version: 1.0
  5. ;;; Repository at http://bitbucket.org/eeeickythump/org-drill/
  6. ;;;
  7. ;;;
  8. ;;; Synopsis
  9. ;;; ========
  10. ;;;
  11. ;;; Uses the spaced repetition algorithm in `org-learn' to conduct interactive
  12. ;;; "drill sessions", where the material to be remembered is presented to the
  13. ;;; student in random order. The student rates his or her recall of each item,
  14. ;;; and this information is fed back to `org-learn' to schedule the item for
  15. ;;; later revision.
  16. ;;;
  17. ;;; Each drill session can be restricted to topics in the current buffer
  18. ;;; (default), one or several files, all agenda files, or a subtree. A single
  19. ;;; topic can also be drilled.
  20. ;;;
  21. ;;; Different "card types" can be defined, which present their information to
  22. ;;; the student in different ways.
  23. ;;;
  24. ;;; See the file README.org for more detailed documentation.
  25. (eval-when-compile (require 'cl))
  26. (eval-when-compile (require 'hi-lock))
  27. (require 'org)
  28. (require 'org-learn)
  29. (defgroup org-drill nil
  30. "Options concerning interactive drill sessions in Org mode (org-drill)."
  31. :tag "Org-Drill"
  32. :group 'org-link)
  33. (defcustom org-drill-question-tag
  34. "drill"
  35. "Tag which topics must possess in order to be identified as review topics
  36. by `org-drill'."
  37. :group 'org-drill
  38. :type 'string)
  39. (defcustom org-drill-maximum-items-per-session
  40. 30
  41. "Each drill session will present at most this many topics for review.
  42. Nil means unlimited."
  43. :group 'org-drill
  44. :type '(choice integer (const nil)))
  45. (defcustom org-drill-maximum-duration
  46. 20
  47. "Maximum duration of a drill session, in minutes.
  48. Nil means unlimited."
  49. :group 'org-drill
  50. :type '(choice integer (const nil)))
  51. (defcustom org-drill-failure-quality
  52. 2
  53. "If the quality of recall for an item is this number or lower,
  54. it is regarded as an unambiguous failure, and the repetition
  55. interval for the card is reset to 0 days. By default this is
  56. 2. For Mnemosyne-like behaviour, set it to 1. Other values are
  57. not really sensible."
  58. :group 'org-drill
  59. :type '(choice (const 2) (const 1)))
  60. (defcustom org-drill-leech-failure-threshold
  61. 15
  62. "If an item is forgotten more than this many times, it is tagged
  63. as a 'leech' item."
  64. :group 'org-drill
  65. :type '(choice integer (const nil)))
  66. (defcustom org-drill-leech-method
  67. 'skip
  68. "How should 'leech items' be handled during drill sessions?
  69. Possible values:
  70. - nil :: Leech items are treated the same as normal items.
  71. - skip :: Leech items are not included in drill sessions.
  72. - warn :: Leech items are still included in drill sessions,
  73. but a warning message is printed when each leech item is
  74. presented."
  75. :group 'org-drill
  76. :type '(choice (const 'warn) (const 'skip) (const nil)))
  77. (defface org-drill-visible-cloze-face
  78. '((t (:foreground "dark slate blue")))
  79. "The face used to hide the contents of cloze phrases."
  80. :group 'org-drill)
  81. (defcustom org-drill-use-visible-cloze-face-p
  82. nil
  83. "Use a special face to highlight cloze-deleted text in org mode
  84. buffers?"
  85. :group 'org-drill
  86. :type 'boolean)
  87. (defface org-drill-hidden-cloze-face
  88. '((t (:foreground "deep sky blue" :background "blue")))
  89. "The face used to hide the contents of cloze phrases."
  90. :group 'org-drill)
  91. (setplist 'org-drill-cloze-overlay-defaults
  92. '(display "[...]"
  93. face org-drill-hidden-cloze-face
  94. window t))
  95. (defvar org-drill-cloze-regexp
  96. ;; ver 1 "[^][]\\(\\[[^][][^]]*\\]\\)"
  97. ;; ver 2 "\\(\\[.*?\\]\\|^[^[[:cntrl:]]*?\\]\\|\\[.*?$\\)"
  98. "\\(\\[.*?\\]\\|\\[.*?[[:cntrl:]]+.*?\\]\\)")
  99. (defcustom org-drill-card-type-alist
  100. '((nil . org-drill-present-simple-card)
  101. ("simple" . org-drill-present-simple-card)
  102. ("twosided" . org-drill-present-two-sided-card)
  103. ("multisided" . org-drill-present-multi-sided-card)
  104. ("spanish_verb" . org-drill-present-spanish-verb))
  105. "Alist associating card types with presentation functions. Each entry in the
  106. alist takes the form (CARDTYPE . FUNCTION), where CARDTYPE is a string
  107. or nil, and FUNCTION is a function which takes no arguments and returns a
  108. boolean value."
  109. :group 'org-drill
  110. :type '(alist :key-type (choice string (const nil)) :value-type function))
  111. (defcustom org-drill-spaced-repetition-algorithm
  112. 'sm5
  113. "Which SuperMemo spaced repetition algorithm to use for scheduling items.
  114. Available choices are SM2 and SM5."
  115. :group 'org-drill
  116. :type '(choice (const 'sm2) (const 'sm5)))
  117. (defcustom org-drill-add-random-noise-to-intervals-p
  118. nil
  119. "If true, the number of days until an item's next repetition
  120. will vary slightly from the interval calculated by the SM2
  121. algorithm. The variation is very small when the interval is
  122. small, and scales up with the interval. The code for calculating
  123. random noise is adapted from Mnemosyne."
  124. :group 'org-drill
  125. :type 'boolean)
  126. (defvar *org-drill-done-entry-count* 0)
  127. (defvar *org-drill-pending-entry-count* 0)
  128. (defvar *org-drill-session-qualities* nil)
  129. (defvar *org-drill-start-time* 0)
  130. (defun shuffle-list (list)
  131. "Randomly permute the elements of LIST (all permutations equally likely)."
  132. ;; Adapted from 'shuffle-vector' in cookie1.el
  133. (let ((i 0)
  134. j
  135. temp
  136. (len (length list)))
  137. (while (< i len)
  138. (setq j (+ i (random (- len i))))
  139. (setq temp (nth i list))
  140. (setf (nth i list) (nth j list))
  141. (setf (nth j list) temp)
  142. (setq i (1+ i))))
  143. list)
  144. (defun org-drill-entry-p ()
  145. "Is the current entry a 'drill item'?"
  146. (or (assoc "LEARN_DATA" (org-entry-properties nil))
  147. (member org-drill-question-tag (org-get-local-tags))))
  148. (defun org-part-of-drill-entry-p ()
  149. "Is the current entry either the main heading of a 'drill item',
  150. or a subheading within a drill item?"
  151. (or (org-drill-entry-p)
  152. ;; Does this heading INHERIT the drill tag
  153. (member org-drill-question-tag (org-get-tags-at))))
  154. (defun org-drill-entry-leech-p ()
  155. "Is the current entry a 'leech item'?"
  156. (and (org-drill-entry-p)
  157. (member "leech" (org-get-local-tags))))
  158. (defun org-drill-entry-due-p ()
  159. (let ((item-time (org-get-scheduled-time (point))))
  160. (and (org-drill-entry-p)
  161. (or (not (eql 'skip org-drill-leech-method))
  162. (not (org-drill-entry-leech-p)))
  163. (or (null item-time)
  164. (not (minusp ; scheduled for today/in future
  165. (- (time-to-days (current-time))
  166. (time-to-days item-time))))))))
  167. (defun org-drill-entry-new-p ()
  168. (let ((item-time (org-get-scheduled-time (point))))
  169. (and (org-drill-entry-p)
  170. (null item-time))))
  171. (defun org-drill-entry-last-quality ()
  172. (let ((quality (cdr (assoc "DRILL_LAST_QUALITY" (org-entry-properties nil)))))
  173. (if quality
  174. (string-to-number quality)
  175. nil)))
  176. ;;; SM2 Algorithm =============================================================
  177. (defun determine-next-interval-sm2 (last-interval n ef quality of-matrix)
  178. "Arguments:
  179. - LAST-INTERVAL -- the number of days since the item was last reviewed.
  180. - N -- the number of times the item has been successfully reviewed
  181. - EF -- the 'easiness factor'
  182. - QUALITY -- 0 to 5
  183. - OF-MATRIX -- a matrix of values, used by SM5 but not by SM2.
  184. Returns a list: (INTERVAL N EF OFMATRIX), where:
  185. - INTERVAL is the number of days until the item should next be reviewed
  186. - N is incremented by 1.
  187. - EF is modified based on the recall quality for the item.
  188. - OF-MATRIX is not modified."
  189. (assert (> n 0))
  190. (assert (and (>= quality 0) (<= quality 5)))
  191. (if (<= quality org-drill-failure-quality)
  192. ;; When an item is failed, its interval is reset to 0,
  193. ;; but its EF is unchanged
  194. (list -1 1 ef of-matrix)
  195. ;; else:
  196. (let* ((next-ef (modify-e-factor ef quality))
  197. (interval
  198. (cond
  199. ((<= n 1) 1)
  200. ((= n 2)
  201. (cond
  202. (org-drill-add-random-noise-to-intervals-p
  203. (case quality
  204. (5 6)
  205. (4 4)
  206. (3 3)
  207. (2 1)
  208. (t -1)))
  209. (t 6)))
  210. (t (ceiling (* last-interval next-ef))))))
  211. (list (round
  212. (if org-drill-add-random-noise-to-intervals-p
  213. (+ last-interval (* (- interval last-interval)
  214. (org-drill-random-dispersal-factor)))
  215. interval))
  216. (1+ n) next-ef of-matrix))))
  217. ;;; SM5 Algorithm =============================================================
  218. ;;; From http://www.supermemo.com/english/ol/sm5.htm
  219. (defun org-drill-random-dispersal-factor ()
  220. (let ((a 0.047)
  221. (b 0.092)
  222. (p (- (random* 1.0) 0.5)))
  223. (flet ((sign (n)
  224. (cond ((zerop n) 0)
  225. ((plusp n) 1)
  226. (t -1))))
  227. (/ (+ 100 (* (* (/ -1 b) (log (- 1 (* (/ b a ) (abs p)))))
  228. (sign p)))
  229. 100))))
  230. (defun inter-repetition-interval-sm5 (last-interval n ef &optional of-matrix)
  231. (let ((of (get-optimal-factor n ef of-matrix)))
  232. (if (= 1 n)
  233. of
  234. (* of last-interval))))
  235. (defun determine-next-interval-sm5 (last-interval n ef quality of-matrix)
  236. (assert (> n 0))
  237. (assert (and (>= quality 0) (<= quality 5)))
  238. (let ((next-ef (modify-e-factor ef quality))
  239. (interval nil))
  240. (setq of-matrix
  241. (set-optimal-factor n next-ef of-matrix
  242. (modify-of (get-optimal-factor n ef of-matrix)
  243. quality org-learn-fraction))
  244. ef next-ef)
  245. (cond
  246. ;; "Failed" -- reset repetitions to 0,
  247. ((<= quality org-drill-failure-quality)
  248. (list -1 1 ef of-matrix)) ; Not clear if OF matrix is supposed to be
  249. ; preserved
  250. ;; For a zero-based quality of 4 or 5, don't repeat
  251. ((and (>= quality 4)
  252. (not org-learn-always-reschedule))
  253. (list 0 (1+ n) ef of-matrix)) ; 0 interval = unschedule
  254. (t
  255. (setq interval (inter-repetition-interval-sm5
  256. last-interval n ef of-matrix))
  257. (if org-drill-add-random-noise-to-intervals-p
  258. (setq interval (+ last-interval
  259. (* (- interval last-interval)
  260. (org-drill-random-dispersal-factor)))))
  261. (list (round interval) (1+ n) ef of-matrix)))))
  262. ;;; Essentially copied from `org-learn.el', but modified to
  263. ;;; optionally call the SM2 function above.
  264. (defun org-drill-smart-reschedule (quality)
  265. (interactive "nHow well did you remember the information (on a scale of 0-5)? ")
  266. (let* ((learn-str (org-entry-get (point) "LEARN_DATA"))
  267. (learn-data (or (and learn-str
  268. (read learn-str))
  269. (copy-list initial-repetition-state)))
  270. closed-dates)
  271. (setq learn-data
  272. (case org-drill-spaced-repetition-algorithm
  273. (sm5 (determine-next-interval-sm5 (nth 0 learn-data)
  274. (nth 1 learn-data)
  275. (nth 2 learn-data)
  276. quality
  277. (nth 3 learn-data)))
  278. (sm2 (determine-next-interval-sm2 (nth 0 learn-data)
  279. (nth 1 learn-data)
  280. (nth 2 learn-data)
  281. quality
  282. (nth 3 learn-data)))))
  283. (org-entry-put (point) "LEARN_DATA" (prin1-to-string learn-data))
  284. (cond
  285. ((= 0 (nth 0 learn-data))
  286. (org-schedule t))
  287. (t
  288. (org-schedule nil (time-add (current-time)
  289. (days-to-time (nth 0 learn-data))))))))
  290. (defun org-drill-reschedule ()
  291. "Returns quality rating (0-5), or nil if the user quit."
  292. (let ((ch nil))
  293. (while (not (memq ch '(?q ?0 ?1 ?2 ?3 ?4 ?5)))
  294. (setq ch (read-char
  295. (if (eq ch ??)
  296. "0-2 Means you have forgotten the item.
  297. 3-5 Means you have remembered the item.
  298. 0 - Completely forgot.
  299. 1 - Even after seeing the answer, it still took a bit to sink in.
  300. 2 - After seeing the answer, you remembered it.
  301. 3 - It took you awhile, but you finally remembered.
  302. 4 - After a little bit of thought you remembered.
  303. 5 - You remembered the item really easily.
  304. How well did you do? (0-5, ?=help, q=quit)"
  305. "How well did you do? (0-5, ?=help, q=quit)"))))
  306. (cond
  307. ((and (>= ch ?0) (<= ch ?5))
  308. (let ((quality (- ch ?0))
  309. (failures (cdr (assoc "DRILL_FAILURE_COUNT" (org-entry-properties nil)))))
  310. (save-excursion
  311. (org-drill-smart-reschedule quality))
  312. (push quality *org-drill-session-qualities*)
  313. (cond
  314. ((<= quality org-drill-failure-quality)
  315. (when org-drill-leech-failure-threshold
  316. (setq failures (if failures (string-to-number failures) 0))
  317. (org-set-property "DRILL_FAILURE_COUNT"
  318. (format "%d" (1+ failures)))
  319. (if (> (1+ failures) org-drill-leech-failure-threshold)
  320. (org-toggle-tag "leech" 'on)))))
  321. (org-set-property "DRILL_LAST_QUALITY" (format "%d" quality))
  322. quality))
  323. (t
  324. nil))))
  325. (defun org-drill-hide-all-subheadings-except (heading-list)
  326. "Returns a list containing the position of each immediate subheading of
  327. the current topic."
  328. (let ((drill-entry-level (org-current-level))
  329. (drill-sections nil)
  330. (drill-heading nil))
  331. (org-show-subtree)
  332. (save-excursion
  333. (org-map-entries
  334. (lambda ()
  335. (when (= (org-current-level) (1+ drill-entry-level))
  336. (setq drill-heading (org-get-heading t))
  337. (unless (member drill-heading heading-list)
  338. (hide-subtree))
  339. (push (point) drill-sections)))
  340. "" 'tree))
  341. (reverse drill-sections)))
  342. (defun org-drill-presentation-prompt (&rest fmt-and-args)
  343. (let ((ch nil)
  344. (prompt
  345. (if fmt-and-args
  346. (apply 'format
  347. (first fmt-and-args)
  348. (rest fmt-and-args))
  349. "Press any key to see the answer, 'e' to edit, 'q' to quit.")))
  350. (setq prompt
  351. (format "(%d) %s" *org-drill-pending-entry-count* prompt))
  352. (if (and (eql 'warn org-drill-leech-method)
  353. (org-drill-entry-leech-p))
  354. (setq prompt (concat "!!! LEECH ITEM !!!
  355. You seem to be having a lot of trouble memorising this item.
  356. Consider reformulating the item to make it easier to remember.\n" prompt)))
  357. (setq ch (read-char prompt))
  358. (case ch
  359. (?q nil)
  360. (?e 'edit)
  361. (otherwise t))))
  362. (defun org-drill-hide-clozed-text ()
  363. (let ((ovl nil))
  364. (save-excursion
  365. (while (re-search-forward org-drill-cloze-regexp nil t)
  366. (setf ovl (make-overlay (match-beginning 0) (match-end 0)))
  367. (overlay-put ovl 'category
  368. 'org-drill-cloze-overlay-defaults)
  369. (when (find ?| (match-string 0))
  370. (overlay-put ovl
  371. 'display
  372. (format "[...%s]"
  373. (substring-no-properties
  374. (match-string 0)
  375. (1+ (position ?| (match-string 0)))
  376. (1- (length (match-string 0)))))))))))
  377. (defun org-drill-unhide-clozed-text ()
  378. (save-excursion
  379. (dolist (ovl (overlays-in (point-min) (point-max)))
  380. (when (eql 'org-drill-cloze-overlay-defaults (overlay-get ovl 'category))
  381. (delete-overlay ovl)))))
  382. ;;; Presentation functions ====================================================
  383. ;; Each of these is called with point on topic heading. Each needs to show the
  384. ;; topic in the form of a 'question' or with some information 'hidden', as
  385. ;; appropriate for the card type. The user should then be prompted to press a
  386. ;; key. The function should then reveal either the 'answer' or the entire
  387. ;; topic, and should return t if the user chose to see the answer and rate their
  388. ;; recall, nil if they chose to quit.
  389. (defun org-drill-present-simple-card ()
  390. (org-drill-hide-all-subheadings-except nil)
  391. (prog1 (org-drill-presentation-prompt)
  392. (org-show-subtree)))
  393. (defun org-drill-present-two-sided-card ()
  394. (let ((drill-sections (org-drill-hide-all-subheadings-except nil)))
  395. (when drill-sections
  396. (save-excursion
  397. (goto-char (nth (random (min 2 (length drill-sections))) drill-sections))
  398. (org-show-subtree)))
  399. (prog1
  400. (org-drill-presentation-prompt)
  401. (org-show-subtree))))
  402. (defun org-drill-present-multi-sided-card ()
  403. (let ((drill-sections (org-drill-hide-all-subheadings-except nil)))
  404. (when drill-sections
  405. (save-excursion
  406. (goto-char (nth (random (length drill-sections)) drill-sections))
  407. (org-show-subtree)))
  408. (prog1
  409. (org-drill-presentation-prompt)
  410. (org-show-subtree))))
  411. (defun org-drill-present-spanish-verb ()
  412. (case (random 6)
  413. (0
  414. (org-drill-hide-all-subheadings-except '("Infinitive"))
  415. (prog1
  416. (org-drill-presentation-prompt
  417. "Translate this Spanish verb, and conjugate it for the *present* tense.")
  418. (org-drill-hide-all-subheadings-except '("English" "Present Tense"
  419. "Notes"))))
  420. (1
  421. (org-drill-hide-all-subheadings-except '("English"))
  422. (prog1
  423. (org-drill-presentation-prompt
  424. "For the *present* tense, conjugate the Spanish translation of this English verb.")
  425. (org-drill-hide-all-subheadings-except '("Infinitive" "Present Tense"
  426. "Notes"))))
  427. (2
  428. (org-drill-hide-all-subheadings-except '("Infinitive"))
  429. (prog1
  430. (org-drill-presentation-prompt
  431. "Translate this Spanish verb, and conjugate it for the *past* tense.")
  432. (org-drill-hide-all-subheadings-except '("English" "Past Tense"
  433. "Notes"))))
  434. (3
  435. (org-drill-hide-all-subheadings-except '("English"))
  436. (prog1
  437. (org-drill-presentation-prompt
  438. "For the *past* tense, conjugate the Spanish translation of this English verb.")
  439. (org-drill-hide-all-subheadings-except '("Infinitive" "Past Tense"
  440. "Notes"))))
  441. (4
  442. (org-drill-hide-all-subheadings-except '("Infinitive"))
  443. (prog1
  444. (org-drill-presentation-prompt
  445. "Translate this Spanish verb, and conjugate it for the *future perfect* tense.")
  446. (org-drill-hide-all-subheadings-except '("English" "Future Perfect Tense"
  447. "Notes"))))
  448. (5
  449. (org-drill-hide-all-subheadings-except '("English"))
  450. (prog1
  451. (org-drill-presentation-prompt
  452. "For the *future perfect* tense, conjugate the Spanish translation of this English verb.")
  453. (org-drill-hide-all-subheadings-except '("Infinitive" "Future Perfect Tense"
  454. "Notes"))))))
  455. (defun org-drill-entry ()
  456. "Present the current topic for interactive review, as in `org-drill'.
  457. Review will occur regardless of whether the topic is due for review or whether
  458. it meets the definition of a 'review topic' used by `org-drill'.
  459. Returns a quality rating from 0 to 5, or nil if the user quit, or the symbol
  460. EDIT if the user chose to exit the drill and edit the current item.
  461. See `org-drill' for more details."
  462. (interactive)
  463. (unless (org-at-heading-p)
  464. (org-back-to-heading))
  465. (let ((card-type (cdr (assoc "DRILL_CARD_TYPE" (org-entry-properties nil))))
  466. (cont nil))
  467. (save-restriction
  468. (org-narrow-to-subtree)
  469. (org-show-subtree)
  470. (org-cycle-hide-drawers 'all)
  471. (let ((presentation-fn (cdr (assoc card-type org-drill-card-type-alist))))
  472. (cond
  473. (presentation-fn
  474. (org-drill-hide-clozed-text)
  475. ;;(highlight-regexp org-drill-cloze-regexp
  476. ;; 'org-drill-hidden-cloze-face)
  477. (unwind-protect
  478. (progn
  479. (setq cont (funcall presentation-fn)))
  480. (org-drill-unhide-clozed-text))
  481. ;;(unhighlight-regexp org-drill-cloze-regexp)
  482. )
  483. (t
  484. (error "Unknown card type: '%s'" card-type))))
  485. (cond
  486. ((not cont)
  487. (message "Quit")
  488. nil)
  489. ((eql cont 'edit)
  490. 'edit)
  491. (t
  492. (save-excursion
  493. (org-drill-reschedule)))))))
  494. (defun org-drill-entries (entries)
  495. "Returns nil, t, or a list of markers representing entries that were
  496. 'failed' and need to be presented again before the session ends."
  497. (let ((again-entries nil)
  498. (*org-drill-done-entry-count* 0)
  499. (*org-drill-pending-entry-count* (length entries)))
  500. (if (and org-drill-maximum-items-per-session
  501. (> (length entries)
  502. org-drill-maximum-items-per-session))
  503. (setq entries (subseq entries 0
  504. org-drill-maximum-items-per-session)))
  505. (block org-drill-entries
  506. (dolist (m entries)
  507. (save-restriction
  508. (switch-to-buffer (marker-buffer m))
  509. (goto-char (marker-position m))
  510. (setq result (org-drill-entry))
  511. (cond
  512. ((null result)
  513. (message "Quit")
  514. (return-from org-drill-entries nil))
  515. ((eql result 'edit)
  516. (setq end-pos (point-marker))
  517. (return-from org-drill-entries nil))
  518. (t
  519. (cond
  520. ((< result 3)
  521. (push m again-entries))
  522. (t
  523. (decf *org-drill-pending-entry-count*)
  524. (incf *org-drill-done-entry-count*)))
  525. (when (and org-drill-maximum-duration
  526. (> (- (float-time (current-time)) *org-drill-start-time*)
  527. (* org-drill-maximum-duration 60)))
  528. (message "This drill session has reached its maximum duration.")
  529. (return-from org-drill-entries nil))))))
  530. (or again-entries
  531. t))))
  532. (defun org-drill-final-report ()
  533. (read-char
  534. (format
  535. "%d items reviewed, %d items awaiting review
  536. Session duration %s
  537. Recall of reviewed items:
  538. Excellent (5): %3d%%
  539. Good (4): %3d%%
  540. Hard (3): %3d%%
  541. Near miss (2): %3d%%
  542. Failure (1): %3d%%
  543. Total failure (0): %3d%%
  544. Session finished. Press a key to continue..."
  545. *org-drill-done-entry-count*
  546. *org-drill-pending-entry-count*
  547. (format-seconds "%h:%.2m:%.2s"
  548. (- (float-time (current-time)) *org-drill-start-time*))
  549. (round (* 100 (count 5 *org-drill-session-qualities*))
  550. (max 1 (length *org-drill-session-qualities*)))
  551. (round (* 100 (count 4 *org-drill-session-qualities*))
  552. (max 1 (length *org-drill-session-qualities*)))
  553. (round (* 100 (count 3 *org-drill-session-qualities*))
  554. (max 1 (length *org-drill-session-qualities*)))
  555. (round (* 100 (count 2 *org-drill-session-qualities*))
  556. (max 1 (length *org-drill-session-qualities*)))
  557. (round (* 100 (count 1 *org-drill-session-qualities*))
  558. (max 1 (length *org-drill-session-qualities*)))
  559. (round (* 100 (count 0 *org-drill-session-qualities*))
  560. (max 1 (length *org-drill-session-qualities*)))
  561. )))
  562. (defun org-drill (&optional scope)
  563. "Begin an interactive 'drill session'. The user is asked to
  564. review a series of topics (headers). Each topic is initially
  565. presented as a 'question', often with part of the topic content
  566. hidden. The user attempts to recall the hidden information or
  567. answer the question, then presses a key to reveal the answer. The
  568. user then rates his or her recall or performance on that
  569. topic. This rating information is used to reschedule the topic
  570. for future review using the `org-learn' library.
  571. Org-drill proceeds by:
  572. - Finding all topics (headings) in SCOPE which have either been
  573. used and rescheduled by org-learn before (i.e. the LEARN_DATA
  574. property is set), or which have a tag that matches
  575. `org-drill-question-tag'.
  576. - All matching topics which are either unscheduled, or are
  577. scheduled for the current date or a date in the past, are
  578. considered to be candidates for the drill session.
  579. - If `org-drill-maximum-items-per-session' is set, a random
  580. subset of these topics is presented. Otherwise, all of the
  581. eligible topics will be presented.
  582. SCOPE determines the scope in which to search for
  583. questions. It is passed to `org-map-entries', and can be any of:
  584. nil The current buffer, respecting the restriction if any.
  585. This is the default.
  586. tree The subtree started with the entry at point
  587. file The current buffer, without restriction
  588. file-with-archives
  589. The current buffer, and any archives associated with it
  590. agenda All agenda files
  591. agenda-with-archives
  592. All agenda files with any archive files associated with them
  593. (file1 file2 ...)
  594. If this is a list, all files in the list will be scanned."
  595. (interactive)
  596. (let ((entries nil)
  597. (failed-entries nil)
  598. (new-entries nil)
  599. (old-entries nil)
  600. (result nil)
  601. (results nil)
  602. (end-pos nil))
  603. (block org-drill
  604. (setq *org-drill-session-qualities* nil)
  605. (setq *org-drill-start-time* (float-time (current-time)))
  606. (save-excursion
  607. (org-map-entries
  608. (lambda () (when (org-drill-entry-due-p)
  609. (cond
  610. ((org-drill-entry-new-p)
  611. (push (point-marker) new-entries))
  612. ((<= (org-drill-entry-last-quality)
  613. org-drill-failure-quality)
  614. (push (point-marker) failed-entries))
  615. (t
  616. (push (point-marker) old-entries)))))
  617. "" scope)
  618. ;; Failed first, then random mix of old + new
  619. (setq entries (append (shuffle-list failed-entries)
  620. (shuffle-list (append old-entries
  621. new-entries))))
  622. (cond
  623. ((null entries)
  624. (message "I did not find any pending drill items."))
  625. (t
  626. (let ((again t))
  627. (while again
  628. (when (listp again)
  629. (setq entries (shuffle-list again)))
  630. (setq again (org-drill-entries entries))
  631. (cond
  632. ((null again)
  633. (return-from org-drill nil))
  634. ((eql t again)
  635. (setq again nil))))
  636. (message "Drill session finished!")
  637. )))))
  638. (cond
  639. (end-pos
  640. (switch-to-buffer (marker-buffer end-pos))
  641. (goto-char (marker-position end-pos))
  642. (message "Edit topic."))
  643. (t
  644. (org-drill-final-report)))))
  645. (add-hook 'org-mode-hook
  646. (lambda ()
  647. (if org-drill-use-visible-cloze-face-p
  648. (font-lock-add-keywords
  649. 'org-mode
  650. `((,org-drill-cloze-regexp
  651. (0 'org-drill-visible-cloze-face nil)))
  652. t))))
  653. (provide 'org-drill)