org-drill.el 115 KB

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