org-drill.el 120 KB

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