org-drill.el 127 KB

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