org-drill.el 120 KB

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