org-drill.el 112 KB

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