org-drill.el 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920
  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.3.5
  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.
  25. (eval-when-compile (require 'cl))
  26. (eval-when-compile (require 'hi-lock))
  27. (require 'org)
  28. (require 'org-id)
  29. (require 'org-learn)
  30. (defgroup org-drill nil
  31. "Options concerning interactive drill sessions in Org mode (org-drill)."
  32. :tag "Org-Drill"
  33. :group 'org-link)
  34. (defcustom org-drill-question-tag
  35. "drill"
  36. "Tag which topics must possess in order to be identified as review topics
  37. by `org-drill'."
  38. :group 'org-drill
  39. :type 'string)
  40. (defcustom org-drill-maximum-items-per-session
  41. 30
  42. "Each drill session will present at most this many topics for review.
  43. Nil means unlimited."
  44. :group 'org-drill
  45. :type '(choice integer (const nil)))
  46. (defcustom org-drill-maximum-duration
  47. 20
  48. "Maximum duration of a drill session, in minutes.
  49. Nil means unlimited."
  50. :group 'org-drill
  51. :type '(choice integer (const nil)))
  52. (defcustom org-drill-failure-quality
  53. 2
  54. "If the quality of recall for an item is this number or lower,
  55. it is regarded as an unambiguous failure, and the repetition
  56. interval for the card is reset to 0 days. If the quality is higher
  57. than this number, it is regarded as successfully recalled, but the
  58. time interval to the next repetition will be lowered if the quality
  59. was near to a fail.
  60. By default this is 2, for SuperMemo-like behaviour. For
  61. Mnemosyne-like behaviour, set it to 1. Other values are not
  62. really sensible."
  63. :group 'org-drill
  64. :type '(choice (const 2) (const 1)))
  65. (defcustom org-drill-forgetting-index
  66. 10
  67. "What percentage of items do you consider it is 'acceptable' to
  68. forget each drill session? The default is 10%. A warning message
  69. is displayed at the end of the session if the percentage forgotten
  70. climbs above this number."
  71. :group 'org-drill
  72. :type 'integer)
  73. (defcustom org-drill-leech-failure-threshold
  74. 15
  75. "If an item is forgotten more than this many times, it is tagged
  76. as a 'leech' item."
  77. :group 'org-drill
  78. :type '(choice integer (const nil)))
  79. (defcustom org-drill-leech-method
  80. 'skip
  81. "How should 'leech items' be handled during drill sessions?
  82. Possible values:
  83. - nil :: Leech items are treated the same as normal items.
  84. - skip :: Leech items are not included in drill sessions.
  85. - warn :: Leech items are still included in drill sessions,
  86. but a warning message is printed when each leech item is
  87. presented."
  88. :group 'org-drill
  89. :type '(choice (const 'warn) (const 'skip) (const nil)))
  90. (defface org-drill-visible-cloze-face
  91. '((t (:foreground "darkseagreen")))
  92. "The face used to hide the contents of cloze phrases."
  93. :group 'org-drill)
  94. (defface org-drill-visible-cloze-hint-face
  95. '((t (:foreground "dark slate blue")))
  96. "The face used to hide the contents of cloze phrases."
  97. :group 'org-drill)
  98. (defface org-drill-hidden-cloze-face
  99. '((t (:foreground "deep sky blue" :background "blue")))
  100. "The face used to hide the contents of cloze phrases."
  101. :group 'org-drill)
  102. (defcustom org-drill-use-visible-cloze-face-p
  103. nil
  104. "Use a special face to highlight cloze-deleted text in org mode
  105. buffers?"
  106. :group 'org-drill
  107. :type 'boolean)
  108. (defcustom org-drill-hide-item-headings-p
  109. nil
  110. "Conceal the contents of the main heading of each item during drill
  111. sessions? You may want to enable this behaviour if item headings or tags
  112. contain information that could 'give away' the answer."
  113. :group 'org-drill
  114. :type 'boolean)
  115. (defcustom org-drill-new-count-color
  116. "royal blue"
  117. "Foreground colour used to display the count of remaining new items
  118. during a drill session."
  119. :group 'org-drill
  120. :type 'color)
  121. (defcustom org-drill-mature-count-color
  122. "green"
  123. "Foreground colour used to display the count of remaining mature items
  124. during a drill session. Mature items are due for review, but are not new."
  125. :group 'org-drill
  126. :type 'color)
  127. (defcustom org-drill-failed-count-color
  128. "red"
  129. "Foreground colour used to display the count of remaining failed items
  130. during a drill session."
  131. :group 'org-drill
  132. :type 'color)
  133. (defcustom org-drill-done-count-color
  134. "sienna"
  135. "Foreground colour used to display the count of reviewed items
  136. during a drill session."
  137. :group 'org-drill
  138. :type 'color)
  139. (setplist 'org-drill-cloze-overlay-defaults
  140. '(display "[...]"
  141. face org-drill-hidden-cloze-face
  142. window t))
  143. (setplist 'org-drill-hidden-text-overlay
  144. '(invisible t))
  145. (setplist 'org-drill-replaced-text-overlay
  146. '(display "Replaced text"
  147. face default
  148. window t))
  149. (defvar org-drill-cloze-regexp
  150. ;; ver 1 "[^][]\\(\\[[^][][^]]*\\]\\)"
  151. ;; ver 2 "\\(\\[.*?\\]\\|^[^[[:cntrl:]]*?\\]\\|\\[.*?$\\)"
  152. ;; ver 3! "\\(\\[.*?\\]\\|\\[.*?[[:cntrl:]]+.*?\\]\\)"
  153. "\\(\\[[[:cntrl:][:graph:][:space:]]*?\\)\\(\\||.+?\\)\\(\\]\\)")
  154. (defvar org-drill-cloze-keywords
  155. `((,org-drill-cloze-regexp
  156. (1 'org-drill-visible-cloze-face nil)
  157. (2 'org-drill-visible-cloze-hint-face t)
  158. (3 'org-drill-visible-cloze-face nil)
  159. )))
  160. (defcustom org-drill-card-type-alist
  161. '((nil . org-drill-present-simple-card)
  162. ("simple" . org-drill-present-simple-card)
  163. ("twosided" . org-drill-present-two-sided-card)
  164. ("multisided" . org-drill-present-multi-sided-card)
  165. ("hide1cloze" . org-drill-present-multicloze-hide1)
  166. ("hide2cloze" . org-drill-present-multicloze-hide2)
  167. ("show1cloze" . org-drill-present-multicloze-show1)
  168. ("show2cloze" . org-drill-present-multicloze-show2)
  169. ("multicloze" . org-drill-present-multicloze-hide1)
  170. ("hidefirst" . org-drill-present-multicloze-hide-first)
  171. ("hidelast" . org-drill-present-multicloze-hide-last)
  172. ("hide1_firstmore" . org-drill-present-multicloze-hide1-firstmore)
  173. ("show1_lastmore" . org-drill-present-multicloze-show1-lastmore)
  174. ("show1_firstless" . org-drill-present-multicloze-show1-firstless)
  175. ("conjugate" org-drill-present-verb-conjugation
  176. org-drill-show-answer-verb-conjugation)
  177. ("spanish_verb" . org-drill-present-spanish-verb)
  178. ("translate_number" org-drill-present-translate-number
  179. org-drill-show-answer-translate-number))
  180. "Alist associating card types with presentation functions. Each entry in the
  181. alist takes one of two forms:
  182. 1. (CARDTYPE . QUESTION-FN), where CARDTYPE is a string or nil (for default),
  183. and QUESTION-FN is a function which takes no arguments and returns a boolean
  184. value.
  185. 2. (CARDTYPE QUESTION-FN ANSWER-FN), where ANSWER-FN is a function that takes
  186. one argument -- the argument is a function that itself takes no arguments.
  187. ANSWER-FN is called with the point on the active item's
  188. heading, just prior to displaying the item's 'answer'. It can therefore be
  189. used to modify the appearance of the answer. ANSWER-FN must call its argument
  190. before returning. (Its argument is a function that prompts the user and
  191. performs rescheduling)."
  192. :group 'org-drill
  193. :type '(alist :key-type (choice string (const nil)) :value-type function))
  194. (defcustom org-drill-scope
  195. 'file
  196. "The scope in which to search for drill items when conducting a
  197. drill session. This can be any of:
  198. file The current buffer, respecting the restriction if any.
  199. This is the default.
  200. tree The subtree started with the entry at point
  201. file-no-restriction The current buffer, without restriction
  202. file-with-archives The current buffer, and any archives associated with it.
  203. agenda All agenda files
  204. agenda-with-archives All agenda files with any archive files associated
  205. with them.
  206. directory All files with the extension '.org' in the same
  207. directory as the current file (includes the current
  208. file if it is an .org file.)
  209. (FILE1 FILE2 ...) If this is a list, all files in the list will be scanned.
  210. "
  211. ;; Note -- meanings differ slightly from the argument to org-map-entries:
  212. ;; 'file' means current file/buffer, respecting any restriction
  213. ;; 'file-no-restriction' means current file/buffer, ignoring restrictions
  214. ;; 'directory' means all *.org files in current directory
  215. :group 'org-drill
  216. :type '(choice (const 'file) (const 'tree) (const 'file-no-restriction)
  217. (const 'file-with-archives) (const 'agenda)
  218. (const 'agenda-with-archives) (const 'directory)
  219. list))
  220. (defcustom org-drill-save-buffers-after-drill-sessions-p
  221. t
  222. "If non-nil, prompt to save all modified buffers after a drill session
  223. finishes."
  224. :group 'org-drill
  225. :type 'boolean)
  226. (defcustom org-drill-spaced-repetition-algorithm
  227. 'sm5
  228. "Which SuperMemo spaced repetition algorithm to use for scheduling items.
  229. Available choices are:
  230. - SM2 :: the SM2 algorithm, used in SuperMemo 2.0
  231. - SM5 :: the SM5 algorithm, used in SuperMemo 5.0
  232. - Simple8 :: a modified version of the SM8 algorithm. SM8 is used in
  233. SuperMemo 98. The version implemented here is simplified in that while it
  234. 'learns' the difficulty of each item using quality grades and number of
  235. failures, it does not modify the matrix of values that
  236. governs how fast the inter-repetition intervals increase. A method for
  237. adjusting intervals when items are reviewed early or late has been taken
  238. from SM11, a later version of the algorithm, and included in Simple8."
  239. :group 'org-drill
  240. :type '(choice (const 'sm2) (const 'sm5) (const 'simple8)))
  241. (defcustom org-drill-optimal-factor-matrix
  242. nil
  243. "DO NOT CHANGE THE VALUE OF THIS VARIABLE.
  244. Persistent matrix of optimal factors, used by the SuperMemo SM5 algorithm.
  245. The matrix is saved (using the 'customize' facility) at the end of each
  246. drill session.
  247. Over time, values in the matrix will adapt to the individual user's
  248. pace of learning."
  249. :group 'org-drill
  250. :type 'sexp)
  251. (defcustom org-drill-sm5-initial-interval
  252. 4.0
  253. "In the SM5 algorithm, the initial interval after the first
  254. successful presentation of an item is always 4 days. If you wish to change
  255. this, you can do so here."
  256. :group 'org-drill
  257. :type 'float)
  258. (defcustom org-drill-add-random-noise-to-intervals-p
  259. nil
  260. "If true, the number of days until an item's next repetition
  261. will vary slightly from the interval calculated by the SM2
  262. algorithm. The variation is very small when the interval is
  263. small, but scales up with the interval."
  264. :group 'org-drill
  265. :type 'boolean)
  266. (defcustom org-drill-adjust-intervals-for-early-and-late-repetitions-p
  267. nil
  268. "If true, when the student successfully reviews an item 1 or more days
  269. before or after the scheduled review date, this will affect that date of
  270. the item's next scheduled review, according to the algorithm presented at
  271. [[http://www.supermemo.com/english/algsm11.htm#Advanced%20repetitions]].
  272. Items that were reviewed early will have their next review date brought
  273. forward. Those that were reviewed late will have their next review
  274. date postponed further.
  275. Note that this option currently has no effect if the SM2 algorithm
  276. is used."
  277. :group 'org-drill
  278. :type 'boolean)
  279. (defcustom org-drill-cloze-text-weight
  280. 4
  281. "For card types 'hide1_firstmore', 'show1_lastmore' and 'show1_firstless',
  282. this number determines how often the 'less favoured' situation
  283. should arise. It will occur 1 in every N trials, where N is the
  284. value of the variable.
  285. For example, with the hide1_firstmore card type, the first piece
  286. of clozed text should be hidden more often than the other
  287. pieces. If this variable is set to 4 (default), the first item
  288. will only be shown 25% of the time (1 in 4 trials). Similarly for
  289. show1_lastmore, the last item will be shown 75% of the time, and
  290. for show1_firstless, the first item would only be shown 25% of the
  291. time.
  292. If the value of this variable is NIL, then weighting is disabled, and
  293. all weighted card types are treated as their unweighted equivalents."
  294. :group 'org-drill
  295. :type '(choice integer (const nil)))
  296. (defcustom org-drill-cram-hours
  297. 12
  298. "When in cram mode, items are considered due for review if
  299. they were reviewed at least this many hours ago."
  300. :group 'org-drill
  301. :type 'integer)
  302. ;;; NEW items have never been presented in a drill session before.
  303. ;;; MATURE items HAVE been presented at least once before.
  304. ;;; - YOUNG mature items were scheduled no more than
  305. ;;; ORG-DRILL-DAYS-BEFORE-OLD days after their last
  306. ;;; repetition. These items will have been learned 'recently' and will have a
  307. ;;; low repetition count.
  308. ;;; - OLD mature items have intervals greater than
  309. ;;; ORG-DRILL-DAYS-BEFORE-OLD.
  310. ;;; - OVERDUE items are past their scheduled review date by more than
  311. ;;; LAST-INTERVAL * (ORG-DRILL-OVERDUE-INTERVAL-FACTOR - 1) days,
  312. ;;; regardless of young/old status.
  313. (defcustom org-drill-days-before-old
  314. 10
  315. "When an item's inter-repetition interval rises above this value in days,
  316. it is no longer considered a 'young' (recently learned) item."
  317. :group 'org-drill
  318. :type 'integer)
  319. (defcustom org-drill-overdue-interval-factor
  320. 1.2
  321. "An item is considered overdue if its scheduled review date is
  322. more than (ORG-DRILL-OVERDUE-INTERVAL-FACTOR - 1) * LAST-INTERVAL
  323. days in the past. For example, a value of 1.2 means an additional
  324. 20% of the last scheduled interval is allowed to elapse before
  325. the item is overdue. A value of 1.0 means no extra time is
  326. allowed at all - items are immediately considered overdue if
  327. there is even one day's delay in reviewing them. This variable
  328. should never be less than 1.0."
  329. :group 'org-drill
  330. :type 'float)
  331. (defcustom org-drill-learn-fraction
  332. 0.5
  333. "Fraction between 0 and 1 that governs how quickly the spaces
  334. between successive repetitions increase, for all items. The
  335. default value is 0.5. Higher values make spaces increase more
  336. quickly with each successful repetition. You should only change
  337. this in small increments (for example 0.05-0.1) as it has an
  338. exponential effect on inter-repetition spacing."
  339. :group 'org-drill
  340. :type 'float)
  341. (defvar *org-drill-session-qualities* nil)
  342. (defvar *org-drill-start-time* 0)
  343. (defvar *org-drill-new-entries* nil)
  344. (defvar *org-drill-dormant-entry-count* 0)
  345. (defvar *org-drill-due-entry-count* 0)
  346. (defvar *org-drill-overdue-entry-count* 0)
  347. (defvar *org-drill-due-tomorrow-count* 0)
  348. (defvar *org-drill-overdue-entries* nil
  349. "List of markers for items that are considered 'overdue', based on
  350. the value of ORG-DRILL-OVERDUE-INTERVAL-FACTOR.")
  351. (defvar *org-drill-young-mature-entries* nil
  352. "List of markers for mature entries whose last inter-repetition
  353. interval was <= ORG-DRILL-DAYS-BEFORE-OLD days.")
  354. (defvar *org-drill-old-mature-entries* nil
  355. "List of markers for mature entries whose last inter-repetition
  356. interval was greater than ORG-DRILL-DAYS-BEFORE-OLD days.")
  357. (defvar *org-drill-failed-entries* nil)
  358. (defvar *org-drill-again-entries* nil)
  359. (defvar *org-drill-done-entries* nil)
  360. (defvar *org-drill-current-item* nil
  361. "Set to the marker for the item currently being tested.")
  362. (defvar *org-drill-cram-mode* nil
  363. "Are we in 'cram mode', where all items are considered due
  364. for review unless they were already reviewed in the recent past?")
  365. (defvar org-drill-scheduling-properties
  366. '("LEARN_DATA" "DRILL_LAST_INTERVAL" "DRILL_REPEATS_SINCE_FAIL"
  367. "DRILL_TOTAL_REPEATS" "DRILL_FAILURE_COUNT" "DRILL_AVERAGE_QUALITY"
  368. "DRILL_EASE" "DRILL_LAST_QUALITY" "DRILL_LAST_REVIEWED"))
  369. ;;; Make the above settings safe as file-local variables.
  370. (put 'org-drill-question-tag 'safe-local-variable 'stringp)
  371. (put 'org-drill-maximum-items-per-session 'safe-local-variable
  372. '(lambda (val) (or (integerp val) (null val))))
  373. (put 'org-drill-maximum-duration 'safe-local-variable
  374. '(lambda (val) (or (integerp val) (null val))))
  375. (put 'org-drill-failure-quality 'safe-local-variable 'integerp)
  376. (put 'org-drill-forgetting-index 'safe-local-variable 'integerp)
  377. (put 'org-drill-leech-failure-threshold 'safe-local-variable 'integerp)
  378. (put 'org-drill-leech-method 'safe-local-variable
  379. '(lambda (val) (memq val '(nil skip warn))))
  380. (put 'org-drill-use-visible-cloze-face-p 'safe-local-variable 'booleanp)
  381. (put 'org-drill-hide-item-headings-p 'safe-local-variable 'booleanp)
  382. (put 'org-drill-spaced-repetition-algorithm 'safe-local-variable
  383. '(lambda (val) (memq val '(simple8 sm5 sm2))))
  384. (put 'org-drill-sm5-initial-interval 'safe-local-variable 'floatp)
  385. (put 'org-drill-add-random-noise-to-intervals-p 'safe-local-variable 'booleanp)
  386. (put 'org-drill-adjust-intervals-for-early-and-late-repetitions-p
  387. 'safe-local-variable 'booleanp)
  388. (put 'org-drill-cram-hours 'safe-local-variable 'integerp)
  389. (put 'org-drill-learn-fraction 'safe-local-variable 'floatp)
  390. (put 'org-drill-days-before-old 'safe-local-variable 'integerp)
  391. (put 'org-drill-overdue-interval-factor 'safe-local-variable 'floatp)
  392. (put 'org-drill-scope 'safe-local-variable
  393. '(lambda (val) (or (symbolp val) (listp val))))
  394. (put 'org-drill-save-buffers-after-drill-sessions-p 'safe-local-variable 'booleanp)
  395. (put 'org-drill-cloze-text-weight 'safe-local-variable
  396. '(lambda (val) (or (null val) (integerp val))))
  397. ;;;; Utilities ================================================================
  398. (defun free-marker (m)
  399. (set-marker m nil))
  400. (defmacro pop-random (place)
  401. (let ((idx (gensym)))
  402. `(if (null ,place)
  403. nil
  404. (let ((,idx (random* (length ,place))))
  405. (prog1 (nth ,idx ,place)
  406. (setq ,place (append (subseq ,place 0 ,idx)
  407. (subseq ,place (1+ ,idx)))))))))
  408. (defmacro push-end (val place)
  409. "Add VAL to the end of the sequence stored in PLACE. Return the new
  410. value."
  411. `(setq ,place (append ,place (list ,val))))
  412. (defun shuffle-list (list)
  413. "Randomly permute the elements of LIST (all permutations equally likely)."
  414. ;; Adapted from 'shuffle-vector' in cookie1.el
  415. (let ((i 0)
  416. j
  417. temp
  418. (len (length list)))
  419. (while (< i len)
  420. (setq j (+ i (random* (- len i))))
  421. (setq temp (nth i list))
  422. (setf (nth i list) (nth j list))
  423. (setf (nth j list) temp)
  424. (setq i (1+ i))))
  425. list)
  426. (defun round-float (floatnum fix)
  427. "Round the floating point number FLOATNUM to FIX decimal places.
  428. Example: (round-float 3.56755765 3) -> 3.568"
  429. (let ((n (expt 10 fix)))
  430. (/ (float (round (* floatnum n))) n)))
  431. (defun command-keybinding-to-string (cmd)
  432. "Return a human-readable description of the key/keys to which the command
  433. CMD is bound, or nil if it is not bound to a key."
  434. (let ((key (where-is-internal cmd overriding-local-map t)))
  435. (if key (key-description key))))
  436. (defun time-to-inactive-org-timestamp (time)
  437. (format-time-string
  438. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  439. time))
  440. (defun org-map-drill-entries (func &optional scope &rest skip)
  441. "Like `org-map-entries', but only drill entries are processed."
  442. (let ((org-drill-scope (or scope org-drill-scope)))
  443. (apply 'org-map-entries func
  444. (concat "+" org-drill-question-tag)
  445. (case org-drill-scope
  446. (file nil)
  447. (file-no-restriction 'file)
  448. (directory
  449. (directory-files (file-name-directory (buffer-file-name))
  450. t "\\.org$"))
  451. (t org-drill-scope))
  452. skip)))
  453. (defmacro with-hidden-cloze-text (&rest body)
  454. `(progn
  455. (org-drill-hide-clozed-text)
  456. (unwind-protect
  457. (progn
  458. ,@body)
  459. (org-drill-unhide-clozed-text))))
  460. (defmacro with-hidden-cloze-hints (&rest body)
  461. `(progn
  462. (org-drill-hide-cloze-hints)
  463. (unwind-protect
  464. (progn
  465. ,@body)
  466. (org-drill-unhide-text))))
  467. (defmacro with-hidden-comments (&rest body)
  468. `(progn
  469. (if org-drill-hide-item-headings-p
  470. (org-drill-hide-heading-at-point))
  471. (org-drill-hide-comments)
  472. (unwind-protect
  473. (progn
  474. ,@body)
  475. (org-drill-unhide-text))))
  476. (defun org-drill-days-since-last-review ()
  477. "Nil means a last review date has not yet been stored for
  478. the item.
  479. Zero means it was reviewed today.
  480. A positive number means it was reviewed that many days ago.
  481. A negative number means the date of last review is in the future --
  482. this should never happen."
  483. (let ((datestr (org-entry-get (point) "DRILL_LAST_REVIEWED")))
  484. (when datestr
  485. (- (time-to-days (current-time))
  486. (time-to-days (apply 'encode-time
  487. (org-parse-time-string datestr)))))))
  488. (defun org-drill-hours-since-last-review ()
  489. "Like `org-drill-days-since-last-review', but return value is
  490. in hours rather than days."
  491. (let ((datestr (org-entry-get (point) "DRILL_LAST_REVIEWED")))
  492. (when datestr
  493. (floor
  494. (/ (- (time-to-seconds (current-time))
  495. (time-to-seconds (apply 'encode-time
  496. (org-parse-time-string datestr))))
  497. (* 60 60))))))
  498. (defun org-drill-entry-p (&optional marker)
  499. "Is MARKER, or the point, in a 'drill item'? This will return nil if
  500. the point is inside a subheading of a drill item -- to handle that
  501. situation use `org-part-of-drill-entry-p'."
  502. (save-excursion
  503. (when marker
  504. (org-drill-goto-entry marker))
  505. (member org-drill-question-tag (org-get-local-tags))))
  506. (defun org-drill-goto-entry (marker)
  507. (org-pop-to-buffer-same-window (marker-buffer marker))
  508. (goto-char marker))
  509. (defun org-part-of-drill-entry-p ()
  510. "Is the current entry either the main heading of a 'drill item',
  511. or a subheading within a drill item?"
  512. (or (org-drill-entry-p)
  513. ;; Does this heading INHERIT the drill tag
  514. (member org-drill-question-tag (org-get-tags-at))))
  515. (defun org-drill-goto-drill-entry-heading ()
  516. "Move the point to the heading which holds the :drill: tag for this
  517. drill entry."
  518. (unless (org-at-heading-p)
  519. (org-back-to-heading))
  520. (unless (org-part-of-drill-entry-p)
  521. (error "Point is not inside a drill entry"))
  522. (while (not (org-drill-entry-p))
  523. (unless (org-up-heading-safe)
  524. (error "Cannot find a parent heading that is marked as a drill entry"))))
  525. (defun org-drill-entry-leech-p ()
  526. "Is the current entry a 'leech item'?"
  527. (and (org-drill-entry-p)
  528. (member "leech" (org-get-local-tags))))
  529. ;; (defun org-drill-entry-due-p ()
  530. ;; (cond
  531. ;; (*org-drill-cram-mode*
  532. ;; (let ((hours (org-drill-hours-since-last-review)))
  533. ;; (and (org-drill-entry-p)
  534. ;; (or (null hours)
  535. ;; (>= hours org-drill-cram-hours)))))
  536. ;; (t
  537. ;; (let ((item-time (org-get-scheduled-time (point))))
  538. ;; (and (org-drill-entry-p)
  539. ;; (or (not (eql 'skip org-drill-leech-method))
  540. ;; (not (org-drill-entry-leech-p)))
  541. ;; (or (null item-time) ; not scheduled
  542. ;; (not (minusp ; scheduled for today/in past
  543. ;; (- (time-to-days (current-time))
  544. ;; (time-to-days item-time))))))))))
  545. (defun org-drill-entry-days-overdue ()
  546. "Returns:
  547. - NIL if the item is not to be regarded as scheduled for review at all.
  548. This is the case if it is not a drill item, or if it is a leech item
  549. that we wish to skip, or if we are in cram mode and have already reviewed
  550. the item within the last few hours.
  551. - 0 if the item is new, or if it scheduled for review today.
  552. - A negative integer - item is scheduled that many days in the future.
  553. - A positive integer - item is scheduled that many days in the past."
  554. (cond
  555. (*org-drill-cram-mode*
  556. (let ((hours (org-drill-hours-since-last-review)))
  557. (and (org-drill-entry-p)
  558. (or (null hours)
  559. (>= hours org-drill-cram-hours))
  560. 0)))
  561. (t
  562. (let ((item-time (org-get-scheduled-time (point))))
  563. (cond
  564. ((or (not (org-drill-entry-p))
  565. (and (eql 'skip org-drill-leech-method)
  566. (org-drill-entry-leech-p)))
  567. nil)
  568. ((null item-time) ; not scheduled -> due now
  569. 0)
  570. (t
  571. (- (time-to-days (current-time))
  572. (time-to-days item-time))))))))
  573. (defun org-drill-entry-overdue-p (&optional days-overdue last-interval)
  574. "Returns true if entry that is scheduled DAYS-OVERDUE dasy in the past,
  575. and whose last inter-repetition interval was LAST-INTERVAL, should be
  576. considered 'overdue'. If the arguments are not given they are extracted
  577. from the entry at point."
  578. (unless days-overdue
  579. (setq days-overdue (org-drill-entry-days-overdue)))
  580. (unless last-interval
  581. (setq last-interval (org-drill-entry-last-interval 1)))
  582. (and (numberp days-overdue)
  583. (> days-overdue 1) ; enforce a sane minimum 'overdue' gap
  584. ;;(> due org-drill-days-before-overdue)
  585. (> (/ (+ days-overdue last-interval 1.0) last-interval)
  586. org-drill-overdue-interval-factor)))
  587. (defun org-drill-entry-due-p ()
  588. (let ((due (org-drill-entry-days-overdue)))
  589. (and (not (null due))
  590. (not (minusp due)))))
  591. (defun org-drill-entry-new-p ()
  592. (and (org-drill-entry-p)
  593. (let ((item-time (org-get-scheduled-time (point))))
  594. (null item-time))))
  595. (defun org-drill-entry-last-quality (&optional default)
  596. (let ((quality (org-entry-get (point) "DRILL_LAST_QUALITY")))
  597. (if quality
  598. (string-to-number quality)
  599. default)))
  600. (defun org-drill-entry-failure-count ()
  601. (let ((quality (org-entry-get (point) "DRILL_FAILURE_COUNT")))
  602. (if quality
  603. (string-to-number quality)
  604. 0)))
  605. (defun org-drill-entry-average-quality (&optional default)
  606. (let ((val (org-entry-get (point) "DRILL_AVERAGE_QUALITY")))
  607. (if val
  608. (string-to-number val)
  609. (or default nil))))
  610. (defun org-drill-entry-last-interval (&optional default)
  611. (let ((val (org-entry-get (point) "DRILL_LAST_INTERVAL")))
  612. (if val
  613. (string-to-number val)
  614. (or default 0))))
  615. (defun org-drill-entry-repeats-since-fail (&optional default)
  616. (let ((val (org-entry-get (point) "DRILL_REPEATS_SINCE_FAIL")))
  617. (if val
  618. (string-to-number val)
  619. (or default 0))))
  620. (defun org-drill-entry-total-repeats (&optional default)
  621. (let ((val (org-entry-get (point) "DRILL_TOTAL_REPEATS")))
  622. (if val
  623. (string-to-number val)
  624. (or default 0))))
  625. (defun org-drill-entry-ease (&optional default)
  626. (let ((val (org-entry-get (point) "DRILL_EASE")))
  627. (if val
  628. (string-to-number val)
  629. default)))
  630. ;;; From http://www.supermemo.com/english/ol/sm5.htm
  631. (defun org-drill-random-dispersal-factor ()
  632. "Returns a random number between 0.5 and 1.5."
  633. (let ((a 0.047)
  634. (b 0.092)
  635. (p (- (random* 1.0) 0.5)))
  636. (flet ((sign (n)
  637. (cond ((zerop n) 0)
  638. ((plusp n) 1)
  639. (t -1))))
  640. (/ (+ 100 (* (* (/ -1 b) (log (- 1 (* (/ b a ) (abs p)))))
  641. (sign p)))
  642. 100.0))))
  643. (defun pseudonormal (mean variation)
  644. "Random numbers in a pseudo-normal distribution with mean MEAN, range
  645. MEAN-VARIATION to MEAN+VARIATION"
  646. (+ (random* variation)
  647. (random* variation)
  648. (- variation)
  649. mean))
  650. (defun org-drill-early-interval-factor (optimal-factor
  651. optimal-interval
  652. days-ahead)
  653. "Arguments:
  654. - OPTIMAL-FACTOR: interval-factor if the item had been tested
  655. exactly when it was supposed to be.
  656. - OPTIMAL-INTERVAL: interval for next repetition (days) if the item had been
  657. tested exactly when it was supposed to be.
  658. - DAYS-AHEAD: how many days ahead of time the item was reviewed.
  659. Returns an adjusted optimal factor which should be used to
  660. calculate the next interval, instead of the optimal factor found
  661. in the matrix."
  662. (let ((delta-ofmax (* (1- optimal-factor)
  663. (/ (+ optimal-interval
  664. (* 0.6 optimal-interval) -1) (1- optimal-interval)))))
  665. (- optimal-factor
  666. (* delta-ofmax (/ days-ahead (+ days-ahead (* 0.6 optimal-interval)))))))
  667. (defun org-drill-get-item-data ()
  668. "Returns a list of 6 items, containing all the stored recall
  669. data for the item at point:
  670. - LAST-INTERVAL is the interval in days that was used to schedule the item's
  671. current review date.
  672. - REPEATS is the number of items the item has been successfully recalled without
  673. without any failures. It is reset to 0 upon failure to recall the item.
  674. - FAILURES is the total number of times the user has failed to recall the item.
  675. - TOTAL-REPEATS includes both successful and unsuccessful repetitions.
  676. - AVERAGE-QUALITY is the mean quality of recall of the item over
  677. all its repetitions, successful and unsuccessful.
  678. - EASE is a number reflecting how easy the item is to learn. Higher is easier.
  679. "
  680. (let ((learn-str (org-entry-get (point) "LEARN_DATA"))
  681. (repeats (org-drill-entry-total-repeats :missing)))
  682. (cond
  683. (learn-str
  684. (let ((learn-data (or (and learn-str
  685. (read learn-str))
  686. (copy-list initial-repetition-state))))
  687. (list (nth 0 learn-data) ; last interval
  688. (nth 1 learn-data) ; repetitions
  689. (org-drill-entry-failure-count)
  690. (nth 1 learn-data)
  691. (org-drill-entry-last-quality)
  692. (nth 2 learn-data) ; EF
  693. )))
  694. ((not (eql :missing repeats))
  695. (list (org-drill-entry-last-interval)
  696. (org-drill-entry-repeats-since-fail)
  697. (org-drill-entry-failure-count)
  698. (org-drill-entry-total-repeats)
  699. (org-drill-entry-average-quality)
  700. (org-drill-entry-ease)))
  701. (t ; virgin item
  702. (list 0 0 0 0 nil nil)))))
  703. (defun org-drill-store-item-data (last-interval repeats failures
  704. total-repeats meanq
  705. ease)
  706. "Stores the given data in the item at point."
  707. (org-entry-delete (point) "LEARN_DATA")
  708. (org-set-property "DRILL_LAST_INTERVAL"
  709. (number-to-string (round-float last-interval 4)))
  710. (org-set-property "DRILL_REPEATS_SINCE_FAIL" (number-to-string repeats))
  711. (org-set-property "DRILL_TOTAL_REPEATS" (number-to-string total-repeats))
  712. (org-set-property "DRILL_FAILURE_COUNT" (number-to-string failures))
  713. (org-set-property "DRILL_AVERAGE_QUALITY"
  714. (number-to-string (round-float meanq 3)))
  715. (org-set-property "DRILL_EASE"
  716. (number-to-string (round-float ease 3))))
  717. ;;; SM2 Algorithm =============================================================
  718. (defun determine-next-interval-sm2 (last-interval n ef quality
  719. failures meanq total-repeats)
  720. "Arguments:
  721. - LAST-INTERVAL -- the number of days since the item was last reviewed.
  722. - REPEATS -- the number of times the item has been successfully reviewed
  723. - EF -- the 'easiness factor'
  724. - QUALITY -- 0 to 5
  725. Returns a list: (INTERVAL REPEATS EF FAILURES MEAN TOTAL-REPEATS OFMATRIX), where:
  726. - INTERVAL is the number of days until the item should next be reviewed
  727. - REPEATS is incremented by 1.
  728. - EF is modified based on the recall quality for the item.
  729. - OF-MATRIX is not modified."
  730. (assert (> n 0))
  731. (assert (and (>= quality 0) (<= quality 5)))
  732. (if (<= quality org-drill-failure-quality)
  733. ;; When an item is failed, its interval is reset to 0,
  734. ;; but its EF is unchanged
  735. (list -1 1 ef (1+ failures) meanq (1+ total-repeats)
  736. org-drill-optimal-factor-matrix)
  737. ;; else:
  738. (let* ((next-ef (modify-e-factor ef quality))
  739. (interval
  740. (cond
  741. ((<= n 1) 1)
  742. ((= n 2)
  743. (cond
  744. (org-drill-add-random-noise-to-intervals-p
  745. (case quality
  746. (5 6)
  747. (4 4)
  748. (3 3)
  749. (2 1)
  750. (t -1)))
  751. (t 6)))
  752. (t (* last-interval next-ef)))))
  753. (list (if org-drill-add-random-noise-to-intervals-p
  754. (+ last-interval (* (- interval last-interval)
  755. (org-drill-random-dispersal-factor)))
  756. interval)
  757. (1+ n)
  758. next-ef
  759. failures meanq (1+ total-repeats)
  760. org-drill-optimal-factor-matrix))))
  761. ;;; SM5 Algorithm =============================================================
  762. (defun initial-optimal-factor-sm5 (n ef)
  763. (if (= 1 n)
  764. org-drill-sm5-initial-interval
  765. ef))
  766. (defun get-optimal-factor-sm5 (n ef of-matrix)
  767. (let ((factors (assoc n of-matrix)))
  768. (or (and factors
  769. (let ((ef-of (assoc ef (cdr factors))))
  770. (and ef-of (cdr ef-of))))
  771. (initial-optimal-factor-sm5 n ef))))
  772. (defun inter-repetition-interval-sm5 (last-interval n ef &optional of-matrix)
  773. (let ((of (get-optimal-factor-sm5 n ef (or of-matrix
  774. org-drill-optimal-factor-matrix))))
  775. (if (= 1 n)
  776. of
  777. (* of last-interval))))
  778. (defun determine-next-interval-sm5 (last-interval n ef quality
  779. failures meanq total-repeats
  780. of-matrix &optional delta-days)
  781. (if (zerop n) (setq n 1))
  782. (if (null ef) (setq ef 2.5))
  783. (assert (> n 0))
  784. (assert (and (>= quality 0) (<= quality 5)))
  785. (unless of-matrix
  786. (setq of-matrix org-drill-optimal-factor-matrix))
  787. (setq of-matrix (cl-copy-tree of-matrix))
  788. (setq meanq (if meanq
  789. (/ (+ quality (* meanq total-repeats 1.0))
  790. (1+ total-repeats))
  791. quality))
  792. (let ((next-ef (modify-e-factor ef quality))
  793. (old-ef ef)
  794. (new-of (modify-of (get-optimal-factor-sm5 n ef of-matrix)
  795. quality org-drill-learn-fraction))
  796. (interval nil))
  797. (when (and org-drill-adjust-intervals-for-early-and-late-repetitions-p
  798. delta-days (minusp delta-days))
  799. (setq new-of (org-drill-early-interval-factor
  800. (get-optimal-factor-sm5 n ef of-matrix)
  801. (inter-repetition-interval-sm5
  802. last-interval n ef of-matrix)
  803. delta-days)))
  804. (setq of-matrix
  805. (set-optimal-factor n next-ef of-matrix
  806. (round-float new-of 3))) ; round OF to 3 d.p.
  807. (setq ef next-ef)
  808. (cond
  809. ;; "Failed" -- reset repetitions to 0,
  810. ((<= quality org-drill-failure-quality)
  811. (list -1 1 old-ef (1+ failures) meanq (1+ total-repeats)
  812. of-matrix)) ; Not clear if OF matrix is supposed to be
  813. ; preserved
  814. ;; For a zero-based quality of 4 or 5, don't repeat
  815. ;; ((and (>= quality 4)
  816. ;; (not org-learn-always-reschedule))
  817. ;; (list 0 (1+ n) ef failures meanq
  818. ;; (1+ total-repeats) of-matrix)) ; 0 interval = unschedule
  819. (t
  820. (setq interval (inter-repetition-interval-sm5
  821. last-interval n ef of-matrix))
  822. (if org-drill-add-random-noise-to-intervals-p
  823. (setq interval (* interval (org-drill-random-dispersal-factor))))
  824. (list interval
  825. (1+ n)
  826. ef
  827. failures
  828. meanq
  829. (1+ total-repeats)
  830. of-matrix)))))
  831. ;;; Simple8 Algorithm =========================================================
  832. (defun org-drill-simple8-first-interval (failures)
  833. "Arguments:
  834. - FAILURES: integer >= 0. The total number of times the item has
  835. been forgotten, ever.
  836. Returns the optimal FIRST interval for an item which has previously been
  837. forgotten on FAILURES occasions."
  838. (* 2.4849 (exp (* -0.057 failures))))
  839. (defun org-drill-simple8-interval-factor (ease repetition)
  840. "Arguments:
  841. - EASE: floating point number >= 1.2. Corresponds to `AF' in SM8 algorithm.
  842. - REPETITION: the number of times the item has been tested.
  843. 1 is the first repetition (ie the second trial).
  844. Returns:
  845. The factor by which the last interval should be
  846. multiplied to give the next interval. Corresponds to `RF' or `OF'."
  847. (+ 1.2 (* (- ease 1.2) (expt org-drill-learn-fraction (log repetition 2)))))
  848. (defun org-drill-simple8-quality->ease (quality)
  849. "Returns the ease (`AF' in the SM8 algorithm) which corresponds
  850. to a mean item quality of QUALITY."
  851. (+ (* 0.0542 (expt quality 4))
  852. (* -0.4848 (expt quality 3))
  853. (* 1.4916 (expt quality 2))
  854. (* -1.2403 quality)
  855. 1.4515))
  856. (defun determine-next-interval-simple8 (last-interval repeats quality
  857. failures meanq totaln
  858. &optional delta-days)
  859. "Arguments:
  860. - LAST-INTERVAL -- the number of days since the item was last reviewed.
  861. - REPEATS -- the number of times the item has been successfully reviewed
  862. - EASE -- the 'easiness factor'
  863. - QUALITY -- 0 to 5
  864. - DELTA-DAYS -- how many days overdue was the item when it was reviewed.
  865. 0 = reviewed on the scheduled day. +N = N days overdue.
  866. -N = reviewed N days early.
  867. Returns the new item data, as a list of 6 values:
  868. - NEXT-INTERVAL
  869. - REPEATS
  870. - EASE
  871. - FAILURES
  872. - AVERAGE-QUALITY
  873. - TOTAL-REPEATS.
  874. See the documentation for `org-drill-get-item-data' for a description of these."
  875. (assert (>= repeats 0))
  876. (assert (and (>= quality 0) (<= quality 5)))
  877. (assert (or (null meanq) (and (>= meanq 0) (<= meanq 5))))
  878. (let ((next-interval nil))
  879. (setf meanq (if meanq
  880. (/ (+ quality (* meanq totaln 1.0)) (1+ totaln))
  881. quality))
  882. (cond
  883. ((<= quality org-drill-failure-quality)
  884. (incf failures)
  885. (setf repeats 0
  886. next-interval -1))
  887. ((or (zerop repeats)
  888. (zerop last-interval))
  889. (setf next-interval (org-drill-simple8-first-interval failures))
  890. (incf repeats)
  891. (incf totaln))
  892. (t
  893. (let* ((use-n
  894. (if (and
  895. org-drill-adjust-intervals-for-early-and-late-repetitions-p
  896. (numberp delta-days) (plusp delta-days)
  897. (plusp last-interval))
  898. (+ repeats (min 1 (/ delta-days last-interval 1.0)))
  899. repeats))
  900. (factor (org-drill-simple8-interval-factor
  901. (org-drill-simple8-quality->ease meanq) use-n))
  902. (next-int (* last-interval factor)))
  903. (when (and org-drill-adjust-intervals-for-early-and-late-repetitions-p
  904. (numberp delta-days) (minusp delta-days))
  905. ;; The item was reviewed earlier than scheduled.
  906. (setf factor (org-drill-early-interval-factor
  907. factor next-int (abs delta-days))
  908. next-int (* last-interval factor)))
  909. (setf next-interval next-int)
  910. (incf repeats)
  911. (incf totaln))))
  912. (list
  913. (if (and org-drill-add-random-noise-to-intervals-p
  914. (plusp next-interval))
  915. (* next-interval (org-drill-random-dispersal-factor))
  916. next-interval)
  917. repeats
  918. (org-drill-simple8-quality->ease meanq)
  919. failures
  920. meanq
  921. totaln
  922. )))
  923. ;;; Essentially copied from `org-learn.el', but modified to
  924. ;;; optionally call the SM2 or simple8 functions.
  925. (defun org-drill-smart-reschedule (quality &optional days-ahead)
  926. "If DAYS-AHEAD is supplied it must be a positive integer. The
  927. item will be scheduled exactly this many days into the future."
  928. (let ((delta-days (- (time-to-days (current-time))
  929. (time-to-days (or (org-get-scheduled-time (point))
  930. (current-time)))))
  931. (ofmatrix org-drill-optimal-factor-matrix)
  932. ;; Entries can have weights, 1 by default. Intervals are divided by the
  933. ;; item's weight, so an item with a weight of 2 will have all intervals
  934. ;; halved, meaning you will end up reviewing it twice as often.
  935. ;; Useful for entries which randomly present any of several facts.
  936. (weight (org-entry-get (point) "DRILL_CARD_WEIGHT")))
  937. (if (stringp weight)
  938. (setq weight (read weight)))
  939. (destructuring-bind (last-interval repetitions failures
  940. total-repeats meanq ease)
  941. (org-drill-get-item-data)
  942. (destructuring-bind (next-interval repetitions ease
  943. failures meanq total-repeats
  944. &optional new-ofmatrix)
  945. (case org-drill-spaced-repetition-algorithm
  946. (sm5 (determine-next-interval-sm5 last-interval repetitions
  947. ease quality failures
  948. meanq total-repeats ofmatrix))
  949. (sm2 (determine-next-interval-sm2 last-interval repetitions
  950. ease quality failures
  951. meanq total-repeats))
  952. (simple8 (determine-next-interval-simple8 last-interval repetitions
  953. quality failures meanq
  954. total-repeats
  955. delta-days)))
  956. (if (numberp days-ahead)
  957. (setq next-interval days-ahead))
  958. (if (and (null days-ahead)
  959. (numberp weight) (plusp weight)
  960. (not (minusp next-interval)))
  961. (setq next-interval
  962. (max 1.0 (+ last-interval
  963. (/ (- next-interval last-interval) weight)))))
  964. (org-drill-store-item-data next-interval repetitions failures
  965. total-repeats meanq ease)
  966. (if (eql 'sm5 org-drill-spaced-repetition-algorithm)
  967. (setq org-drill-optimal-factor-matrix new-ofmatrix))
  968. (cond
  969. ((= 0 days-ahead)
  970. (org-schedule t))
  971. ((minusp days-ahead)
  972. (org-schedule nil (current-time)))
  973. (t
  974. (org-schedule nil (time-add (current-time)
  975. (days-to-time
  976. (round next-interval))))))))))
  977. (defun org-drill-hypothetical-next-review-date (quality)
  978. "Returns an integer representing the number of days into the future
  979. that the current item would be scheduled, based on a recall quality
  980. of QUALITY."
  981. (let ((weight (org-entry-get (point) "DRILL_CARD_WEIGHT")))
  982. (destructuring-bind (last-interval repetitions failures
  983. total-repeats meanq ease)
  984. (org-drill-get-item-data)
  985. (if (stringp weight)
  986. (setq weight (read weight)))
  987. (destructuring-bind (next-interval repetitions ease
  988. failures meanq total-repeats
  989. &optional ofmatrix)
  990. (case org-drill-spaced-repetition-algorithm
  991. (sm5 (determine-next-interval-sm5 last-interval repetitions
  992. ease quality failures
  993. meanq total-repeats
  994. org-drill-optimal-factor-matrix))
  995. (sm2 (determine-next-interval-sm2 last-interval repetitions
  996. ease quality failures
  997. meanq total-repeats))
  998. (simple8 (determine-next-interval-simple8 last-interval repetitions
  999. quality failures meanq
  1000. total-repeats)))
  1001. (cond
  1002. ((not (plusp next-interval))
  1003. 0)
  1004. ((and (numberp weight) (plusp weight))
  1005. (+ last-interval
  1006. (max 1.0 (/ (- next-interval last-interval) weight))))
  1007. (t
  1008. next-interval))))))
  1009. (defun org-drill-hypothetical-next-review-dates ()
  1010. (let ((intervals nil))
  1011. (dotimes (q 6)
  1012. (push (max (or (car intervals) 0)
  1013. (org-drill-hypothetical-next-review-date q))
  1014. intervals))
  1015. (reverse intervals)))
  1016. (defun org-drill-reschedule ()
  1017. "Returns quality rating (0-5), or nil if the user quit."
  1018. (let ((ch nil)
  1019. (input nil)
  1020. (next-review-dates (org-drill-hypothetical-next-review-dates)))
  1021. (save-excursion
  1022. (while (not (memq ch '(?q ?e ?0 ?1 ?2 ?3 ?4 ?5)))
  1023. (setq input (read-key-sequence
  1024. (if (eq ch ??)
  1025. (format "0-2 Means you have forgotten the item.
  1026. 3-5 Means you have remembered the item.
  1027. 0 - Completely forgot.
  1028. 1 - Even after seeing the answer, it still took a bit to sink in.
  1029. 2 - After seeing the answer, you remembered it.
  1030. 3 - It took you awhile, but you finally remembered. (+%s days)
  1031. 4 - After a little bit of thought you remembered. (+%s days)
  1032. 5 - You remembered the item really easily. (+%s days)
  1033. How well did you do? (0-5, ?=help, e=edit, t=tags, q=quit)"
  1034. (round (nth 3 next-review-dates))
  1035. (round (nth 4 next-review-dates))
  1036. (round (nth 5 next-review-dates)))
  1037. "How well did you do? (0-5, ?=help, e=edit, t=tags, q=quit)")))
  1038. (cond
  1039. ((stringp input)
  1040. (setq ch (elt input 0)))
  1041. ((and (vectorp input) (symbolp (elt input 0)))
  1042. (case (elt input 0)
  1043. (up (ignore-errors (forward-line -1)))
  1044. (down (ignore-errors (forward-line 1)))
  1045. (left (ignore-errors (backward-char)))
  1046. (right (ignore-errors (forward-char)))
  1047. (prior (ignore-errors (scroll-down))) ; pgup
  1048. (next (ignore-errors (scroll-up))))) ; pgdn
  1049. ((and (vectorp input) (listp (elt input 0))
  1050. (eventp (elt input 0)))
  1051. (case (car (elt input 0))
  1052. (wheel-up (ignore-errors (mwheel-scroll (elt input 0))))
  1053. (wheel-down (ignore-errors (mwheel-scroll (elt input 0)))))))
  1054. (if (eql ch ?t)
  1055. (org-set-tags-command))))
  1056. (cond
  1057. ((and (>= ch ?0) (<= ch ?5))
  1058. (let ((quality (- ch ?0))
  1059. (failures (org-drill-entry-failure-count)))
  1060. (save-excursion
  1061. (org-drill-smart-reschedule quality
  1062. (nth quality next-review-dates)))
  1063. (push quality *org-drill-session-qualities*)
  1064. (cond
  1065. ((<= quality org-drill-failure-quality)
  1066. (when org-drill-leech-failure-threshold
  1067. ;;(setq failures (if failures (string-to-number failures) 0))
  1068. ;; (org-set-property "DRILL_FAILURE_COUNT"
  1069. ;; (format "%d" (1+ failures)))
  1070. (if (> (1+ failures) org-drill-leech-failure-threshold)
  1071. (org-toggle-tag "leech" 'on))))
  1072. (t
  1073. (let ((scheduled-time (org-get-scheduled-time (point))))
  1074. (when scheduled-time
  1075. (message "Next review in %d days"
  1076. (- (time-to-days scheduled-time)
  1077. (time-to-days (current-time))))
  1078. (sit-for 0.5)))))
  1079. (org-set-property "DRILL_LAST_QUALITY" (format "%d" quality))
  1080. (org-set-property "DRILL_LAST_REVIEWED"
  1081. (time-to-inactive-org-timestamp (current-time)))
  1082. quality))
  1083. ((= ch ?e)
  1084. 'edit)
  1085. (t
  1086. nil))))
  1087. ;; (defun org-drill-hide-all-subheadings-except (heading-list)
  1088. ;; "Returns a list containing the position of each immediate subheading of
  1089. ;; the current topic."
  1090. ;; (let ((drill-entry-level (org-current-level))
  1091. ;; (drill-sections nil)
  1092. ;; (drill-heading nil))
  1093. ;; (org-show-subtree)
  1094. ;; (save-excursion
  1095. ;; (org-map-entries
  1096. ;; (lambda ()
  1097. ;; (when (and (not (outline-invisible-p))
  1098. ;; (> (org-current-level) drill-entry-level))
  1099. ;; (setq drill-heading (org-get-heading t))
  1100. ;; (unless (and (= (org-current-level) (1+ drill-entry-level))
  1101. ;; (member drill-heading heading-list))
  1102. ;; (hide-subtree))
  1103. ;; (push (point) drill-sections)))
  1104. ;; "" 'tree))
  1105. ;; (reverse drill-sections)))
  1106. (defun org-drill-hide-subheadings-if (test)
  1107. "TEST is a function taking no arguments. TEST will be called for each
  1108. of the immediate subheadings of the current drill item, with the point
  1109. on the relevant subheading. TEST should return nil if the subheading is
  1110. to be revealed, non-nil if it is to be hidden.
  1111. Returns a list containing the position of each immediate subheading of
  1112. the current topic."
  1113. (let ((drill-entry-level (org-current-level))
  1114. (drill-sections nil))
  1115. (org-show-subtree)
  1116. (save-excursion
  1117. (org-map-entries
  1118. (lambda ()
  1119. (when (and (not (outline-invisible-p))
  1120. (> (org-current-level) drill-entry-level))
  1121. (when (or (/= (org-current-level) (1+ drill-entry-level))
  1122. (funcall test))
  1123. (hide-subtree))
  1124. (push (point) drill-sections)))
  1125. "" 'tree))
  1126. (reverse drill-sections)))
  1127. (defun org-drill-hide-all-subheadings-except (heading-list)
  1128. (org-drill-hide-subheadings-if
  1129. (lambda () (let ((drill-heading (org-get-heading t)))
  1130. (not (member drill-heading heading-list))))))
  1131. (defun org-drill-presentation-prompt (&rest fmt-and-args)
  1132. (let* ((item-start-time (current-time))
  1133. (input nil)
  1134. (ch nil)
  1135. (last-second 0)
  1136. (mature-entry-count (+ (length *org-drill-young-mature-entries*)
  1137. (length *org-drill-old-mature-entries*)
  1138. (length *org-drill-overdue-entries*)))
  1139. (status (first (org-drill-entry-status)))
  1140. (prompt
  1141. (if fmt-and-args
  1142. (apply 'format
  1143. (first fmt-and-args)
  1144. (rest fmt-and-args))
  1145. (concat "Press key for answer, "
  1146. "e=edit, t=tags, s=skip, q=quit."))))
  1147. (setq prompt
  1148. (format "%s %s %s %s %s %s"
  1149. (propertize
  1150. (char-to-string
  1151. (case status
  1152. (:new ?N) (:young ?Y) (:old ?o) (:overdue ?!)
  1153. (:failed ?F) (t ??)))
  1154. 'face `(:foreground
  1155. ,(case status
  1156. (:new org-drill-new-count-color)
  1157. ((:young :old) org-drill-mature-count-color)
  1158. ((:overdue :failed) org-drill-failed-count-color)
  1159. (t org-drill-done-count-color))))
  1160. (propertize
  1161. (number-to-string (length *org-drill-done-entries*))
  1162. 'face `(:foreground ,org-drill-done-count-color)
  1163. 'help-echo "The number of items you have reviewed this session.")
  1164. (propertize
  1165. (number-to-string (+ (length *org-drill-again-entries*)
  1166. (length *org-drill-failed-entries*)))
  1167. 'face `(:foreground ,org-drill-failed-count-color)
  1168. 'help-echo (concat "The number of items that you failed, "
  1169. "and need to review again."))
  1170. (propertize
  1171. (number-to-string mature-entry-count)
  1172. 'face `(:foreground ,org-drill-mature-count-color)
  1173. 'help-echo "The number of old items due for review.")
  1174. (propertize
  1175. (number-to-string (length *org-drill-new-entries*))
  1176. 'face `(:foreground ,org-drill-new-count-color)
  1177. 'help-echo (concat "The number of new items that you "
  1178. "have never reviewed."))
  1179. prompt))
  1180. (if (and (eql 'warn org-drill-leech-method)
  1181. (org-drill-entry-leech-p))
  1182. (setq prompt (concat
  1183. (propertize "!!! LEECH ITEM !!!
  1184. You seem to be having a lot of trouble memorising this item.
  1185. Consider reformulating the item to make it easier to remember.\n"
  1186. 'face '(:foreground "red"))
  1187. prompt)))
  1188. (while (memq ch '(nil ?t))
  1189. (setq ch nil)
  1190. (while (not (input-pending-p))
  1191. (let ((elapsed (time-subtract (current-time) item-start-time)))
  1192. (message (concat (if (>= (time-to-seconds elapsed) (* 60 60))
  1193. "++:++ "
  1194. (format-time-string "%M:%S " elapsed))
  1195. prompt))
  1196. (sit-for 1)))
  1197. (setq input (read-key-sequence nil))
  1198. (if (stringp input) (setq ch (elt input 0)))
  1199. (if (eql ch ?t)
  1200. (org-set-tags-command)))
  1201. (case ch
  1202. (?q nil)
  1203. (?e 'edit)
  1204. (?s 'skip)
  1205. (otherwise t))))
  1206. (defun org-pos-in-regexp (pos regexp &optional nlines)
  1207. (save-excursion
  1208. (goto-char pos)
  1209. (org-in-regexp regexp nlines)))
  1210. (defun org-drill-hide-region (beg end &optional text)
  1211. "Hide the buffer region between BEG and END with an 'invisible text'
  1212. visual overlay, or with the string TEXT if it is supplied."
  1213. (let ((ovl (make-overlay beg end)))
  1214. (overlay-put ovl 'category
  1215. 'org-drill-hidden-text-overlay)
  1216. (when (stringp text)
  1217. (overlay-put ovl 'invisible nil)
  1218. (overlay-put ovl 'face 'default)
  1219. (overlay-put ovl 'display text))))
  1220. (defun org-drill-hide-heading-at-point (&optional text)
  1221. (unless (org-at-heading-p)
  1222. (error "Point is not on a heading."))
  1223. (save-excursion
  1224. (let ((beg (point)))
  1225. (end-of-line)
  1226. (org-drill-hide-region beg (point) text))))
  1227. (defun org-drill-hide-comments ()
  1228. (save-excursion
  1229. (while (re-search-forward "^#.*$" nil t)
  1230. (org-drill-hide-region (match-beginning 0) (match-end 0)))))
  1231. (defun org-drill-unhide-text ()
  1232. ;; This will also unhide the item's heading.
  1233. (save-excursion
  1234. (dolist (ovl (overlays-in (point-min) (point-max)))
  1235. (when (eql 'org-drill-hidden-text-overlay (overlay-get ovl 'category))
  1236. (delete-overlay ovl)))))
  1237. (defun org-drill-hide-clozed-text ()
  1238. (save-excursion
  1239. (while (re-search-forward org-drill-cloze-regexp nil t)
  1240. ;; Don't hide org links, partly because they might contain inline
  1241. ;; images which we want to keep visible
  1242. (unless (save-match-data
  1243. (org-pos-in-regexp (match-beginning 0)
  1244. org-bracket-link-regexp 1))
  1245. (org-drill-hide-matched-cloze-text)))))
  1246. (defun org-drill-hide-matched-cloze-text ()
  1247. "Hide the current match with a 'cloze' visual overlay."
  1248. (let ((ovl (make-overlay (match-beginning 0) (match-end 0))))
  1249. (overlay-put ovl 'category
  1250. 'org-drill-cloze-overlay-defaults)
  1251. (when (find ?| (match-string 0))
  1252. (let ((hint (substring-no-properties
  1253. (match-string 0)
  1254. (1+ (position ?| (match-string 0)))
  1255. (1- (length (match-string 0))))))
  1256. (overlay-put
  1257. ovl 'display
  1258. ;; If hint is like `X...' then display [X...]
  1259. ;; otherwise display [...X]
  1260. (format (if (string-match-p "\\.\\.\\." hint) "[%s]" "[%s...]")
  1261. hint))))))
  1262. (defun org-drill-hide-cloze-hints ()
  1263. (save-excursion
  1264. (while (re-search-forward org-drill-cloze-regexp nil t)
  1265. (unless (or (save-match-data
  1266. (org-pos-in-regexp (match-beginning 0)
  1267. org-bracket-link-regexp 1))
  1268. (null (match-beginning 2))) ; hint subexpression matched
  1269. (org-drill-hide-region (match-beginning 2) (match-end 2))))))
  1270. (defmacro with-replaced-entry-text (text &rest body)
  1271. "During the execution of BODY, the entire text of the current entry is
  1272. concealed by an overlay that displays the string TEXT."
  1273. `(progn
  1274. (org-drill-replace-entry-text ,text)
  1275. (unwind-protect
  1276. (progn
  1277. ,@body)
  1278. (org-drill-unreplace-entry-text))))
  1279. (defun org-drill-replace-entry-text (text)
  1280. "Make an overlay that conceals the entire text of the item, not
  1281. including properties or the contents of subheadings. The overlay shows
  1282. the string TEXT.
  1283. Note: does not actually alter the item."
  1284. (let ((ovl (make-overlay (point-min)
  1285. (save-excursion
  1286. (outline-next-heading)
  1287. (point)))))
  1288. (overlay-put ovl 'category
  1289. 'org-drill-replaced-text-overlay)
  1290. (overlay-put ovl 'display text)))
  1291. (defun org-drill-unreplace-entry-text ()
  1292. (save-excursion
  1293. (dolist (ovl (overlays-in (point-min) (point-max)))
  1294. (when (eql 'org-drill-replaced-text-overlay (overlay-get ovl 'category))
  1295. (delete-overlay ovl)))))
  1296. (defmacro with-replaced-entry-heading (heading &rest body)
  1297. `(progn
  1298. (org-drill-replace-entry-heading ,heading)
  1299. (unwind-protect
  1300. (progn
  1301. ,@body)
  1302. (org-drill-unhide-text))))
  1303. (defun org-drill-replace-entry-heading (heading)
  1304. "Make an overlay that conceals the heading of the item. The overlay shows
  1305. the string TEXT.
  1306. Note: does not actually alter the item."
  1307. (org-drill-hide-heading-at-point heading))
  1308. (defun org-drill-unhide-clozed-text ()
  1309. (save-excursion
  1310. (dolist (ovl (overlays-in (point-min) (point-max)))
  1311. (when (eql 'org-drill-cloze-overlay-defaults (overlay-get ovl 'category))
  1312. (delete-overlay ovl)))))
  1313. (defun org-drill-get-entry-text (&optional keep-properties-p)
  1314. (let ((text (org-agenda-get-some-entry-text (point-marker) 100)))
  1315. (if keep-properties-p
  1316. text
  1317. (substring-no-properties text))))
  1318. (defun org-drill-entry-empty-p ()
  1319. (zerop (length (org-drill-get-entry-text))))
  1320. ;;; Presentation functions ====================================================
  1321. ;; Each of these is called with point on topic heading. Each needs to show the
  1322. ;; topic in the form of a 'question' or with some information 'hidden', as
  1323. ;; appropriate for the card type. The user should then be prompted to press a
  1324. ;; key. The function should then reveal either the 'answer' or the entire
  1325. ;; topic, and should return t if the user chose to see the answer and rate their
  1326. ;; recall, nil if they chose to quit.
  1327. (defun org-drill-present-simple-card ()
  1328. (with-hidden-comments
  1329. (with-hidden-cloze-hints
  1330. (with-hidden-cloze-text
  1331. (org-drill-hide-all-subheadings-except nil)
  1332. (org-display-inline-images t)
  1333. (org-cycle-hide-drawers 'all)
  1334. (prog1 (org-drill-presentation-prompt)
  1335. (org-drill-hide-subheadings-if 'org-drill-entry-p))))))
  1336. (defun org-drill-present-default-answer (reschedule-fn)
  1337. (org-drill-hide-subheadings-if 'org-drill-entry-p)
  1338. (org-drill-unhide-clozed-text)
  1339. (with-hidden-cloze-hints
  1340. (funcall reschedule-fn)))
  1341. (defun org-drill-present-two-sided-card ()
  1342. (with-hidden-comments
  1343. (with-hidden-cloze-hints
  1344. (with-hidden-cloze-text
  1345. (let ((drill-sections (org-drill-hide-all-subheadings-except nil)))
  1346. (when drill-sections
  1347. (save-excursion
  1348. (goto-char (nth (random* (min 2 (length drill-sections)))
  1349. drill-sections))
  1350. (org-show-subtree)))
  1351. (org-display-inline-images t)
  1352. (org-cycle-hide-drawers 'all)
  1353. (prog1 (org-drill-presentation-prompt)
  1354. (org-drill-hide-subheadings-if 'org-drill-entry-p)))))))
  1355. (defun org-drill-present-multi-sided-card ()
  1356. (with-hidden-comments
  1357. (with-hidden-cloze-hints
  1358. (with-hidden-cloze-text
  1359. (let ((drill-sections (org-drill-hide-all-subheadings-except nil)))
  1360. (when drill-sections
  1361. (save-excursion
  1362. (goto-char (nth (random* (length drill-sections)) drill-sections))
  1363. (org-show-subtree)))
  1364. (org-display-inline-images t)
  1365. (org-cycle-hide-drawers 'all)
  1366. (prog1 (org-drill-presentation-prompt)
  1367. (org-drill-hide-subheadings-if 'org-drill-entry-p)))))))
  1368. (defun org-drill-present-multicloze-hide-n (number-to-hide
  1369. &optional
  1370. force-show-first
  1371. force-show-last
  1372. force-hide-first)
  1373. "Hides NUMBER-TO-HIDE pieces of text that are marked for cloze deletion,
  1374. chosen at random.
  1375. If NUMBER-TO-HIDE is negative, show only (ABS NUMBER-TO-HIDE) pieces,
  1376. hiding all the rest.
  1377. If FORCE-HIDE-FIRST is non-nil, force the first piece of text to be one of
  1378. the hidden items.
  1379. If FORCE-SHOW-FIRST is non-nil, never hide the first piece of text.
  1380. If FORCE-SHOW-LAST is non-nil, never hide the last piece of text.
  1381. If the number of text pieces in the item is less than
  1382. NUMBER-TO-HIDE, then all text pieces will be hidden (except the first or last
  1383. items if FORCE-SHOW-FIRST or FORCE-SHOW-LAST is non-nil)."
  1384. (with-hidden-comments
  1385. (with-hidden-cloze-hints
  1386. (let ((item-end nil)
  1387. (match-count 0)
  1388. (body-start (or (cdr (org-get-property-block))
  1389. (point))))
  1390. (if (and force-hide-first force-show-first)
  1391. (error "FORCE-HIDE-FIRST and FORCE-SHOW-FIRST are mutually exclusive"))
  1392. (org-drill-hide-all-subheadings-except nil)
  1393. (save-excursion
  1394. (outline-next-heading)
  1395. (setq item-end (point)))
  1396. (save-excursion
  1397. (goto-char body-start)
  1398. (while (re-search-forward org-drill-cloze-regexp item-end t)
  1399. (let ((in-regexp? (save-match-data
  1400. (org-pos-in-regexp (match-beginning 0)
  1401. org-bracket-link-regexp 1))))
  1402. (unless in-regexp?
  1403. (incf match-count)))))
  1404. (if (minusp number-to-hide)
  1405. (setq number-to-hide (+ match-count number-to-hide)))
  1406. (when (plusp match-count)
  1407. (let* ((positions (shuffle-list (loop for i from 1
  1408. to match-count
  1409. collect i)))
  1410. (match-nums nil)
  1411. (cnt nil))
  1412. (if force-hide-first
  1413. ;; Force '1' to be in the list, and to be the first item
  1414. ;; in the list.
  1415. (setq positions (cons 1 (remove 1 positions))))
  1416. (if force-show-first
  1417. (setq positions (remove 1 positions)))
  1418. (if force-show-last
  1419. (setq positions (remove match-count positions)))
  1420. (setq match-nums
  1421. (subseq positions
  1422. 0 (min number-to-hide (length positions))))
  1423. ;; (dolist (pos-to-hide match-nums)
  1424. (save-excursion
  1425. (goto-char body-start)
  1426. (setq cnt 0)
  1427. (while (re-search-forward org-drill-cloze-regexp item-end t)
  1428. (unless (save-match-data
  1429. (org-pos-in-regexp (match-beginning 0)
  1430. org-bracket-link-regexp 1))
  1431. (incf cnt)
  1432. (if (memq cnt match-nums)
  1433. (org-drill-hide-matched-cloze-text)))))))
  1434. ;; (loop
  1435. ;; do (re-search-forward org-drill-cloze-regexp
  1436. ;; item-end t pos-to-hide)
  1437. ;; while (org-pos-in-regexp (match-beginning 0)
  1438. ;; org-bracket-link-regexp 1))
  1439. ;; (org-drill-hide-matched-cloze-text)))))
  1440. (org-display-inline-images t)
  1441. (org-cycle-hide-drawers 'all)
  1442. (prog1 (org-drill-presentation-prompt)
  1443. (org-drill-hide-subheadings-if 'org-drill-entry-p)
  1444. (org-drill-unhide-clozed-text))))))
  1445. (defun org-drill-present-multicloze-hide-nth (to-hide)
  1446. "Hide the TO-HIDE'th piece of clozed text. 1 is the first piece. If
  1447. TO-HIDE is negative, count backwards, so -1 means the last item, -2
  1448. the second to last, etc."
  1449. (with-hidden-comments
  1450. (with-hidden-cloze-hints
  1451. (let ((item-end nil)
  1452. (match-count 0)
  1453. (body-start (or (cdr (org-get-property-block))
  1454. (point)))
  1455. (cnt 0))
  1456. (org-drill-hide-all-subheadings-except nil)
  1457. (save-excursion
  1458. (outline-next-heading)
  1459. (setq item-end (point)))
  1460. (save-excursion
  1461. (goto-char body-start)
  1462. (while (re-search-forward org-drill-cloze-regexp item-end t)
  1463. (let ((in-regexp? (save-match-data
  1464. (org-pos-in-regexp (match-beginning 0)
  1465. org-bracket-link-regexp 1))))
  1466. (unless in-regexp?
  1467. (incf match-count)))))
  1468. (if (minusp to-hide)
  1469. (setq to-hide (+ 1 to-hide match-count)))
  1470. (cond
  1471. ((or (not (plusp match-count))
  1472. (> to-hide match-count))
  1473. nil)
  1474. (t
  1475. (save-excursion
  1476. (goto-char body-start)
  1477. (setq cnt 0)
  1478. (while (re-search-forward org-drill-cloze-regexp item-end t)
  1479. (unless (save-match-data
  1480. (org-pos-in-regexp (match-beginning 0)
  1481. org-bracket-link-regexp 1))
  1482. (incf cnt)
  1483. (if (= cnt to-hide)
  1484. (org-drill-hide-matched-cloze-text)))))))
  1485. (org-display-inline-images t)
  1486. (org-cycle-hide-drawers 'all)
  1487. (prog1 (org-drill-presentation-prompt)
  1488. (org-drill-hide-subheadings-if 'org-drill-entry-p)
  1489. (org-drill-unhide-clozed-text))))))
  1490. (defun org-drill-present-multicloze-hide1 ()
  1491. "Hides one of the pieces of text that are marked for cloze deletion,
  1492. chosen at random."
  1493. (org-drill-present-multicloze-hide-n 1))
  1494. (defun org-drill-present-multicloze-hide2 ()
  1495. "Hides two of the pieces of text that are marked for cloze deletion,
  1496. chosen at random."
  1497. (org-drill-present-multicloze-hide-n 2))
  1498. (defun org-drill-present-multicloze-hide-first ()
  1499. "Hides the first piece of text that is marked for cloze deletion."
  1500. (org-drill-present-multicloze-hide-nth 1))
  1501. (defun org-drill-present-multicloze-hide-last ()
  1502. "Hides the last piece of text that is marked for cloze deletion."
  1503. (org-drill-present-multicloze-hide-nth -1))
  1504. (defun org-drill-present-multicloze-hide1-firstmore ()
  1505. "Commonly, hides the FIRST piece of text that is marked for
  1506. cloze deletion. Uncommonly, hide one of the other pieces of text,
  1507. chosen at random.
  1508. The definitions of 'commonly' and 'uncommonly' are determined by
  1509. the value of `org-drill-cloze-text-weight'."
  1510. ;; The 'firstmore' and 'lastmore' functions used to randomly choose whether
  1511. ;; to hide the 'favoured' piece of text. However even when the chance of
  1512. ;; hiding it was set quite high (80%), the outcome was too unpredictable over
  1513. ;; the small number of repetitions where most learning takes place for each
  1514. ;; item. In other words, the actual frequency during the first 10 repetitions
  1515. ;; was often very different from 80%. Hence we use modulo instead.
  1516. (cond
  1517. ((null org-drill-cloze-text-weight)
  1518. ;; Behave as hide1cloze
  1519. (org-drill-present-multicloze-hide1))
  1520. ((not (and (integerp org-drill-cloze-text-weight)
  1521. (plusp org-drill-cloze-text-weight)))
  1522. (error "Illegal value for org-drill-cloze-text-weight: %S"
  1523. org-drill-cloze-text-weight))
  1524. ((zerop (mod (1+ (org-drill-entry-total-repeats 0))
  1525. org-drill-cloze-text-weight))
  1526. ;; Uncommonly, hide any item except the first
  1527. (org-drill-present-multicloze-hide-n 1 t))
  1528. (t
  1529. ;; Commonly, hide first item
  1530. (org-drill-present-multicloze-hide-first))))
  1531. (defun org-drill-present-multicloze-show1-lastmore ()
  1532. "Commonly, hides all pieces except the last. Uncommonly, shows
  1533. any random piece. The effect is similar to 'show1cloze' except
  1534. that the last item is much less likely to be the item that is
  1535. visible.
  1536. The definitions of 'commonly' and 'uncommonly' are determined by
  1537. the value of `org-drill-cloze-text-weight'."
  1538. (cond
  1539. ((null org-drill-cloze-text-weight)
  1540. ;; Behave as show1cloze
  1541. (org-drill-present-multicloze-show1))
  1542. ((not (and (integerp org-drill-cloze-text-weight)
  1543. (plusp org-drill-cloze-text-weight)))
  1544. (error "Illegal value for org-drill-cloze-text-weight: %S"
  1545. org-drill-cloze-text-weight))
  1546. ((zerop (mod (1+ (org-drill-entry-total-repeats 0))
  1547. org-drill-cloze-text-weight))
  1548. ;; Uncommonly, show any item except the last
  1549. (org-drill-present-multicloze-hide-n -1 nil nil t))
  1550. (t
  1551. ;; Commonly, show the LAST item
  1552. (org-drill-present-multicloze-hide-n -1 nil t))))
  1553. (defun org-drill-present-multicloze-show1-firstless ()
  1554. "Commonly, hides all pieces except one, where the shown piece
  1555. is guaranteed NOT to be the first piece. Uncommonly, shows any
  1556. random piece. The effect is similar to 'show1cloze' except that
  1557. the first item is much less likely to be the item that is
  1558. visible.
  1559. The definitions of 'commonly' and 'uncommonly' are determined by
  1560. the value of `org-drill-cloze-text-weight'."
  1561. (cond
  1562. ((null org-drill-cloze-text-weight)
  1563. ;; Behave as show1cloze
  1564. (org-drill-present-multicloze-show1))
  1565. ((not (and (integerp org-drill-cloze-text-weight)
  1566. (plusp org-drill-cloze-text-weight)))
  1567. (error "Illegal value for org-drill-cloze-text-weight: %S"
  1568. org-drill-cloze-text-weight))
  1569. ((zerop (mod (1+ (org-drill-entry-total-repeats 0))
  1570. org-drill-cloze-text-weight))
  1571. ;; Uncommonly, show the first item
  1572. (org-drill-present-multicloze-hide-n -1 t))
  1573. (t
  1574. ;; Commonly, show any item, except the first
  1575. (org-drill-present-multicloze-hide-n -1 nil nil t))))
  1576. (defun org-drill-present-multicloze-show1 ()
  1577. "Similar to `org-drill-present-multicloze-hide1', but hides all
  1578. the pieces of text that are marked for cloze deletion, except for one
  1579. piece which is chosen at random."
  1580. (org-drill-present-multicloze-hide-n -1))
  1581. (defun org-drill-present-multicloze-show2 ()
  1582. "Similar to `org-drill-present-multicloze-show1', but reveals two
  1583. pieces rather than one."
  1584. (org-drill-present-multicloze-hide-n -2))
  1585. ;; (defun org-drill-present-multicloze-show1 ()
  1586. ;; "Similar to `org-drill-present-multicloze-hide1', but hides all
  1587. ;; the pieces of text that are marked for cloze deletion, except for one
  1588. ;; piece which is chosen at random."
  1589. ;; (with-hidden-comments
  1590. ;; (with-hidden-cloze-hints
  1591. ;; (let ((item-end nil)
  1592. ;; (match-count 0)
  1593. ;; (body-start (or (cdr (org-get-property-block))
  1594. ;; (point))))
  1595. ;; (org-drill-hide-all-subheadings-except nil)
  1596. ;; (save-excursion
  1597. ;; (outline-next-heading)
  1598. ;; (setq item-end (point)))
  1599. ;; (save-excursion
  1600. ;; (goto-char body-start)
  1601. ;; (while (re-search-forward org-drill-cloze-regexp item-end t)
  1602. ;; (incf match-count)))
  1603. ;; (when (plusp match-count)
  1604. ;; (let ((match-to-hide (random* match-count)))
  1605. ;; (save-excursion
  1606. ;; (goto-char body-start)
  1607. ;; (dotimes (n match-count)
  1608. ;; (re-search-forward org-drill-cloze-regexp
  1609. ;; item-end t)
  1610. ;; (unless (= n match-to-hide)
  1611. ;; (org-drill-hide-matched-cloze-text))))))
  1612. ;; (org-display-inline-images t)
  1613. ;; (org-cycle-hide-drawers 'all)
  1614. ;; (prog1 (org-drill-presentation-prompt)
  1615. ;; (org-drill-hide-subheadings-if 'org-drill-entry-p)
  1616. ;; (org-drill-unhide-clozed-text))))))
  1617. (defun org-drill-present-card-using-text (question &optional answer)
  1618. "Present the string QUESTION as the only visible content of the card."
  1619. (with-hidden-comments
  1620. (with-replaced-entry-text
  1621. question
  1622. (org-drill-hide-all-subheadings-except nil)
  1623. (org-cycle-hide-drawers 'all)
  1624. (prog1 (org-drill-presentation-prompt)
  1625. (org-drill-hide-subheadings-if 'org-drill-entry-p)))))
  1626. ;;; The following macro is necessary because `org-save-outline-visibility'
  1627. ;;; currently discards the value returned by its body and returns a garbage
  1628. ;;; value instead. (as at org mode v 7.5)
  1629. (defmacro org-drill-save-visibility (&rest body)
  1630. "Store the current visibility state of the org buffer, and restore it
  1631. after executing BODY. Return the value of the last expression
  1632. in BODY."
  1633. (let ((retval (gensym)))
  1634. `(let ((,retval nil))
  1635. (org-save-outline-visibility t
  1636. (setq ,retval
  1637. (progn
  1638. ,@body)))
  1639. ,retval)))
  1640. (defun org-drill-entry ()
  1641. "Present the current topic for interactive review, as in `org-drill'.
  1642. Review will occur regardless of whether the topic is due for review or whether
  1643. it meets the definition of a 'review topic' used by `org-drill'.
  1644. Returns a quality rating from 0 to 5, or nil if the user quit, or the symbol
  1645. EDIT if the user chose to exit the drill and edit the current item. Choosing
  1646. the latter option leaves the drill session suspended; it can be resumed
  1647. later using `org-drill-resume'.
  1648. See `org-drill' for more details."
  1649. (interactive)
  1650. (org-drill-goto-drill-entry-heading)
  1651. ;;(unless (org-part-of-drill-entry-p)
  1652. ;; (error "Point is not inside a drill entry"))
  1653. ;;(unless (org-at-heading-p)
  1654. ;; (org-back-to-heading))
  1655. (let ((card-type (org-entry-get (point) "DRILL_CARD_TYPE"))
  1656. (answer-fn 'org-drill-present-default-answer)
  1657. (cont nil))
  1658. (org-drill-save-visibility
  1659. (save-restriction
  1660. (org-narrow-to-subtree)
  1661. (org-show-subtree)
  1662. (org-cycle-hide-drawers 'all)
  1663. (let ((presentation-fn (cdr (assoc card-type org-drill-card-type-alist))))
  1664. (if (listp presentation-fn)
  1665. (psetq answer-fn (or (second presentation-fn)
  1666. 'org-drill-present-default-answer)
  1667. presentation-fn (first presentation-fn)))
  1668. (cond
  1669. ((null presentation-fn)
  1670. (message "%s:%d: Unrecognised card type '%s', skipping..."
  1671. (buffer-name) (point) card-type)
  1672. (sit-for 0.5)
  1673. 'skip)
  1674. (t
  1675. (setq cont (funcall presentation-fn))
  1676. (cond
  1677. ((not cont)
  1678. (message "Quit")
  1679. nil)
  1680. ((eql cont 'edit)
  1681. 'edit)
  1682. ((eql cont 'skip)
  1683. 'skip)
  1684. (t
  1685. (save-excursion
  1686. (funcall answer-fn
  1687. (lambda () (org-drill-reschedule)))))))))))))
  1688. (defun org-drill-entries-pending-p ()
  1689. (or *org-drill-again-entries*
  1690. (and (not (org-drill-maximum-item-count-reached-p))
  1691. (not (org-drill-maximum-duration-reached-p))
  1692. (or *org-drill-new-entries*
  1693. *org-drill-failed-entries*
  1694. *org-drill-young-mature-entries*
  1695. *org-drill-old-mature-entries*
  1696. *org-drill-overdue-entries*
  1697. *org-drill-again-entries*))))
  1698. (defun org-drill-pending-entry-count ()
  1699. (+ (length *org-drill-new-entries*)
  1700. (length *org-drill-failed-entries*)
  1701. (length *org-drill-young-mature-entries*)
  1702. (length *org-drill-old-mature-entries*)
  1703. (length *org-drill-overdue-entries*)
  1704. (length *org-drill-again-entries*)))
  1705. (defun org-drill-maximum-duration-reached-p ()
  1706. "Returns true if the current drill session has continued past its
  1707. maximum duration."
  1708. (and org-drill-maximum-duration
  1709. *org-drill-start-time*
  1710. (> (- (float-time (current-time)) *org-drill-start-time*)
  1711. (* org-drill-maximum-duration 60))))
  1712. (defun org-drill-maximum-item-count-reached-p ()
  1713. "Returns true if the current drill session has reached the
  1714. maximum number of items."
  1715. (and org-drill-maximum-items-per-session
  1716. (>= (length *org-drill-done-entries*)
  1717. org-drill-maximum-items-per-session)))
  1718. (defun org-drill-pop-next-pending-entry ()
  1719. (block org-drill-pop-next-pending-entry
  1720. (let ((m nil))
  1721. (while (or (null m)
  1722. (not (org-drill-entry-p m)))
  1723. (setq
  1724. m
  1725. (cond
  1726. ;; First priority is items we failed in a prior session.
  1727. ((and *org-drill-failed-entries*
  1728. (not (org-drill-maximum-item-count-reached-p))
  1729. (not (org-drill-maximum-duration-reached-p)))
  1730. (pop-random *org-drill-failed-entries*))
  1731. ;; Next priority is overdue items.
  1732. ((and *org-drill-overdue-entries*
  1733. (not (org-drill-maximum-item-count-reached-p))
  1734. (not (org-drill-maximum-duration-reached-p)))
  1735. ;; We use `pop', not `pop-random', because we have already
  1736. ;; sorted overdue items into a random order which takes
  1737. ;; number of days overdue into account.
  1738. (pop *org-drill-overdue-entries*))
  1739. ;; Next priority is 'young' items.
  1740. ((and *org-drill-young-mature-entries*
  1741. (not (org-drill-maximum-item-count-reached-p))
  1742. (not (org-drill-maximum-duration-reached-p)))
  1743. (pop-random *org-drill-young-mature-entries*))
  1744. ;; Next priority is newly added items, and older entries.
  1745. ;; We pool these into a single group.
  1746. ((and (or *org-drill-new-entries*
  1747. *org-drill-old-mature-entries*)
  1748. (not (org-drill-maximum-item-count-reached-p))
  1749. (not (org-drill-maximum-duration-reached-p)))
  1750. (cond
  1751. ((< (random* (+ (length *org-drill-new-entries*)
  1752. (length *org-drill-old-mature-entries*)))
  1753. (length *org-drill-new-entries*))
  1754. (pop-random *org-drill-new-entries*))
  1755. (t
  1756. (pop-random *org-drill-old-mature-entries*))))
  1757. ;; After all the above are done, last priority is items
  1758. ;; that were failed earlier THIS SESSION.
  1759. (*org-drill-again-entries*
  1760. (pop *org-drill-again-entries*))
  1761. (t ; nothing left -- return nil
  1762. (return-from org-drill-pop-next-pending-entry nil)))))
  1763. m)))
  1764. (defun org-drill-entries (&optional resuming-p)
  1765. "Returns nil, t, or a list of markers representing entries that were
  1766. 'failed' and need to be presented again before the session ends.
  1767. RESUMING-P is true if we are resuming a suspended drill session."
  1768. (block org-drill-entries
  1769. (while (org-drill-entries-pending-p)
  1770. (let ((m (cond
  1771. ((or (not resuming-p)
  1772. (null *org-drill-current-item*)
  1773. (not (org-drill-entry-p *org-drill-current-item*)))
  1774. (org-drill-pop-next-pending-entry))
  1775. (t ; resuming a suspended session.
  1776. (setq resuming-p nil)
  1777. *org-drill-current-item*))))
  1778. (setq *org-drill-current-item* m)
  1779. (unless m
  1780. (error "Unexpectedly ran out of pending drill items"))
  1781. (save-excursion
  1782. (org-drill-goto-entry m)
  1783. (cond
  1784. ((not (org-drill-entry-due-p))
  1785. ;; The entry is not due anymore. This could arise if the user
  1786. ;; suspends a drill session, then drills an individual entry,
  1787. ;; then resumes the session.
  1788. (message "Entry no longer due, skipping...")
  1789. (sit-for 0.3)
  1790. nil)
  1791. (t
  1792. (setq result (org-drill-entry))
  1793. (cond
  1794. ((null result)
  1795. (message "Quit")
  1796. (setq end-pos :quit)
  1797. (return-from org-drill-entries nil))
  1798. ((eql result 'edit)
  1799. (setq end-pos (point-marker))
  1800. (return-from org-drill-entries nil))
  1801. ((eql result 'skip)
  1802. nil) ; skip this item
  1803. (t
  1804. (cond
  1805. ((<= result org-drill-failure-quality)
  1806. (if *org-drill-again-entries*
  1807. (setq *org-drill-again-entries*
  1808. (shuffle-list *org-drill-again-entries*)))
  1809. (push-end m *org-drill-again-entries*))
  1810. (t
  1811. (push m *org-drill-done-entries*))))))))))))
  1812. (defun org-drill-final-report ()
  1813. (let ((pass-percent
  1814. (round (* 100 (count-if (lambda (qual)
  1815. (> qual org-drill-failure-quality))
  1816. *org-drill-session-qualities*))
  1817. (max 1 (length *org-drill-session-qualities*))))
  1818. (prompt nil))
  1819. (setq prompt
  1820. (format
  1821. "%d items reviewed. Session duration %s.
  1822. Recall of reviewed items:
  1823. Excellent (5): %3d%% | Near miss (2): %3d%%
  1824. Good (4): %3d%% | Failure (1): %3d%%
  1825. Hard (3): %3d%% | Abject failure (0): %3d%%
  1826. You successfully recalled %d%% of reviewed items (quality > %s)
  1827. %d/%d items still await review (%s, %s, %s, %s, %s).
  1828. Tomorrow, %d more items will become due for review.
  1829. Session finished. Press a key to continue..."
  1830. (length *org-drill-done-entries*)
  1831. (format-seconds "%h:%.2m:%.2s"
  1832. (- (float-time (current-time)) *org-drill-start-time*))
  1833. (round (* 100 (count 5 *org-drill-session-qualities*))
  1834. (max 1 (length *org-drill-session-qualities*)))
  1835. (round (* 100 (count 2 *org-drill-session-qualities*))
  1836. (max 1 (length *org-drill-session-qualities*)))
  1837. (round (* 100 (count 4 *org-drill-session-qualities*))
  1838. (max 1 (length *org-drill-session-qualities*)))
  1839. (round (* 100 (count 1 *org-drill-session-qualities*))
  1840. (max 1 (length *org-drill-session-qualities*)))
  1841. (round (* 100 (count 3 *org-drill-session-qualities*))
  1842. (max 1 (length *org-drill-session-qualities*)))
  1843. (round (* 100 (count 0 *org-drill-session-qualities*))
  1844. (max 1 (length *org-drill-session-qualities*)))
  1845. pass-percent
  1846. org-drill-failure-quality
  1847. (org-drill-pending-entry-count)
  1848. (+ (org-drill-pending-entry-count)
  1849. *org-drill-dormant-entry-count*)
  1850. (propertize
  1851. (format "%d failed"
  1852. (+ (length *org-drill-failed-entries*)
  1853. (length *org-drill-again-entries*)))
  1854. 'face `(:foreground ,org-drill-failed-count-color))
  1855. (propertize
  1856. (format "%d overdue"
  1857. (length *org-drill-overdue-entries*))
  1858. 'face `(:foreground ,org-drill-failed-count-color))
  1859. (propertize
  1860. (format "%d new"
  1861. (length *org-drill-new-entries*))
  1862. 'face `(:foreground ,org-drill-new-count-color))
  1863. (propertize
  1864. (format "%d young"
  1865. (length *org-drill-young-mature-entries*))
  1866. 'face `(:foreground ,org-drill-mature-count-color))
  1867. (propertize
  1868. (format "%d old"
  1869. (length *org-drill-old-mature-entries*))
  1870. 'face `(:foreground ,org-drill-mature-count-color))
  1871. *org-drill-due-tomorrow-count*
  1872. ))
  1873. (while (not (input-pending-p))
  1874. (message "%s" prompt)
  1875. (sit-for 0.5))
  1876. (read-char-exclusive)
  1877. (if (and *org-drill-session-qualities*
  1878. (< pass-percent (- 100 org-drill-forgetting-index)))
  1879. (read-char-exclusive
  1880. (format
  1881. "%s
  1882. You failed %d%% of the items you reviewed during this session.
  1883. %d (%d%%) of all items scanned were overdue.
  1884. Are you keeping up with your items, and reviewing them
  1885. when they are scheduled? If so, you may want to consider
  1886. lowering the value of `org-drill-learn-fraction' slightly in
  1887. order to make items appear more frequently over time."
  1888. (propertize "WARNING!" 'face 'org-warning)
  1889. (- 100 pass-percent)
  1890. *org-drill-overdue-entry-count*
  1891. (round (* 100 *org-drill-overdue-entry-count*)
  1892. (+ *org-drill-dormant-entry-count*
  1893. *org-drill-due-entry-count*)))
  1894. ))))
  1895. (defun org-drill-free-markers (markers)
  1896. "MARKERS is a list of markers, all of which will be freed (set to
  1897. point nowhere). Alternatively, MARKERS can be 't', in which case
  1898. all the markers used by Org-Drill will be freed."
  1899. (dolist (m (if (eql t markers)
  1900. (append *org-drill-done-entries*
  1901. *org-drill-new-entries*
  1902. *org-drill-failed-entries*
  1903. *org-drill-again-entries*
  1904. *org-drill-overdue-entries*
  1905. *org-drill-young-mature-entries*
  1906. *org-drill-old-mature-entries*)
  1907. markers))
  1908. (free-marker m)))
  1909. (defun org-drill-order-overdue-entries (overdue-data)
  1910. (setq *org-drill-overdue-entries*
  1911. (mapcar 'car
  1912. (sort (shuffle-list overdue-data)
  1913. (lambda (a b) (> (cdr a) (cdr b)))))))
  1914. (defun org-drill-entry-status ()
  1915. "Returns a list (STATUS DUE) where DUE is the number of days overdue,
  1916. zero being due today, -1 being scheduled 1 day in the future. STATUS is
  1917. one of the following values:
  1918. - nil, if the item is not a drill entry, or has an empty body
  1919. - :unscheduled
  1920. - :future
  1921. - :new
  1922. - :failed
  1923. - :overdue
  1924. - :young
  1925. - :old
  1926. "
  1927. (save-excursion
  1928. (unless (org-at-heading-p)
  1929. (org-back-to-heading))
  1930. (let ((due (org-drill-entry-days-overdue))
  1931. (last-int (org-drill-entry-last-interval 1)))
  1932. (list
  1933. (cond
  1934. ((not (org-drill-entry-p))
  1935. nil)
  1936. ((org-drill-entry-empty-p)
  1937. nil) ; skip -- item body is empty
  1938. ((null due) ; unscheduled - usually a skipped leech
  1939. :unscheduled)
  1940. ;; ((eql -1 due)
  1941. ;; :tomorrow)
  1942. ((minusp due) ; scheduled in the future
  1943. :future)
  1944. ;; The rest of the stati all denote 'due' items ==========================
  1945. ((<= (org-drill-entry-last-quality 9999)
  1946. org-drill-failure-quality)
  1947. ;; Mature entries that were failed last time are
  1948. ;; FAILED, regardless of how young, old or overdue
  1949. ;; they are.
  1950. :failed)
  1951. ((org-drill-entry-new-p)
  1952. :new)
  1953. ((org-drill-entry-overdue-p due last-int)
  1954. ;; Overdue status overrides young versus old
  1955. ;; distinction.
  1956. ;; Store marker + due, for sorting of overdue entries
  1957. :overdue)
  1958. ((<= (org-drill-entry-last-interval 9999)
  1959. org-drill-days-before-old)
  1960. :young)
  1961. (t
  1962. :old))
  1963. due))))
  1964. (defun org-drill-progress-message (collected scanned)
  1965. (when (zerop (% scanned 50))
  1966. (let* ((meter-width 40)
  1967. (sym1 (if (oddp (floor scanned (* 50 meter-width))) ?| ?.))
  1968. (sym2 (if (eql sym1 ?.) ?| ?.)))
  1969. (message "Collecting due drill items:%4d %s%s"
  1970. collected
  1971. (make-string (% (ceiling scanned 50) meter-width)
  1972. sym2)
  1973. (make-string (- meter-width (% (ceiling scanned 50) meter-width))
  1974. sym1)))))
  1975. (defun org-drill (&optional scope resume-p)
  1976. "Begin an interactive 'drill session'. The user is asked to
  1977. review a series of topics (headers). Each topic is initially
  1978. presented as a 'question', often with part of the topic content
  1979. hidden. The user attempts to recall the hidden information or
  1980. answer the question, then presses a key to reveal the answer. The
  1981. user then rates his or her recall or performance on that
  1982. topic. This rating information is used to reschedule the topic
  1983. for future review.
  1984. Org-drill proceeds by:
  1985. - Finding all topics (headings) in SCOPE which have either been
  1986. used and rescheduled before, or which have a tag that matches
  1987. `org-drill-question-tag'.
  1988. - All matching topics which are either unscheduled, or are
  1989. scheduled for the current date or a date in the past, are
  1990. considered to be candidates for the drill session.
  1991. - If `org-drill-maximum-items-per-session' is set, a random
  1992. subset of these topics is presented. Otherwise, all of the
  1993. eligible topics will be presented.
  1994. SCOPE determines the scope in which to search for
  1995. questions. It accepts the same values as `org-drill-scope',
  1996. which see.
  1997. If RESUME-P is non-nil, resume a suspended drill session rather
  1998. than starting a new one."
  1999. (interactive)
  2000. (let ((end-pos nil)
  2001. (overdue-data nil)
  2002. (cnt 0))
  2003. (block org-drill
  2004. (unless resume-p
  2005. (org-drill-free-markers t)
  2006. (setq *org-drill-current-item* nil
  2007. *org-drill-done-entries* nil
  2008. *org-drill-dormant-entry-count* 0
  2009. *org-drill-due-entry-count* 0
  2010. *org-drill-due-tomorrow-count* 0
  2011. *org-drill-overdue-entry-count* 0
  2012. *org-drill-new-entries* nil
  2013. *org-drill-overdue-entries* nil
  2014. *org-drill-young-mature-entries* nil
  2015. *org-drill-old-mature-entries* nil
  2016. *org-drill-failed-entries* nil
  2017. *org-drill-again-entries* nil)
  2018. (setq *org-drill-session-qualities* nil)
  2019. (setq *org-drill-start-time* (float-time (current-time))))
  2020. (setq *random-state* (make-random-state t)) ; reseed RNG
  2021. (unwind-protect
  2022. (save-excursion
  2023. (unless resume-p
  2024. (let ((org-trust-scanner-tags t)
  2025. (warned-about-id-creation nil))
  2026. (org-map-drill-entries
  2027. (lambda ()
  2028. (org-drill-progress-message
  2029. (+ (length *org-drill-new-entries*)
  2030. (length *org-drill-overdue-entries*)
  2031. (length *org-drill-young-mature-entries*)
  2032. (length *org-drill-old-mature-entries*)
  2033. (length *org-drill-failed-entries*))
  2034. (incf cnt))
  2035. (cond
  2036. ((not (org-drill-entry-p))
  2037. nil) ; skip
  2038. (t
  2039. (when (and (not warned-about-id-creation)
  2040. (null (org-id-get)))
  2041. (message (concat "Creating unique IDs for items "
  2042. "(slow, but only happens once)"))
  2043. (sit-for 0.5)
  2044. (setq warned-about-id-creation t))
  2045. (org-id-get-create) ; ensure drill entry has unique ID
  2046. (destructuring-bind (status due) (org-drill-entry-status)
  2047. (case status
  2048. (:unscheduled
  2049. (incf *org-drill-dormant-entry-count*))
  2050. ;; (:tomorrow
  2051. ;; (incf *org-drill-dormant-entry-count*)
  2052. ;; (incf *org-drill-due-tomorrow-count*))
  2053. (:future
  2054. (incf *org-drill-dormant-entry-count*)
  2055. (if (eq -1 due)
  2056. (incf *org-drill-due-tomorrow-count*)))
  2057. (:new
  2058. (push (point-marker) *org-drill-new-entries*))
  2059. (:failed
  2060. (push (point-marker) *org-drill-failed-entries*))
  2061. (:young
  2062. (push (point-marker) *org-drill-young-mature-entries*))
  2063. (:overdue
  2064. (push (cons (point-marker) due) overdue-data))
  2065. (:old
  2066. (push (point-marker) *org-drill-old-mature-entries*)))))))
  2067. scope)
  2068. ;; (let ((due (org-drill-entry-days-overdue))
  2069. ;; (last-int (org-drill-entry-last-interval 1)))
  2070. ;; (cond
  2071. ;; ((org-drill-entry-empty-p)
  2072. ;; nil) ; skip -- item body is empty
  2073. ;; ((or (null due) ; unscheduled - usually a skipped leech
  2074. ;; (minusp due)) ; scheduled in the future
  2075. ;; (incf *org-drill-dormant-entry-count*)
  2076. ;; (if (eq -1 due)
  2077. ;; (incf *org-drill-due-tomorrow-count*)))
  2078. ;; ((org-drill-entry-new-p)
  2079. ;; (push (point-marker) *org-drill-new-entries*))
  2080. ;; ((<= (org-drill-entry-last-quality 9999)
  2081. ;; org-drill-failure-quality)
  2082. ;; ;; Mature entries that were failed last time are
  2083. ;; ;; FAILED, regardless of how young, old or overdue
  2084. ;; ;; they are.
  2085. ;; (push (point-marker) *org-drill-failed-entries*))
  2086. ;; ((org-drill-entry-overdue-p due last-int)
  2087. ;; ;; Overdue status overrides young versus old
  2088. ;; ;; distinction.
  2089. ;; ;; Store marker + due, for sorting of overdue entries
  2090. ;; (push (cons (point-marker) due) overdue-data))
  2091. ;; ((<= (org-drill-entry-last-interval 9999)
  2092. ;; org-drill-days-before-old)
  2093. ;; ;; Item is 'young'.
  2094. ;; (push (point-marker)
  2095. ;; *org-drill-young-mature-entries*))
  2096. ;; (t
  2097. ;; (push (point-marker)
  2098. ;; *org-drill-old-mature-entries*))))
  2099. ;; Order 'overdue' items so that the most overdue will tend to
  2100. ;; come up for review first, while keeping exact order random
  2101. (org-drill-order-overdue-entries overdue-data)
  2102. (setq *org-drill-overdue-entry-count*
  2103. (length *org-drill-overdue-entries*))))
  2104. (setq *org-drill-due-entry-count* (org-drill-pending-entry-count))
  2105. (cond
  2106. ((and (null *org-drill-new-entries*)
  2107. (null *org-drill-failed-entries*)
  2108. (null *org-drill-overdue-entries*)
  2109. (null *org-drill-young-mature-entries*)
  2110. (null *org-drill-old-mature-entries*))
  2111. (message "I did not find any pending drill items."))
  2112. (t
  2113. (org-drill-entries resume-p)
  2114. (message "Drill session finished!"))))
  2115. (progn
  2116. (unless end-pos
  2117. (org-drill-free-markers *org-drill-done-entries*)))))
  2118. (cond
  2119. (end-pos
  2120. (when (markerp end-pos)
  2121. (org-drill-goto-entry end-pos))
  2122. (let ((keystr (command-keybinding-to-string 'org-drill-resume)))
  2123. (message
  2124. "You can continue the drill session with the command `org-drill-resume'.%s"
  2125. (if keystr (format "\nYou can run this command by pressing %s." keystr)
  2126. ""))))
  2127. (t
  2128. (org-drill-final-report)
  2129. (if (eql 'sm5 org-drill-spaced-repetition-algorithm)
  2130. (org-drill-save-optimal-factor-matrix))
  2131. (if org-drill-save-buffers-after-drill-sessions-p
  2132. (save-some-buffers))
  2133. (message "Drill session finished!")
  2134. ))))
  2135. (defun org-drill-save-optimal-factor-matrix ()
  2136. (message "Saving optimal factor matrix...")
  2137. (customize-save-variable 'org-drill-optimal-factor-matrix
  2138. org-drill-optimal-factor-matrix))
  2139. (defun org-drill-cram (&optional scope)
  2140. "Run an interactive drill session in 'cram mode'. In cram mode,
  2141. all drill items are considered to be due for review, unless they
  2142. have been reviewed within the last `org-drill-cram-hours'
  2143. hours."
  2144. (interactive)
  2145. (let ((*org-drill-cram-mode* t))
  2146. (org-drill scope)))
  2147. (defun org-drill-tree ()
  2148. "Run an interactive drill session using drill items within the
  2149. subtree at point."
  2150. (interactive)
  2151. (org-drill 'tree))
  2152. (defun org-drill-directory ()
  2153. "Run an interactive drill session using drill items from all org
  2154. files in the same directory as the current file."
  2155. (interactive)
  2156. (org-drill 'directory))
  2157. (defun org-drill-again (&optional scope)
  2158. "Run a new drill session, but try to use leftover due items that
  2159. were not reviewed during the last session, rather than scanning for
  2160. unreviewed items. If there are no leftover items in memory, a full
  2161. scan will be performed."
  2162. (interactive)
  2163. (cond
  2164. ((plusp (org-drill-pending-entry-count))
  2165. (org-drill-free-markers *org-drill-done-entries*)
  2166. (if (markerp *org-drill-current-item*)
  2167. (free-marker *org-drill-current-item*))
  2168. (setq *org-drill-start-time* (float-time (current-time))
  2169. *org-drill-done-entries* nil
  2170. *org-drill-current-item* nil)
  2171. (org-drill scope t))
  2172. (t
  2173. (org-drill scope))))
  2174. (defun org-drill-resume ()
  2175. "Resume a suspended drill session. Sessions are suspended by
  2176. exiting them with the `edit' or `quit' options."
  2177. (interactive)
  2178. (cond
  2179. ((org-drill-entries-pending-p)
  2180. (org-drill nil t))
  2181. ((and (plusp (org-drill-pending-entry-count))
  2182. ;; Current drill session is finished, but there are still
  2183. ;; more items which need to be reviewed.
  2184. (y-or-n-p (format
  2185. "You have finished the drill session. However, %d items still
  2186. need reviewing. Start a new drill session? "
  2187. (org-drill-pending-entry-count))))
  2188. (org-drill-again))
  2189. (t
  2190. (message "You have finished the drill session."))))
  2191. (defun org-drill-strip-entry-data ()
  2192. (dolist (prop org-drill-scheduling-properties)
  2193. (org-delete-property prop))
  2194. (org-schedule t))
  2195. (defun org-drill-strip-all-data (&optional scope)
  2196. "Delete scheduling data from every drill entry in scope. This
  2197. function may be useful if you want to give your collection of
  2198. entries to someone else. Scope defaults to the current buffer,
  2199. and is specified by the argument SCOPE, which accepts the same
  2200. values as `org-drill-scope'."
  2201. (interactive)
  2202. (when (yes-or-no-p
  2203. "Delete scheduling data from ALL items in scope: are you sure?")
  2204. (cond
  2205. ((null scope)
  2206. ;; Scope is the current buffer. This means we can use
  2207. ;; `org-delete-property-globally', which is faster.
  2208. (dolist (prop org-drill-scheduling-properties)
  2209. (org-delete-property-globally prop))
  2210. (org-map-drill-entries (lambda () (org-schedule t)) scope))
  2211. (t
  2212. (org-map-drill-entries 'org-drill-strip-entry-data scope)))
  2213. (message "Done.")))
  2214. (defun org-drill-add-cloze-fontification ()
  2215. (when org-drill-use-visible-cloze-face-p
  2216. (font-lock-add-keywords 'org-mode
  2217. org-drill-cloze-keywords
  2218. nil)))
  2219. (add-hook 'org-mode-hook 'org-drill-add-cloze-fontification)
  2220. (org-drill-add-cloze-fontification)
  2221. ;;; Synching card collections =================================================
  2222. (defvar *org-drill-dest-id-table* (make-hash-table :test 'equal))
  2223. (defun org-drill-copy-entry-to-other-buffer (dest &optional path)
  2224. "Copy the subtree at point to the buffer DEST. The copy will receive
  2225. the tag 'imported'."
  2226. (block org-drill-copy-entry-to-other-buffer
  2227. (save-excursion
  2228. (let ((src (current-buffer))
  2229. (m nil))
  2230. (flet ((paste-tree-here (&optional level)
  2231. (org-paste-subtree level)
  2232. (org-drill-strip-entry-data)
  2233. (org-toggle-tag "imported" 'on)
  2234. (org-map-drill-entries
  2235. (lambda ()
  2236. (let ((id (org-id-get)))
  2237. (org-drill-strip-entry-data)
  2238. (unless (gethash id *org-drill-dest-id-table*)
  2239. (puthash id (point-marker)
  2240. *org-drill-dest-id-table*))))
  2241. 'tree)))
  2242. (unless path
  2243. (setq path (org-get-outline-path)))
  2244. (org-copy-subtree)
  2245. (org-pop-to-buffer-same-window dest)
  2246. (setq m
  2247. (condition-case nil
  2248. (org-find-olp path t)
  2249. (error ; path does not exist in DEST
  2250. (return-from org-drill-copy-entry-to-other-buffer
  2251. (cond
  2252. ((cdr path)
  2253. (org-drill-copy-entry-to-other-buffer
  2254. dest (butlast path)))
  2255. (t
  2256. ;; We've looked all the way up the path
  2257. ;; Default to appending to the end of DEST
  2258. (goto-char (point-max))
  2259. (newline)
  2260. (paste-tree-here)))))))
  2261. (goto-char m)
  2262. (outline-next-heading)
  2263. (newline)
  2264. (forward-line -1)
  2265. (paste-tree-here (1+ (or (org-current-level) 0)))
  2266. )))))
  2267. (defun org-drill-merge-buffers (src &optional dest ignore-new-items-p)
  2268. "SRC and DEST are two org mode buffers containing drill items.
  2269. For each drill item in DEST that shares an ID with an item in SRC,
  2270. overwrite scheduling data in DEST with data taken from the item in SRC.
  2271. This is intended for use when two people are sharing a set of drill items,
  2272. one person has made some updates to the item set, and the other person
  2273. wants to migrate to the updated set without losing their scheduling data.
  2274. By default, any drill items in SRC which do not exist in DEST are
  2275. copied into DEST. We attempt to place the copied item in the
  2276. equivalent location in DEST to its location in SRC, by matching
  2277. the heading hierarchy. However if IGNORE-NEW-ITEMS-P is non-nil,
  2278. we simply ignore any items that do not exist in DEST, and do not
  2279. copy them across."
  2280. (interactive "bImport scheduling info from which buffer?")
  2281. (unless dest
  2282. (setq dest (current-buffer)))
  2283. (setq src (get-buffer src)
  2284. dest (get-buffer dest))
  2285. (when (yes-or-no-p
  2286. (format
  2287. (concat "About to overwrite all scheduling data for drill items in `%s' "
  2288. "with information taken from matching items in `%s'. Proceed? ")
  2289. (buffer-name dest) (buffer-name src)))
  2290. ;; Compile list of all IDs in the destination buffer.
  2291. (clrhash *org-drill-dest-id-table*)
  2292. (with-current-buffer dest
  2293. (org-map-drill-entries
  2294. (lambda ()
  2295. (let ((this-id (org-id-get)))
  2296. (when this-id
  2297. (puthash this-id (point-marker) *org-drill-dest-id-table*))))
  2298. 'file))
  2299. ;; Look through all entries in source buffer.
  2300. (with-current-buffer src
  2301. (org-map-drill-entries
  2302. (lambda ()
  2303. (let ((id (org-id-get))
  2304. (last-quality nil) (last-reviewed nil)
  2305. (scheduled-time nil))
  2306. (cond
  2307. ((or (null id)
  2308. (not (org-drill-entry-p)))
  2309. nil)
  2310. ((gethash id *org-drill-dest-id-table*)
  2311. ;; This entry matches an entry in dest. Retrieve all its
  2312. ;; scheduling data, then go to the matching location in dest
  2313. ;; and write the data.
  2314. (let ((marker (gethash id *org-drill-dest-id-table*)))
  2315. (destructuring-bind (last-interval repetitions failures
  2316. total-repeats meanq ease)
  2317. (org-drill-get-item-data)
  2318. (setq last-reviewed (org-entry-get (point) "DRILL_LAST_REVIEWED")
  2319. last-quality (org-entry-get (point) "DRILL_LAST_QUALITY")
  2320. scheduled-time (org-get-scheduled-time (point)))
  2321. (save-excursion
  2322. ;; go to matching entry in destination buffer
  2323. (org-pop-to-buffer-same-window (marker-buffer marker))
  2324. (goto-char marker)
  2325. (org-drill-strip-entry-data)
  2326. (unless (zerop total-repeats)
  2327. (org-drill-store-item-data last-interval repetitions failures
  2328. total-repeats meanq ease)
  2329. (if last-quality
  2330. (org-set-property "LAST_QUALITY" last-quality)
  2331. (org-delete-property "LAST_QUALITY"))
  2332. (if last-reviewed
  2333. (org-set-property "LAST_REVIEWED" last-reviewed)
  2334. (org-delete-property "LAST_REVIEWED"))
  2335. (if scheduled-time
  2336. (org-schedule nil scheduled-time)))))
  2337. (remhash id *org-drill-dest-id-table*)
  2338. (free-marker marker)))
  2339. (t
  2340. ;; item in SRC has ID, but no matching ID in DEST.
  2341. ;; It must be a new item that does not exist in DEST.
  2342. ;; Copy the entire item to the *end* of DEST.
  2343. (unless ignore-new-items-p
  2344. (org-drill-copy-entry-to-other-buffer dest))))))
  2345. 'file))
  2346. ;; Finally: there may be some items in DEST which are not in SRC, and
  2347. ;; which have been scheduled by another user of DEST. Clear out the
  2348. ;; scheduling info from all the unmatched items in DEST.
  2349. (with-current-buffer dest
  2350. (maphash (lambda (id m)
  2351. (goto-char m)
  2352. (org-drill-strip-entry-data)
  2353. (free-marker m))
  2354. *org-drill-dest-id-table*))))
  2355. ;;; Card types for learning languages =========================================
  2356. ;;; Get spell-number.el from:
  2357. ;;; http://www.emacswiki.org/emacs/spell-number.el
  2358. (autoload 'spelln-integer-in-words "spell-number")
  2359. ;;; `conjugate' card type =====================================================
  2360. ;;; See spanish.org for usage
  2361. (defvar org-drill-verb-tense-alist
  2362. '(("present" "tomato")
  2363. ("simple present" "tomato")
  2364. ("present indicative" "tomato")
  2365. ;; past tenses
  2366. ("past" "purple")
  2367. ("simple past" "purple")
  2368. ("preterite" "purple")
  2369. ("imperfect" "darkturquoise")
  2370. ("present perfect" "royalblue")
  2371. ;; future tenses
  2372. ("future" "green"))
  2373. "Alist where each entry has the form (TENSE COLOUR), where
  2374. TENSE is a string naming a tense in which verbs can be
  2375. conjugated, and COLOUR is a string specifying a foreground colour
  2376. which will be used by `org-drill-present-verb-conjugation' and
  2377. `org-drill-show-answer-verb-conjugation' to fontify the verb and
  2378. the name of the tense.")
  2379. (defun org-drill-get-verb-conjugation-info ()
  2380. "Auxiliary function used by `org-drill-present-verb-conjugation' and
  2381. `org-drill-show-answer-verb-conjugation'."
  2382. (let ((infinitive (org-entry-get (point) "VERB_INFINITIVE" t))
  2383. (inf-hint (org-entry-get (point) "VERB_INFINITIVE_HINT" t))
  2384. (translation (org-entry-get (point) "VERB_TRANSLATION" t))
  2385. (tense (org-entry-get (point) "VERB_TENSE" nil))
  2386. (highlight-face nil))
  2387. (unless (and infinitive translation tense)
  2388. (error "Missing information for verb conjugation card (%s, %s, %s) at %s"
  2389. infinitive translation tense (point)))
  2390. (setq tense (downcase (car (read-from-string tense)))
  2391. infinitive (car (read-from-string infinitive))
  2392. inf-hint (if inf-hint (car (read-from-string inf-hint)))
  2393. translation (car (read-from-string translation)))
  2394. (setq highlight-face
  2395. (list :foreground
  2396. (or (second (assoc-string tense org-drill-verb-tense-alist t))
  2397. "red")))
  2398. (setq infinitive (propertize infinitive 'face highlight-face))
  2399. (setq translation (propertize translation 'face highlight-face))
  2400. (setq tense (propertize tense 'face highlight-face))
  2401. (list infinitive inf-hint translation tense)))
  2402. (defun org-drill-present-verb-conjugation ()
  2403. "Present a drill entry whose card type is 'conjugate'."
  2404. (destructuring-bind (infinitive inf-hint translation tense)
  2405. (org-drill-get-verb-conjugation-info)
  2406. (org-drill-present-card-using-text
  2407. (cond
  2408. ((zerop (random* 2))
  2409. (format "\nTranslate the verb\n\n%s\n\nand conjugate for the %s tense.\n\n"
  2410. infinitive tense))
  2411. (t
  2412. (format "\nGive the verb that means\n\n%s %s\n
  2413. and conjugate for the %s tense.\n\n"
  2414. translation
  2415. (if inf-hint (format " [HINT: %s]" inf-hint) "")
  2416. tense))))))
  2417. (defun org-drill-show-answer-verb-conjugation (reschedule-fn)
  2418. "Show the answer for a drill item whose card type is 'conjugate'.
  2419. RESCHEDULE-FN must be a function that calls `org-drill-reschedule' and
  2420. returns its return value."
  2421. (destructuring-bind (infinitive inf-hint translation tense)
  2422. (org-drill-get-verb-conjugation-info)
  2423. (with-replaced-entry-heading
  2424. (format "%s tense of %s ==> %s\n\n"
  2425. (capitalize tense)
  2426. infinitive translation)
  2427. (funcall reschedule-fn))))
  2428. ;;; `translate_number' card type ==============================================
  2429. ;;; See spanish.org for usage
  2430. (defvar *drilled-number* 0)
  2431. (defvar *drilled-number-direction* 'to-english)
  2432. (defun org-drill-present-translate-number ()
  2433. (let ((num-min (read (org-entry-get (point) "DRILL_NUMBER_MIN")))
  2434. (num-max (read (org-entry-get (point) "DRILL_NUMBER_MAX")))
  2435. (language (read (org-entry-get (point) "DRILL_LANGUAGE" t)))
  2436. (highlight-face 'font-lock-warning-face))
  2437. (cond
  2438. ((not (fboundp 'spelln-integer-in-words))
  2439. (message "`spell-number.el' not loaded, skipping 'translate_number' card...")
  2440. (sit-for 0.5)
  2441. 'skip)
  2442. ((not (and (numberp num-min) (numberp num-max) language))
  2443. (error "Missing language or minimum or maximum numbers for number card"))
  2444. (t
  2445. (if (> num-min num-max)
  2446. (psetf num-min num-max
  2447. num-max num-min))
  2448. (setq *drilled-number*
  2449. (+ num-min (random* (abs (1+ (- num-max num-min))))))
  2450. (setq *drilled-number-direction*
  2451. (if (zerop (random* 2)) 'from-english 'to-english))
  2452. (org-drill-present-card-using-text
  2453. (if (eql 'to-english *drilled-number-direction*)
  2454. (format "\nTranslate into English:\n\n%s\n"
  2455. (let ((spelln-language language))
  2456. (propertize
  2457. (spelln-integer-in-words *drilled-number*)
  2458. 'face highlight-face)))
  2459. (format "\nTranslate into %s:\n\n%s\n"
  2460. (capitalize (format "%s" language))
  2461. (let ((spelln-language 'english-gb))
  2462. (propertize
  2463. (spelln-integer-in-words *drilled-number*)
  2464. 'face highlight-face)))))))))
  2465. (defun org-drill-show-answer-translate-number (reschedule-fn)
  2466. (let* ((language (read (org-entry-get (point) "DRILL_LANGUAGE" t)))
  2467. (highlight-face 'font-lock-warning-face)
  2468. (non-english
  2469. (let ((spelln-language language))
  2470. (propertize (spelln-integer-in-words *drilled-number*)
  2471. 'face highlight-face)))
  2472. (english
  2473. (let ((spelln-language 'english-gb))
  2474. (propertize (spelln-integer-in-words *drilled-number*)
  2475. 'face 'highlight-face))))
  2476. (with-replaced-entry-text
  2477. (cond
  2478. ((eql 'to-english *drilled-number-direction*)
  2479. (format "\nThe English translation of %s is:\n\n%s\n"
  2480. non-english english))
  2481. (t
  2482. (format "\nThe %s translation of %s is:\n\n%s\n"
  2483. (capitalize (format "%s" language))
  2484. english non-english)))
  2485. (funcall reschedule-fn))))
  2486. ;;; `spanish_verb' card type ==================================================
  2487. ;;; Not very interesting, but included to demonstrate how a presentation
  2488. ;;; function can manipulate which subheading are hidden versus shown.
  2489. (defun org-drill-present-spanish-verb ()
  2490. (let ((prompt nil)
  2491. (reveal-headings nil))
  2492. (with-hidden-comments
  2493. (with-hidden-cloze-hints
  2494. (with-hidden-cloze-text
  2495. (case (random* 6)
  2496. (0
  2497. (org-drill-hide-all-subheadings-except '("Infinitive"))
  2498. (setq prompt
  2499. (concat "Translate this Spanish verb, and conjugate it "
  2500. "for the *present* tense.")
  2501. reveal-headings '("English" "Present Tense" "Notes")))
  2502. (1
  2503. (org-drill-hide-all-subheadings-except '("English"))
  2504. (setq prompt (concat "For the *present* tense, conjugate the "
  2505. "Spanish translation of this English verb.")
  2506. reveal-headings '("Infinitive" "Present Tense" "Notes")))
  2507. (2
  2508. (org-drill-hide-all-subheadings-except '("Infinitive"))
  2509. (setq prompt (concat "Translate this Spanish verb, and "
  2510. "conjugate it for the *past* tense.")
  2511. reveal-headings '("English" "Past Tense" "Notes")))
  2512. (3
  2513. (org-drill-hide-all-subheadings-except '("English"))
  2514. (setq prompt (concat "For the *past* tense, conjugate the "
  2515. "Spanish translation of this English verb.")
  2516. reveal-headings '("Infinitive" "Past Tense" "Notes")))
  2517. (4
  2518. (org-drill-hide-all-subheadings-except '("Infinitive"))
  2519. (setq prompt (concat "Translate this Spanish verb, and "
  2520. "conjugate it for the *future perfect* tense.")
  2521. reveal-headings '("English" "Future Perfect Tense" "Notes")))
  2522. (5
  2523. (org-drill-hide-all-subheadings-except '("English"))
  2524. (setq prompt (concat "For the *future perfect* tense, conjugate the "
  2525. "Spanish translation of this English verb.")
  2526. reveal-headings '("Infinitive" "Future Perfect Tense" "Notes"))))
  2527. (org-cycle-hide-drawers 'all)
  2528. (prog1 (org-drill-presentation-prompt)
  2529. (org-drill-hide-subheadings-if 'org-drill-entry-p)))))))
  2530. (provide 'org-drill)