org-drill.el 120 KB

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