org-drill.el 129 KB

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