org-element: Fix cache error on empty drawers
[org-mode/org-tableheadings.git] / contrib / lisp / org-drill.el
blobe5b0d49780878a1321be91e835bfcba8e056f606
1 ;;; -*- coding: utf-8-unix -*-
2 ;;; org-drill.el - Self-testing using spaced repetition
3 ;;;
4 ;;; Author: Paul Sexton <eeeickythump@gmail.com>
5 ;;; Version: 2.4.1
6 ;;; Repository at http://bitbucket.org/eeeickythump/org-drill/
7 ;;;
8 ;;;
9 ;;; Synopsis
10 ;;; ========
11 ;;;
12 ;;; Uses the SuperMemo spaced repetition algorithms to conduct interactive
13 ;;; "drill sessions", where the material to be remembered is presented to the
14 ;;; student in random order. The student rates his or her recall of each item,
15 ;;; and this information is used to schedule the item for 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.
27 (eval-when-compile (require 'cl))
28 (eval-when-compile (require 'hi-lock))
29 (require 'cl-lib)
30 (require 'hi-lock)
31 (require 'org)
32 (require 'org-id)
33 (require 'org-learn)
36 (defgroup org-drill nil
37 "Options concerning interactive drill sessions in Org mode (org-drill)."
38 :tag "Org-Drill"
39 :group 'org-link)
43 (defcustom org-drill-question-tag
44 "drill"
45 "Tag which topics must possess in order to be identified as review topics
46 by `org-drill'."
47 :group 'org-drill
48 :type 'string)
51 (defcustom org-drill-maximum-items-per-session
53 "Each drill session will present at most this many topics for review.
54 Nil means unlimited."
55 :group 'org-drill
56 :type '(choice integer (const nil)))
60 (defcustom org-drill-maximum-duration
62 "Maximum duration of a drill session, in minutes.
63 Nil means unlimited."
64 :group 'org-drill
65 :type '(choice integer (const nil)))
68 (defcustom org-drill-failure-quality
70 "If the quality of recall for an item is this number or lower,
71 it is regarded as an unambiguous failure, and the repetition
72 interval for the card is reset to 0 days. If the quality is higher
73 than this number, it is regarded as successfully recalled, but the
74 time interval to the next repetition will be lowered if the quality
75 was near to a fail.
77 By default this is 2, for SuperMemo-like behaviour. For
78 Mnemosyne-like behaviour, set it to 1. Other values are not
79 really sensible."
80 :group 'org-drill
81 :type '(choice (const 2) (const 1)))
84 (defcustom org-drill-forgetting-index
86 "What percentage of items do you consider it is 'acceptable' to
87 forget each drill session? The default is 10%. A warning message
88 is displayed at the end of the session if the percentage forgotten
89 climbs above this number."
90 :group 'org-drill
91 :type 'integer)
94 (defcustom org-drill-leech-failure-threshold
96 "If an item is forgotten more than this many times, it is tagged
97 as a 'leech' item."
98 :group 'org-drill
99 :type '(choice integer (const nil)))
102 (defcustom org-drill-leech-method
103 'skip
104 "How should 'leech items' be handled during drill sessions?
105 Possible values:
106 - nil :: Leech items are treated the same as normal items.
107 - skip :: Leech items are not included in drill sessions.
108 - warn :: Leech items are still included in drill sessions,
109 but a warning message is printed when each leech item is
110 presented."
111 :group 'org-drill
112 :type '(choice (const warn) (const skip) (const nil)))
115 (defface org-drill-visible-cloze-face
116 '((t (:foreground "darkseagreen")))
117 "The face used to hide the contents of cloze phrases."
118 :group 'org-drill)
121 (defface org-drill-visible-cloze-hint-face
122 '((t (:foreground "dark slate blue")))
123 "The face used to hide the contents of cloze phrases."
124 :group 'org-drill)
127 (defface org-drill-hidden-cloze-face
128 '((t (:foreground "deep sky blue" :background "blue")))
129 "The face used to hide the contents of cloze phrases."
130 :group 'org-drill)
133 (defcustom org-drill-use-visible-cloze-face-p
135 "Use a special face to highlight cloze-deleted text in org mode
136 buffers?"
137 :group 'org-drill
138 :type 'boolean)
141 (defcustom org-drill-hide-item-headings-p
143 "Conceal the contents of the main heading of each item during drill
144 sessions? You may want to enable this behaviour if item headings or tags
145 contain information that could 'give away' the answer."
146 :group 'org-drill
147 :type 'boolean)
150 (defcustom org-drill-new-count-color
151 "royal blue"
152 "Foreground colour used to display the count of remaining new items
153 during a drill session."
154 :group 'org-drill
155 :type 'color)
157 (defcustom org-drill-mature-count-color
158 "green"
159 "Foreground colour used to display the count of remaining mature items
160 during a drill session. Mature items are due for review, but are not new."
161 :group 'org-drill
162 :type 'color)
164 (defcustom org-drill-failed-count-color
165 "red"
166 "Foreground colour used to display the count of remaining failed items
167 during a drill session."
168 :group 'org-drill
169 :type 'color)
171 (defcustom org-drill-done-count-color
172 "sienna"
173 "Foreground colour used to display the count of reviewed items
174 during a drill session."
175 :group 'org-drill
176 :type 'color)
178 (defcustom org-drill-left-cloze-delimiter
180 "String used within org buffers to delimit cloze deletions."
181 :group 'org-drill
182 :type 'string)
184 (defcustom org-drill-right-cloze-delimiter
186 "String used within org buffers to delimit cloze deletions."
187 :group 'org-drill
188 :type 'string)
191 (setplist 'org-drill-cloze-overlay-defaults
192 `(display ,(format "%s...%s"
193 org-drill-left-cloze-delimiter
194 org-drill-right-cloze-delimiter)
195 face org-drill-hidden-cloze-face
196 window t))
198 (setplist 'org-drill-hidden-text-overlay
199 '(invisible t))
201 (setplist 'org-drill-replaced-text-overlay
202 '(display "Replaced text"
203 face default
204 window t))
207 (defvar org-drill-hint-separator "||"
208 "String which, if it occurs within a cloze expression, signifies that the
209 rest of the expression after the string is a `hint', to be displayed instead of
210 the hidden cloze during a test.")
212 (defun org-drill--compute-cloze-regexp ()
213 (concat "\\("
214 (regexp-quote org-drill-left-cloze-delimiter)
215 "[[:cntrl:][:graph:][:space:]]+?\\)\\(\\|"
216 (regexp-quote org-drill-hint-separator)
217 ".+?\\)\\("
218 (regexp-quote org-drill-right-cloze-delimiter)
219 "\\)"))
221 (defun org-drill--compute-cloze-keywords ()
222 (list (list (org-drill--compute-cloze-regexp)
223 (copy-list '(1 'org-drill-visible-cloze-face nil))
224 (copy-list '(2 'org-drill-visible-cloze-hint-face t))
225 (copy-list '(3 'org-drill-visible-cloze-face nil))
228 (defvar-local org-drill-cloze-regexp
229 (org-drill--compute-cloze-regexp))
232 (defvar-local org-drill-cloze-keywords
233 (org-drill--compute-cloze-keywords))
236 (defcustom org-drill-card-type-alist
237 '((nil org-drill-present-simple-card)
238 ("simple" org-drill-present-simple-card)
239 ("twosided" org-drill-present-two-sided-card nil t)
240 ("multisided" org-drill-present-multi-sided-card nil t)
241 ("hide1cloze" org-drill-present-multicloze-hide1)
242 ("hide2cloze" org-drill-present-multicloze-hide2)
243 ("show1cloze" org-drill-present-multicloze-show1)
244 ("show2cloze" org-drill-present-multicloze-show2)
245 ("multicloze" org-drill-present-multicloze-hide1)
246 ("hidefirst" org-drill-present-multicloze-hide-first)
247 ("hidelast" org-drill-present-multicloze-hide-last)
248 ("hide1_firstmore" org-drill-present-multicloze-hide1-firstmore)
249 ("show1_lastmore" org-drill-present-multicloze-show1-lastmore)
250 ("show1_firstless" org-drill-present-multicloze-show1-firstless)
251 ("conjugate"
252 org-drill-present-verb-conjugation
253 org-drill-show-answer-verb-conjugation)
254 ("decline_noun"
255 org-drill-present-noun-declension
256 org-drill-show-answer-noun-declension)
257 ("spanish_verb" org-drill-present-spanish-verb)
258 ("translate_number" org-drill-present-translate-number))
259 "Alist associating card types with presentation functions. Each
260 entry in the alist takes the form:
262 ;;; (CARDTYPE QUESTION-FN [ANSWER-FN DRILL-EMPTY-P])
264 Where CARDTYPE is a string or nil (for default), and QUESTION-FN
265 is a function which takes no arguments and returns a boolean
266 value.
268 When supplied, ANSWER-FN is a function that takes one argument --
269 that argument is a function of no arguments, which when called,
270 prompts the user to rate their recall and performs rescheduling
271 of the drill item. ANSWER-FN is called with the point on the
272 active item's heading, just prior to displaying the item's
273 'answer'. It can therefore be used to modify the appearance of
274 the answer. ANSWER-FN must call its argument before returning.
276 When supplied, DRILL-EMPTY-P is a boolean value, default nil.
277 When non-nil, cards of this type will be presented during tests
278 even if their bodies are empty."
279 :group 'org-drill
280 :type '(alist :key-type (choice string (const nil))
281 :value-type function))
284 (defcustom org-drill-scope
285 'file
286 "The scope in which to search for drill items when conducting a
287 drill session. This can be any of:
289 file The current buffer, respecting the restriction if any.
290 This is the default.
291 tree The subtree started with the entry at point
292 file-no-restriction The current buffer, without restriction
293 file-with-archives The current buffer, and any archives associated with it.
294 agenda All agenda files
295 agenda-with-archives All agenda files with any archive files associated
296 with them.
297 directory All files with the extension '.org' in the same
298 directory as the current file (includes the current
299 file if it is an .org file.)
300 (FILE1 FILE2 ...) If this is a list, all files in the list will be scanned.
302 ;; Note -- meanings differ slightly from the argument to org-map-entries:
303 ;; 'file' means current file/buffer, respecting any restriction
304 ;; 'file-no-restriction' means current file/buffer, ignoring restrictions
305 ;; 'directory' means all *.org files in current directory
306 :group 'org-drill
307 :type '(choice (const :tag "The current buffer, respecting the restriction if any." file)
308 (const :tag "The subtree started with the entry at point" tree)
309 (const :tag "The current buffer, without restriction" file-no-restriction)
310 (const :tag "The current buffer, and any archives associated with it." file-with-archives)
311 (const :tag "All agenda files" agenda)
312 (const :tag "All agenda files with any archive files associated with them." agenda-with-archives)
313 (const :tag "All files with the extension '.org' in the same directory as the current file (includes the current file if it is an .org file.)" directory)
314 (repeat :tag "List of files to scan for drill items." file)))
317 (defcustom org-drill-match
319 "If non-nil, a string specifying a tags/property/TODO query. During
320 drill sessions, only items that match this query will be considered."
321 :group 'org-drill
322 :type '(choice (const nil) string))
325 (defcustom org-drill-save-buffers-after-drill-sessions-p
327 "If non-nil, prompt to save all modified buffers after a drill session
328 finishes."
329 :group 'org-drill
330 :type 'boolean)
333 (defcustom org-drill-spaced-repetition-algorithm
334 'sm5
335 "Which SuperMemo spaced repetition algorithm to use for scheduling items.
336 Available choices are:
337 - SM2 :: the SM2 algorithm, used in SuperMemo 2.0
338 - SM5 :: the SM5 algorithm, used in SuperMemo 5.0
339 - Simple8 :: a modified version of the SM8 algorithm. SM8 is used in
340 SuperMemo 98. The version implemented here is simplified in that while it
341 'learns' the difficulty of each item using quality grades and number of
342 failures, it does not modify the matrix of values that
343 governs how fast the inter-repetition intervals increase. A method for
344 adjusting intervals when items are reviewed early or late has been taken
345 from SM11, a later version of the algorithm, and included in Simple8."
346 :group 'org-drill
347 :type '(choice (const sm2) (const sm5) (const simple8)))
350 (defcustom org-drill-optimal-factor-matrix
352 "DO NOT CHANGE THE VALUE OF THIS VARIABLE.
354 Persistent matrix of optimal factors, used by the SuperMemo SM5 algorithm.
355 The matrix is saved (using the 'customize' facility) at the end of each
356 drill session.
358 Over time, values in the matrix will adapt to the individual user's
359 pace of learning."
360 :group 'org-drill
361 :type 'sexp)
364 (defcustom org-drill-sm5-initial-interval
366 "In the SM5 algorithm, the initial interval after the first
367 successful presentation of an item is always 4 days. If you wish to change
368 this, you can do so here."
369 :group 'org-drill
370 :type 'float)
373 (defcustom org-drill-add-random-noise-to-intervals-p
375 "If true, the number of days until an item's next repetition
376 will vary slightly from the interval calculated by the SM2
377 algorithm. The variation is very small when the interval is
378 small, but scales up with the interval."
379 :group 'org-drill
380 :type 'boolean)
383 (defcustom org-drill-adjust-intervals-for-early-and-late-repetitions-p
385 "If true, when the student successfully reviews an item 1 or more days
386 before or after the scheduled review date, this will affect that date of
387 the item's next scheduled review, according to the algorithm presented at
388 [[http://www.supermemo.com/english/algsm11.htm#Advanced%20repetitions]].
390 Items that were reviewed early will have their next review date brought
391 forward. Those that were reviewed late will have their next review
392 date postponed further.
394 Note that this option currently has no effect if the SM2 algorithm
395 is used."
396 :group 'org-drill
397 :type 'boolean)
400 (defcustom org-drill-cloze-text-weight
402 "For card types 'hide1_firstmore', 'show1_lastmore' and 'show1_firstless',
403 this number determines how often the 'less favoured' situation
404 should arise. It will occur 1 in every N trials, where N is the
405 value of the variable.
407 For example, with the hide1_firstmore card type, the first piece
408 of clozed text should be hidden more often than the other
409 pieces. If this variable is set to 4 (default), the first item
410 will only be shown 25% of the time (1 in 4 trials). Similarly for
411 show1_lastmore, the last item will be shown 75% of the time, and
412 for show1_firstless, the first item would only be shown 25% of the
413 time.
415 If the value of this variable is NIL, then weighting is disabled, and
416 all weighted card types are treated as their unweighted equivalents."
417 :group 'org-drill
418 :type '(choice integer (const nil)))
421 (defcustom org-drill-cram-hours
423 "When in cram mode, items are considered due for review if
424 they were reviewed at least this many hours ago."
425 :group 'org-drill
426 :type 'integer)
429 ;;; NEW items have never been presented in a drill session before.
430 ;;; MATURE items HAVE been presented at least once before.
431 ;;; - YOUNG mature items were scheduled no more than
432 ;;; ORG-DRILL-DAYS-BEFORE-OLD days after their last
433 ;;; repetition. These items will have been learned 'recently' and will have a
434 ;;; low repetition count.
435 ;;; - OLD mature items have intervals greater than
436 ;;; ORG-DRILL-DAYS-BEFORE-OLD.
437 ;;; - OVERDUE items are past their scheduled review date by more than
438 ;;; LAST-INTERVAL * (ORG-DRILL-OVERDUE-INTERVAL-FACTOR - 1) days,
439 ;;; regardless of young/old status.
442 (defcustom org-drill-days-before-old
444 "When an item's inter-repetition interval rises above this value in days,
445 it is no longer considered a 'young' (recently learned) item."
446 :group 'org-drill
447 :type 'integer)
450 (defcustom org-drill-overdue-interval-factor
452 "An item is considered overdue if its scheduled review date is
453 more than (ORG-DRILL-OVERDUE-INTERVAL-FACTOR - 1) * LAST-INTERVAL
454 days in the past. For example, a value of 1.2 means an additional
455 20% of the last scheduled interval is allowed to elapse before
456 the item is overdue. A value of 1.0 means no extra time is
457 allowed at all - items are immediately considered overdue if
458 there is even one day's delay in reviewing them. This variable
459 should never be less than 1.0."
460 :group 'org-drill
461 :type 'float)
464 (defcustom org-drill-learn-fraction
466 "Fraction between 0 and 1 that governs how quickly the spaces
467 between successive repetitions increase, for all items. The
468 default value is 0.5. Higher values make spaces increase more
469 quickly with each successful repetition. You should only change
470 this in small increments (for example 0.05-0.1) as it has an
471 exponential effect on inter-repetition spacing."
472 :group 'org-drill
473 :type 'float)
476 (defvar drill-answer nil
477 "Global variable that can be bound to a correct answer when an
478 item is being presented. If this variable is non-nil, the default
479 presentation function will show its value instead of the default
480 behaviour of revealing the contents of the drilled item.
482 This variable is useful for card types that compute their answers
483 -- for example, a card type that asks the student to translate a
484 random number to another language. ")
487 (defvar *org-drill-session-qualities* nil)
488 (defvar *org-drill-start-time* 0)
489 (defvar *org-drill-new-entries* nil)
490 (defvar *org-drill-dormant-entry-count* 0)
491 (defvar *org-drill-due-entry-count* 0)
492 (defvar *org-drill-overdue-entry-count* 0)
493 (defvar *org-drill-due-tomorrow-count* 0)
494 (defvar *org-drill-overdue-entries* nil
495 "List of markers for items that are considered 'overdue', based on
496 the value of ORG-DRILL-OVERDUE-INTERVAL-FACTOR.")
497 (defvar *org-drill-young-mature-entries* nil
498 "List of markers for mature entries whose last inter-repetition
499 interval was <= ORG-DRILL-DAYS-BEFORE-OLD days.")
500 (defvar *org-drill-old-mature-entries* nil
501 "List of markers for mature entries whose last inter-repetition
502 interval was greater than ORG-DRILL-DAYS-BEFORE-OLD days.")
503 (defvar *org-drill-failed-entries* nil)
504 (defvar *org-drill-again-entries* nil)
505 (defvar *org-drill-done-entries* nil)
506 (defvar *org-drill-current-item* nil
507 "Set to the marker for the item currently being tested.")
508 (defvar *org-drill-cram-mode* nil
509 "Are we in 'cram mode', where all items are considered due
510 for review unless they were already reviewed in the recent past?")
511 (defvar org-drill-scheduling-properties
512 '("LEARN_DATA" "DRILL_LAST_INTERVAL" "DRILL_REPEATS_SINCE_FAIL"
513 "DRILL_TOTAL_REPEATS" "DRILL_FAILURE_COUNT" "DRILL_AVERAGE_QUALITY"
514 "DRILL_EASE" "DRILL_LAST_QUALITY" "DRILL_LAST_REVIEWED"))
517 ;;; Make the above settings safe as file-local variables.
520 (put 'org-drill-question-tag 'safe-local-variable 'stringp)
521 (put 'org-drill-maximum-items-per-session 'safe-local-variable
522 '(lambda (val) (or (integerp val) (null val))))
523 (put 'org-drill-maximum-duration 'safe-local-variable
524 '(lambda (val) (or (integerp val) (null val))))
525 (put 'org-drill-failure-quality 'safe-local-variable 'integerp)
526 (put 'org-drill-forgetting-index 'safe-local-variable 'integerp)
527 (put 'org-drill-leech-failure-threshold 'safe-local-variable 'integerp)
528 (put 'org-drill-leech-method 'safe-local-variable
529 '(lambda (val) (memq val '(nil skip warn))))
530 (put 'org-drill-use-visible-cloze-face-p 'safe-local-variable 'booleanp)
531 (put 'org-drill-hide-item-headings-p 'safe-local-variable 'booleanp)
532 (put 'org-drill-spaced-repetition-algorithm 'safe-local-variable
533 '(lambda (val) (memq val '(simple8 sm5 sm2))))
534 (put 'org-drill-sm5-initial-interval 'safe-local-variable 'floatp)
535 (put 'org-drill-add-random-noise-to-intervals-p 'safe-local-variable 'booleanp)
536 (put 'org-drill-adjust-intervals-for-early-and-late-repetitions-p
537 'safe-local-variable 'booleanp)
538 (put 'org-drill-cram-hours 'safe-local-variable 'integerp)
539 (put 'org-drill-learn-fraction 'safe-local-variable 'floatp)
540 (put 'org-drill-days-before-old 'safe-local-variable 'integerp)
541 (put 'org-drill-overdue-interval-factor 'safe-local-variable 'floatp)
542 (put 'org-drill-scope 'safe-local-variable
543 '(lambda (val) (or (symbolp val) (listp val))))
544 (put 'org-drill-match 'safe-local-variable
545 '(lambda (val) (or (stringp val) (null val))))
546 (put 'org-drill-save-buffers-after-drill-sessions-p 'safe-local-variable 'booleanp)
547 (put 'org-drill-cloze-text-weight 'safe-local-variable
548 '(lambda (val) (or (null val) (integerp val))))
549 (put 'org-drill-left-cloze-delimiter 'safe-local-variable 'stringp)
550 (put 'org-drill-right-cloze-delimiter 'safe-local-variable 'stringp)
553 ;;;; Utilities ================================================================
556 (defun free-marker (m)
557 (set-marker m nil))
560 (defmacro pop-random (place)
561 (let ((idx (gensym)))
562 `(if (null ,place)
564 (let ((,idx (random* (length ,place))))
565 (prog1 (nth ,idx ,place)
566 (setq ,place (append (subseq ,place 0 ,idx)
567 (subseq ,place (1+ ,idx)))))))))
570 (defmacro push-end (val place)
571 "Add VAL to the end of the sequence stored in PLACE. Return the new
572 value."
573 `(setq ,place (append ,place (list ,val))))
576 (defun shuffle-list (list)
577 "Randomly permute the elements of LIST (all permutations equally likely)."
578 ;; Adapted from 'shuffle-vector' in cookie1.el
579 (let ((i 0)
581 temp
582 (len (length list)))
583 (while (< i len)
584 (setq j (+ i (random* (- len i))))
585 (setq temp (nth i list))
586 (setf (nth i list) (nth j list))
587 (setf (nth j list) temp)
588 (setq i (1+ i))))
589 list)
592 (defun round-float (floatnum fix)
593 "Round the floating point number FLOATNUM to FIX decimal places.
594 Example: (round-float 3.56755765 3) -> 3.568"
595 (let ((n (expt 10 fix)))
596 (/ (float (round (* floatnum n))) n)))
599 (defun command-keybinding-to-string (cmd)
600 "Return a human-readable description of the key/keys to which the command
601 CMD is bound, or nil if it is not bound to a key."
602 (let ((key (where-is-internal cmd overriding-local-map t)))
603 (if key (key-description key))))
606 (defun time-to-inactive-org-timestamp (time)
607 (format-time-string
608 (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
609 time))
612 (defun time-to-active-org-timestamp (time)
613 (format-time-string
614 (concat "<" (substring (cdr org-time-stamp-formats) 1 -1) ">")
615 time))
618 (defun org-map-drill-entries (func &optional scope drill-match &rest skip)
619 "Like `org-map-entries', but only drill entries are processed."
620 (let ((org-drill-scope (or scope org-drill-scope))
621 (org-drill-match (or drill-match org-drill-match)))
622 (apply 'org-map-entries func
623 (concat "+" org-drill-question-tag
624 (if (and (stringp org-drill-match)
625 (not (member '(?+ ?- ?|) (elt org-drill-match 0))))
626 "+" "")
627 (or org-drill-match ""))
628 (case org-drill-scope
629 (file nil)
630 (file-no-restriction 'file)
631 (directory
632 (directory-files (file-name-directory (buffer-file-name))
633 t "\\.org$"))
634 (t org-drill-scope))
635 skip)))
638 (defmacro with-hidden-cloze-text (&rest body)
639 `(progn
640 (org-drill-hide-clozed-text)
641 (unwind-protect
642 (progn
643 ,@body)
644 (org-drill-unhide-clozed-text))))
647 (defmacro with-hidden-cloze-hints (&rest body)
648 `(progn
649 (org-drill-hide-cloze-hints)
650 (unwind-protect
651 (progn
652 ,@body)
653 (org-drill-unhide-text))))
656 (defmacro with-hidden-comments (&rest body)
657 `(progn
658 (if org-drill-hide-item-headings-p
659 (org-drill-hide-heading-at-point))
660 (org-drill-hide-comments)
661 (unwind-protect
662 (progn
663 ,@body)
664 (org-drill-unhide-text))))
667 (defun org-drill-days-since-last-review ()
668 "Nil means a last review date has not yet been stored for
669 the item.
670 Zero means it was reviewed today.
671 A positive number means it was reviewed that many days ago.
672 A negative number means the date of last review is in the future --
673 this should never happen."
674 (let ((datestr (org-entry-get (point) "DRILL_LAST_REVIEWED")))
675 (when datestr
676 (- (time-to-days (current-time))
677 (time-to-days (apply 'encode-time
678 (org-parse-time-string datestr)))))))
681 (defun org-drill-hours-since-last-review ()
682 "Like `org-drill-days-since-last-review', but return value is
683 in hours rather than days."
684 (let ((datestr (org-entry-get (point) "DRILL_LAST_REVIEWED")))
685 (when datestr
686 (floor
687 (/ (- (time-to-seconds (current-time))
688 (time-to-seconds (apply 'encode-time
689 (org-parse-time-string datestr))))
690 (* 60 60))))))
693 (defun org-drill-entry-p (&optional marker)
694 "Is MARKER, or the point, in a 'drill item'? This will return nil if
695 the point is inside a subheading of a drill item -- to handle that
696 situation use `org-part-of-drill-entry-p'."
697 (save-excursion
698 (when marker
699 (org-drill-goto-entry marker))
700 (member org-drill-question-tag (org-get-local-tags))))
703 (defun org-drill-goto-entry (marker)
704 (switch-to-buffer (marker-buffer marker))
705 (goto-char marker))
708 (defun org-part-of-drill-entry-p ()
709 "Is the current entry either the main heading of a 'drill item',
710 or a subheading within a drill item?"
711 (or (org-drill-entry-p)
712 ;; Does this heading INHERIT the drill tag
713 (member org-drill-question-tag (org-get-tags-at))))
716 (defun org-drill-goto-drill-entry-heading ()
717 "Move the point to the heading which holds the :drill: tag for this
718 drill entry."
719 (unless (org-at-heading-p)
720 (org-back-to-heading))
721 (unless (org-part-of-drill-entry-p)
722 (error "Point is not inside a drill entry"))
723 (while (not (org-drill-entry-p))
724 (unless (org-up-heading-safe)
725 (error "Cannot find a parent heading that is marked as a drill entry"))))
729 (defun org-drill-entry-leech-p ()
730 "Is the current entry a 'leech item'?"
731 (and (org-drill-entry-p)
732 (member "leech" (org-get-local-tags))))
735 ;; (defun org-drill-entry-due-p ()
736 ;; (cond
737 ;; (*org-drill-cram-mode*
738 ;; (let ((hours (org-drill-hours-since-last-review)))
739 ;; (and (org-drill-entry-p)
740 ;; (or (null hours)
741 ;; (>= hours org-drill-cram-hours)))))
742 ;; (t
743 ;; (let ((item-time (org-get-scheduled-time (point))))
744 ;; (and (org-drill-entry-p)
745 ;; (or (not (eql 'skip org-drill-leech-method))
746 ;; (not (org-drill-entry-leech-p)))
747 ;; (or (null item-time) ; not scheduled
748 ;; (not (minusp ; scheduled for today/in past
749 ;; (- (time-to-days (current-time))
750 ;; (time-to-days item-time))))))))))
753 (defun org-drill-entry-days-overdue ()
754 "Returns:
755 - NIL if the item is not to be regarded as scheduled for review at all.
756 This is the case if it is not a drill item, or if it is a leech item
757 that we wish to skip, or if we are in cram mode and have already reviewed
758 the item within the last few hours.
759 - 0 if the item is new, or if it scheduled for review today.
760 - A negative integer - item is scheduled that many days in the future.
761 - A positive integer - item is scheduled that many days in the past."
762 (cond
763 (*org-drill-cram-mode*
764 (let ((hours (org-drill-hours-since-last-review)))
765 (and (org-drill-entry-p)
766 (or (null hours)
767 (>= hours org-drill-cram-hours))
768 0)))
770 (let ((item-time (org-get-scheduled-time (point))))
771 (cond
772 ((or (not (org-drill-entry-p))
773 (and (eql 'skip org-drill-leech-method)
774 (org-drill-entry-leech-p)))
775 nil)
776 ((null item-time) ; not scheduled -> due now
779 (- (time-to-days (current-time))
780 (time-to-days item-time))))))))
783 (defun org-drill-entry-overdue-p (&optional days-overdue last-interval)
784 "Returns true if entry that is scheduled DAYS-OVERDUE dasy in the past,
785 and whose last inter-repetition interval was LAST-INTERVAL, should be
786 considered 'overdue'. If the arguments are not given they are extracted
787 from the entry at point."
788 (unless days-overdue
789 (setq days-overdue (org-drill-entry-days-overdue)))
790 (unless last-interval
791 (setq last-interval (org-drill-entry-last-interval 1)))
792 (and (numberp days-overdue)
793 (> days-overdue 1) ; enforce a sane minimum 'overdue' gap
794 ;;(> due org-drill-days-before-overdue)
795 (> (/ (+ days-overdue last-interval 1.0) last-interval)
796 org-drill-overdue-interval-factor)))
800 (defun org-drill-entry-due-p ()
801 (let ((due (org-drill-entry-days-overdue)))
802 (and (not (null due))
803 (not (minusp due)))))
806 (defun org-drill-entry-new-p ()
807 (and (org-drill-entry-p)
808 (let ((item-time (org-get-scheduled-time (point))))
809 (null item-time))))
812 (defun org-drill-entry-last-quality (&optional default)
813 (let ((quality (org-entry-get (point) "DRILL_LAST_QUALITY")))
814 (if quality
815 (string-to-number quality)
816 default)))
819 (defun org-drill-entry-failure-count ()
820 (let ((quality (org-entry-get (point) "DRILL_FAILURE_COUNT")))
821 (if quality
822 (string-to-number quality)
823 0)))
826 (defun org-drill-entry-average-quality (&optional default)
827 (let ((val (org-entry-get (point) "DRILL_AVERAGE_QUALITY")))
828 (if val
829 (string-to-number val)
830 (or default nil))))
832 (defun org-drill-entry-last-interval (&optional default)
833 (let ((val (org-entry-get (point) "DRILL_LAST_INTERVAL")))
834 (if val
835 (string-to-number val)
836 (or default 0))))
838 (defun org-drill-entry-repeats-since-fail (&optional default)
839 (let ((val (org-entry-get (point) "DRILL_REPEATS_SINCE_FAIL")))
840 (if val
841 (string-to-number val)
842 (or default 0))))
844 (defun org-drill-entry-total-repeats (&optional default)
845 (let ((val (org-entry-get (point) "DRILL_TOTAL_REPEATS")))
846 (if val
847 (string-to-number val)
848 (or default 0))))
850 (defun org-drill-entry-ease (&optional default)
851 (let ((val (org-entry-get (point) "DRILL_EASE")))
852 (if val
853 (string-to-number val)
854 default)))
857 ;;; From http://www.supermemo.com/english/ol/sm5.htm
858 (defun org-drill-random-dispersal-factor ()
859 "Returns a random number between 0.5 and 1.5."
860 (let ((a 0.047)
861 (b 0.092)
862 (p (- (random* 1.0) 0.5)))
863 (cl-flet ((sign (n)
864 (cond ((zerop n) 0)
865 ((plusp n) 1)
866 (t -1))))
867 (/ (+ 100 (* (* (/ -1 b) (log (- 1 (* (/ b a ) (abs p)))))
868 (sign p)))
869 100.0))))
871 (defun pseudonormal (mean variation)
872 "Random numbers in a pseudo-normal distribution with mean MEAN, range
873 MEAN-VARIATION to MEAN+VARIATION"
874 (+ (random* variation)
875 (random* variation)
876 (- variation)
877 mean))
880 (defun org-drill-early-interval-factor (optimal-factor
881 optimal-interval
882 days-ahead)
883 "Arguments:
884 - OPTIMAL-FACTOR: interval-factor if the item had been tested
885 exactly when it was supposed to be.
886 - OPTIMAL-INTERVAL: interval for next repetition (days) if the item had been
887 tested exactly when it was supposed to be.
888 - DAYS-AHEAD: how many days ahead of time the item was reviewed.
890 Returns an adjusted optimal factor which should be used to
891 calculate the next interval, instead of the optimal factor found
892 in the matrix."
893 (let ((delta-ofmax (* (1- optimal-factor)
894 (/ (+ optimal-interval
895 (* 0.6 optimal-interval) -1) (1- optimal-interval)))))
896 (- optimal-factor
897 (* delta-ofmax (/ days-ahead (+ days-ahead (* 0.6 optimal-interval)))))))
900 (defun org-drill-get-item-data ()
901 "Returns a list of 6 items, containing all the stored recall
902 data for the item at point:
903 - LAST-INTERVAL is the interval in days that was used to schedule the item's
904 current review date.
905 - REPEATS is the number of items the item has been successfully recalled without
906 without any failures. It is reset to 0 upon failure to recall the item.
907 - FAILURES is the total number of times the user has failed to recall the item.
908 - TOTAL-REPEATS includes both successful and unsuccessful repetitions.
909 - AVERAGE-QUALITY is the mean quality of recall of the item over
910 all its repetitions, successful and unsuccessful.
911 - EASE is a number reflecting how easy the item is to learn. Higher is easier.
913 (let ((learn-str (org-entry-get (point) "LEARN_DATA"))
914 (repeats (org-drill-entry-total-repeats :missing)))
915 (cond
916 (learn-str
917 (let ((learn-data (or (and learn-str
918 (read learn-str))
919 (copy-list initial-repetition-state))))
920 (list (nth 0 learn-data) ; last interval
921 (nth 1 learn-data) ; repetitions
922 (org-drill-entry-failure-count)
923 (nth 1 learn-data)
924 (org-drill-entry-last-quality)
925 (nth 2 learn-data) ; EF
927 ((not (eql :missing repeats))
928 (list (org-drill-entry-last-interval)
929 (org-drill-entry-repeats-since-fail)
930 (org-drill-entry-failure-count)
931 (org-drill-entry-total-repeats)
932 (org-drill-entry-average-quality)
933 (org-drill-entry-ease)))
934 (t ; virgin item
935 (list 0 0 0 0 nil nil)))))
938 (defun org-drill-store-item-data (last-interval repeats failures
939 total-repeats meanq
940 ease)
941 "Stores the given data in the item at point."
942 (org-entry-delete (point) "LEARN_DATA")
943 (org-set-property "DRILL_LAST_INTERVAL"
944 (number-to-string (round-float last-interval 4)))
945 (org-set-property "DRILL_REPEATS_SINCE_FAIL" (number-to-string repeats))
946 (org-set-property "DRILL_TOTAL_REPEATS" (number-to-string total-repeats))
947 (org-set-property "DRILL_FAILURE_COUNT" (number-to-string failures))
948 (org-set-property "DRILL_AVERAGE_QUALITY"
949 (number-to-string (round-float meanq 3)))
950 (org-set-property "DRILL_EASE"
951 (number-to-string (round-float ease 3))))
955 ;;; SM2 Algorithm =============================================================
958 (defun determine-next-interval-sm2 (last-interval n ef quality
959 failures meanq total-repeats)
960 "Arguments:
961 - LAST-INTERVAL -- the number of days since the item was last reviewed.
962 - REPEATS -- the number of times the item has been successfully reviewed
963 - EF -- the 'easiness factor'
964 - QUALITY -- 0 to 5
966 Returns a list: (INTERVAL REPEATS EF FAILURES MEAN TOTAL-REPEATS OFMATRIX), where:
967 - INTERVAL is the number of days until the item should next be reviewed
968 - REPEATS is incremented by 1.
969 - EF is modified based on the recall quality for the item.
970 - OF-MATRIX is not modified."
971 (assert (> n 0))
972 (assert (and (>= quality 0) (<= quality 5)))
973 (if (<= quality org-drill-failure-quality)
974 ;; When an item is failed, its interval is reset to 0,
975 ;; but its EF is unchanged
976 (list -1 1 ef (1+ failures) meanq (1+ total-repeats)
977 org-drill-optimal-factor-matrix)
978 ;; else:
979 (let* ((next-ef (modify-e-factor ef quality))
980 (interval
981 (cond
982 ((<= n 1) 1)
983 ((= n 2)
984 (cond
985 (org-drill-add-random-noise-to-intervals-p
986 (case quality
987 (5 6)
988 (4 4)
989 (3 3)
990 (2 1)
991 (t -1)))
992 (t 6)))
993 (t (* last-interval next-ef)))))
994 (list (if org-drill-add-random-noise-to-intervals-p
995 (+ last-interval (* (- interval last-interval)
996 (org-drill-random-dispersal-factor)))
997 interval)
998 (1+ n)
999 next-ef
1000 failures meanq (1+ total-repeats)
1001 org-drill-optimal-factor-matrix))))
1004 ;;; SM5 Algorithm =============================================================
1008 (defun initial-optimal-factor-sm5 (n ef)
1009 (if (= 1 n)
1010 org-drill-sm5-initial-interval
1011 ef))
1013 (defun get-optimal-factor-sm5 (n ef of-matrix)
1014 (let ((factors (assoc n of-matrix)))
1015 (or (and factors
1016 (let ((ef-of (assoc ef (cdr factors))))
1017 (and ef-of (cdr ef-of))))
1018 (initial-optimal-factor-sm5 n ef))))
1021 (defun inter-repetition-interval-sm5 (last-interval n ef &optional of-matrix)
1022 (let ((of (get-optimal-factor-sm5 n ef (or of-matrix
1023 org-drill-optimal-factor-matrix))))
1024 (if (= 1 n)
1026 (* of last-interval))))
1029 (defun determine-next-interval-sm5 (last-interval n ef quality
1030 failures meanq total-repeats
1031 of-matrix &optional delta-days)
1032 (if (zerop n) (setq n 1))
1033 (if (null ef) (setq ef 2.5))
1034 (assert (> n 0))
1035 (assert (and (>= quality 0) (<= quality 5)))
1036 (unless of-matrix
1037 (setq of-matrix org-drill-optimal-factor-matrix))
1038 (setq of-matrix (cl-copy-tree of-matrix))
1040 (setq meanq (if meanq
1041 (/ (+ quality (* meanq total-repeats 1.0))
1042 (1+ total-repeats))
1043 quality))
1045 (let ((next-ef (modify-e-factor ef quality))
1046 (old-ef ef)
1047 (new-of (modify-of (get-optimal-factor-sm5 n ef of-matrix)
1048 quality org-drill-learn-fraction))
1049 (interval nil))
1050 (when (and org-drill-adjust-intervals-for-early-and-late-repetitions-p
1051 delta-days (minusp delta-days))
1052 (setq new-of (org-drill-early-interval-factor
1053 (get-optimal-factor-sm5 n ef of-matrix)
1054 (inter-repetition-interval-sm5
1055 last-interval n ef of-matrix)
1056 delta-days)))
1058 (setq of-matrix
1059 (set-optimal-factor n next-ef of-matrix
1060 (round-float new-of 3))) ; round OF to 3 d.p.
1062 (setq ef next-ef)
1064 (cond
1065 ;; "Failed" -- reset repetitions to 0,
1066 ((<= quality org-drill-failure-quality)
1067 (list -1 1 old-ef (1+ failures) meanq (1+ total-repeats)
1068 of-matrix)) ; Not clear if OF matrix is supposed to be
1069 ; preserved
1070 ;; For a zero-based quality of 4 or 5, don't repeat
1071 ;; ((and (>= quality 4)
1072 ;; (not org-learn-always-reschedule))
1073 ;; (list 0 (1+ n) ef failures meanq
1074 ;; (1+ total-repeats) of-matrix)) ; 0 interval = unschedule
1076 (setq interval (inter-repetition-interval-sm5
1077 last-interval n ef of-matrix))
1078 (if org-drill-add-random-noise-to-intervals-p
1079 (setq interval (* interval (org-drill-random-dispersal-factor))))
1080 (list interval
1081 (1+ n)
1083 failures
1084 meanq
1085 (1+ total-repeats)
1086 of-matrix)))))
1089 ;;; Simple8 Algorithm =========================================================
1092 (defun org-drill-simple8-first-interval (failures)
1093 "Arguments:
1094 - FAILURES: integer >= 0. The total number of times the item has
1095 been forgotten, ever.
1097 Returns the optimal FIRST interval for an item which has previously been
1098 forgotten on FAILURES occasions."
1099 (* 2.4849 (exp (* -0.057 failures))))
1102 (defun org-drill-simple8-interval-factor (ease repetition)
1103 "Arguments:
1104 - EASE: floating point number >= 1.2. Corresponds to `AF' in SM8 algorithm.
1105 - REPETITION: the number of times the item has been tested.
1106 1 is the first repetition (ie the second trial).
1107 Returns:
1108 The factor by which the last interval should be
1109 multiplied to give the next interval. Corresponds to `RF' or `OF'."
1110 (+ 1.2 (* (- ease 1.2) (expt org-drill-learn-fraction (log repetition 2)))))
1113 (defun org-drill-simple8-quality->ease (quality)
1114 "Returns the ease (`AF' in the SM8 algorithm) which corresponds
1115 to a mean item quality of QUALITY."
1116 (+ (* 0.0542 (expt quality 4))
1117 (* -0.4848 (expt quality 3))
1118 (* 1.4916 (expt quality 2))
1119 (* -1.2403 quality)
1120 1.4515))
1123 (defun determine-next-interval-simple8 (last-interval repeats quality
1124 failures meanq totaln
1125 &optional delta-days)
1126 "Arguments:
1127 - LAST-INTERVAL -- the number of days since the item was last reviewed.
1128 - REPEATS -- the number of times the item has been successfully reviewed
1129 - EASE -- the 'easiness factor'
1130 - QUALITY -- 0 to 5
1131 - DELTA-DAYS -- how many days overdue was the item when it was reviewed.
1132 0 = reviewed on the scheduled day. +N = N days overdue.
1133 -N = reviewed N days early.
1135 Returns the new item data, as a list of 6 values:
1136 - NEXT-INTERVAL
1137 - REPEATS
1138 - EASE
1139 - FAILURES
1140 - AVERAGE-QUALITY
1141 - TOTAL-REPEATS.
1142 See the documentation for `org-drill-get-item-data' for a description of these."
1143 (assert (>= repeats 0))
1144 (assert (and (>= quality 0) (<= quality 5)))
1145 (assert (or (null meanq) (and (>= meanq 0) (<= meanq 5))))
1146 (let ((next-interval nil))
1147 (setf meanq (if meanq
1148 (/ (+ quality (* meanq totaln 1.0)) (1+ totaln))
1149 quality))
1150 (cond
1151 ((<= quality org-drill-failure-quality)
1152 (incf failures)
1153 (setf repeats 0
1154 next-interval -1))
1155 ((or (zerop repeats)
1156 (zerop last-interval))
1157 (setf next-interval (org-drill-simple8-first-interval failures))
1158 (incf repeats)
1159 (incf totaln))
1161 (let* ((use-n
1162 (if (and
1163 org-drill-adjust-intervals-for-early-and-late-repetitions-p
1164 (numberp delta-days) (plusp delta-days)
1165 (plusp last-interval))
1166 (+ repeats (min 1 (/ delta-days last-interval 1.0)))
1167 repeats))
1168 (factor (org-drill-simple8-interval-factor
1169 (org-drill-simple8-quality->ease meanq) use-n))
1170 (next-int (* last-interval factor)))
1171 (when (and org-drill-adjust-intervals-for-early-and-late-repetitions-p
1172 (numberp delta-days) (minusp delta-days))
1173 ;; The item was reviewed earlier than scheduled.
1174 (setf factor (org-drill-early-interval-factor
1175 factor next-int (abs delta-days))
1176 next-int (* last-interval factor)))
1177 (setf next-interval next-int)
1178 (incf repeats)
1179 (incf totaln))))
1180 (list
1181 (if (and org-drill-add-random-noise-to-intervals-p
1182 (plusp next-interval))
1183 (* next-interval (org-drill-random-dispersal-factor))
1184 next-interval)
1185 repeats
1186 (org-drill-simple8-quality->ease meanq)
1187 failures
1188 meanq
1189 totaln
1195 ;;; Essentially copied from `org-learn.el', but modified to
1196 ;;; optionally call the SM2 or simple8 functions.
1197 (defun org-drill-smart-reschedule (quality &optional days-ahead)
1198 "If DAYS-AHEAD is supplied it must be a positive integer. The
1199 item will be scheduled exactly this many days into the future."
1200 (let ((delta-days (- (time-to-days (current-time))
1201 (time-to-days (or (org-get-scheduled-time (point))
1202 (current-time)))))
1203 (ofmatrix org-drill-optimal-factor-matrix)
1204 ;; Entries can have weights, 1 by default. Intervals are divided by the
1205 ;; item's weight, so an item with a weight of 2 will have all intervals
1206 ;; halved, meaning you will end up reviewing it twice as often.
1207 ;; Useful for entries which randomly present any of several facts.
1208 (weight (org-entry-get (point) "DRILL_CARD_WEIGHT")))
1209 (if (stringp weight)
1210 (setq weight (read weight)))
1211 (destructuring-bind (last-interval repetitions failures
1212 total-repeats meanq ease)
1213 (org-drill-get-item-data)
1214 (destructuring-bind (next-interval repetitions ease
1215 failures meanq total-repeats
1216 &optional new-ofmatrix)
1217 (case org-drill-spaced-repetition-algorithm
1218 (sm5 (determine-next-interval-sm5 last-interval repetitions
1219 ease quality failures
1220 meanq total-repeats ofmatrix))
1221 (sm2 (determine-next-interval-sm2 last-interval repetitions
1222 ease quality failures
1223 meanq total-repeats))
1224 (simple8 (determine-next-interval-simple8 last-interval repetitions
1225 quality failures meanq
1226 total-repeats
1227 delta-days)))
1228 (if (numberp days-ahead)
1229 (setq next-interval days-ahead))
1231 (if (and (null days-ahead)
1232 (numberp weight) (plusp weight)
1233 (not (minusp next-interval)))
1234 (setq next-interval
1235 (max 1.0 (+ last-interval
1236 (/ (- next-interval last-interval) weight)))))
1238 (org-drill-store-item-data next-interval repetitions failures
1239 total-repeats meanq ease)
1241 (if (eql 'sm5 org-drill-spaced-repetition-algorithm)
1242 (setq org-drill-optimal-factor-matrix new-ofmatrix))
1244 (cond
1245 ((= 0 days-ahead)
1246 (org-schedule '(4)))
1247 ((minusp days-ahead)
1248 (org-schedule nil (current-time)))
1250 (org-schedule nil (time-add (current-time)
1251 (days-to-time
1252 (round next-interval))))))))))
1255 (defun org-drill-hypothetical-next-review-date (quality)
1256 "Returns an integer representing the number of days into the future
1257 that the current item would be scheduled, based on a recall quality
1258 of QUALITY."
1259 (let ((weight (org-entry-get (point) "DRILL_CARD_WEIGHT")))
1260 (destructuring-bind (last-interval repetitions failures
1261 total-repeats meanq ease)
1262 (org-drill-get-item-data)
1263 (if (stringp weight)
1264 (setq weight (read weight)))
1265 (destructuring-bind (next-interval repetitions ease
1266 failures meanq total-repeats
1267 &optional ofmatrix)
1268 (case org-drill-spaced-repetition-algorithm
1269 (sm5 (determine-next-interval-sm5 last-interval repetitions
1270 ease quality failures
1271 meanq total-repeats
1272 org-drill-optimal-factor-matrix))
1273 (sm2 (determine-next-interval-sm2 last-interval repetitions
1274 ease quality failures
1275 meanq total-repeats))
1276 (simple8 (determine-next-interval-simple8 last-interval repetitions
1277 quality failures meanq
1278 total-repeats)))
1279 (cond
1280 ((not (plusp next-interval))
1282 ((and (numberp weight) (plusp weight))
1283 (+ last-interval
1284 (max 1.0 (/ (- next-interval last-interval) weight))))
1286 next-interval))))))
1289 (defun org-drill-hypothetical-next-review-dates ()
1290 (let ((intervals nil))
1291 (dotimes (q 6)
1292 (push (max (or (car intervals) 0)
1293 (org-drill-hypothetical-next-review-date q))
1294 intervals))
1295 (reverse intervals)))
1298 (defun org-drill-reschedule ()
1299 "Returns quality rating (0-5), or nil if the user quit."
1300 (let ((ch nil)
1301 (input nil)
1302 (next-review-dates (org-drill-hypothetical-next-review-dates)))
1303 (save-excursion
1304 (while (not (memq ch '(?q ?e ?0 ?1 ?2 ?3 ?4 ?5)))
1305 (setq input (read-key-sequence
1306 (if (eq ch ??)
1307 (format "0-2 Means you have forgotten the item.
1308 3-5 Means you have remembered the item.
1310 0 - Completely forgot.
1311 1 - Even after seeing the answer, it still took a bit to sink in.
1312 2 - After seeing the answer, you remembered it.
1313 3 - It took you awhile, but you finally remembered. (+%s days)
1314 4 - After a little bit of thought you remembered. (+%s days)
1315 5 - You remembered the item really easily. (+%s days)
1317 How well did you do? (0-5, ?=help, e=edit, t=tags, q=quit)"
1318 (round (nth 3 next-review-dates))
1319 (round (nth 4 next-review-dates))
1320 (round (nth 5 next-review-dates)))
1321 "How well did you do? (0-5, ?=help, e=edit, t=tags, q=quit)")))
1322 (cond
1323 ((stringp input)
1324 (setq ch (elt input 0)))
1325 ((and (vectorp input) (symbolp (elt input 0)))
1326 (case (elt input 0)
1327 (up (ignore-errors (forward-line -1)))
1328 (down (ignore-errors (forward-line 1)))
1329 (left (ignore-errors (backward-char)))
1330 (right (ignore-errors (forward-char)))
1331 (prior (ignore-errors (scroll-down))) ; pgup
1332 (next (ignore-errors (scroll-up))))) ; pgdn
1333 ((and (vectorp input) (listp (elt input 0))
1334 (eventp (elt input 0)))
1335 (case (car (elt input 0))
1336 (wheel-up (ignore-errors (mwheel-scroll (elt input 0))))
1337 (wheel-down (ignore-errors (mwheel-scroll (elt input 0)))))))
1338 (if (eql ch ?t)
1339 (org-set-tags-command))))
1340 (cond
1341 ((and (>= ch ?0) (<= ch ?5))
1342 (let ((quality (- ch ?0))
1343 (failures (org-drill-entry-failure-count)))
1344 (unless *org-drill-cram-mode*
1345 (save-excursion
1346 (org-drill-smart-reschedule quality
1347 (nth quality next-review-dates)))
1348 (push quality *org-drill-session-qualities*)
1349 (cond
1350 ((<= quality org-drill-failure-quality)
1351 (when org-drill-leech-failure-threshold
1352 ;;(setq failures (if failures (string-to-number failures) 0))
1353 ;; (org-set-property "DRILL_FAILURE_COUNT"
1354 ;; (format "%d" (1+ failures)))
1355 (if (> (1+ failures) org-drill-leech-failure-threshold)
1356 (org-toggle-tag "leech" 'on))))
1358 (let ((scheduled-time (org-get-scheduled-time (point))))
1359 (when scheduled-time
1360 (message "Next review in %d days"
1361 (- (time-to-days scheduled-time)
1362 (time-to-days (current-time))))
1363 (sit-for 0.5)))))
1364 (org-set-property "DRILL_LAST_QUALITY" (format "%d" quality))
1365 (org-set-property "DRILL_LAST_REVIEWED"
1366 (time-to-active-org-timestamp (current-time))))
1367 quality))
1368 ((= ch ?e)
1369 'edit)
1371 nil))))
1374 ;; (defun org-drill-hide-all-subheadings-except (heading-list)
1375 ;; "Returns a list containing the position of each immediate subheading of
1376 ;; the current topic."
1377 ;; (let ((drill-entry-level (org-current-level))
1378 ;; (drill-sections nil)
1379 ;; (drill-heading nil))
1380 ;; (org-show-subtree)
1381 ;; (save-excursion
1382 ;; (org-map-entries
1383 ;; (lambda ()
1384 ;; (when (and (not (outline-invisible-p))
1385 ;; (> (org-current-level) drill-entry-level))
1386 ;; (setq drill-heading (org-get-heading t))
1387 ;; (unless (and (= (org-current-level) (1+ drill-entry-level))
1388 ;; (member drill-heading heading-list))
1389 ;; (hide-subtree))
1390 ;; (push (point) drill-sections)))
1391 ;; "" 'tree))
1392 ;; (reverse drill-sections)))
1396 (defun org-drill-hide-subheadings-if (test)
1397 "TEST is a function taking no arguments. TEST will be called for each
1398 of the immediate subheadings of the current drill item, with the point
1399 on the relevant subheading. TEST should return nil if the subheading is
1400 to be revealed, non-nil if it is to be hidden.
1401 Returns a list containing the position of each immediate subheading of
1402 the current topic."
1403 (let ((drill-entry-level (org-current-level))
1404 (drill-sections nil))
1405 (org-show-subtree)
1406 (save-excursion
1407 (org-map-entries
1408 (lambda ()
1409 (when (and (not (outline-invisible-p))
1410 (> (org-current-level) drill-entry-level))
1411 (when (or (/= (org-current-level) (1+ drill-entry-level))
1412 (funcall test))
1413 (hide-subtree))
1414 (push (point) drill-sections)))
1415 "" 'tree))
1416 (reverse drill-sections)))
1419 (defun org-drill-hide-all-subheadings-except (heading-list)
1420 (org-drill-hide-subheadings-if
1421 (lambda () (let ((drill-heading (org-get-heading t)))
1422 (not (member drill-heading heading-list))))))
1425 (defun org-drill-presentation-prompt (&rest fmt-and-args)
1426 (let* ((item-start-time (current-time))
1427 (input nil)
1428 (ch nil)
1429 (last-second 0)
1430 (mature-entry-count (+ (length *org-drill-young-mature-entries*)
1431 (length *org-drill-old-mature-entries*)
1432 (length *org-drill-overdue-entries*)))
1433 (status (first (org-drill-entry-status)))
1434 (prompt
1435 (if fmt-and-args
1436 (apply 'format
1437 (first fmt-and-args)
1438 (rest fmt-and-args))
1439 (concat "Press key for answer, "
1440 "e=edit, t=tags, s=skip, q=quit."))))
1441 (setq prompt
1442 (format "%s %s %s %s %s %s"
1443 (propertize
1444 (char-to-string
1445 (cond
1446 ((eql status :failed) ?F)
1447 (*org-drill-cram-mode* ?C)
1449 (case status
1450 (:new ?N) (:young ?Y) (:old ?o) (:overdue ?!)
1451 (t ??)))))
1452 'face `(:foreground
1453 ,(case status
1454 (:new org-drill-new-count-color)
1455 ((:young :old) org-drill-mature-count-color)
1456 ((:overdue :failed) org-drill-failed-count-color)
1457 (t org-drill-done-count-color))))
1458 (propertize
1459 (number-to-string (length *org-drill-done-entries*))
1460 'face `(:foreground ,org-drill-done-count-color)
1461 'help-echo "The number of items you have reviewed this session.")
1462 (propertize
1463 (number-to-string (+ (length *org-drill-again-entries*)
1464 (length *org-drill-failed-entries*)))
1465 'face `(:foreground ,org-drill-failed-count-color)
1466 'help-echo (concat "The number of items that you failed, "
1467 "and need to review again."))
1468 (propertize
1469 (number-to-string mature-entry-count)
1470 'face `(:foreground ,org-drill-mature-count-color)
1471 'help-echo "The number of old items due for review.")
1472 (propertize
1473 (number-to-string (length *org-drill-new-entries*))
1474 'face `(:foreground ,org-drill-new-count-color)
1475 'help-echo (concat "The number of new items that you "
1476 "have never reviewed."))
1477 prompt))
1478 (if (and (eql 'warn org-drill-leech-method)
1479 (org-drill-entry-leech-p))
1480 (setq prompt (concat
1481 (propertize "!!! LEECH ITEM !!!
1482 You seem to be having a lot of trouble memorising this item.
1483 Consider reformulating the item to make it easier to remember.\n"
1484 'face '(:foreground "red"))
1485 prompt)))
1486 (while (memq ch '(nil ?t))
1487 (setq ch nil)
1488 (while (not (input-pending-p))
1489 (let ((elapsed (time-subtract (current-time) item-start-time)))
1490 (message (concat (if (>= (time-to-seconds elapsed) (* 60 60))
1491 "++:++ "
1492 (format-time-string "%M:%S " elapsed))
1493 prompt))
1494 (sit-for 1)))
1495 (setq input (read-key-sequence nil))
1496 (if (stringp input) (setq ch (elt input 0)))
1497 (if (eql ch ?t)
1498 (org-set-tags-command)))
1499 (case ch
1500 (?q nil)
1501 (?e 'edit)
1502 (?s 'skip)
1503 (otherwise t))))
1506 (defun org-pos-in-regexp (pos regexp &optional nlines)
1507 (save-excursion
1508 (goto-char pos)
1509 (org-in-regexp regexp nlines)))
1512 (defun org-drill-hide-region (beg end &optional text)
1513 "Hide the buffer region between BEG and END with an 'invisible text'
1514 visual overlay, or with the string TEXT if it is supplied."
1515 (let ((ovl (make-overlay beg end)))
1516 (overlay-put ovl 'category
1517 'org-drill-hidden-text-overlay)
1518 (overlay-put ovl 'priority 9999)
1519 (when (stringp text)
1520 (overlay-put ovl 'invisible nil)
1521 (overlay-put ovl 'face 'default)
1522 (overlay-put ovl 'display text))))
1525 (defun org-drill-hide-heading-at-point (&optional text)
1526 (unless (org-at-heading-p)
1527 (error "Point is not on a heading."))
1528 (save-excursion
1529 (let ((beg (point)))
1530 (end-of-line)
1531 (org-drill-hide-region beg (point) text))))
1534 (defun org-drill-hide-comments ()
1535 (save-excursion
1536 (while (re-search-forward "^#.*$" nil t)
1537 (org-drill-hide-region (match-beginning 0) (match-end 0)))))
1540 (defun org-drill-unhide-text ()
1541 ;; This will also unhide the item's heading.
1542 (save-excursion
1543 (dolist (ovl (overlays-in (point-min) (point-max)))
1544 (when (eql 'org-drill-hidden-text-overlay (overlay-get ovl 'category))
1545 (delete-overlay ovl)))))
1548 (defun org-drill-hide-clozed-text ()
1549 (save-excursion
1550 (while (re-search-forward org-drill-cloze-regexp nil t)
1551 ;; Don't hide org links, partly because they might contain inline
1552 ;; images which we want to keep visible.
1553 ;; And don't hide LaTeX math fragments.
1554 (unless (save-match-data
1555 (or (org-pos-in-regexp (match-beginning 0)
1556 org-bracket-link-regexp 1)
1557 (org-inside-LaTeX-fragment-p)))
1558 (org-drill-hide-matched-cloze-text)))))
1561 (defun org-drill-hide-matched-cloze-text ()
1562 "Hide the current match with a 'cloze' visual overlay."
1563 (let ((ovl (make-overlay (match-beginning 0) (match-end 0)))
1564 (hint-sep-pos (string-match-p (regexp-quote org-drill-hint-separator)
1565 (match-string 0))))
1566 (overlay-put ovl 'category
1567 'org-drill-cloze-overlay-defaults)
1568 (overlay-put ovl 'priority 9999)
1569 (when (and hint-sep-pos
1570 (> hint-sep-pos 1))
1571 (let ((hint (substring-no-properties
1572 (match-string 0)
1573 (+ hint-sep-pos (length org-drill-hint-separator))
1574 (1- (length (match-string 0))))))
1575 (overlay-put
1576 ovl 'display
1577 ;; If hint is like `X...' then display [X...]
1578 ;; otherwise display [...X]
1579 (format (if (string-match-p (regexp-quote "...") hint) "[%s]" "[%s...]")
1580 hint))))))
1583 (defun org-drill-hide-cloze-hints ()
1584 (save-excursion
1585 (while (re-search-forward org-drill-cloze-regexp nil t)
1586 (unless (or (save-match-data
1587 (org-pos-in-regexp (match-beginning 0)
1588 org-bracket-link-regexp 1))
1589 (null (match-beginning 2))) ; hint subexpression matched
1590 (org-drill-hide-region (match-beginning 2) (match-end 2))))))
1593 (defmacro with-replaced-entry-text (text &rest body)
1594 "During the execution of BODY, the entire text of the current entry is
1595 concealed by an overlay that displays the string TEXT."
1596 `(progn
1597 (org-drill-replace-entry-text ,text)
1598 (unwind-protect
1599 (progn
1600 ,@body)
1601 (org-drill-unreplace-entry-text))))
1604 (defmacro with-replaced-entry-text-multi (replacements &rest body)
1605 "During the execution of BODY, the entire text of the current entry is
1606 concealed by an overlay that displays the overlays in REPLACEMENTS."
1607 `(progn
1608 (org-drill-replace-entry-text ,replacements t)
1609 (unwind-protect
1610 (progn
1611 ,@body)
1612 (org-drill-unreplace-entry-text))))
1615 (defun org-drill-replace-entry-text (text &optional multi-p)
1616 "Make an overlay that conceals the entire text of the item, not
1617 including properties or the contents of subheadings. The overlay shows
1618 the string TEXT.
1619 If MULTI-P is non-nil, TEXT must be a list of values which are legal
1620 for the `display' text property. The text of the item will be temporarily
1621 replaced by all of these items, in the order in which they appear in
1622 the list.
1623 Note: does not actually alter the item."
1624 (cond
1625 ((and multi-p
1626 (listp text))
1627 (org-drill-replace-entry-text-multi text))
1629 (let ((ovl (make-overlay (point-min)
1630 (save-excursion
1631 (outline-next-heading)
1632 (point)))))
1633 (overlay-put ovl 'priority 9999)
1634 (overlay-put ovl 'category
1635 'org-drill-replaced-text-overlay)
1636 (overlay-put ovl 'display text)))))
1639 (defun org-drill-unreplace-entry-text ()
1640 (save-excursion
1641 (dolist (ovl (overlays-in (point-min) (point-max)))
1642 (when (eql 'org-drill-replaced-text-overlay (overlay-get ovl 'category))
1643 (delete-overlay ovl)))))
1646 (defun org-drill-replace-entry-text-multi (replacements)
1647 "Make overlays that conceal the entire text of the item, not
1648 including properties or the contents of subheadings. The overlay shows
1649 the string TEXT.
1650 Note: does not actually alter the item."
1651 (let ((ovl nil)
1652 (p-min (point-min))
1653 (p-max (save-excursion
1654 (outline-next-heading)
1655 (point))))
1656 (assert (>= (- p-max p-min) (length replacements)))
1657 (dotimes (i (length replacements))
1658 (setq ovl (make-overlay (+ p-min (* 2 i))
1659 (if (= i (1- (length replacements)))
1660 p-max
1661 (+ p-min (* 2 i) 1))))
1662 (overlay-put ovl 'priority 9999)
1663 (overlay-put ovl 'category
1664 'org-drill-replaced-text-overlay)
1665 (overlay-put ovl 'display (nth i replacements)))))
1668 (defmacro with-replaced-entry-heading (heading &rest body)
1669 `(progn
1670 (org-drill-replace-entry-heading ,heading)
1671 (unwind-protect
1672 (progn
1673 ,@body)
1674 (org-drill-unhide-text))))
1677 (defun org-drill-replace-entry-heading (heading)
1678 "Make an overlay that conceals the heading of the item. The overlay shows
1679 the string TEXT.
1680 Note: does not actually alter the item."
1681 (org-drill-hide-heading-at-point heading))
1684 (defun org-drill-unhide-clozed-text ()
1685 (save-excursion
1686 (dolist (ovl (overlays-in (point-min) (point-max)))
1687 (when (eql 'org-drill-cloze-overlay-defaults (overlay-get ovl 'category))
1688 (delete-overlay ovl)))))
1691 (defun org-drill-get-entry-text (&optional keep-properties-p)
1692 (let ((text (org-agenda-get-some-entry-text (point-marker) 100)))
1693 (if keep-properties-p
1694 text
1695 (substring-no-properties text))))
1698 ;; (defun org-entry-empty-p ()
1699 ;; (zerop (length (org-drill-get-entry-text))))
1701 ;; This version is about 5x faster than the old version, above.
1702 (defun org-entry-empty-p ()
1703 (save-excursion
1704 (org-back-to-heading t)
1705 (let ((lim (save-excursion
1706 (outline-next-heading) (point))))
1707 (org-end-of-meta-data-and-drawers)
1708 (or (>= (point) lim)
1709 (null (re-search-forward "[[:graph:]]" lim t))))))
1711 (defun org-drill-entry-empty-p () (org-entry-empty-p))
1714 ;;; Presentation functions ====================================================
1716 ;; Each of these is called with point on topic heading. Each needs to show the
1717 ;; topic in the form of a 'question' or with some information 'hidden', as
1718 ;; appropriate for the card type. The user should then be prompted to press a
1719 ;; key. The function should then reveal either the 'answer' or the entire
1720 ;; topic, and should return t if the user chose to see the answer and rate their
1721 ;; recall, nil if they chose to quit.
1723 (defun org-drill-present-simple-card ()
1724 (with-hidden-comments
1725 (with-hidden-cloze-hints
1726 (with-hidden-cloze-text
1727 (org-drill-hide-all-subheadings-except nil)
1728 (org-preview-latex-fragment) ; overlay all LaTeX fragments with images
1729 (ignore-errors
1730 (org-display-inline-images t))
1731 (org-cycle-hide-drawers 'all)
1732 (prog1 (org-drill-presentation-prompt)
1733 (org-drill-hide-subheadings-if 'org-drill-entry-p))))))
1736 (defun org-drill-present-default-answer (reschedule-fn)
1737 (cond
1738 (drill-answer
1739 (with-replaced-entry-text
1740 (format "\nAnswer:\n\n %s\n" drill-answer)
1741 (prog1
1742 (funcall reschedule-fn)
1743 (setq drill-answer nil))))
1745 (org-drill-hide-subheadings-if 'org-drill-entry-p)
1746 (org-drill-unhide-clozed-text)
1747 (org-preview-latex-fragment)
1748 (ignore-errors
1749 (org-display-inline-images t))
1750 (org-cycle-hide-drawers 'all)
1751 (with-hidden-cloze-hints
1752 (funcall reschedule-fn)))))
1755 (defun org-drill-present-two-sided-card ()
1756 (with-hidden-comments
1757 (with-hidden-cloze-hints
1758 (with-hidden-cloze-text
1759 (let ((drill-sections (org-drill-hide-all-subheadings-except nil)))
1760 (when drill-sections
1761 (save-excursion
1762 (goto-char (nth (random* (min 2 (length drill-sections)))
1763 drill-sections))
1764 (org-show-subtree)))
1765 (org-preview-latex-fragment)
1766 (ignore-errors
1767 (org-display-inline-images t))
1768 (org-cycle-hide-drawers 'all)
1769 (prog1 (org-drill-presentation-prompt)
1770 (org-drill-hide-subheadings-if 'org-drill-entry-p)))))))
1774 (defun org-drill-present-multi-sided-card ()
1775 (with-hidden-comments
1776 (with-hidden-cloze-hints
1777 (with-hidden-cloze-text
1778 (let ((drill-sections (org-drill-hide-all-subheadings-except nil)))
1779 (when drill-sections
1780 (save-excursion
1781 (goto-char (nth (random* (length drill-sections)) drill-sections))
1782 (org-show-subtree)))
1783 (org-preview-latex-fragment)
1784 (ignore-errors
1785 (org-display-inline-images t))
1786 (org-cycle-hide-drawers 'all)
1787 (prog1 (org-drill-presentation-prompt)
1788 (org-drill-hide-subheadings-if 'org-drill-entry-p)))))))
1791 (defun org-drill-present-multicloze-hide-n (number-to-hide
1792 &optional
1793 force-show-first
1794 force-show-last
1795 force-hide-first)
1796 "Hides NUMBER-TO-HIDE pieces of text that are marked for cloze deletion,
1797 chosen at random.
1798 If NUMBER-TO-HIDE is negative, show only (ABS NUMBER-TO-HIDE) pieces,
1799 hiding all the rest.
1800 If FORCE-HIDE-FIRST is non-nil, force the first piece of text to be one of
1801 the hidden items.
1802 If FORCE-SHOW-FIRST is non-nil, never hide the first piece of text.
1803 If FORCE-SHOW-LAST is non-nil, never hide the last piece of text.
1804 If the number of text pieces in the item is less than
1805 NUMBER-TO-HIDE, then all text pieces will be hidden (except the first or last
1806 items if FORCE-SHOW-FIRST or FORCE-SHOW-LAST is non-nil)."
1807 (with-hidden-comments
1808 (with-hidden-cloze-hints
1809 (let ((item-end nil)
1810 (match-count 0)
1811 (body-start (or (cdr (org-get-property-block))
1812 (point))))
1813 (if (and force-hide-first force-show-first)
1814 (error "FORCE-HIDE-FIRST and FORCE-SHOW-FIRST are mutually exclusive"))
1815 (org-drill-hide-all-subheadings-except nil)
1816 (save-excursion
1817 (outline-next-heading)
1818 (setq item-end (point)))
1819 (save-excursion
1820 (goto-char body-start)
1821 (while (re-search-forward org-drill-cloze-regexp item-end t)
1822 (let ((in-regexp? (save-match-data
1823 (org-pos-in-regexp (match-beginning 0)
1824 org-bracket-link-regexp 1))))
1825 (unless (or in-regexp?
1826 (org-inside-LaTeX-fragment-p))
1827 (incf match-count)))))
1828 (if (minusp number-to-hide)
1829 (setq number-to-hide (+ match-count number-to-hide)))
1830 (when (plusp match-count)
1831 (let* ((positions (shuffle-list (loop for i from 1
1832 to match-count
1833 collect i)))
1834 (match-nums nil)
1835 (cnt nil))
1836 (if force-hide-first
1837 ;; Force '1' to be in the list, and to be the first item
1838 ;; in the list.
1839 (setq positions (cons 1 (remove 1 positions))))
1840 (if force-show-first
1841 (setq positions (remove 1 positions)))
1842 (if force-show-last
1843 (setq positions (remove match-count positions)))
1844 (setq match-nums
1845 (subseq positions
1846 0 (min number-to-hide (length positions))))
1847 ;; (dolist (pos-to-hide match-nums)
1848 (save-excursion
1849 (goto-char body-start)
1850 (setq cnt 0)
1851 (while (re-search-forward org-drill-cloze-regexp item-end t)
1852 (unless (save-match-data
1853 (or (org-pos-in-regexp (match-beginning 0)
1854 org-bracket-link-regexp 1)
1855 (org-inside-LaTeX-fragment-p)))
1856 (incf cnt)
1857 (if (memq cnt match-nums)
1858 (org-drill-hide-matched-cloze-text)))))))
1859 ;; (loop
1860 ;; do (re-search-forward org-drill-cloze-regexp
1861 ;; item-end t pos-to-hide)
1862 ;; while (org-pos-in-regexp (match-beginning 0)
1863 ;; org-bracket-link-regexp 1))
1864 ;; (org-drill-hide-matched-cloze-text)))))
1865 (org-preview-latex-fragment)
1866 (ignore-errors
1867 (org-display-inline-images t))
1868 (org-cycle-hide-drawers 'all)
1869 (prog1 (org-drill-presentation-prompt)
1870 (org-drill-hide-subheadings-if 'org-drill-entry-p)
1871 (org-drill-unhide-clozed-text))))))
1874 (defun org-drill-present-multicloze-hide-nth (to-hide)
1875 "Hide the TO-HIDE'th piece of clozed text. 1 is the first piece. If
1876 TO-HIDE is negative, count backwards, so -1 means the last item, -2
1877 the second to last, etc."
1878 (with-hidden-comments
1879 (with-hidden-cloze-hints
1880 (let ((item-end nil)
1881 (match-count 0)
1882 (body-start (or (cdr (org-get-property-block))
1883 (point)))
1884 (cnt 0))
1885 (org-drill-hide-all-subheadings-except nil)
1886 (save-excursion
1887 (outline-next-heading)
1888 (setq item-end (point)))
1889 (save-excursion
1890 (goto-char body-start)
1891 (while (re-search-forward org-drill-cloze-regexp item-end t)
1892 (let ((in-regexp? (save-match-data
1893 (org-pos-in-regexp (match-beginning 0)
1894 org-bracket-link-regexp 1))))
1895 (unless (or in-regexp?
1896 (org-inside-LaTeX-fragment-p))
1897 (incf match-count)))))
1898 (if (minusp to-hide)
1899 (setq to-hide (+ 1 to-hide match-count)))
1900 (cond
1901 ((or (not (plusp match-count))
1902 (> to-hide match-count))
1903 nil)
1905 (save-excursion
1906 (goto-char body-start)
1907 (setq cnt 0)
1908 (while (re-search-forward org-drill-cloze-regexp item-end t)
1909 (unless (save-match-data
1910 ;; Don't consider this a cloze region if it is part of an
1911 ;; org link, or if it occurs inside a LaTeX math
1912 ;; fragment
1913 (or (org-pos-in-regexp (match-beginning 0)
1914 org-bracket-link-regexp 1)
1915 (org-inside-LaTeX-fragment-p)))
1916 (incf cnt)
1917 (if (= cnt to-hide)
1918 (org-drill-hide-matched-cloze-text)))))))
1919 (org-preview-latex-fragment)
1920 (ignore-errors
1921 (org-display-inline-images t))
1922 (org-cycle-hide-drawers 'all)
1923 (prog1 (org-drill-presentation-prompt)
1924 (org-drill-hide-subheadings-if 'org-drill-entry-p)
1925 (org-drill-unhide-clozed-text))))))
1928 (defun org-drill-present-multicloze-hide1 ()
1929 "Hides one of the pieces of text that are marked for cloze deletion,
1930 chosen at random."
1931 (org-drill-present-multicloze-hide-n 1))
1934 (defun org-drill-present-multicloze-hide2 ()
1935 "Hides two of the pieces of text that are marked for cloze deletion,
1936 chosen at random."
1937 (org-drill-present-multicloze-hide-n 2))
1940 (defun org-drill-present-multicloze-hide-first ()
1941 "Hides the first piece of text that is marked for cloze deletion."
1942 (org-drill-present-multicloze-hide-nth 1))
1945 (defun org-drill-present-multicloze-hide-last ()
1946 "Hides the last piece of text that is marked for cloze deletion."
1947 (org-drill-present-multicloze-hide-nth -1))
1950 (defun org-drill-present-multicloze-hide1-firstmore ()
1951 "Commonly, hides the FIRST piece of text that is marked for
1952 cloze deletion. Uncommonly, hide one of the other pieces of text,
1953 chosen at random.
1955 The definitions of 'commonly' and 'uncommonly' are determined by
1956 the value of `org-drill-cloze-text-weight'."
1957 ;; The 'firstmore' and 'lastmore' functions used to randomly choose whether
1958 ;; to hide the 'favoured' piece of text. However even when the chance of
1959 ;; hiding it was set quite high (80%), the outcome was too unpredictable over
1960 ;; the small number of repetitions where most learning takes place for each
1961 ;; item. In other words, the actual frequency during the first 10 repetitions
1962 ;; was often very different from 80%. Hence we use modulo instead.
1963 (cond
1964 ((null org-drill-cloze-text-weight)
1965 ;; Behave as hide1cloze
1966 (org-drill-present-multicloze-hide1))
1967 ((not (and (integerp org-drill-cloze-text-weight)
1968 (plusp org-drill-cloze-text-weight)))
1969 (error "Illegal value for org-drill-cloze-text-weight: %S"
1970 org-drill-cloze-text-weight))
1971 ((zerop (mod (1+ (org-drill-entry-total-repeats 0))
1972 org-drill-cloze-text-weight))
1973 ;; Uncommonly, hide any item except the first
1974 (org-drill-present-multicloze-hide-n 1 t))
1976 ;; Commonly, hide first item
1977 (org-drill-present-multicloze-hide-first))))
1980 (defun org-drill-present-multicloze-show1-lastmore ()
1981 "Commonly, hides all pieces except the last. Uncommonly, shows
1982 any random piece. The effect is similar to 'show1cloze' except
1983 that the last item is much less likely to be the item that is
1984 visible.
1986 The definitions of 'commonly' and 'uncommonly' are determined by
1987 the value of `org-drill-cloze-text-weight'."
1988 (cond
1989 ((null org-drill-cloze-text-weight)
1990 ;; Behave as show1cloze
1991 (org-drill-present-multicloze-show1))
1992 ((not (and (integerp org-drill-cloze-text-weight)
1993 (plusp org-drill-cloze-text-weight)))
1994 (error "Illegal value for org-drill-cloze-text-weight: %S"
1995 org-drill-cloze-text-weight))
1996 ((zerop (mod (1+ (org-drill-entry-total-repeats 0))
1997 org-drill-cloze-text-weight))
1998 ;; Uncommonly, show any item except the last
1999 (org-drill-present-multicloze-hide-n -1 nil nil t))
2001 ;; Commonly, show the LAST item
2002 (org-drill-present-multicloze-hide-n -1 nil t))))
2005 (defun org-drill-present-multicloze-show1-firstless ()
2006 "Commonly, hides all pieces except one, where the shown piece
2007 is guaranteed NOT to be the first piece. Uncommonly, shows any
2008 random piece. The effect is similar to 'show1cloze' except that
2009 the first item is much less likely to be the item that is
2010 visible.
2012 The definitions of 'commonly' and 'uncommonly' are determined by
2013 the value of `org-drill-cloze-text-weight'."
2014 (cond
2015 ((null org-drill-cloze-text-weight)
2016 ;; Behave as show1cloze
2017 (org-drill-present-multicloze-show1))
2018 ((not (and (integerp org-drill-cloze-text-weight)
2019 (plusp org-drill-cloze-text-weight)))
2020 (error "Illegal value for org-drill-cloze-text-weight: %S"
2021 org-drill-cloze-text-weight))
2022 ((zerop (mod (1+ (org-drill-entry-total-repeats 0))
2023 org-drill-cloze-text-weight))
2024 ;; Uncommonly, show the first item
2025 (org-drill-present-multicloze-hide-n -1 t))
2027 ;; Commonly, show any item, except the first
2028 (org-drill-present-multicloze-hide-n -1 nil nil t))))
2031 (defun org-drill-present-multicloze-show1 ()
2032 "Similar to `org-drill-present-multicloze-hide1', but hides all
2033 the pieces of text that are marked for cloze deletion, except for one
2034 piece which is chosen at random."
2035 (org-drill-present-multicloze-hide-n -1))
2038 (defun org-drill-present-multicloze-show2 ()
2039 "Similar to `org-drill-present-multicloze-show1', but reveals two
2040 pieces rather than one."
2041 (org-drill-present-multicloze-hide-n -2))
2044 (defun org-drill-present-card-using-text (question &optional answer)
2045 "Present the string QUESTION as the only visible content of the card.
2046 If ANSWER is supplied, set the global variable `drill-answer' to its value."
2047 (if answer (setq drill-answer answer))
2048 (with-hidden-comments
2049 (with-replaced-entry-text
2050 (concat "\n" question)
2051 (org-drill-hide-all-subheadings-except nil)
2052 (org-cycle-hide-drawers 'all)
2053 (ignore-errors
2054 (org-display-inline-images t))
2055 (prog1 (org-drill-presentation-prompt)
2056 (org-drill-hide-subheadings-if 'org-drill-entry-p)))))
2059 (defun org-drill-present-card-using-multiple-overlays (replacements &optional answer)
2060 "TEXTS is a list of valid values for the 'display' text property.
2061 Present these overlays, in sequence, as the only
2062 visible content of the card.
2063 If ANSWER is supplied, set the global variable `drill-answer' to its value."
2064 (if answer (setq drill-answer answer))
2065 (with-hidden-comments
2066 (with-replaced-entry-text-multi
2067 replacements
2068 (org-drill-hide-all-subheadings-except nil)
2069 (org-cycle-hide-drawers 'all)
2070 (ignore-errors
2071 (org-display-inline-images t))
2072 (prog1 (org-drill-presentation-prompt)
2073 (org-drill-hide-subheadings-if 'org-drill-entry-p)))))
2076 (defun org-drill-entry ()
2077 "Present the current topic for interactive review, as in `org-drill'.
2078 Review will occur regardless of whether the topic is due for review or whether
2079 it meets the definition of a 'review topic' used by `org-drill'.
2081 Returns a quality rating from 0 to 5, or nil if the user quit, or the symbol
2082 EDIT if the user chose to exit the drill and edit the current item. Choosing
2083 the latter option leaves the drill session suspended; it can be resumed
2084 later using `org-drill-resume'.
2086 See `org-drill' for more details."
2087 (interactive)
2088 (org-drill-goto-drill-entry-heading)
2089 ;;(unless (org-part-of-drill-entry-p)
2090 ;; (error "Point is not inside a drill entry"))
2091 ;;(unless (org-at-heading-p)
2092 ;; (org-back-to-heading))
2093 (let ((card-type (org-entry-get (point) "DRILL_CARD_TYPE" t))
2094 (answer-fn 'org-drill-present-default-answer)
2095 (present-empty-cards nil)
2096 (cont nil)
2097 ;; fontification functions in `outline-view-change-hook' can cause big
2098 ;; slowdowns, so we temporarily bind this variable to nil here.
2099 (outline-view-change-hook nil))
2100 (setq drill-answer nil)
2101 (org-save-outline-visibility t
2102 (save-restriction
2103 (org-narrow-to-subtree)
2104 (org-show-subtree)
2105 (org-cycle-hide-drawers 'all)
2107 (let ((presentation-fn
2108 (cdr (assoc card-type org-drill-card-type-alist))))
2109 (if (listp presentation-fn)
2110 (psetq answer-fn (or (second presentation-fn)
2111 'org-drill-present-default-answer)
2112 present-empty-cards (third presentation-fn)
2113 presentation-fn (first presentation-fn)))
2114 (cond
2115 ((null presentation-fn)
2116 (message "%s:%d: Unrecognised card type '%s', skipping..."
2117 (buffer-name) (point) card-type)
2118 (sit-for 0.5)
2119 'skip)
2121 (setq cont (funcall presentation-fn))
2122 (cond
2123 ((not cont)
2124 (message "Quit")
2125 nil)
2126 ((eql cont 'edit)
2127 'edit)
2128 ((eql cont 'skip)
2129 'skip)
2131 (save-excursion
2132 (funcall answer-fn
2133 (lambda () (org-drill-reschedule)))))))))))))
2136 (defun org-drill-entries-pending-p ()
2137 (or *org-drill-again-entries*
2138 *org-drill-current-item*
2139 (and (not (org-drill-maximum-item-count-reached-p))
2140 (not (org-drill-maximum-duration-reached-p))
2141 (or *org-drill-new-entries*
2142 *org-drill-failed-entries*
2143 *org-drill-young-mature-entries*
2144 *org-drill-old-mature-entries*
2145 *org-drill-overdue-entries*
2146 *org-drill-again-entries*))))
2149 (defun org-drill-pending-entry-count ()
2150 (+ (if (markerp *org-drill-current-item*) 1 0)
2151 (length *org-drill-new-entries*)
2152 (length *org-drill-failed-entries*)
2153 (length *org-drill-young-mature-entries*)
2154 (length *org-drill-old-mature-entries*)
2155 (length *org-drill-overdue-entries*)
2156 (length *org-drill-again-entries*)))
2159 (defun org-drill-maximum-duration-reached-p ()
2160 "Returns true if the current drill session has continued past its
2161 maximum duration."
2162 (and org-drill-maximum-duration
2163 (not *org-drill-cram-mode*)
2164 *org-drill-start-time*
2165 (> (- (float-time (current-time)) *org-drill-start-time*)
2166 (* org-drill-maximum-duration 60))))
2169 (defun org-drill-maximum-item-count-reached-p ()
2170 "Returns true if the current drill session has reached the
2171 maximum number of items."
2172 (and org-drill-maximum-items-per-session
2173 (not *org-drill-cram-mode*)
2174 (>= (length *org-drill-done-entries*)
2175 org-drill-maximum-items-per-session)))
2178 (defun org-drill-pop-next-pending-entry ()
2179 (block org-drill-pop-next-pending-entry
2180 (let ((m nil))
2181 (while (or (null m)
2182 (not (org-drill-entry-p m)))
2183 (setq
2185 (cond
2186 ;; First priority is items we failed in a prior session.
2187 ((and *org-drill-failed-entries*
2188 (not (org-drill-maximum-item-count-reached-p))
2189 (not (org-drill-maximum-duration-reached-p)))
2190 (pop-random *org-drill-failed-entries*))
2191 ;; Next priority is overdue items.
2192 ((and *org-drill-overdue-entries*
2193 (not (org-drill-maximum-item-count-reached-p))
2194 (not (org-drill-maximum-duration-reached-p)))
2195 ;; We use `pop', not `pop-random', because we have already
2196 ;; sorted overdue items into a random order which takes
2197 ;; number of days overdue into account.
2198 (pop *org-drill-overdue-entries*))
2199 ;; Next priority is 'young' items.
2200 ((and *org-drill-young-mature-entries*
2201 (not (org-drill-maximum-item-count-reached-p))
2202 (not (org-drill-maximum-duration-reached-p)))
2203 (pop-random *org-drill-young-mature-entries*))
2204 ;; Next priority is newly added items, and older entries.
2205 ;; We pool these into a single group.
2206 ((and (or *org-drill-new-entries*
2207 *org-drill-old-mature-entries*)
2208 (not (org-drill-maximum-item-count-reached-p))
2209 (not (org-drill-maximum-duration-reached-p)))
2210 (cond
2211 ((< (random* (+ (length *org-drill-new-entries*)
2212 (length *org-drill-old-mature-entries*)))
2213 (length *org-drill-new-entries*))
2214 (pop-random *org-drill-new-entries*))
2216 (pop-random *org-drill-old-mature-entries*))))
2217 ;; After all the above are done, last priority is items
2218 ;; that were failed earlier THIS SESSION.
2219 (*org-drill-again-entries*
2220 (pop *org-drill-again-entries*))
2221 (t ; nothing left -- return nil
2222 (return-from org-drill-pop-next-pending-entry nil)))))
2223 m)))
2226 (defun org-drill-entries (&optional resuming-p)
2227 "Returns nil, t, or a list of markers representing entries that were
2228 'failed' and need to be presented again before the session ends.
2230 RESUMING-P is true if we are resuming a suspended drill session."
2231 (block org-drill-entries
2232 (while (org-drill-entries-pending-p)
2233 (let ((m (cond
2234 ((or (not resuming-p)
2235 (null *org-drill-current-item*)
2236 (not (org-drill-entry-p *org-drill-current-item*)))
2237 (org-drill-pop-next-pending-entry))
2238 (t ; resuming a suspended session.
2239 (setq resuming-p nil)
2240 *org-drill-current-item*))))
2241 (setq *org-drill-current-item* m)
2242 (unless m
2243 (error "Unexpectedly ran out of pending drill items"))
2244 (save-excursion
2245 (org-drill-goto-entry m)
2246 (cond
2247 ((not (org-drill-entry-due-p))
2248 ;; The entry is not due anymore. This could arise if the user
2249 ;; suspends a drill session, then drills an individual entry,
2250 ;; then resumes the session.
2251 (message "Entry no longer due, skipping...")
2252 (sit-for 0.3)
2253 nil)
2255 (setq result (org-drill-entry))
2256 (cond
2257 ((null result)
2258 (message "Quit")
2259 (setq end-pos :quit)
2260 (return-from org-drill-entries nil))
2261 ((eql result 'edit)
2262 (setq end-pos (point-marker))
2263 (return-from org-drill-entries nil))
2264 ((eql result 'skip)
2265 (setq *org-drill-current-item* nil)
2266 nil) ; skip this item
2268 (cond
2269 ((<= result org-drill-failure-quality)
2270 (if *org-drill-again-entries*
2271 (setq *org-drill-again-entries*
2272 (shuffle-list *org-drill-again-entries*)))
2273 (push-end m *org-drill-again-entries*))
2275 (push m *org-drill-done-entries*)))
2276 (setq *org-drill-current-item* nil))))))))))
2280 (defun org-drill-final-report ()
2281 (let ((pass-percent
2282 (round (* 100 (count-if (lambda (qual)
2283 (> qual org-drill-failure-quality))
2284 *org-drill-session-qualities*))
2285 (max 1 (length *org-drill-session-qualities*))))
2286 (prompt nil)
2287 (max-mini-window-height 0.6))
2288 (setq prompt
2289 (format
2290 "%d items reviewed. Session duration %s.
2291 Recall of reviewed items:
2292 Excellent (5): %3d%% | Near miss (2): %3d%%
2293 Good (4): %3d%% | Failure (1): %3d%%
2294 Hard (3): %3d%% | Abject failure (0): %3d%%
2296 You successfully recalled %d%% of reviewed items (quality > %s)
2297 %d/%d items still await review (%s, %s, %s, %s, %s).
2298 Tomorrow, %d more items will become due for review.
2299 Session finished. Press a key to continue..."
2300 (length *org-drill-done-entries*)
2301 (format-seconds "%h:%.2m:%.2s"
2302 (- (float-time (current-time)) *org-drill-start-time*))
2303 (round (* 100 (count 5 *org-drill-session-qualities*))
2304 (max 1 (length *org-drill-session-qualities*)))
2305 (round (* 100 (count 2 *org-drill-session-qualities*))
2306 (max 1 (length *org-drill-session-qualities*)))
2307 (round (* 100 (count 4 *org-drill-session-qualities*))
2308 (max 1 (length *org-drill-session-qualities*)))
2309 (round (* 100 (count 1 *org-drill-session-qualities*))
2310 (max 1 (length *org-drill-session-qualities*)))
2311 (round (* 100 (count 3 *org-drill-session-qualities*))
2312 (max 1 (length *org-drill-session-qualities*)))
2313 (round (* 100 (count 0 *org-drill-session-qualities*))
2314 (max 1 (length *org-drill-session-qualities*)))
2315 pass-percent
2316 org-drill-failure-quality
2317 (org-drill-pending-entry-count)
2318 (+ (org-drill-pending-entry-count)
2319 *org-drill-dormant-entry-count*)
2320 (propertize
2321 (format "%d failed"
2322 (+ (length *org-drill-failed-entries*)
2323 (length *org-drill-again-entries*)))
2324 'face `(:foreground ,org-drill-failed-count-color))
2325 (propertize
2326 (format "%d overdue"
2327 (length *org-drill-overdue-entries*))
2328 'face `(:foreground ,org-drill-failed-count-color))
2329 (propertize
2330 (format "%d new"
2331 (length *org-drill-new-entries*))
2332 'face `(:foreground ,org-drill-new-count-color))
2333 (propertize
2334 (format "%d young"
2335 (length *org-drill-young-mature-entries*))
2336 'face `(:foreground ,org-drill-mature-count-color))
2337 (propertize
2338 (format "%d old"
2339 (length *org-drill-old-mature-entries*))
2340 'face `(:foreground ,org-drill-mature-count-color))
2341 *org-drill-due-tomorrow-count*
2344 (while (not (input-pending-p))
2345 (message "%s" prompt)
2346 (sit-for 0.5))
2347 (read-char-exclusive)
2349 (if (and *org-drill-session-qualities*
2350 (< pass-percent (- 100 org-drill-forgetting-index)))
2351 (read-char-exclusive
2352 (format
2354 You failed %d%% of the items you reviewed during this session.
2355 %d (%d%%) of all items scanned were overdue.
2357 Are you keeping up with your items, and reviewing them
2358 when they are scheduled? If so, you may want to consider
2359 lowering the value of `org-drill-learn-fraction' slightly in
2360 order to make items appear more frequently over time."
2361 (propertize "WARNING!" 'face 'org-warning)
2362 (- 100 pass-percent)
2363 *org-drill-overdue-entry-count*
2364 (round (* 100 *org-drill-overdue-entry-count*)
2365 (+ *org-drill-dormant-entry-count*
2366 *org-drill-due-entry-count*)))
2367 ))))
2371 (defun org-drill-free-markers (markers)
2372 "MARKERS is a list of markers, all of which will be freed (set to
2373 point nowhere). Alternatively, MARKERS can be 't', in which case
2374 all the markers used by Org-Drill will be freed."
2375 (dolist (m (if (eql t markers)
2376 (append *org-drill-done-entries*
2377 *org-drill-new-entries*
2378 *org-drill-failed-entries*
2379 *org-drill-again-entries*
2380 *org-drill-overdue-entries*
2381 *org-drill-young-mature-entries*
2382 *org-drill-old-mature-entries*)
2383 markers))
2384 (free-marker m)))
2387 (defun org-drill-order-overdue-entries (overdue-data)
2388 (setq *org-drill-overdue-entries*
2389 (mapcar 'car
2390 (sort (shuffle-list overdue-data)
2391 (lambda (a b) (> (cdr a) (cdr b)))))))
2394 (defun org-drill-entry-status ()
2395 "Returns a list (STATUS DUE) where DUE is the number of days overdue,
2396 zero being due today, -1 being scheduled 1 day in the future. STATUS is
2397 one of the following values:
2398 - nil, if the item is not a drill entry, or has an empty body
2399 - :unscheduled
2400 - :future
2401 - :new
2402 - :failed
2403 - :overdue
2404 - :young
2405 - :old
2407 (save-excursion
2408 (unless (org-at-heading-p)
2409 (org-back-to-heading))
2410 (let ((due (org-drill-entry-days-overdue))
2411 (last-int (org-drill-entry-last-interval 1)))
2412 (list
2413 (cond
2414 ((not (org-drill-entry-p))
2415 nil)
2416 ((and (org-entry-empty-p)
2417 (let* ((card-type (org-entry-get (point) "DRILL_CARD_TYPE" nil))
2418 (dat (cdr (assoc card-type org-drill-card-type-alist))))
2419 (or (null card-type)
2420 (not (third dat)))))
2421 ;; body is empty, and this is not a card type where empty bodies are
2422 ;; meaningful, so skip it.
2423 nil)
2424 ((null due) ; unscheduled - usually a skipped leech
2425 :unscheduled)
2426 ;; ((eql -1 due)
2427 ;; :tomorrow)
2428 ((minusp due) ; scheduled in the future
2429 :future)
2430 ;; The rest of the stati all denote 'due' items ==========================
2431 ((<= (org-drill-entry-last-quality 9999)
2432 org-drill-failure-quality)
2433 ;; Mature entries that were failed last time are
2434 ;; FAILED, regardless of how young, old or overdue
2435 ;; they are.
2436 :failed)
2437 ((org-drill-entry-new-p)
2438 :new)
2439 ((org-drill-entry-overdue-p due last-int)
2440 ;; Overdue status overrides young versus old
2441 ;; distinction.
2442 ;; Store marker + due, for sorting of overdue entries
2443 :overdue)
2444 ((<= (org-drill-entry-last-interval 9999)
2445 org-drill-days-before-old)
2446 :young)
2448 :old))
2449 due))))
2452 (defun org-drill-progress-message (collected scanned)
2453 (when (zerop (% scanned 50))
2454 (let* ((meter-width 40)
2455 (sym1 (if (oddp (floor scanned (* 50 meter-width))) ?| ?.))
2456 (sym2 (if (eql sym1 ?.) ?| ?.)))
2457 (message "Collecting due drill items:%4d %s%s"
2458 collected
2459 (make-string (% (ceiling scanned 50) meter-width)
2460 sym2)
2461 (make-string (- meter-width (% (ceiling scanned 50) meter-width))
2462 sym1)))))
2465 (defun org-drill (&optional scope drill-match resume-p)
2466 "Begin an interactive 'drill session'. The user is asked to
2467 review a series of topics (headers). Each topic is initially
2468 presented as a 'question', often with part of the topic content
2469 hidden. The user attempts to recall the hidden information or
2470 answer the question, then presses a key to reveal the answer. The
2471 user then rates his or her recall or performance on that
2472 topic. This rating information is used to reschedule the topic
2473 for future review.
2475 Org-drill proceeds by:
2477 - Finding all topics (headings) in SCOPE which have either been
2478 used and rescheduled before, or which have a tag that matches
2479 `org-drill-question-tag'.
2481 - All matching topics which are either unscheduled, or are
2482 scheduled for the current date or a date in the past, are
2483 considered to be candidates for the drill session.
2485 - If `org-drill-maximum-items-per-session' is set, a random
2486 subset of these topics is presented. Otherwise, all of the
2487 eligible topics will be presented.
2489 SCOPE determines the scope in which to search for
2490 questions. It accepts the same values as `org-drill-scope',
2491 which see.
2493 DRILL-MATCH, if supplied, is a string specifying a tags/property/
2494 todo query. Only items matching the query will be considered.
2495 It accepts the same values as `org-drill-match', which see.
2497 If RESUME-P is non-nil, resume a suspended drill session rather
2498 than starting a new one."
2500 (interactive)
2501 ;; Check org version. Org 7.9.3f introduced a backwards-incompatible change
2502 ;; to the arguments accepted by `org-schedule'. At the time of writing there
2503 ;; are still lots of people using versions of org older than this.
2504 (let ((majorv (first (mapcar 'string-to-number (split-string (org-release) "[.]")))))
2505 (if (and (< majorv 8)
2506 (not (string-match-p "universal prefix argument" (documentation 'org-schedule))))
2507 (read-char-exclusive
2508 (format "Warning: org-drill requires org mode 7.9.3f or newer. Scheduling of failed cards will not
2509 work correctly with older versions of org mode. Your org mode version (%s) appears to be older than
2510 7.9.3f. Please consider installing a more recent version of org mode." (org-release)))))
2511 (let ((end-pos nil)
2512 (overdue-data nil)
2513 (cnt 0))
2514 (block org-drill
2515 (unless resume-p
2516 (org-drill-free-markers t)
2517 (setq *org-drill-current-item* nil
2518 *org-drill-done-entries* nil
2519 *org-drill-dormant-entry-count* 0
2520 *org-drill-due-entry-count* 0
2521 *org-drill-due-tomorrow-count* 0
2522 *org-drill-overdue-entry-count* 0
2523 *org-drill-new-entries* nil
2524 *org-drill-overdue-entries* nil
2525 *org-drill-young-mature-entries* nil
2526 *org-drill-old-mature-entries* nil
2527 *org-drill-failed-entries* nil
2528 *org-drill-again-entries* nil)
2529 (setq *org-drill-session-qualities* nil)
2530 (setq *org-drill-start-time* (float-time (current-time))))
2531 (setq *random-state* (make-random-state t)) ; reseed RNG
2532 (unwind-protect
2533 (save-excursion
2534 (unless resume-p
2535 (let ((org-trust-scanner-tags t)
2536 (warned-about-id-creation nil))
2537 (org-map-drill-entries
2538 (lambda ()
2539 (org-drill-progress-message
2540 (+ (length *org-drill-new-entries*)
2541 (length *org-drill-overdue-entries*)
2542 (length *org-drill-young-mature-entries*)
2543 (length *org-drill-old-mature-entries*)
2544 (length *org-drill-failed-entries*))
2545 (incf cnt))
2546 (cond
2547 ((not (org-drill-entry-p))
2548 nil) ; skip
2550 (when (and (not warned-about-id-creation)
2551 (null (org-id-get)))
2552 (message (concat "Creating unique IDs for items "
2553 "(slow, but only happens once)"))
2554 (sit-for 0.5)
2555 (setq warned-about-id-creation t))
2556 (org-id-get-create) ; ensure drill entry has unique ID
2557 (destructuring-bind (status due) (org-drill-entry-status)
2558 (case status
2559 (:unscheduled
2560 (incf *org-drill-dormant-entry-count*))
2561 ;; (:tomorrow
2562 ;; (incf *org-drill-dormant-entry-count*)
2563 ;; (incf *org-drill-due-tomorrow-count*))
2564 (:future
2565 (incf *org-drill-dormant-entry-count*)
2566 (if (eq -1 due)
2567 (incf *org-drill-due-tomorrow-count*)))
2568 (:new
2569 (push (point-marker) *org-drill-new-entries*))
2570 (:failed
2571 (push (point-marker) *org-drill-failed-entries*))
2572 (:young
2573 (push (point-marker) *org-drill-young-mature-entries*))
2574 (:overdue
2575 (push (cons (point-marker) due) overdue-data))
2576 (:old
2577 (push (point-marker) *org-drill-old-mature-entries*))
2578 )))))
2579 scope drill-match)
2580 (org-drill-order-overdue-entries overdue-data)
2581 (setq *org-drill-overdue-entry-count*
2582 (length *org-drill-overdue-entries*))))
2583 (setq *org-drill-due-entry-count* (org-drill-pending-entry-count))
2584 (cond
2585 ((and (null *org-drill-current-item*)
2586 (null *org-drill-new-entries*)
2587 (null *org-drill-failed-entries*)
2588 (null *org-drill-overdue-entries*)
2589 (null *org-drill-young-mature-entries*)
2590 (null *org-drill-old-mature-entries*))
2591 (message "I did not find any pending drill items."))
2593 (org-drill-entries resume-p)
2594 (message "Drill session finished!"))))
2595 (progn
2596 (unless end-pos
2597 (setq *org-drill-cram-mode* nil)
2598 (org-drill-free-markers *org-drill-done-entries*)))))
2599 (cond
2600 (end-pos
2601 (when (markerp end-pos)
2602 (org-drill-goto-entry end-pos)
2603 (org-reveal)
2604 (org-show-entry))
2605 (let ((keystr (command-keybinding-to-string 'org-drill-resume)))
2606 (message
2607 "You can continue the drill session with the command `org-drill-resume'.%s"
2608 (if keystr (format "\nYou can run this command by pressing %s." keystr)
2609 ""))))
2611 (org-drill-final-report)
2612 (if (eql 'sm5 org-drill-spaced-repetition-algorithm)
2613 (org-drill-save-optimal-factor-matrix))
2614 (if org-drill-save-buffers-after-drill-sessions-p
2615 (save-some-buffers))
2616 (message "Drill session finished!")
2617 ))))
2620 (defun org-drill-save-optimal-factor-matrix ()
2621 (message "Saving optimal factor matrix...")
2622 (customize-save-variable 'org-drill-optimal-factor-matrix
2623 org-drill-optimal-factor-matrix))
2626 (defun org-drill-cram (&optional scope drill-match)
2627 "Run an interactive drill session in 'cram mode'. In cram mode,
2628 all drill items are considered to be due for review, unless they
2629 have been reviewed within the last `org-drill-cram-hours'
2630 hours."
2631 (interactive)
2632 (setq *org-drill-cram-mode* t)
2633 (org-drill scope drill-match))
2636 (defun org-drill-tree ()
2637 "Run an interactive drill session using drill items within the
2638 subtree at point."
2639 (interactive)
2640 (org-drill 'tree))
2643 (defun org-drill-directory ()
2644 "Run an interactive drill session using drill items from all org
2645 files in the same directory as the current file."
2646 (interactive)
2647 (org-drill 'directory))
2650 (defun org-drill-again (&optional scope drill-match)
2651 "Run a new drill session, but try to use leftover due items that
2652 were not reviewed during the last session, rather than scanning for
2653 unreviewed items. If there are no leftover items in memory, a full
2654 scan will be performed."
2655 (interactive)
2656 (setq *org-drill-cram-mode* nil)
2657 (cond
2658 ((plusp (org-drill-pending-entry-count))
2659 (org-drill-free-markers *org-drill-done-entries*)
2660 (if (markerp *org-drill-current-item*)
2661 (free-marker *org-drill-current-item*))
2662 (setq *org-drill-start-time* (float-time (current-time))
2663 *org-drill-done-entries* nil
2664 *org-drill-current-item* nil)
2665 (org-drill scope drill-match t))
2667 (org-drill scope drill-match))))
2671 (defun org-drill-resume ()
2672 "Resume a suspended drill session. Sessions are suspended by
2673 exiting them with the `edit' or `quit' options."
2674 (interactive)
2675 (cond
2676 ((org-drill-entries-pending-p)
2677 (org-drill nil nil t))
2678 ((and (plusp (org-drill-pending-entry-count))
2679 ;; Current drill session is finished, but there are still
2680 ;; more items which need to be reviewed.
2681 (y-or-n-p (format
2682 "You have finished the drill session. However, %d items still
2683 need reviewing. Start a new drill session? "
2684 (org-drill-pending-entry-count))))
2685 (org-drill-again))
2687 (message "You have finished the drill session."))))
2690 (defun org-drill-relearn-item ()
2691 "Make the current item due for revision, and set its last interval to 0.
2692 Makes the item behave as if it has been failed, without actually recording a
2693 failure. This command can be used to 'reset' repetitions for an item."
2694 (interactive)
2695 (org-drill-smart-reschedule 4 0))
2698 (defun org-drill-strip-entry-data ()
2699 (dolist (prop org-drill-scheduling-properties)
2700 (org-delete-property prop))
2701 (org-schedule '(4)))
2704 (defun org-drill-strip-all-data (&optional scope)
2705 "Delete scheduling data from every drill entry in scope. This
2706 function may be useful if you want to give your collection of
2707 entries to someone else. Scope defaults to the current buffer,
2708 and is specified by the argument SCOPE, which accepts the same
2709 values as `org-drill-scope'."
2710 (interactive)
2711 (when (yes-or-no-p
2712 "Delete scheduling data from ALL items in scope: are you sure?")
2713 (cond
2714 ((null scope)
2715 ;; Scope is the current buffer. This means we can use
2716 ;; `org-delete-property-globally', which is faster.
2717 (dolist (prop org-drill-scheduling-properties)
2718 (org-delete-property-globally prop))
2719 (org-map-drill-entries (lambda () (org-schedule '(4))) scope))
2721 (org-map-drill-entries 'org-drill-strip-entry-data scope)))
2722 (message "Done.")))
2725 (defun org-drill-add-cloze-fontification ()
2726 ;; Compute local versions of the regexp for cloze deletions, in case
2727 ;; the left and right delimiters are redefined locally.
2728 (setq-local org-drill-cloze-regexp (org-drill--compute-cloze-regexp))
2729 (setq-local org-drill-cloze-keywords (org-drill--compute-cloze-keywords))
2730 (when org-drill-use-visible-cloze-face-p
2731 (add-to-list 'org-font-lock-extra-keywords
2732 (first org-drill-cloze-keywords))))
2734 (add-hook 'org-font-lock-set-keywords-hook 'org-drill-add-cloze-fontification)
2736 ;; Can't add to org-mode-hook, because local variables won't have been loaded
2737 ;; yet.
2739 ;; (defun org-drill-add-cloze-fontification ()
2740 ;; (when (eql major-mode 'org-mode)
2741 ;; ;; Compute local versions of the regexp for cloze deletions, in case
2742 ;; ;; the left and right delimiters are redefined locally.
2743 ;; (setq-local org-drill-cloze-regexp (org-drill--compute-cloze-regexp))
2744 ;; (setq-local org-drill-cloze-keywords (org-drill--compute-cloze-keywords))
2745 ;; (when org-drill-use-visible-cloze-face-p
2746 ;; (font-lock-add-keywords nil ;'org-mode
2747 ;; org-drill-cloze-keywords
2748 ;; nil))))
2750 ;; XXX
2751 ;; (add-hook 'hack-local-variables-hook
2752 ;; 'org-drill-add-cloze-fontification)
2754 ;; (org-drill-add-cloze-fontification)
2757 ;;; Synching card collections =================================================
2760 (defvar *org-drill-dest-id-table* (make-hash-table :test 'equal))
2763 (defun org-drill-copy-entry-to-other-buffer (dest &optional path)
2764 "Copy the subtree at point to the buffer DEST. The copy will receive
2765 the tag 'imported'."
2766 (block org-drill-copy-entry-to-other-buffer
2767 (save-excursion
2768 (let ((src (current-buffer))
2769 (m nil))
2770 (cl-flet ((paste-tree-here (&optional level)
2771 (org-paste-subtree level)
2772 (org-drill-strip-entry-data)
2773 (org-toggle-tag "imported" 'on)
2774 (org-map-drill-entries
2775 (lambda ()
2776 (let ((id (org-id-get)))
2777 (org-drill-strip-entry-data)
2778 (unless (gethash id *org-drill-dest-id-table*)
2779 (puthash id (point-marker)
2780 *org-drill-dest-id-table*))))
2781 'tree)))
2782 (unless path
2783 (setq path (org-get-outline-path)))
2784 (org-copy-subtree)
2785 (switch-to-buffer dest)
2786 (setq m
2787 (condition-case nil
2788 (org-find-olp path t)
2789 (error ; path does not exist in DEST
2790 (return-from org-drill-copy-entry-to-other-buffer
2791 (cond
2792 ((cdr path)
2793 (org-drill-copy-entry-to-other-buffer
2794 dest (butlast path)))
2796 ;; We've looked all the way up the path
2797 ;; Default to appending to the end of DEST
2798 (goto-char (point-max))
2799 (newline)
2800 (paste-tree-here)))))))
2801 (goto-char m)
2802 (outline-next-heading)
2803 (newline)
2804 (forward-line -1)
2805 (paste-tree-here (1+ (or (org-current-level) 0)))
2806 )))))
2810 (defun org-drill-merge-buffers (src &optional dest ignore-new-items-p)
2811 "SRC and DEST are two org mode buffers containing drill items.
2812 For each drill item in DEST that shares an ID with an item in SRC,
2813 overwrite scheduling data in DEST with data taken from the item in SRC.
2814 This is intended for use when two people are sharing a set of drill items,
2815 one person has made some updates to the item set, and the other person
2816 wants to migrate to the updated set without losing their scheduling data.
2818 By default, any drill items in SRC which do not exist in DEST are
2819 copied into DEST. We attempt to place the copied item in the
2820 equivalent location in DEST to its location in SRC, by matching
2821 the heading hierarchy. However if IGNORE-NEW-ITEMS-P is non-nil,
2822 we simply ignore any items that do not exist in DEST, and do not
2823 copy them across."
2824 (interactive "bImport scheduling info from which buffer?")
2825 (unless dest
2826 (setq dest (current-buffer)))
2827 (setq src (get-buffer src)
2828 dest (get-buffer dest))
2829 (when (yes-or-no-p
2830 (format
2831 (concat "About to overwrite all scheduling data for drill items in `%s' "
2832 "with information taken from matching items in `%s'. Proceed? ")
2833 (buffer-name dest) (buffer-name src)))
2834 ;; Compile list of all IDs in the destination buffer.
2835 (clrhash *org-drill-dest-id-table*)
2836 (with-current-buffer dest
2837 (org-map-drill-entries
2838 (lambda ()
2839 (let ((this-id (org-id-get)))
2840 (when this-id
2841 (puthash this-id (point-marker) *org-drill-dest-id-table*))))
2842 'file))
2843 ;; Look through all entries in source buffer.
2844 (with-current-buffer src
2845 (org-map-drill-entries
2846 (lambda ()
2847 (let ((id (org-id-get))
2848 (last-quality nil) (last-reviewed nil)
2849 (scheduled-time nil))
2850 (cond
2851 ((or (null id)
2852 (not (org-drill-entry-p)))
2853 nil)
2854 ((gethash id *org-drill-dest-id-table*)
2855 ;; This entry matches an entry in dest. Retrieve all its
2856 ;; scheduling data, then go to the matching location in dest
2857 ;; and write the data.
2858 (let ((marker (gethash id *org-drill-dest-id-table*)))
2859 (destructuring-bind (last-interval repetitions failures
2860 total-repeats meanq ease)
2861 (org-drill-get-item-data)
2862 (setq last-reviewed (org-entry-get (point) "DRILL_LAST_REVIEWED")
2863 last-quality (org-entry-get (point) "DRILL_LAST_QUALITY")
2864 scheduled-time (org-get-scheduled-time (point)))
2865 (save-excursion
2866 ;; go to matching entry in destination buffer
2867 (switch-to-buffer (marker-buffer marker))
2868 (goto-char marker)
2869 (org-drill-strip-entry-data)
2870 (unless (zerop total-repeats)
2871 (org-drill-store-item-data last-interval repetitions failures
2872 total-repeats meanq ease)
2873 (if last-quality
2874 (org-set-property "LAST_QUALITY" last-quality)
2875 (org-delete-property "LAST_QUALITY"))
2876 (if last-reviewed
2877 (org-set-property "LAST_REVIEWED" last-reviewed)
2878 (org-delete-property "LAST_REVIEWED"))
2879 (if scheduled-time
2880 (org-schedule nil scheduled-time)))))
2881 (remhash id *org-drill-dest-id-table*)
2882 (free-marker marker)))
2884 ;; item in SRC has ID, but no matching ID in DEST.
2885 ;; It must be a new item that does not exist in DEST.
2886 ;; Copy the entire item to the *end* of DEST.
2887 (unless ignore-new-items-p
2888 (org-drill-copy-entry-to-other-buffer dest))))))
2889 'file))
2890 ;; Finally: there may be some items in DEST which are not in SRC, and
2891 ;; which have been scheduled by another user of DEST. Clear out the
2892 ;; scheduling info from all the unmatched items in DEST.
2893 (with-current-buffer dest
2894 (maphash (lambda (id m)
2895 (goto-char m)
2896 (org-drill-strip-entry-data)
2897 (free-marker m))
2898 *org-drill-dest-id-table*))))
2902 ;;; Card types for learning languages =========================================
2904 ;;; Get spell-number.el from:
2905 ;;; http://www.emacswiki.org/emacs/spell-number.el
2906 (autoload 'spelln-integer-in-words "spell-number")
2909 ;;; `conjugate' card type =====================================================
2910 ;;; See spanish.org for usage
2912 (defvar org-drill-verb-tense-alist
2913 '(("present" "tomato")
2914 ("simple present" "tomato")
2915 ("present indicative" "tomato")
2916 ;; past tenses
2917 ("past" "purple")
2918 ("simple past" "purple")
2919 ("preterite" "purple")
2920 ("imperfect" "darkturquoise")
2921 ("present perfect" "royalblue")
2922 ;; future tenses
2923 ("future" "green")
2924 ;; moods (backgrounds).
2925 ("indicative" nil) ; default
2926 ("subjunctive" "medium blue")
2927 ("conditional" "grey30")
2928 ("negative imperative" "red4")
2929 ("positive imperative" "darkgreen")
2931 "Alist where each entry has the form (TENSE COLOUR), where
2932 TENSE is a string naming a tense in which verbs can be
2933 conjugated, and COLOUR is a string specifying a foreground colour
2934 which will be used by `org-drill-present-verb-conjugation' and
2935 `org-drill-show-answer-verb-conjugation' to fontify the verb and
2936 the name of the tense.")
2939 (defun org-drill-get-verb-conjugation-info ()
2940 "Auxiliary function used by `org-drill-present-verb-conjugation' and
2941 `org-drill-show-answer-verb-conjugation'."
2942 (let ((infinitive (org-entry-get (point) "VERB_INFINITIVE" t))
2943 (inf-hint (org-entry-get (point) "VERB_INFINITIVE_HINT" t))
2944 (translation (org-entry-get (point) "VERB_TRANSLATION" t))
2945 (tense (org-entry-get (point) "VERB_TENSE" nil))
2946 (mood (org-entry-get (point) "VERB_MOOD" nil))
2947 (highlight-face nil))
2948 (unless (and infinitive translation (or tense mood))
2949 (error "Missing information for verb conjugation card (%s, %s, %s, %s) at %s"
2950 infinitive translation tense mood (point)))
2951 (setq tense (if tense (downcase (car (read-from-string tense))))
2952 mood (if mood (downcase (car (read-from-string mood))))
2953 infinitive (car (read-from-string infinitive))
2954 inf-hint (if inf-hint (car (read-from-string inf-hint)))
2955 translation (car (read-from-string translation)))
2956 (setq highlight-face
2957 (list :foreground
2958 (or (second (assoc-string tense org-drill-verb-tense-alist t))
2959 "hotpink")
2960 :background
2961 (second (assoc-string mood org-drill-verb-tense-alist t))))
2962 (setq infinitive (propertize infinitive 'face highlight-face))
2963 (setq translation (propertize translation 'face highlight-face))
2964 (if tense (setq tense (propertize tense 'face highlight-face)))
2965 (if mood (setq mood (propertize mood 'face highlight-face)))
2966 (list infinitive inf-hint translation tense mood)))
2969 (defun org-drill-present-verb-conjugation ()
2970 "Present a drill entry whose card type is 'conjugate'."
2971 (cl-flet ((tense-and-mood-to-string
2972 (tense mood)
2973 (cond
2974 ((and tense mood)
2975 (format "%s tense, %s mood" tense mood))
2976 (tense
2977 (format "%s tense" tense))
2978 (mood
2979 (format "%s mood" mood)))))
2980 (destructuring-bind (infinitive inf-hint translation tense mood)
2981 (org-drill-get-verb-conjugation-info)
2982 (org-drill-present-card-using-text
2983 (cond
2984 ((zerop (random* 2))
2985 (format "\nTranslate the verb\n\n%s\n\nand conjugate for the %s.\n\n"
2986 infinitive (tense-and-mood-to-string tense mood)))
2989 (format "\nGive the verb that means\n\n%s %s\n
2990 and conjugate for the %s.\n\n"
2991 translation
2992 (if inf-hint (format " [HINT: %s]" inf-hint) "")
2993 (tense-and-mood-to-string tense mood))))))))
2996 (defun org-drill-show-answer-verb-conjugation (reschedule-fn)
2997 "Show the answer for a drill item whose card type is 'conjugate'.
2998 RESCHEDULE-FN must be a function that calls `org-drill-reschedule' and
2999 returns its return value."
3000 (destructuring-bind (infinitive inf-hint translation tense mood)
3001 (org-drill-get-verb-conjugation-info)
3002 (with-replaced-entry-heading
3003 (format "%s of %s ==> %s\n\n"
3004 (capitalize
3005 (cond
3006 ((and tense mood)
3007 (format "%s tense, %s mood" tense mood))
3008 (tense
3009 (format "%s tense" tense))
3010 (mood
3011 (format "%s mood" mood))))
3012 infinitive translation)
3013 (org-cycle-hide-drawers 'all)
3014 (funcall reschedule-fn))))
3017 ;;; `decline_noun' card type ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3020 (defvar org-drill-noun-gender-alist
3021 '(("masculine" "dodgerblue")
3022 ("masc" "dodgerblue")
3023 ("male" "dodgerblue")
3024 ("m" "dodgerblue")
3025 ("feminine" "orchid")
3026 ("fem" "orchid")
3027 ("female" "orchid")
3028 ("f" "orchid")
3029 ("neuter" "green")
3030 ("neutral" "green")
3031 ("neut" "green")
3032 ("n" "green")
3036 (defun org-drill-get-noun-info ()
3037 "Auxiliary function used by `org-drill-present-noun-declension' and
3038 `org-drill-show-answer-noun-declension'."
3039 (let ((noun (org-entry-get (point) "NOUN" t))
3040 (noun-hint (org-entry-get (point) "NOUN_HINT" t))
3041 (noun-root (org-entry-get (point) "NOUN_ROOT" t))
3042 (noun-gender (org-entry-get (point) "NOUN_GENDER" t))
3043 (translation (org-entry-get (point) "NOUN_TRANSLATION" t))
3044 (highlight-face nil))
3045 (unless (and noun translation)
3046 (error "Missing information for `decline_noun' card (%s, %s, %s, %s) at %s"
3047 noun translation noun-hint noun-root (point)))
3048 (setq noun-root (if noun-root (car (read-from-string noun-root)))
3049 noun (car (read-from-string noun))
3050 noun-gender (downcase (car (read-from-string noun-gender)))
3051 noun-hint (if noun-hint (car (read-from-string noun-hint)))
3052 translation (car (read-from-string translation)))
3053 (setq highlight-face
3054 (list :foreground
3055 (or (second (assoc-string noun-gender
3056 org-drill-noun-gender-alist t))
3057 "red")))
3058 (setq noun (propertize noun 'face highlight-face))
3059 (setq translation (propertize translation 'face highlight-face))
3060 (list noun noun-root noun-gender noun-hint translation)))
3063 (defun org-drill-present-noun-declension ()
3064 "Present a drill entry whose card type is 'decline_noun'."
3065 (destructuring-bind (noun noun-root noun-gender noun-hint translation)
3066 (org-drill-get-noun-info)
3067 (let* ((props (org-entry-properties (point)))
3068 (definite
3069 (cond
3070 ((assoc "DECLINE_DEFINITE" props)
3071 (propertize (if (org-entry-get (point) "DECLINE_DEFINITE")
3072 "definite" "indefinite")
3073 'face 'warning))
3074 (t nil)))
3075 (plural
3076 (cond
3077 ((assoc "DECLINE_PLURAL" props)
3078 (propertize (if (org-entry-get (point) "DECLINE_PLURAL")
3079 "plural" "singular")
3080 'face 'warning))
3081 (t nil))))
3082 (org-drill-present-card-using-text
3083 (cond
3084 ((zerop (random* 2))
3085 (format "\nTranslate the noun\n\n%s (%s)\n\nand list its declensions%s.\n\n"
3086 noun noun-gender
3087 (if (or plural definite)
3088 (format " for the %s %s form" definite plural)
3089 "")))
3091 (format "\nGive the noun that means\n\n%s %s\n
3092 and list its declensions%s.\n\n"
3093 translation
3094 (if noun-hint (format " [HINT: %s]" noun-hint) "")
3095 (if (or plural definite)
3096 (format " for the %s %s form" definite plural)
3097 ""))))))))
3100 (defun org-drill-show-answer-noun-declension (reschedule-fn)
3101 "Show the answer for a drill item whose card type is 'decline_noun'.
3102 RESCHEDULE-FN must be a function that calls `org-drill-reschedule' and
3103 returns its return value."
3104 (destructuring-bind (noun noun-root noun-gender noun-hint translation)
3105 (org-drill-get-noun-info)
3106 (with-replaced-entry-heading
3107 (format "Declensions of %s (%s) ==> %s\n\n"
3108 noun noun-gender translation)
3109 (org-cycle-hide-drawers 'all)
3110 (funcall reschedule-fn))))
3113 ;;; `translate_number' card type ==============================================
3114 ;;; See spanish.org for usage
3117 (defun spelln-integer-in-language (n lang)
3118 (let ((spelln-language lang))
3119 (spelln-integer-in-words n)))
3121 (defun org-drill-present-translate-number ()
3122 (let ((num-min (read (org-entry-get (point) "DRILL_NUMBER_MIN")))
3123 (num-max (read (org-entry-get (point) "DRILL_NUMBER_MAX")))
3124 (language (read (org-entry-get (point) "DRILL_LANGUAGE" t)))
3125 (drilled-number 0)
3126 (drilled-number-direction 'to-english)
3127 (highlight-face 'font-lock-warning-face))
3128 (cond
3129 ((not (fboundp 'spelln-integer-in-words))
3130 (message "`spell-number.el' not loaded, skipping 'translate_number' card...")
3131 (sit-for 0.5)
3132 'skip)
3133 ((not (and (numberp num-min) (numberp num-max) language))
3134 (error "Missing language or minimum or maximum numbers for number card"))
3136 (if (> num-min num-max)
3137 (psetf num-min num-max
3138 num-max num-min))
3139 (setq drilled-number
3140 (+ num-min (random* (abs (1+ (- num-max num-min))))))
3141 (setq drilled-number-direction
3142 (if (zerop (random* 2)) 'from-english 'to-english))
3143 (cond
3144 ((eql 'to-english drilled-number-direction)
3145 (org-drill-present-card-using-text
3146 (format "\nTranslate into English:\n\n%s\n"
3147 (propertize
3148 (spelln-integer-in-language drilled-number language)
3149 'face highlight-face))
3150 (spelln-integer-in-language drilled-number 'english-gb)))
3152 (org-drill-present-card-using-text
3153 (format "\nTranslate into %s:\n\n%s\n"
3154 (capitalize (format "%s" language))
3155 (propertize
3156 (spelln-integer-in-language drilled-number 'english-gb)
3157 'face highlight-face))
3158 (spelln-integer-in-language drilled-number language))))))))
3161 ;; (defun org-drill-show-answer-translate-number (reschedule-fn)
3162 ;; (let* ((language (read (org-entry-get (point) "DRILL_LANGUAGE" t)))
3163 ;; (highlight-face 'font-lock-warning-face)
3164 ;; (non-english
3165 ;; (let ((spelln-language language))
3166 ;; (propertize (spelln-integer-in-words *drilled-number*)
3167 ;; 'face highlight-face)))
3168 ;; (english
3169 ;; (let ((spelln-language 'english-gb))
3170 ;; (propertize (spelln-integer-in-words *drilled-number*)
3171 ;; 'face 'highlight-face))))
3172 ;; (with-replaced-entry-text
3173 ;; (cond
3174 ;; ((eql 'to-english *drilled-number-direction*)
3175 ;; (format "\nThe English translation of %s is:\n\n%s\n"
3176 ;; non-english english))
3177 ;; (t
3178 ;; (format "\nThe %s translation of %s is:\n\n%s\n"
3179 ;; (capitalize (format "%s" language))
3180 ;; english non-english)))
3181 ;; (funcall reschedule-fn))))
3184 ;;; `spanish_verb' card type ==================================================
3185 ;;; Not very interesting, but included to demonstrate how a presentation
3186 ;;; function can manipulate which subheading are hidden versus shown.
3189 (defun org-drill-present-spanish-verb ()
3190 (let ((prompt nil)
3191 (reveal-headings nil))
3192 (with-hidden-comments
3193 (with-hidden-cloze-hints
3194 (with-hidden-cloze-text
3195 (case (random* 6)
3197 (org-drill-hide-all-subheadings-except '("Infinitive"))
3198 (setq prompt
3199 (concat "Translate this Spanish verb, and conjugate it "
3200 "for the *present* tense.")
3201 reveal-headings '("English" "Present Tense" "Notes")))
3203 (org-drill-hide-all-subheadings-except '("English"))
3204 (setq prompt (concat "For the *present* tense, conjugate the "
3205 "Spanish translation of this English verb.")
3206 reveal-headings '("Infinitive" "Present Tense" "Notes")))
3208 (org-drill-hide-all-subheadings-except '("Infinitive"))
3209 (setq prompt (concat "Translate this Spanish verb, and "
3210 "conjugate it for the *past* tense.")
3211 reveal-headings '("English" "Past Tense" "Notes")))
3213 (org-drill-hide-all-subheadings-except '("English"))
3214 (setq prompt (concat "For the *past* tense, conjugate the "
3215 "Spanish translation of this English verb.")
3216 reveal-headings '("Infinitive" "Past Tense" "Notes")))
3218 (org-drill-hide-all-subheadings-except '("Infinitive"))
3219 (setq prompt (concat "Translate this Spanish verb, and "
3220 "conjugate it for the *future perfect* tense.")
3221 reveal-headings '("English" "Future Perfect Tense" "Notes")))
3223 (org-drill-hide-all-subheadings-except '("English"))
3224 (setq prompt (concat "For the *future perfect* tense, conjugate the "
3225 "Spanish translation of this English verb.")
3226 reveal-headings '("Infinitive" "Future Perfect Tense" "Notes"))))
3227 (org-cycle-hide-drawers 'all)
3228 (prog1 (org-drill-presentation-prompt)
3229 (org-drill-hide-subheadings-if 'org-drill-entry-p)))))))
3232 (provide 'org-drill)