org-drill.el 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480
  1. ;;; org-drill.el - Self-testing using spaced repetition
  2. ;;;
  3. ;;; Author: Paul Sexton <eeeickythump@gmail.com>
  4. ;;; Version: 2.3
  5. ;;; Repository at http://bitbucket.org/eeeickythump/org-drill/
  6. ;;;
  7. ;;;
  8. ;;; Synopsis
  9. ;;; ========
  10. ;;;
  11. ;;; Uses the SuperMemo spaced repetition algorithms to conduct interactive
  12. ;;; "drill sessions", where the material to be remembered is presented to the
  13. ;;; student in random order. The student rates his or her recall of each item,
  14. ;;; and this information is used to schedule the item for later revision.
  15. ;;;
  16. ;;; Each drill session can be restricted to topics in the current buffer
  17. ;;; (default), one or several files, all agenda files, or a subtree. A single
  18. ;;; topic can also be drilled.
  19. ;;;
  20. ;;; Different "card types" can be defined, which present their information to
  21. ;;; the student in different ways.
  22. ;;;
  23. ;;; See the file README.org for more detailed documentation.
  24. (eval-when-compile (require 'cl))
  25. (eval-when-compile (require 'hi-lock))
  26. (require 'org)
  27. (require 'org-id)
  28. (require 'org-learn)
  29. (defgroup org-drill nil
  30. "Options concerning interactive drill sessions in Org mode (org-drill)."
  31. :tag "Org-Drill"
  32. :group 'org-link)
  33. (defcustom org-drill-question-tag
  34. "drill"
  35. "Tag which topics must possess in order to be identified as review topics
  36. by `org-drill'."
  37. :group 'org-drill
  38. :type 'string)
  39. (defcustom org-drill-maximum-items-per-session
  40. 30
  41. "Each drill session will present at most this many topics for review.
  42. Nil means unlimited."
  43. :group 'org-drill
  44. :type '(choice integer (const nil)))
  45. (defcustom org-drill-maximum-duration
  46. 20
  47. "Maximum duration of a drill session, in minutes.
  48. Nil means unlimited."
  49. :group 'org-drill
  50. :type '(choice integer (const nil)))
  51. (defcustom org-drill-failure-quality
  52. 2
  53. "If the quality of recall for an item is this number or lower,
  54. it is regarded as an unambiguous failure, and the repetition
  55. interval for the card is reset to 0 days. If the quality is higher
  56. than this number, it is regarded as successfully recalled, but the
  57. time interval to the next repetition will be lowered if the quality
  58. was near to a fail.
  59. By default this is 2, for SuperMemo-like behaviour. For
  60. Mnemosyne-like behaviour, set it to 1. Other values are not
  61. really sensible."
  62. :group 'org-drill
  63. :type '(choice (const 2) (const 1)))
  64. (defcustom org-drill-forgetting-index
  65. 10
  66. "What percentage of items do you consider it is 'acceptable' to
  67. forget each drill session? The default is 10%. A warning message
  68. is displayed at the end of the session if the percentage forgotten
  69. climbs above this number."
  70. :group 'org-drill
  71. :type 'integer)
  72. (defcustom org-drill-leech-failure-threshold
  73. 15
  74. "If an item is forgotten more than this many times, it is tagged
  75. as a 'leech' item."
  76. :group 'org-drill
  77. :type '(choice integer (const nil)))
  78. (defcustom org-drill-leech-method
  79. 'skip
  80. "How should 'leech items' be handled during drill sessions?
  81. Possible values:
  82. - nil :: Leech items are treated the same as normal items.
  83. - skip :: Leech items are not included in drill sessions.
  84. - warn :: Leech items are still included in drill sessions,
  85. but a warning message is printed when each leech item is
  86. presented."
  87. :group 'org-drill
  88. :type '(choice (const 'warn) (const 'skip) (const nil)))
  89. (defface org-drill-visible-cloze-face
  90. '((t (:foreground "darkseagreen")))
  91. "The face used to hide the contents of cloze phrases."
  92. :group 'org-drill)
  93. (defface org-drill-visible-cloze-hint-face
  94. '((t (:foreground "dark slate blue")))
  95. "The face used to hide the contents of cloze phrases."
  96. :group 'org-drill)
  97. (defface org-drill-hidden-cloze-face
  98. '((t (:foreground "deep sky blue" :background "blue")))
  99. "The face used to hide the contents of cloze phrases."
  100. :group 'org-drill)
  101. (defcustom org-drill-use-visible-cloze-face-p
  102. nil
  103. "Use a special face to highlight cloze-deleted text in org mode
  104. buffers?"
  105. :group 'org-drill
  106. :type 'boolean)
  107. (defcustom org-drill-hide-item-headings-p
  108. nil
  109. "Conceal the contents of the main heading of each item during drill
  110. sessions? You may want to enable this behaviour if item headings or tags
  111. contain information that could 'give away' the answer."
  112. :group 'org-drill
  113. :type 'boolean)
  114. (defcustom org-drill-new-count-color
  115. "royal blue"
  116. "Foreground colour used to display the count of remaining new items
  117. during a drill session."
  118. :group 'org-drill
  119. :type 'color)
  120. (defcustom org-drill-mature-count-color
  121. "green"
  122. "Foreground colour used to display the count of remaining mature items
  123. during a drill session. Mature items are due for review, but are not new."
  124. :group 'org-drill
  125. :type 'color)
  126. (defcustom org-drill-failed-count-color
  127. "red"
  128. "Foreground colour used to display the count of remaining failed items
  129. during a drill session."
  130. :group 'org-drill
  131. :type 'color)
  132. (defcustom org-drill-done-count-color
  133. "sienna"
  134. "Foreground colour used to display the count of reviewed items
  135. during a drill session."
  136. :group 'org-drill
  137. :type 'color)
  138. (setplist 'org-drill-cloze-overlay-defaults
  139. '(display "[...]"
  140. face org-drill-hidden-cloze-face
  141. window t))
  142. (setplist 'org-drill-hidden-text-overlay
  143. '(invisible t))
  144. (setplist 'org-drill-replaced-text-overlay
  145. '(display "Replaced text"
  146. face default
  147. window t))
  148. (defvar org-drill-cloze-regexp
  149. ;; ver 1 "[^][]\\(\\[[^][][^]]*\\]\\)"
  150. ;; ver 2 "\\(\\[.*?\\]\\|^[^[[:cntrl:]]*?\\]\\|\\[.*?$\\)"
  151. ;; ver 3! "\\(\\[.*?\\]\\|\\[.*?[[:cntrl:]]+.*?\\]\\)"
  152. "\\(\\[[[:cntrl:][:graph:][:space:]]*?\\)\\(\\||.+?\\)\\(\\]\\)")
  153. (defvar org-drill-cloze-keywords
  154. `((,org-drill-cloze-regexp
  155. (1 'org-drill-visible-cloze-face nil)
  156. (2 'org-drill-visible-cloze-hint-face t)
  157. (3 'org-drill-visible-cloze-face nil)
  158. )))
  159. (defcustom org-drill-card-type-alist
  160. '((nil . org-drill-present-simple-card)
  161. ("simple" . org-drill-present-simple-card)
  162. ("twosided" . org-drill-present-two-sided-card)
  163. ("multisided" . org-drill-present-multi-sided-card)
  164. ("hide1cloze" . org-drill-present-multicloze-hide1)
  165. ("hide2cloze" . org-drill-present-multicloze-hide2)
  166. ("show1cloze" . org-drill-present-multicloze-show1)
  167. ("multicloze" . org-drill-present-multicloze-hide1)
  168. ("conjugate" org-drill-present-verb-conjugation
  169. org-drill-show-answer-verb-conjugation)
  170. ("spanish_verb" . org-drill-present-spanish-verb)
  171. ("translate_number" org-drill-present-translate-number
  172. org-drill-show-answer-translate-number))
  173. "Alist associating card types with presentation functions. Each entry in the
  174. alist takes one of two forms:
  175. 1. (CARDTYPE . QUESTION-FN), where CARDTYPE is a string or nil (for default),
  176. and QUESTION-FN is a function which takes no arguments and returns a boolean
  177. value.
  178. 2. (CARDTYPE QUESTION-FN ANSWER-FN), where ANSWER-FN is a function that takes
  179. one argument -- the argument is a function that itself takes no arguments.
  180. ANSWER-FN is called with the point on the active item's
  181. heading, just prior to displaying the item's 'answer'. It can therefore be
  182. used to modify the appearance of the answer. ANSWER-FN must call its argument
  183. before returning. (Its argument is a function that prompts the user and
  184. performs rescheduling)."
  185. :group 'org-drill
  186. :type '(alist :key-type (choice string (const nil)) :value-type function))
  187. (defcustom org-drill-spaced-repetition-algorithm
  188. 'sm5
  189. "Which SuperMemo spaced repetition algorithm to use for scheduling items.
  190. Available choices are:
  191. - SM2 :: the SM2 algorithm, used in SuperMemo 2.0
  192. - SM5 :: the SM5 algorithm, used in SuperMemo 5.0
  193. - Simple8 :: a modified version of the SM8 algorithm. SM8 is used in
  194. SuperMemo 98. The version implemented here is simplified in that while it
  195. 'learns' the difficulty of each item using quality grades and number of
  196. failures, it does not modify the matrix of values that
  197. governs how fast the inter-repetition intervals increase. A method for
  198. adjusting intervals when items are reviewed early or late has been taken
  199. from SM11, a later version of the algorithm, and included in Simple8."
  200. :group 'org-drill
  201. :type '(choice (const 'sm2) (const 'sm5) (const 'simple8)))
  202. (defcustom org-drill-optimal-factor-matrix
  203. nil
  204. "DO NOT CHANGE THE VALUE OF THIS VARIABLE.
  205. Persistent matrix of optimal factors, used by the SuperMemo SM5 algorithm.
  206. The matrix is saved (using the 'customize' facility) at the end of each
  207. drill session.
  208. Over time, values in the matrix will adapt to the individual user's
  209. pace of learning."
  210. :group 'org-drill
  211. :type 'sexp)
  212. (defcustom org-drill-add-random-noise-to-intervals-p
  213. nil
  214. "If true, the number of days until an item's next repetition
  215. will vary slightly from the interval calculated by the SM2
  216. algorithm. The variation is very small when the interval is
  217. small, but scales up with the interval."
  218. :group 'org-drill
  219. :type 'boolean)
  220. (defcustom org-drill-adjust-intervals-for-early-and-late-repetitions-p
  221. nil
  222. "If true, when the student successfully reviews an item 1 or more days
  223. before or after the scheduled review date, this will affect that date of
  224. the item's next scheduled review, according to the algorithm presented at
  225. [[http://www.supermemo.com/english/algsm11.htm#Advanced%20repetitions]].
  226. Items that were reviewed early will have their next review date brought
  227. forward. Those that were reviewed late will have their next review
  228. date postponed further.
  229. Note that this option currently has no effect if the SM2 algorithm
  230. is used."
  231. :group 'org-drill
  232. :type 'boolean)
  233. (defcustom org-drill-cram-hours
  234. 12
  235. "When in cram mode, items are considered due for review if
  236. they were reviewed at least this many hours ago."
  237. :group 'org-drill
  238. :type 'integer)
  239. ;;; NEW items have never been presented in a drill session before.
  240. ;;; MATURE items HAVE been presented at least once before.
  241. ;;; - YOUNG mature items were scheduled no more than
  242. ;;; ORG-DRILL-DAYS-BEFORE-OLD days after their last
  243. ;;; repetition. These items will have been learned 'recently' and will have a
  244. ;;; low repetition count.
  245. ;;; - OLD mature items have intervals greater than
  246. ;;; ORG-DRILL-DAYS-BEFORE-OLD.
  247. ;;; - OVERDUE items are past their scheduled review date by more than
  248. ;;; LAST-INTERVAL * (ORG-DRILL-OVERDUE-INTERVAL-FACTOR - 1) days,
  249. ;;; regardless of young/old status.
  250. (defcustom org-drill-days-before-old
  251. 10
  252. "When an item's inter-repetition interval rises above this value in days,
  253. it is no longer considered a 'young' (recently learned) item."
  254. :group 'org-drill
  255. :type 'integer)
  256. (defcustom org-drill-overdue-interval-factor
  257. 1.2
  258. "An item is considered overdue if its scheduled review date is
  259. more than (ORG-DRILL-OVERDUE-INTERVAL-FACTOR - 1) * LAST-INTERVAL
  260. days in the past. For example, a value of 1.2 means an additional
  261. 20% of the last scheduled interval is allowed to elapse before
  262. the item is overdue. A value of 1.0 means no extra time is
  263. allowed at all - items are immediately considered overdue if
  264. there is even one day's delay in reviewing them. This variable
  265. should never be less than 1.0."
  266. :group 'org-drill
  267. :type 'float)
  268. (defcustom org-drill-learn-fraction
  269. 0.5
  270. "Fraction between 0 and 1 that governs how quickly the spaces
  271. between successive repetitions increase, for all items. The
  272. default value is 0.5. Higher values make spaces increase more
  273. quickly with each successful repetition. You should only change
  274. this in small increments (for example 0.05-0.1) as it has an
  275. exponential effect on inter-repetition spacing."
  276. :group 'org-drill
  277. :type 'float)
  278. (defvar *org-drill-session-qualities* nil)
  279. (defvar *org-drill-start-time* 0)
  280. (defvar *org-drill-new-entries* nil)
  281. (defvar *org-drill-dormant-entry-count* 0)
  282. (defvar *org-drill-due-entry-count* 0)
  283. (defvar *org-drill-overdue-entry-count* 0)
  284. (defvar *org-drill-due-tomorrow-count* 0)
  285. (defvar *org-drill-current-entry-schedule-type* nil)
  286. (defvar *org-drill-overdue-entries* nil
  287. "List of markers for items that are considered 'overdue', based on
  288. the value of ORG-DRILL-OVERDUE-INTERVAL-FACTOR.")
  289. (defvar *org-drill-young-mature-entries* nil
  290. "List of markers for mature entries whose last inter-repetition
  291. interval was <= ORG-DRILL-DAYS-BEFORE-OLD days.")
  292. (defvar *org-drill-old-mature-entries* nil
  293. "List of markers for mature entries whose last inter-repetition
  294. interval was greater than ORG-DRILL-DAYS-BEFORE-OLD days.")
  295. (defvar *org-drill-failed-entries* nil)
  296. (defvar *org-drill-again-entries* nil)
  297. (defvar *org-drill-done-entries* nil)
  298. (defvar *org-drill-current-item* nil
  299. "Set to the marker for the item currently being tested.")
  300. (defvar *org-drill-cram-mode* nil
  301. "Are we in 'cram mode', where all items are considered due
  302. for review unless they were already reviewed in the recent past?")
  303. (defvar org-drill-scheduling-properties
  304. '("LEARN_DATA" "DRILL_LAST_INTERVAL" "DRILL_REPEATS_SINCE_FAIL"
  305. "DRILL_TOTAL_REPEATS" "DRILL_FAILURE_COUNT" "DRILL_AVERAGE_QUALITY"
  306. "DRILL_EASE" "DRILL_LAST_QUALITY" "DRILL_LAST_REVIEWED"))
  307. ;;; Make the above settings safe as file-local variables.
  308. (put 'org-drill-question-tag 'safe-local-variable 'stringp)
  309. (put 'org-drill-maximum-items-per-session 'safe-local-variable
  310. '(lambda (val) (or (integerp val) (null val))))
  311. (put 'org-drill-maximum-duration 'safe-local-variable
  312. '(lambda (val) (or (integerp val) (null val))))
  313. (put 'org-drill-failure-quality 'safe-local-variable 'integerp)
  314. (put 'org-drill-forgetting-index 'safe-local-variable 'integerp)
  315. (put 'org-drill-leech-failure-threshold 'safe-local-variable 'integerp)
  316. (put 'org-drill-leech-method 'safe-local-variable
  317. '(lambda (val) (memq val '(nil skip warn))))
  318. (put 'org-drill-use-visible-cloze-face-p 'safe-local-variable 'booleanp)
  319. (put 'org-drill-hide-item-headings-p 'safe-local-variable 'booleanp)
  320. (put 'org-drill-spaced-repetition-algorithm 'safe-local-variable
  321. '(lambda (val) (memq val '(simple8 sm5 sm2))))
  322. (put 'org-drill-add-random-noise-to-intervals-p 'safe-local-variable 'booleanp)
  323. (put 'org-drill-adjust-intervals-for-early-and-late-repetitions-p
  324. 'safe-local-variable 'booleanp)
  325. (put 'org-drill-cram-hours 'safe-local-variable 'integerp)
  326. (put 'org-drill-learn-fraction 'safe-local-variable 'floatp)
  327. (put 'org-drill-days-before-old 'safe-local-variable 'integerp)
  328. (put 'org-drill-overdue-interval-factor 'safe-local-variable 'floatp)
  329. ;;;; Utilities ================================================================
  330. (defun free-marker (m)
  331. (set-marker m nil))
  332. (defmacro pop-random (place)
  333. (let ((idx (gensym)))
  334. `(if (null ,place)
  335. nil
  336. (let ((,idx (random* (length ,place))))
  337. (prog1 (nth ,idx ,place)
  338. (setq ,place (append (subseq ,place 0 ,idx)
  339. (subseq ,place (1+ ,idx)))))))))
  340. (defmacro push-end (val place)
  341. "Add VAL to the end of the sequence stored in PLACE. Return the new
  342. value."
  343. `(setq ,place (append ,place (list ,val))))
  344. (defun shuffle-list (list)
  345. "Randomly permute the elements of LIST (all permutations equally likely)."
  346. ;; Adapted from 'shuffle-vector' in cookie1.el
  347. (let ((i 0)
  348. j
  349. temp
  350. (len (length list)))
  351. (while (< i len)
  352. (setq j (+ i (random* (- len i))))
  353. (setq temp (nth i list))
  354. (setf (nth i list) (nth j list))
  355. (setf (nth j list) temp)
  356. (setq i (1+ i))))
  357. list)
  358. (defun round-float (floatnum fix)
  359. "Round the floating point number FLOATNUM to FIX decimal places.
  360. Example: (round-float 3.56755765 3) -> 3.568"
  361. (let ((n (expt 10 fix)))
  362. (/ (float (round (* floatnum n))) n)))
  363. (defun time-to-inactive-org-timestamp (time)
  364. (format-time-string
  365. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  366. time))
  367. (defun org-map-drill-entries (func &optional scope &rest skip)
  368. "Like `org-map-entries', but only drill entries are processed."
  369. (apply 'org-map-entries func
  370. (concat "+" org-drill-question-tag) scope skip))
  371. (defmacro with-hidden-cloze-text (&rest body)
  372. `(progn
  373. (org-drill-hide-clozed-text)
  374. (unwind-protect
  375. (progn
  376. ,@body)
  377. (org-drill-unhide-clozed-text))))
  378. (defmacro with-hidden-cloze-hints (&rest body)
  379. `(progn
  380. (org-drill-hide-cloze-hints)
  381. (unwind-protect
  382. (progn
  383. ,@body)
  384. (org-drill-unhide-text))))
  385. (defmacro with-hidden-comments (&rest body)
  386. `(progn
  387. (if org-drill-hide-item-headings-p
  388. (org-drill-hide-heading-at-point))
  389. (org-drill-hide-comments)
  390. (unwind-protect
  391. (progn
  392. ,@body)
  393. (org-drill-unhide-text))))
  394. (defun org-drill-days-since-last-review ()
  395. "Nil means a last review date has not yet been stored for
  396. the item.
  397. Zero means it was reviewed today.
  398. A positive number means it was reviewed that many days ago.
  399. A negative number means the date of last review is in the future --
  400. this should never happen."
  401. (let ((datestr (org-entry-get (point) "DRILL_LAST_REVIEWED")))
  402. (when datestr
  403. (- (time-to-days (current-time))
  404. (time-to-days (apply 'encode-time
  405. (org-parse-time-string datestr)))))))
  406. (defun org-drill-hours-since-last-review ()
  407. "Like `org-drill-days-since-last-review', but return value is
  408. in hours rather than days."
  409. (let ((datestr (org-entry-get (point) "DRILL_LAST_REVIEWED")))
  410. (when datestr
  411. (floor
  412. (/ (- (time-to-seconds (current-time))
  413. (time-to-seconds (apply 'encode-time
  414. (org-parse-time-string datestr))))
  415. (* 60 60))))))
  416. (defun org-drill-entry-p (&optional marker)
  417. "Is MARKER, or the point, in a 'drill item'? This will return nil if
  418. the point is inside a subheading of a drill item -- to handle that
  419. situation use `org-part-of-drill-entry-p'."
  420. (save-excursion
  421. (when marker
  422. (org-drill-goto-entry marker))
  423. (member org-drill-question-tag (org-get-local-tags))))
  424. (defun org-drill-goto-entry (marker)
  425. (switch-to-buffer (marker-buffer marker))
  426. (goto-char marker))
  427. (defun org-part-of-drill-entry-p ()
  428. "Is the current entry either the main heading of a 'drill item',
  429. or a subheading within a drill item?"
  430. (or (org-drill-entry-p)
  431. ;; Does this heading INHERIT the drill tag
  432. (member org-drill-question-tag (org-get-tags-at))))
  433. (defun org-drill-goto-drill-entry-heading ()
  434. "Move the point to the heading which holds the :drill: tag for this
  435. drill entry."
  436. (unless (org-at-heading-p)
  437. (org-back-to-heading))
  438. (unless (org-part-of-drill-entry-p)
  439. (error "Point is not inside a drill entry"))
  440. (while (not (org-drill-entry-p))
  441. (unless (org-up-heading-safe)
  442. (error "Cannot find a parent heading that is marked as a drill entry"))))
  443. (defun org-drill-entry-leech-p ()
  444. "Is the current entry a 'leech item'?"
  445. (and (org-drill-entry-p)
  446. (member "leech" (org-get-local-tags))))
  447. ;; (defun org-drill-entry-due-p ()
  448. ;; (cond
  449. ;; (*org-drill-cram-mode*
  450. ;; (let ((hours (org-drill-hours-since-last-review)))
  451. ;; (and (org-drill-entry-p)
  452. ;; (or (null hours)
  453. ;; (>= hours org-drill-cram-hours)))))
  454. ;; (t
  455. ;; (let ((item-time (org-get-scheduled-time (point))))
  456. ;; (and (org-drill-entry-p)
  457. ;; (or (not (eql 'skip org-drill-leech-method))
  458. ;; (not (org-drill-entry-leech-p)))
  459. ;; (or (null item-time) ; not scheduled
  460. ;; (not (minusp ; scheduled for today/in past
  461. ;; (- (time-to-days (current-time))
  462. ;; (time-to-days item-time))))))))))
  463. (defun org-drill-entry-days-overdue ()
  464. "Returns:
  465. - NIL if the item is not to be regarded as scheduled for review at all.
  466. This is the case if it is not a drill item, or if it is a leech item
  467. that we wish to skip, or if we are in cram mode and have already reviewed
  468. the item within the last few hours.
  469. - 0 if the item is new, or if it scheduled for review today.
  470. - A negative integer - item is scheduled that many days in the future.
  471. - A positive integer - item is scheduled that many days in the past."
  472. (cond
  473. (*org-drill-cram-mode*
  474. (let ((hours (org-drill-hours-since-last-review)))
  475. (and (org-drill-entry-p)
  476. (or (null hours)
  477. (>= hours org-drill-cram-hours))
  478. 0)))
  479. (t
  480. (let ((item-time (org-get-scheduled-time (point))))
  481. (cond
  482. ((or (not (org-drill-entry-p))
  483. (and (eql 'skip org-drill-leech-method)
  484. (org-drill-entry-leech-p)))
  485. nil)
  486. ((null item-time) ; not scheduled -> due now
  487. 0)
  488. (t
  489. (- (time-to-days (current-time))
  490. (time-to-days item-time))))))))
  491. (defun org-drill-entry-overdue-p (&optional days-overdue last-interval)
  492. "Returns true if entry that is scheduled DAYS-OVERDUE dasy in the past,
  493. and whose last inter-repetition interval was LAST-INTERVAL, should be
  494. considered 'overdue'. If the arguments are not given they are extracted
  495. from the entry at point."
  496. (unless days-overdue
  497. (setq days-overdue (org-drill-entry-days-overdue)))
  498. (unless last-interval
  499. (setq last-interval (org-drill-entry-last-interval 1)))
  500. (and (numberp days-overdue)
  501. (> days-overdue 1) ; enforce a sane minimum 'overdue' gap
  502. ;;(> due org-drill-days-before-overdue)
  503. (> (/ (+ days-overdue last-interval 1.0) last-interval)
  504. org-drill-overdue-interval-factor)))
  505. (defun org-drill-entry-due-p ()
  506. (let ((due (org-drill-entry-days-overdue)))
  507. (and (not (null due))
  508. (not (minusp due)))))
  509. (defun org-drill-entry-new-p ()
  510. (and (org-drill-entry-p)
  511. (let ((item-time (org-get-scheduled-time (point))))
  512. (null item-time))))
  513. (defun org-drill-entry-last-quality (&optional default)
  514. (let ((quality (org-entry-get (point) "DRILL_LAST_QUALITY")))
  515. (if quality
  516. (string-to-number quality)
  517. default)))
  518. (defun org-drill-entry-failure-count ()
  519. (let ((quality (org-entry-get (point) "DRILL_FAILURE_COUNT")))
  520. (if quality
  521. (string-to-number quality)
  522. 0)))
  523. (defun org-drill-entry-average-quality (&optional default)
  524. (let ((val (org-entry-get (point) "DRILL_AVERAGE_QUALITY")))
  525. (if val
  526. (string-to-number val)
  527. (or default nil))))
  528. (defun org-drill-entry-last-interval (&optional default)
  529. (let ((val (org-entry-get (point) "DRILL_LAST_INTERVAL")))
  530. (if val
  531. (string-to-number val)
  532. (or default 0))))
  533. (defun org-drill-entry-repeats-since-fail (&optional default)
  534. (let ((val (org-entry-get (point) "DRILL_REPEATS_SINCE_FAIL")))
  535. (if val
  536. (string-to-number val)
  537. (or default 0))))
  538. (defun org-drill-entry-total-repeats (&optional default)
  539. (let ((val (org-entry-get (point) "DRILL_TOTAL_REPEATS")))
  540. (if val
  541. (string-to-number val)
  542. (or default 0))))
  543. (defun org-drill-entry-ease (&optional default)
  544. (let ((val (org-entry-get (point) "DRILL_EASE")))
  545. (if val
  546. (string-to-number val)
  547. default)))
  548. ;;; From http://www.supermemo.com/english/ol/sm5.htm
  549. (defun org-drill-random-dispersal-factor ()
  550. "Returns a random number between 0.5 and 1.5."
  551. (let ((a 0.047)
  552. (b 0.092)
  553. (p (- (random* 1.0) 0.5)))
  554. (flet ((sign (n)
  555. (cond ((zerop n) 0)
  556. ((plusp n) 1)
  557. (t -1))))
  558. (/ (+ 100 (* (* (/ -1 b) (log (- 1 (* (/ b a ) (abs p)))))
  559. (sign p)))
  560. 100.0))))
  561. (defun pseudonormal (mean variation)
  562. "Random numbers in a pseudo-normal distribution with mean MEAN, range
  563. MEAN-VARIATION to MEAN+VARIATION"
  564. (+ (random* variation)
  565. (random* variation)
  566. (- variation)
  567. mean))
  568. (defun org-drill-early-interval-factor (optimal-factor
  569. optimal-interval
  570. days-ahead)
  571. "Arguments:
  572. - OPTIMAL-FACTOR: interval-factor if the item had been tested
  573. exactly when it was supposed to be.
  574. - OPTIMAL-INTERVAL: interval for next repetition (days) if the item had been
  575. tested exactly when it was supposed to be.
  576. - DAYS-AHEAD: how many days ahead of time the item was reviewed.
  577. Returns an adjusted optimal factor which should be used to
  578. calculate the next interval, instead of the optimal factor found
  579. in the matrix."
  580. (let ((delta-ofmax (* (1- optimal-factor)
  581. (/ (+ optimal-interval
  582. (* 0.6 optimal-interval) -1) (1- optimal-interval)))))
  583. (- optimal-factor
  584. (* delta-ofmax (/ days-ahead (+ days-ahead (* 0.6 optimal-interval)))))))
  585. (defun org-drill-get-item-data ()
  586. "Returns a list of 6 items, containing all the stored recall
  587. data for the item at point:
  588. - LAST-INTERVAL is the interval in days that was used to schedule the item's
  589. current review date.
  590. - REPEATS is the number of items the item has been successfully recalled without
  591. without any failures. It is reset to 0 upon failure to recall the item.
  592. - FAILURES is the total number of times the user has failed to recall the item.
  593. - TOTAL-REPEATS includes both successful and unsuccessful repetitions.
  594. - AVERAGE-QUALITY is the mean quality of recall of the item over
  595. all its repetitions, successful and unsuccessful.
  596. - EASE is a number reflecting how easy the item is to learn. Higher is easier.
  597. "
  598. (let ((learn-str (org-entry-get (point) "LEARN_DATA"))
  599. (repeats (org-drill-entry-total-repeats :missing)))
  600. (cond
  601. (learn-str
  602. (let ((learn-data (or (and learn-str
  603. (read learn-str))
  604. (copy-list initial-repetition-state))))
  605. (list (nth 0 learn-data) ; last interval
  606. (nth 1 learn-data) ; repetitions
  607. (org-drill-entry-failure-count)
  608. (nth 1 learn-data)
  609. (org-drill-entry-last-quality)
  610. (nth 2 learn-data) ; EF
  611. )))
  612. ((not (eql :missing repeats))
  613. (list (org-drill-entry-last-interval)
  614. (org-drill-entry-repeats-since-fail)
  615. (org-drill-entry-failure-count)
  616. (org-drill-entry-total-repeats)
  617. (org-drill-entry-average-quality)
  618. (org-drill-entry-ease)))
  619. (t ; virgin item
  620. (list 0 0 0 0 nil nil)))))
  621. (defun org-drill-store-item-data (last-interval repeats failures
  622. total-repeats meanq
  623. ease)
  624. "Stores the given data in the item at point."
  625. (org-entry-delete (point) "LEARN_DATA")
  626. (org-set-property "DRILL_LAST_INTERVAL"
  627. (number-to-string (round-float last-interval 4)))
  628. (org-set-property "DRILL_REPEATS_SINCE_FAIL" (number-to-string repeats))
  629. (org-set-property "DRILL_TOTAL_REPEATS" (number-to-string total-repeats))
  630. (org-set-property "DRILL_FAILURE_COUNT" (number-to-string failures))
  631. (org-set-property "DRILL_AVERAGE_QUALITY"
  632. (number-to-string (round-float meanq 3)))
  633. (org-set-property "DRILL_EASE"
  634. (number-to-string (round-float ease 3))))
  635. ;;; SM2 Algorithm =============================================================
  636. (defun determine-next-interval-sm2 (last-interval n ef quality
  637. failures meanq total-repeats)
  638. "Arguments:
  639. - LAST-INTERVAL -- the number of days since the item was last reviewed.
  640. - REPEATS -- the number of times the item has been successfully reviewed
  641. - EF -- the 'easiness factor'
  642. - QUALITY -- 0 to 5
  643. Returns a list: (INTERVAL REPEATS EF FAILURES MEAN TOTAL-REPEATS OFMATRIX), where:
  644. - INTERVAL is the number of days until the item should next be reviewed
  645. - REPEATS is incremented by 1.
  646. - EF is modified based on the recall quality for the item.
  647. - OF-MATRIX is not modified."
  648. (assert (> n 0))
  649. (assert (and (>= quality 0) (<= quality 5)))
  650. (if (<= quality org-drill-failure-quality)
  651. ;; When an item is failed, its interval is reset to 0,
  652. ;; but its EF is unchanged
  653. (list -1 1 ef (1+ failures) meanq (1+ total-repeats)
  654. org-drill-optimal-factor-matrix)
  655. ;; else:
  656. (let* ((next-ef (modify-e-factor ef quality))
  657. (interval
  658. (cond
  659. ((<= n 1) 1)
  660. ((= n 2)
  661. (cond
  662. (org-drill-add-random-noise-to-intervals-p
  663. (case quality
  664. (5 6)
  665. (4 4)
  666. (3 3)
  667. (2 1)
  668. (t -1)))
  669. (t 6)))
  670. (t (* last-interval next-ef)))))
  671. (list (if org-drill-add-random-noise-to-intervals-p
  672. (+ last-interval (* (- interval last-interval)
  673. (org-drill-random-dispersal-factor)))
  674. interval)
  675. (1+ n)
  676. next-ef
  677. failures meanq (1+ total-repeats)
  678. org-drill-optimal-factor-matrix))))
  679. ;;; SM5 Algorithm =============================================================
  680. (defun inter-repetition-interval-sm5 (last-interval n ef &optional of-matrix)
  681. (let ((of (get-optimal-factor n ef (or of-matrix
  682. org-drill-optimal-factor-matrix))))
  683. (if (= 1 n)
  684. of
  685. (* of last-interval))))
  686. (defun determine-next-interval-sm5 (last-interval n ef quality
  687. failures meanq total-repeats
  688. of-matrix &optional delta-days)
  689. (if (zerop n) (setq n 1))
  690. (if (null ef) (setq ef 2.5))
  691. (assert (> n 0))
  692. (assert (and (>= quality 0) (<= quality 5)))
  693. (unless of-matrix
  694. (setq of-matrix org-drill-optimal-factor-matrix))
  695. (setq of-matrix (cl-copy-tree of-matrix))
  696. (setq meanq (if meanq
  697. (/ (+ quality (* meanq total-repeats 1.0))
  698. (1+ total-repeats))
  699. quality))
  700. (let ((next-ef (modify-e-factor ef quality))
  701. (old-ef ef)
  702. (new-of (modify-of (get-optimal-factor n ef of-matrix)
  703. quality org-drill-learn-fraction))
  704. (interval nil))
  705. (when (and org-drill-adjust-intervals-for-early-and-late-repetitions-p
  706. delta-days (minusp delta-days))
  707. (setq new-of (org-drill-early-interval-factor
  708. (get-optimal-factor n ef of-matrix)
  709. (inter-repetition-interval-sm5
  710. last-interval n ef of-matrix)
  711. delta-days)))
  712. (setq of-matrix
  713. (set-optimal-factor n next-ef of-matrix
  714. (round-float new-of 3))) ; round OF to 3 d.p.
  715. (setq ef next-ef)
  716. (cond
  717. ;; "Failed" -- reset repetitions to 0,
  718. ((<= quality org-drill-failure-quality)
  719. (list -1 1 old-ef (1+ failures) meanq (1+ total-repeats)
  720. of-matrix)) ; Not clear if OF matrix is supposed to be
  721. ; preserved
  722. ;; For a zero-based quality of 4 or 5, don't repeat
  723. ;; ((and (>= quality 4)
  724. ;; (not org-learn-always-reschedule))
  725. ;; (list 0 (1+ n) ef failures meanq
  726. ;; (1+ total-repeats) of-matrix)) ; 0 interval = unschedule
  727. (t
  728. (setq interval (inter-repetition-interval-sm5
  729. last-interval n ef of-matrix))
  730. (if org-drill-add-random-noise-to-intervals-p
  731. (setq interval (* interval (org-drill-random-dispersal-factor))))
  732. (list interval
  733. (1+ n)
  734. ef
  735. failures
  736. meanq
  737. (1+ total-repeats)
  738. of-matrix)))))
  739. ;;; Simple8 Algorithm =========================================================
  740. (defun org-drill-simple8-first-interval (failures)
  741. "Arguments:
  742. - FAILURES: integer >= 0. The total number of times the item has
  743. been forgotten, ever.
  744. Returns the optimal FIRST interval for an item which has previously been
  745. forgotten on FAILURES occasions."
  746. (* 2.4849 (exp (* -0.057 failures))))
  747. (defun org-drill-simple8-interval-factor (ease repetition)
  748. "Arguments:
  749. - EASE: floating point number >= 1.2. Corresponds to `AF' in SM8 algorithm.
  750. - REPETITION: the number of times the item has been tested.
  751. 1 is the first repetition (ie the second trial).
  752. Returns:
  753. The factor by which the last interval should be
  754. multiplied to give the next interval. Corresponds to `RF' or `OF'."
  755. (+ 1.2 (* (- ease 1.2) (expt org-drill-learn-fraction (log repetition 2)))))
  756. (defun org-drill-simple8-quality->ease (quality)
  757. "Returns the ease (`AF' in the SM8 algorithm) which corresponds
  758. to a mean item quality of QUALITY."
  759. (+ (* 0.0542 (expt quality 4))
  760. (* -0.4848 (expt quality 3))
  761. (* 1.4916 (expt quality 2))
  762. (* -1.2403 quality)
  763. 1.4515))
  764. (defun determine-next-interval-simple8 (last-interval repeats quality
  765. failures meanq totaln
  766. &optional delta-days)
  767. "Arguments:
  768. - LAST-INTERVAL -- the number of days since the item was last reviewed.
  769. - REPEATS -- the number of times the item has been successfully reviewed
  770. - EASE -- the 'easiness factor'
  771. - QUALITY -- 0 to 5
  772. - DELTA-DAYS -- how many days overdue was the item when it was reviewed.
  773. 0 = reviewed on the scheduled day. +N = N days overdue.
  774. -N = reviewed N days early.
  775. Returns the new item data, as a list of 6 values:
  776. - NEXT-INTERVAL
  777. - REPEATS
  778. - EASE
  779. - FAILURES
  780. - AVERAGE-QUALITY
  781. - TOTAL-REPEATS.
  782. See the documentation for `org-drill-get-item-data' for a description of these."
  783. (assert (>= repeats 0))
  784. (assert (and (>= quality 0) (<= quality 5)))
  785. (assert (or (null meanq) (and (>= meanq 0) (<= meanq 5))))
  786. (let ((next-interval nil))
  787. (setf meanq (if meanq
  788. (/ (+ quality (* meanq totaln 1.0)) (1+ totaln))
  789. quality))
  790. (cond
  791. ((<= quality org-drill-failure-quality)
  792. (incf failures)
  793. (setf repeats 0
  794. next-interval -1))
  795. ((or (zerop repeats)
  796. (zerop last-interval))
  797. (setf next-interval (org-drill-simple8-first-interval failures))
  798. (incf repeats)
  799. (incf totaln))
  800. (t
  801. (let* ((use-n
  802. (if (and
  803. org-drill-adjust-intervals-for-early-and-late-repetitions-p
  804. (numberp delta-days) (plusp delta-days)
  805. (plusp last-interval))
  806. (+ repeats (min 1 (/ delta-days last-interval 1.0)))
  807. repeats))
  808. (factor (org-drill-simple8-interval-factor
  809. (org-drill-simple8-quality->ease meanq) use-n))
  810. (next-int (* last-interval factor)))
  811. (when (and org-drill-adjust-intervals-for-early-and-late-repetitions-p
  812. (numberp delta-days) (minusp delta-days))
  813. ;; The item was reviewed earlier than scheduled.
  814. (setf factor (org-drill-early-interval-factor
  815. factor next-int (abs delta-days))
  816. next-int (* last-interval factor)))
  817. (setf next-interval next-int)
  818. (incf repeats)
  819. (incf totaln))))
  820. (list
  821. (if (and org-drill-add-random-noise-to-intervals-p
  822. (plusp next-interval))
  823. (* next-interval (org-drill-random-dispersal-factor))
  824. next-interval)
  825. repeats
  826. (org-drill-simple8-quality->ease meanq)
  827. failures
  828. meanq
  829. totaln
  830. )))
  831. ;;; Essentially copied from `org-learn.el', but modified to
  832. ;;; optionally call the SM2 or simple8 functions.
  833. (defun org-drill-smart-reschedule (quality &optional days-ahead)
  834. "If DAYS-AHEAD is supplied it must be a positive integer. The
  835. item will be scheduled exactly this many days into the future."
  836. (let ((delta-days (- (time-to-days (current-time))
  837. (time-to-days (or (org-get-scheduled-time (point))
  838. (current-time)))))
  839. (ofmatrix org-drill-optimal-factor-matrix)
  840. ;; Entries can have weights, 1 by default. Intervals are divided by the
  841. ;; item's weight, so an item with a weight of 2 will have all intervals
  842. ;; halved, meaning you will end up reviewing it twice as often.
  843. ;; Useful for entries which randomly present any of several facts.
  844. (weight (org-entry-get (point) "DRILL_CARD_WEIGHT")))
  845. (if (stringp weight)
  846. (setq weight (read weight)))
  847. (destructuring-bind (last-interval repetitions failures
  848. total-repeats meanq ease)
  849. (org-drill-get-item-data)
  850. (destructuring-bind (next-interval repetitions ease
  851. failures meanq total-repeats
  852. &optional new-ofmatrix)
  853. (case org-drill-spaced-repetition-algorithm
  854. (sm5 (determine-next-interval-sm5 last-interval repetitions
  855. ease quality failures
  856. meanq total-repeats ofmatrix))
  857. (sm2 (determine-next-interval-sm2 last-interval repetitions
  858. ease quality failures
  859. meanq total-repeats))
  860. (simple8 (determine-next-interval-simple8 last-interval repetitions
  861. quality failures meanq
  862. total-repeats
  863. delta-days)))
  864. (if (numberp days-ahead)
  865. (setq next-interval days-ahead))
  866. (org-drill-store-item-data next-interval repetitions failures
  867. total-repeats meanq ease)
  868. (if (and (null days-ahead)
  869. (numberp weight) (plusp weight)
  870. (not (minusp next-interval)))
  871. (setq next-interval (max 1.0 (/ next-interval weight))))
  872. (if (eql 'sm5 org-drill-spaced-repetition-algorithm)
  873. (setq org-drill-optimal-factor-matrix new-ofmatrix))
  874. (cond
  875. ((= 0 days-ahead)
  876. (org-schedule t))
  877. ((minusp days-ahead)
  878. (org-schedule nil (current-time)))
  879. (t
  880. (org-schedule nil (time-add (current-time)
  881. (days-to-time
  882. (round next-interval))))))))))
  883. (defun org-drill-hypothetical-next-review-date (quality)
  884. "Returns an integer representing the number of days into the future
  885. that the current item would be scheduled, based on a recall quality
  886. of QUALITY."
  887. (let ((weight (org-entry-get (point) "DRILL_CARD_WEIGHT")))
  888. (destructuring-bind (last-interval repetitions failures
  889. total-repeats meanq ease)
  890. (org-drill-get-item-data)
  891. (if (stringp weight)
  892. (setq weight (read weight)))
  893. (destructuring-bind (next-interval repetitions ease
  894. failures meanq total-repeats
  895. &optional ofmatrix)
  896. (case org-drill-spaced-repetition-algorithm
  897. (sm5 (determine-next-interval-sm5 last-interval repetitions
  898. ease quality failures
  899. meanq total-repeats
  900. org-drill-optimal-factor-matrix))
  901. (sm2 (determine-next-interval-sm2 last-interval repetitions
  902. ease quality failures
  903. meanq total-repeats))
  904. (simple8 (determine-next-interval-simple8 last-interval repetitions
  905. quality failures meanq
  906. total-repeats)))
  907. (cond
  908. ((not (plusp next-interval))
  909. 0)
  910. ((and (numberp weight) (plusp weight))
  911. (max 1.0 (/ next-interval weight)))
  912. (t
  913. next-interval))))))
  914. (defun org-drill-hypothetical-next-review-dates ()
  915. (let ((intervals nil))
  916. (dotimes (q 6)
  917. (push (max (or (car intervals) 0)
  918. (org-drill-hypothetical-next-review-date q))
  919. intervals))
  920. (reverse intervals)))
  921. (defun org-drill-reschedule ()
  922. "Returns quality rating (0-5), or nil if the user quit."
  923. (let ((ch nil)
  924. (input nil)
  925. (next-review-dates (org-drill-hypothetical-next-review-dates)))
  926. (save-excursion
  927. (while (not (memq ch '(?q ?e ?0 ?1 ?2 ?3 ?4 ?5)))
  928. (setq input (read-key-sequence
  929. (if (eq ch ??)
  930. (format "0-2 Means you have forgotten the item.
  931. 3-5 Means you have remembered the item.
  932. 0 - Completely forgot.
  933. 1 - Even after seeing the answer, it still took a bit to sink in.
  934. 2 - After seeing the answer, you remembered it.
  935. 3 - It took you awhile, but you finally remembered. (+%s days)
  936. 4 - After a little bit of thought you remembered. (+%s days)
  937. 5 - You remembered the item really easily. (+%s days)
  938. How well did you do? (0-5, ?=help, e=edit, t=tags, q=quit)"
  939. (round (nth 3 next-review-dates))
  940. (round (nth 4 next-review-dates))
  941. (round (nth 5 next-review-dates)))
  942. "How well did you do? (0-5, ?=help, e=edit, q=quit)")))
  943. (cond
  944. ((stringp input)
  945. (setq ch (elt input 0)))
  946. ((and (vectorp input) (symbolp (elt input 0)))
  947. (case (elt input 0)
  948. (up (ignore-errors (forward-line -1)))
  949. (down (ignore-errors (forward-line 1)))
  950. (left (ignore-errors (backward-char)))
  951. (right (ignore-errors (forward-char)))
  952. (prior (ignore-errors (scroll-down))) ; pgup
  953. (next (ignore-errors (scroll-up))))) ; pgdn
  954. ((and (vectorp input) (listp (elt input 0))
  955. (eventp (elt input 0)))
  956. (case (car (elt input 0))
  957. (wheel-up (ignore-errors (mwheel-scroll (elt input 0))))
  958. (wheel-down (ignore-errors (mwheel-scroll (elt input 0)))))))
  959. (if (eql ch ?t)
  960. (org-set-tags-command))))
  961. (cond
  962. ((and (>= ch ?0) (<= ch ?5))
  963. (let ((quality (- ch ?0))
  964. (failures (org-drill-entry-failure-count)))
  965. (save-excursion
  966. (org-drill-smart-reschedule quality
  967. (nth quality next-review-dates)))
  968. (push quality *org-drill-session-qualities*)
  969. (cond
  970. ((<= quality org-drill-failure-quality)
  971. (when org-drill-leech-failure-threshold
  972. ;;(setq failures (if failures (string-to-number failures) 0))
  973. ;; (org-set-property "DRILL_FAILURE_COUNT"
  974. ;; (format "%d" (1+ failures)))
  975. (if (> (1+ failures) org-drill-leech-failure-threshold)
  976. (org-toggle-tag "leech" 'on))))
  977. (t
  978. (let ((scheduled-time (org-get-scheduled-time (point))))
  979. (when scheduled-time
  980. (message "Next review in %d days"
  981. (- (time-to-days scheduled-time)
  982. (time-to-days (current-time))))
  983. (sit-for 0.5)))))
  984. (org-set-property "DRILL_LAST_QUALITY" (format "%d" quality))
  985. (org-set-property "DRILL_LAST_REVIEWED"
  986. (time-to-inactive-org-timestamp (current-time)))
  987. quality))
  988. ((= ch ?e)
  989. 'edit)
  990. (t
  991. nil))))
  992. ;; (defun org-drill-hide-all-subheadings-except (heading-list)
  993. ;; "Returns a list containing the position of each immediate subheading of
  994. ;; the current topic."
  995. ;; (let ((drill-entry-level (org-current-level))
  996. ;; (drill-sections nil)
  997. ;; (drill-heading nil))
  998. ;; (org-show-subtree)
  999. ;; (save-excursion
  1000. ;; (org-map-entries
  1001. ;; (lambda ()
  1002. ;; (when (and (not (outline-invisible-p))
  1003. ;; (> (org-current-level) drill-entry-level))
  1004. ;; (setq drill-heading (org-get-heading t))
  1005. ;; (unless (and (= (org-current-level) (1+ drill-entry-level))
  1006. ;; (member drill-heading heading-list))
  1007. ;; (hide-subtree))
  1008. ;; (push (point) drill-sections)))
  1009. ;; "" 'tree))
  1010. ;; (reverse drill-sections)))
  1011. (defun org-drill-hide-subheadings-if (test)
  1012. "TEST is a function taking no arguments. TEST will be called for each
  1013. of the immediate subheadings of the current drill item, with the point
  1014. on the relevant subheading. TEST should return nil if the subheading is
  1015. to be revealed, non-nil if it is to be hidden.
  1016. Returns a list containing the position of each immediate subheading of
  1017. the current topic."
  1018. (let ((drill-entry-level (org-current-level))
  1019. (drill-sections nil))
  1020. (org-show-subtree)
  1021. (save-excursion
  1022. (org-map-entries
  1023. (lambda ()
  1024. (when (and (not (outline-invisible-p))
  1025. (> (org-current-level) drill-entry-level))
  1026. (when (or (/= (org-current-level) (1+ drill-entry-level))
  1027. (funcall test))
  1028. (hide-subtree))
  1029. (push (point) drill-sections)))
  1030. "" 'tree))
  1031. (reverse drill-sections)))
  1032. (defun org-drill-hide-all-subheadings-except (heading-list)
  1033. (org-drill-hide-subheadings-if
  1034. (lambda () (let ((drill-heading (org-get-heading t)))
  1035. (not (member drill-heading heading-list))))))
  1036. (defun org-drill-presentation-prompt (&rest fmt-and-args)
  1037. (let* ((item-start-time (current-time))
  1038. (input nil)
  1039. (ch nil)
  1040. (last-second 0)
  1041. (mature-entry-count (+ (length *org-drill-young-mature-entries*)
  1042. (length *org-drill-old-mature-entries*)
  1043. (length *org-drill-overdue-entries*)))
  1044. (prompt
  1045. (if fmt-and-args
  1046. (apply 'format
  1047. (first fmt-and-args)
  1048. (rest fmt-and-args))
  1049. (concat "Press key for answer, "
  1050. "e=edit, t=tags, s=skip, q=quit."))))
  1051. (setq prompt
  1052. (format "%s %s %s %s %s %s"
  1053. (propertize
  1054. (char-to-string
  1055. (case *org-drill-current-entry-schedule-type*
  1056. (new ?N) (young ?Y) (old ?o) (overdue ?!) (failed ?F) (t ??)))
  1057. 'face `(:foreground
  1058. ,(case *org-drill-current-entry-schedule-type*
  1059. (new org-drill-new-count-color)
  1060. ((young old) org-drill-mature-count-color)
  1061. ((overdue failed) org-drill-failed-count-color)
  1062. (t org-drill-done-count-color))))
  1063. (propertize
  1064. (number-to-string (length *org-drill-done-entries*))
  1065. 'face `(:foreground ,org-drill-done-count-color)
  1066. 'help-echo "The number of items you have reviewed this session.")
  1067. (propertize
  1068. (number-to-string (+ (length *org-drill-again-entries*)
  1069. (length *org-drill-failed-entries*)))
  1070. 'face `(:foreground ,org-drill-failed-count-color)
  1071. 'help-echo (concat "The number of items that you failed, "
  1072. "and need to review again."))
  1073. (propertize
  1074. (number-to-string mature-entry-count)
  1075. 'face `(:foreground ,org-drill-mature-count-color)
  1076. 'help-echo "The number of old items due for review.")
  1077. (propertize
  1078. (number-to-string (length *org-drill-new-entries*))
  1079. 'face `(:foreground ,org-drill-new-count-color)
  1080. 'help-echo (concat "The number of new items that you "
  1081. "have never reviewed."))
  1082. prompt))
  1083. (if (and (eql 'warn org-drill-leech-method)
  1084. (org-drill-entry-leech-p))
  1085. (setq prompt (concat
  1086. (propertize "!!! LEECH ITEM !!!
  1087. You seem to be having a lot of trouble memorising this item.
  1088. Consider reformulating the item to make it easier to remember.\n"
  1089. 'face '(:foreground "red"))
  1090. prompt)))
  1091. (while (memq ch '(nil ?t))
  1092. (setq ch nil)
  1093. (while (not (input-pending-p))
  1094. (let ((elapsed (time-subtract (current-time) item-start-time)))
  1095. (message (concat (if (>= (time-to-seconds elapsed) (* 60 60))
  1096. "++:++ "
  1097. (format-time-string "%M:%S " elapsed))
  1098. prompt))
  1099. (sit-for 1)))
  1100. (setq input (read-key-sequence nil))
  1101. (if (stringp input) (setq ch (elt input 0)))
  1102. (if (eql ch ?t)
  1103. (org-set-tags-command)))
  1104. (case ch
  1105. (?q nil)
  1106. (?e 'edit)
  1107. (?s 'skip)
  1108. (otherwise t))))
  1109. (defun org-pos-in-regexp (pos regexp &optional nlines)
  1110. (save-excursion
  1111. (goto-char pos)
  1112. (org-in-regexp regexp nlines)))
  1113. (defun org-drill-hide-region (beg end &optional text)
  1114. "Hide the buffer region between BEG and END with an 'invisible text'
  1115. visual overlay, or with the string TEXT if it is supplied."
  1116. (let ((ovl (make-overlay beg end)))
  1117. (overlay-put ovl 'category
  1118. 'org-drill-hidden-text-overlay)
  1119. (when (stringp text)
  1120. (overlay-put ovl 'invisible nil)
  1121. (overlay-put ovl 'face 'default)
  1122. (overlay-put ovl 'display text))))
  1123. (defun org-drill-hide-heading-at-point (&optional text)
  1124. (unless (org-at-heading-p)
  1125. (error "Point is not on a heading."))
  1126. (save-excursion
  1127. (let ((beg (point)))
  1128. (end-of-line)
  1129. (org-drill-hide-region beg (point) text))))
  1130. (defun org-drill-hide-comments ()
  1131. (save-excursion
  1132. (while (re-search-forward "^#.*$" nil t)
  1133. (org-drill-hide-region (match-beginning 0) (match-end 0)))))
  1134. (defun org-drill-unhide-text ()
  1135. ;; This will also unhide the item's heading.
  1136. (save-excursion
  1137. (dolist (ovl (overlays-in (point-min) (point-max)))
  1138. (when (eql 'org-drill-hidden-text-overlay (overlay-get ovl 'category))
  1139. (delete-overlay ovl)))))
  1140. (defun org-drill-hide-clozed-text ()
  1141. (save-excursion
  1142. (while (re-search-forward org-drill-cloze-regexp nil t)
  1143. ;; Don't hide org links, partly because they might contain inline
  1144. ;; images which we want to keep visible
  1145. (unless (org-pos-in-regexp (match-beginning 0)
  1146. org-bracket-link-regexp 1)
  1147. (org-drill-hide-matched-cloze-text)))))
  1148. (defun org-drill-hide-matched-cloze-text ()
  1149. "Hide the current match with a 'cloze' visual overlay."
  1150. (let ((ovl (make-overlay (match-beginning 0) (match-end 0))))
  1151. (overlay-put ovl 'category
  1152. 'org-drill-cloze-overlay-defaults)
  1153. (when (find ?| (match-string 0))
  1154. (let ((hint (substring-no-properties
  1155. (match-string 0)
  1156. (1+ (position ?| (match-string 0)))
  1157. (1- (length (match-string 0))))))
  1158. (overlay-put
  1159. ovl 'display
  1160. ;; If hint is like `X...' then display [X...]
  1161. ;; otherwise display [...X]
  1162. (format (if (string-match-p "\\.\\.\\." hint) "[%s]" "[%s...]")
  1163. hint))))))
  1164. (defun org-drill-hide-cloze-hints ()
  1165. (save-excursion
  1166. (while (re-search-forward org-drill-cloze-regexp nil t)
  1167. (unless (or (org-pos-in-regexp (match-beginning 0)
  1168. org-bracket-link-regexp 1)
  1169. (null (match-beginning 2))) ; hint subexpression matched
  1170. (org-drill-hide-region (match-beginning 2) (match-end 2))))))
  1171. (defmacro with-replaced-entry-text (text &rest body)
  1172. "During the execution of BODY, the entire text of the current entry is
  1173. concealed by an overlay that displays the string TEXT."
  1174. `(progn
  1175. (org-drill-replace-entry-text ,text)
  1176. (unwind-protect
  1177. (progn
  1178. ,@body)
  1179. (org-drill-unreplace-entry-text))))
  1180. (defun org-drill-replace-entry-text (text)
  1181. "Make an overlay that conceals the entire text of the item, not
  1182. including properties or the contents of subheadings. The overlay shows
  1183. the string TEXT.
  1184. Note: does not actually alter the item."
  1185. (let ((ovl (make-overlay (point-min)
  1186. (save-excursion
  1187. (outline-next-heading)
  1188. (point)))))
  1189. (overlay-put ovl 'category
  1190. 'org-drill-replaced-text-overlay)
  1191. (overlay-put ovl 'display text)))
  1192. (defun org-drill-unreplace-entry-text ()
  1193. (save-excursion
  1194. (dolist (ovl (overlays-in (point-min) (point-max)))
  1195. (when (eql 'org-drill-replaced-text-overlay (overlay-get ovl 'category))
  1196. (delete-overlay ovl)))))
  1197. (defmacro with-replaced-entry-heading (heading &rest body)
  1198. `(progn
  1199. (org-drill-replace-entry-heading ,heading)
  1200. (unwind-protect
  1201. (progn
  1202. ,@body)
  1203. (org-drill-unhide-text))))
  1204. (defun org-drill-replace-entry-heading (heading)
  1205. "Make an overlay that conceals the heading of the item. The overlay shows
  1206. the string TEXT.
  1207. Note: does not actually alter the item."
  1208. (org-drill-hide-heading-at-point heading))
  1209. (defun org-drill-unhide-clozed-text ()
  1210. (save-excursion
  1211. (dolist (ovl (overlays-in (point-min) (point-max)))
  1212. (when (eql 'org-drill-cloze-overlay-defaults (overlay-get ovl 'category))
  1213. (delete-overlay ovl)))))
  1214. (defun org-drill-get-entry-text (&optional keep-properties-p)
  1215. (let ((text (org-agenda-get-some-entry-text (point-marker) 100)))
  1216. (if keep-properties-p
  1217. text
  1218. (substring-no-properties text))))
  1219. (defun org-drill-entry-empty-p ()
  1220. (zerop (length (org-drill-get-entry-text))))
  1221. ;;; Presentation functions ====================================================
  1222. ;; Each of these is called with point on topic heading. Each needs to show the
  1223. ;; topic in the form of a 'question' or with some information 'hidden', as
  1224. ;; appropriate for the card type. The user should then be prompted to press a
  1225. ;; key. The function should then reveal either the 'answer' or the entire
  1226. ;; topic, and should return t if the user chose to see the answer and rate their
  1227. ;; recall, nil if they chose to quit.
  1228. (defun org-drill-present-simple-card ()
  1229. (with-hidden-comments
  1230. (with-hidden-cloze-hints
  1231. (with-hidden-cloze-text
  1232. (org-drill-hide-all-subheadings-except nil)
  1233. (org-display-inline-images t)
  1234. (org-cycle-hide-drawers 'all)
  1235. (prog1 (org-drill-presentation-prompt)
  1236. (org-drill-hide-subheadings-if 'org-drill-entry-p))))))
  1237. (defun org-drill-present-default-answer (reschedule-fn)
  1238. (org-drill-hide-subheadings-if 'org-drill-entry-p)
  1239. (org-drill-unhide-clozed-text)
  1240. (with-hidden-cloze-hints
  1241. (funcall reschedule-fn)))
  1242. (defun org-drill-present-two-sided-card ()
  1243. (with-hidden-comments
  1244. (with-hidden-cloze-hints
  1245. (with-hidden-cloze-text
  1246. (let ((drill-sections (org-drill-hide-all-subheadings-except nil)))
  1247. (when drill-sections
  1248. (save-excursion
  1249. (goto-char (nth (random* (min 2 (length drill-sections)))
  1250. drill-sections))
  1251. (org-show-subtree)))
  1252. (org-display-inline-images t)
  1253. (org-cycle-hide-drawers 'all)
  1254. (prog1 (org-drill-presentation-prompt)
  1255. (org-drill-hide-subheadings-if 'org-drill-entry-p)))))))
  1256. (defun org-drill-present-multi-sided-card ()
  1257. (with-hidden-comments
  1258. (with-hidden-cloze-hints
  1259. (with-hidden-cloze-text
  1260. (let ((drill-sections (org-drill-hide-all-subheadings-except nil)))
  1261. (when drill-sections
  1262. (save-excursion
  1263. (goto-char (nth (random* (length drill-sections)) drill-sections))
  1264. (org-show-subtree)))
  1265. (org-display-inline-images t)
  1266. (org-cycle-hide-drawers 'all)
  1267. (prog1 (org-drill-presentation-prompt)
  1268. (org-drill-hide-subheadings-if 'org-drill-entry-p)))))))
  1269. (defun org-drill-present-multicloze-hide-n (number-to-hide)
  1270. "Hides NUMBER-TO-HIDE pieces of text that are marked for cloze deletion,
  1271. chosen at random."
  1272. (with-hidden-comments
  1273. (with-hidden-cloze-hints
  1274. (let ((item-end nil)
  1275. (match-count 0)
  1276. (body-start (or (cdr (org-get-property-block))
  1277. (point))))
  1278. (org-drill-hide-all-subheadings-except nil)
  1279. (save-excursion
  1280. (outline-next-heading)
  1281. (setq item-end (point)))
  1282. (save-excursion
  1283. (goto-char body-start)
  1284. (while (re-search-forward org-drill-cloze-regexp item-end t)
  1285. (incf match-count)))
  1286. (when (plusp match-count)
  1287. (let ((match-nums (subseq (shuffle-list (loop for i from 1 to match-count
  1288. collect i))
  1289. 0 number-to-hide)))
  1290. (dolist (pos-to-hide match-nums)
  1291. (save-excursion
  1292. (goto-char body-start)
  1293. (re-search-forward org-drill-cloze-regexp
  1294. item-end t pos-to-hide)
  1295. (org-drill-hide-matched-cloze-text)))))
  1296. (org-display-inline-images t)
  1297. (org-cycle-hide-drawers 'all)
  1298. (prog1 (org-drill-presentation-prompt)
  1299. (org-drill-hide-subheadings-if 'org-drill-entry-p)
  1300. (org-drill-unhide-clozed-text))))))
  1301. (defun org-drill-present-multicloze-hide1 ()
  1302. "Hides one of the pieces of text that are marked for cloze deletion,
  1303. chosen at random."
  1304. (org-drill-present-multicloze-hide-n 1))
  1305. (defun org-drill-present-multicloze-hide2 ()
  1306. "Hides two of the pieces of text that are marked for cloze deletion,
  1307. chosen at random."
  1308. (org-drill-present-multicloze-hide-n 2))
  1309. ;; (defun org-drill-present-multicloze-hide1 ()
  1310. ;; "Hides one of the pieces of text that are marked for cloze deletion,
  1311. ;; chosen at random."
  1312. ;; (with-hidden-comments
  1313. ;; (let ((item-end nil)
  1314. ;; (match-count 0)
  1315. ;; (body-start (or (cdr (org-get-property-block))
  1316. ;; (point))))
  1317. ;; (org-drill-hide-all-subheadings-except nil)
  1318. ;; (save-excursion
  1319. ;; (outline-next-heading)
  1320. ;; (setq item-end (point)))
  1321. ;; (save-excursion
  1322. ;; (goto-char body-start)
  1323. ;; (while (re-search-forward org-drill-cloze-regexp item-end t)
  1324. ;; (incf match-count)))
  1325. ;; (when (plusp match-count)
  1326. ;; (save-excursion
  1327. ;; (goto-char body-start)
  1328. ;; (re-search-forward org-drill-cloze-regexp
  1329. ;; item-end t (1+ (random match-count)))
  1330. ;; (org-drill-hide-matched-cloze-text)))
  1331. ;; (org-display-inline-images t)
  1332. ;; (org-cycle-hide-drawers 'all)
  1333. ;; (prog1 (org-drill-presentation-prompt)
  1334. ;; (org-drill-hide-subheadings-if 'org-drill-entry-p)
  1335. ;; (org-drill-unhide-clozed-text)))))
  1336. (defun org-drill-present-multicloze-show1 ()
  1337. "Similar to `org-drill-present-multicloze-hide1', but hides all
  1338. the pieces of text that are marked for cloze deletion, except for one
  1339. piece which is chosen at random."
  1340. (with-hidden-comments
  1341. (with-hidden-cloze-hints
  1342. (let ((item-end nil)
  1343. (match-count 0)
  1344. (body-start (or (cdr (org-get-property-block))
  1345. (point))))
  1346. (org-drill-hide-all-subheadings-except nil)
  1347. (save-excursion
  1348. (outline-next-heading)
  1349. (setq item-end (point)))
  1350. (save-excursion
  1351. (goto-char body-start)
  1352. (while (re-search-forward org-drill-cloze-regexp item-end t)
  1353. (incf match-count)))
  1354. (when (plusp match-count)
  1355. (let ((match-to-hide (random* match-count)))
  1356. (save-excursion
  1357. (goto-char body-start)
  1358. (dotimes (n match-count)
  1359. (re-search-forward org-drill-cloze-regexp
  1360. item-end t)
  1361. (unless (= n match-to-hide)
  1362. (org-drill-hide-matched-cloze-text))))))
  1363. (org-display-inline-images t)
  1364. (org-cycle-hide-drawers 'all)
  1365. (prog1 (org-drill-presentation-prompt)
  1366. (org-drill-hide-subheadings-if 'org-drill-entry-p)
  1367. (org-drill-unhide-clozed-text))))))
  1368. (defun org-drill-present-card-using-text (question &optional answer)
  1369. "Present the string QUESTION as the only visible content of the card."
  1370. (with-hidden-comments
  1371. (with-replaced-entry-text
  1372. question
  1373. (org-drill-hide-all-subheadings-except nil)
  1374. (org-cycle-hide-drawers 'all)
  1375. (prog1 (org-drill-presentation-prompt)
  1376. (org-drill-hide-subheadings-if 'org-drill-entry-p)))))
  1377. ;;; The following macro is necessary because `org-save-outline-visibility'
  1378. ;;; currently discards the value returned by its body and returns a garbage
  1379. ;;; value instead. (as at org mode v 7.5)
  1380. (defmacro org-drill-save-visibility (&rest body)
  1381. "Store the current visibility state of the org buffer, and restore it
  1382. after executing BODY. Return the value of the last expression
  1383. in BODY."
  1384. (let ((retval (gensym)))
  1385. `(let ((,retval nil))
  1386. (org-save-outline-visibility t
  1387. (setq ,retval
  1388. (progn
  1389. ,@body)))
  1390. ,retval)))
  1391. (defun org-drill-entry ()
  1392. "Present the current topic for interactive review, as in `org-drill'.
  1393. Review will occur regardless of whether the topic is due for review or whether
  1394. it meets the definition of a 'review topic' used by `org-drill'.
  1395. Returns a quality rating from 0 to 5, or nil if the user quit, or the symbol
  1396. EDIT if the user chose to exit the drill and edit the current item. Choosing
  1397. the latter option leaves the drill session suspended; it can be resumed
  1398. later using `org-drill-resume'.
  1399. See `org-drill' for more details."
  1400. (interactive)
  1401. (org-drill-goto-drill-entry-heading)
  1402. ;;(unless (org-part-of-drill-entry-p)
  1403. ;; (error "Point is not inside a drill entry"))
  1404. ;;(unless (org-at-heading-p)
  1405. ;; (org-back-to-heading))
  1406. (let ((card-type (org-entry-get (point) "DRILL_CARD_TYPE"))
  1407. (answer-fn 'org-drill-present-default-answer)
  1408. (cont nil))
  1409. (org-drill-save-visibility
  1410. (save-restriction
  1411. (org-narrow-to-subtree)
  1412. (org-show-subtree)
  1413. (org-cycle-hide-drawers 'all)
  1414. (let ((presentation-fn (cdr (assoc card-type org-drill-card-type-alist))))
  1415. (if (listp presentation-fn)
  1416. (psetq answer-fn (or (second presentation-fn)
  1417. 'org-drill-present-default-answer)
  1418. presentation-fn (first presentation-fn)))
  1419. (cond
  1420. (presentation-fn
  1421. (setq cont (funcall presentation-fn)))
  1422. (t
  1423. (error "Unknown card type: '%s'" card-type))))
  1424. (cond
  1425. ((not cont)
  1426. (message "Quit")
  1427. nil)
  1428. ((eql cont 'edit)
  1429. 'edit)
  1430. ((eql cont 'skip)
  1431. 'skip)
  1432. (t
  1433. (save-excursion
  1434. (funcall answer-fn
  1435. (lambda () (org-drill-reschedule))))))))))
  1436. (defun org-drill-entries-pending-p ()
  1437. (or *org-drill-again-entries*
  1438. (and (not (org-drill-maximum-item-count-reached-p))
  1439. (not (org-drill-maximum-duration-reached-p))
  1440. (or *org-drill-new-entries*
  1441. *org-drill-failed-entries*
  1442. *org-drill-young-mature-entries*
  1443. *org-drill-old-mature-entries*
  1444. *org-drill-overdue-entries*
  1445. *org-drill-again-entries*))))
  1446. (defun org-drill-pending-entry-count ()
  1447. (+ (length *org-drill-new-entries*)
  1448. (length *org-drill-failed-entries*)
  1449. (length *org-drill-young-mature-entries*)
  1450. (length *org-drill-old-mature-entries*)
  1451. (length *org-drill-overdue-entries*)
  1452. (length *org-drill-again-entries*)))
  1453. (defun org-drill-maximum-duration-reached-p ()
  1454. "Returns true if the current drill session has continued past its
  1455. maximum duration."
  1456. (and org-drill-maximum-duration
  1457. *org-drill-start-time*
  1458. (> (- (float-time (current-time)) *org-drill-start-time*)
  1459. (* org-drill-maximum-duration 60))))
  1460. (defun org-drill-maximum-item-count-reached-p ()
  1461. "Returns true if the current drill session has reached the
  1462. maximum number of items."
  1463. (and org-drill-maximum-items-per-session
  1464. (>= (length *org-drill-done-entries*)
  1465. org-drill-maximum-items-per-session)))
  1466. (defun org-drill-pop-next-pending-entry ()
  1467. (block org-drill-pop-next-pending-entry
  1468. (let ((m nil))
  1469. (while (or (null m)
  1470. (not (org-drill-entry-p m)))
  1471. (setq
  1472. m
  1473. (cond
  1474. ;; First priority is items we failed in a prior session.
  1475. ((and *org-drill-failed-entries*
  1476. (not (org-drill-maximum-item-count-reached-p))
  1477. (not (org-drill-maximum-duration-reached-p)))
  1478. (setq *org-drill-current-entry-schedule-type* 'failed)
  1479. (pop-random *org-drill-failed-entries*))
  1480. ;; Next priority is overdue items.
  1481. ((and *org-drill-overdue-entries*
  1482. (not (org-drill-maximum-item-count-reached-p))
  1483. (not (org-drill-maximum-duration-reached-p)))
  1484. ;; We use `pop', not `pop-random', because we have already
  1485. ;; sorted overdue items into a random order which takes
  1486. ;; number of days overdue into account.
  1487. (setq *org-drill-current-entry-schedule-type* 'overdue)
  1488. (pop *org-drill-overdue-entries*))
  1489. ;; Next priority is 'young' items.
  1490. ((and *org-drill-young-mature-entries*
  1491. (not (org-drill-maximum-item-count-reached-p))
  1492. (not (org-drill-maximum-duration-reached-p)))
  1493. (setq *org-drill-current-entry-schedule-type* 'young)
  1494. (pop-random *org-drill-young-mature-entries*))
  1495. ;; Next priority is newly added items, and older entries.
  1496. ;; We pool these into a single group.
  1497. ((and (or *org-drill-new-entries*
  1498. *org-drill-old-mature-entries*)
  1499. (not (org-drill-maximum-item-count-reached-p))
  1500. (not (org-drill-maximum-duration-reached-p)))
  1501. (cond
  1502. ((< (random* (+ (length *org-drill-new-entries*)
  1503. (length *org-drill-old-mature-entries*)))
  1504. (length *org-drill-new-entries*))
  1505. (setq *org-drill-current-entry-schedule-type* 'new)
  1506. (pop-random *org-drill-new-entries*))
  1507. (t
  1508. (setq *org-drill-current-entry-schedule-type* 'old)
  1509. (pop-random *org-drill-old-mature-entries*))))
  1510. ;; After all the above are done, last priority is items
  1511. ;; that were failed earlier THIS SESSION.
  1512. (*org-drill-again-entries*
  1513. (setq *org-drill-current-entry-schedule-type* 'failed)
  1514. (pop *org-drill-again-entries*))
  1515. (t ; nothing left -- return nil
  1516. (return-from org-drill-pop-next-pending-entry nil)))))
  1517. m)))
  1518. (defun org-drill-entries (&optional resuming-p)
  1519. "Returns nil, t, or a list of markers representing entries that were
  1520. 'failed' and need to be presented again before the session ends.
  1521. RESUMING-P is true if we are resuming a suspended drill session."
  1522. (block org-drill-entries
  1523. (while (org-drill-entries-pending-p)
  1524. (let ((m (cond
  1525. ((or (not resuming-p)
  1526. (null *org-drill-current-item*)
  1527. (not (org-drill-entry-p *org-drill-current-item*)))
  1528. (org-drill-pop-next-pending-entry))
  1529. (t ; resuming a suspended session.
  1530. (setq resuming-p nil)
  1531. *org-drill-current-item*))))
  1532. (setq *org-drill-current-item* m)
  1533. (unless m
  1534. (error "Unexpectedly ran out of pending drill items"))
  1535. (save-excursion
  1536. (org-drill-goto-entry m)
  1537. (setq result (org-drill-entry))
  1538. (cond
  1539. ((null result)
  1540. (message "Quit")
  1541. (setq end-pos :quit)
  1542. (return-from org-drill-entries nil))
  1543. ((eql result 'edit)
  1544. (setq end-pos (point-marker))
  1545. (return-from org-drill-entries nil))
  1546. ((eql result 'skip)
  1547. nil) ; skip this item
  1548. (t
  1549. (cond
  1550. ((<= result org-drill-failure-quality)
  1551. (if *org-drill-again-entries*
  1552. (setq *org-drill-again-entries*
  1553. (shuffle-list *org-drill-again-entries*)))
  1554. (push-end m *org-drill-again-entries*))
  1555. (t
  1556. (push m *org-drill-done-entries*))))))))))
  1557. (defun org-drill-final-report ()
  1558. (let ((pass-percent
  1559. (round (* 100 (count-if (lambda (qual)
  1560. (> qual org-drill-failure-quality))
  1561. *org-drill-session-qualities*))
  1562. (max 1 (length *org-drill-session-qualities*))))
  1563. (prompt nil))
  1564. (setq prompt
  1565. (format
  1566. "%d items reviewed. Session duration %s.
  1567. %d/%d items awaiting review (%s, %s, %s, %s, %s).
  1568. Recall of reviewed items:
  1569. Excellent (5): %3d%% | Near miss (2): %3d%%
  1570. Good (4): %3d%% | Failure (1): %3d%%
  1571. Hard (3): %3d%% | Abject failure (0): %3d%%
  1572. You successfully recalled %d%% of reviewed items (quality > %s)
  1573. Tomorrow, %d more items will become due for review.
  1574. Session finished. Press a key to continue..."
  1575. (length *org-drill-done-entries*)
  1576. (format-seconds "%h:%.2m:%.2s"
  1577. (- (float-time (current-time)) *org-drill-start-time*))
  1578. (org-drill-pending-entry-count)
  1579. (+ (org-drill-pending-entry-count)
  1580. *org-drill-dormant-entry-count*)
  1581. (propertize
  1582. (format "%d failed"
  1583. (+ (length *org-drill-failed-entries*)
  1584. (length *org-drill-again-entries*)))
  1585. 'face `(:foreground ,org-drill-failed-count-color))
  1586. (propertize
  1587. (format "%d overdue"
  1588. (length *org-drill-overdue-entries*))
  1589. 'face `(:foreground ,org-drill-failed-count-color))
  1590. (propertize
  1591. (format "%d new"
  1592. (length *org-drill-new-entries*))
  1593. 'face `(:foreground ,org-drill-new-count-color))
  1594. (propertize
  1595. (format "%d young"
  1596. (length *org-drill-young-mature-entries*))
  1597. 'face `(:foreground ,org-drill-mature-count-color))
  1598. (propertize
  1599. (format "%d old"
  1600. (length *org-drill-old-mature-entries*))
  1601. 'face `(:foreground ,org-drill-mature-count-color))
  1602. (round (* 100 (count 5 *org-drill-session-qualities*))
  1603. (max 1 (length *org-drill-session-qualities*)))
  1604. (round (* 100 (count 2 *org-drill-session-qualities*))
  1605. (max 1 (length *org-drill-session-qualities*)))
  1606. (round (* 100 (count 4 *org-drill-session-qualities*))
  1607. (max 1 (length *org-drill-session-qualities*)))
  1608. (round (* 100 (count 1 *org-drill-session-qualities*))
  1609. (max 1 (length *org-drill-session-qualities*)))
  1610. (round (* 100 (count 3 *org-drill-session-qualities*))
  1611. (max 1 (length *org-drill-session-qualities*)))
  1612. (round (* 100 (count 0 *org-drill-session-qualities*))
  1613. (max 1 (length *org-drill-session-qualities*)))
  1614. pass-percent
  1615. org-drill-failure-quality
  1616. *org-drill-due-tomorrow-count*
  1617. ))
  1618. (while (not (input-pending-p))
  1619. (message "%s" prompt)
  1620. (sit-for 0.5))
  1621. (read-char-exclusive)
  1622. (if (< pass-percent (- 100 org-drill-forgetting-index))
  1623. (read-char-exclusive
  1624. (format
  1625. "%s
  1626. You failed %d%% of the items you reviewed during this session.
  1627. %d (%d%%) of all items scanned were overdue.
  1628. Are you keeping up with your items, and reviewing them
  1629. when they are scheduled? If so, you may want to consider
  1630. lowering the value of `org-drill-learn-fraction' slightly in
  1631. order to make items appear more frequently over time."
  1632. (propertize "WARNING!" 'face 'org-warning)
  1633. (- 100 pass-percent)
  1634. *org-drill-overdue-entry-count*
  1635. (round (* 100 *org-drill-overdue-entry-count*)
  1636. (+ *org-drill-dormant-entry-count*
  1637. *org-drill-due-entry-count*)))
  1638. ))))
  1639. (defun org-drill-free-all-markers ()
  1640. (dolist (m (append *org-drill-done-entries*
  1641. *org-drill-new-entries*
  1642. *org-drill-failed-entries*
  1643. *org-drill-again-entries*
  1644. *org-drill-overdue-entries*
  1645. *org-drill-young-mature-entries*
  1646. *org-drill-old-mature-entries*))
  1647. (free-marker m)))
  1648. (defun org-drill-order-overdue-entries (overdue-data)
  1649. (setq *org-drill-overdue-entries*
  1650. (mapcar 'car
  1651. (sort (shuffle-list overdue-data)
  1652. (lambda (a b) (> (cdr a) (cdr b)))))))
  1653. (defun org-drill (&optional scope resume-p)
  1654. "Begin an interactive 'drill session'. The user is asked to
  1655. review a series of topics (headers). Each topic is initially
  1656. presented as a 'question', often with part of the topic content
  1657. hidden. The user attempts to recall the hidden information or
  1658. answer the question, then presses a key to reveal the answer. The
  1659. user then rates his or her recall or performance on that
  1660. topic. This rating information is used to reschedule the topic
  1661. for future review.
  1662. Org-drill proceeds by:
  1663. - Finding all topics (headings) in SCOPE which have either been
  1664. used and rescheduled before, or which have a tag that matches
  1665. `org-drill-question-tag'.
  1666. - All matching topics which are either unscheduled, or are
  1667. scheduled for the current date or a date in the past, are
  1668. considered to be candidates for the drill session.
  1669. - If `org-drill-maximum-items-per-session' is set, a random
  1670. subset of these topics is presented. Otherwise, all of the
  1671. eligible topics will be presented.
  1672. SCOPE determines the scope in which to search for
  1673. questions. It is passed to `org-map-entries', and can be any of:
  1674. nil The current buffer, respecting the restriction if any.
  1675. This is the default.
  1676. tree The subtree started with the entry at point
  1677. file The current buffer, without restriction
  1678. file-with-archives
  1679. The current buffer, and any archives associated with it
  1680. agenda All agenda files
  1681. agenda-with-archives
  1682. All agenda files with any archive files associated with them
  1683. (file1 file2 ...)
  1684. If this is a list, all files in the list will be scanned.
  1685. If RESUME-P is non-nil, resume a suspended drill session rather
  1686. than starting a new one."
  1687. (interactive)
  1688. (let ((end-pos nil)
  1689. (overdue-data nil)
  1690. (cnt 0))
  1691. (block org-drill
  1692. (unless resume-p
  1693. (org-drill-free-all-markers)
  1694. (setq *org-drill-current-item* nil
  1695. *org-drill-done-entries* nil
  1696. *org-drill-dormant-entry-count* 0
  1697. *org-drill-due-entry-count* 0
  1698. *org-drill-due-tomorrow-count* 0
  1699. *org-drill-overdue-entry-count* 0
  1700. *org-drill-new-entries* nil
  1701. *org-drill-overdue-entries* nil
  1702. *org-drill-young-mature-entries* nil
  1703. *org-drill-old-mature-entries* nil
  1704. *org-drill-failed-entries* nil
  1705. *org-drill-again-entries* nil)
  1706. (setq *org-drill-session-qualities* nil)
  1707. (setq *org-drill-start-time* (float-time (current-time))))
  1708. (setq *random-state* (make-random-state t)) ; reseed RNG
  1709. (unwind-protect
  1710. (save-excursion
  1711. (unless resume-p
  1712. (let ((org-trust-scanner-tags t)
  1713. (warned-about-id-creation nil))
  1714. (org-map-drill-entries
  1715. (lambda ()
  1716. (when (zerop (% (incf cnt) 50))
  1717. (message "Processing drill items: %4d%s"
  1718. (+ (length *org-drill-new-entries*)
  1719. (length *org-drill-overdue-entries*)
  1720. (length *org-drill-young-mature-entries*)
  1721. (length *org-drill-old-mature-entries*)
  1722. (length *org-drill-failed-entries*))
  1723. (make-string (ceiling cnt 50) ?.)))
  1724. (cond
  1725. ((not (org-drill-entry-p))
  1726. nil) ; skip
  1727. (t
  1728. (when (and (not warned-about-id-creation)
  1729. (null (org-id-get)))
  1730. (message (concat "Creating unique IDs for items "
  1731. "(slow, but only happens once)"))
  1732. (sit-for 0.5)
  1733. (setq warned-about-id-creation t))
  1734. (org-id-get-create) ; ensure drill entry has unique ID
  1735. (let ((due (org-drill-entry-days-overdue))
  1736. (last-int (org-drill-entry-last-interval 1)))
  1737. (cond
  1738. ((org-drill-entry-empty-p)
  1739. nil) ; skip -- item body is empty
  1740. ((or (null due) ; unscheduled - usually a skipped leech
  1741. (minusp due)) ; scheduled in the future
  1742. (incf *org-drill-dormant-entry-count*)
  1743. (if (eq -1 due)
  1744. (incf *org-drill-due-tomorrow-count*)))
  1745. ((org-drill-entry-new-p)
  1746. (push (point-marker) *org-drill-new-entries*))
  1747. ((<= (org-drill-entry-last-quality 9999)
  1748. org-drill-failure-quality)
  1749. ;; Mature entries that were failed last time are
  1750. ;; FAILED, regardless of how young, old or overdue
  1751. ;; they are.
  1752. (push (point-marker) *org-drill-failed-entries*))
  1753. ((org-drill-entry-overdue-p due last-int)
  1754. ;; Overdue status overrides young versus old
  1755. ;; distinction.
  1756. ;; Store marker + due, for sorting of overdue entries
  1757. (push (cons (point-marker) due) overdue-data))
  1758. ((<= (org-drill-entry-last-interval 9999)
  1759. org-drill-days-before-old)
  1760. ;; Item is 'young'.
  1761. (push (point-marker)
  1762. *org-drill-young-mature-entries*))
  1763. (t
  1764. (push (point-marker)
  1765. *org-drill-old-mature-entries*)))))))
  1766. scope)
  1767. ;; Order 'overdue' items so that the most overdue will tend to
  1768. ;; come up for review first, while keeping exact order random
  1769. (org-drill-order-overdue-entries overdue-data)
  1770. (setq *org-drill-overdue-entry-count*
  1771. (length *org-drill-overdue-entries*))))
  1772. (setq *org-drill-due-entry-count* (org-drill-pending-entry-count))
  1773. (cond
  1774. ((and (null *org-drill-new-entries*)
  1775. (null *org-drill-failed-entries*)
  1776. (null *org-drill-overdue-entries*)
  1777. (null *org-drill-young-mature-entries*)
  1778. (null *org-drill-old-mature-entries*))
  1779. (message "I did not find any pending drill items."))
  1780. (t
  1781. (org-drill-entries resume-p)
  1782. (message "Drill session finished!"))))
  1783. (progn
  1784. (unless end-pos
  1785. (org-drill-free-all-markers)))))
  1786. (cond
  1787. (end-pos
  1788. (when (markerp end-pos)
  1789. (org-drill-goto-entry end-pos))
  1790. (message
  1791. "You can continue the drill session with `M-x org-drill-resume'."))
  1792. (t
  1793. (org-drill-final-report)
  1794. (if (eql 'sm5 org-drill-spaced-repetition-algorithm)
  1795. (org-drill-save-optimal-factor-matrix))
  1796. ))))
  1797. (defun org-drill-save-optimal-factor-matrix ()
  1798. (message "Saving optimal factor matrix...")
  1799. (customize-save-variable 'org-drill-optimal-factor-matrix
  1800. org-drill-optimal-factor-matrix))
  1801. (defun org-drill-cram (&optional scope)
  1802. "Run an interactive drill session in 'cram mode'. In cram mode,
  1803. all drill items are considered to be due for review, unless they
  1804. have been reviewed within the last `org-drill-cram-hours'
  1805. hours."
  1806. (interactive)
  1807. (let ((*org-drill-cram-mode* t))
  1808. (org-drill scope)))
  1809. (defun org-drill-tree ()
  1810. "Run an interactive drill session using drill items within the
  1811. subtree at point."
  1812. (interactive)
  1813. (org-drill 'tree))
  1814. (defun org-drill-resume ()
  1815. "Resume a suspended drill session. Sessions are suspended by
  1816. exiting them with the `edit' option."
  1817. (interactive)
  1818. (org-drill nil t))
  1819. (defun org-drill-strip-entry-data ()
  1820. (dolist (prop org-drill-scheduling-properties)
  1821. (org-delete-property prop))
  1822. (org-schedule t))
  1823. (defun org-drill-strip-all-data (&optional scope)
  1824. "Delete scheduling data from every drill entry in scope. This
  1825. function may be useful if you want to give your collection of
  1826. entries to someone else. Scope defaults to the current buffer,
  1827. and is specified by the argument SCOPE, which accepts the same
  1828. values as `org-drill'."
  1829. (interactive)
  1830. (when (yes-or-no-p
  1831. "Delete scheduling data from ALL items in scope: are you sure?")
  1832. (cond
  1833. ((null scope)
  1834. ;; Scope is the current buffer. This means we can use
  1835. ;; `org-delete-property-globally', which is faster.
  1836. (dolist (prop org-drill-scheduling-properties)
  1837. (org-delete-property-globally prop))
  1838. (org-map-drill-entries (lambda () (org-schedule t)) scope))
  1839. (t
  1840. (org-map-drill-entries 'org-drill-strip-entry-data scope)))
  1841. (message "Done.")))
  1842. (add-hook 'org-mode-hook
  1843. (lambda ()
  1844. (when org-drill-use-visible-cloze-face-p
  1845. (font-lock-add-keywords 'org-mode
  1846. org-drill-cloze-keywords
  1847. nil))))
  1848. ;;; Synching card collections =================================================
  1849. (defvar *org-drill-dest-id-table* (make-hash-table :test 'equal))
  1850. (defun org-drill-copy-entry-to-other-buffer (dest &optional path)
  1851. "Copy the subtree at point to the buffer DEST. The copy will receive
  1852. the tag 'imported'."
  1853. (block org-drill-copy-entry-to-other-buffer
  1854. (save-excursion
  1855. (let ((src (current-buffer))
  1856. (m nil))
  1857. (flet ((paste-tree-here (&optional level)
  1858. (org-paste-subtree level)
  1859. (org-drill-strip-entry-data)
  1860. (org-toggle-tag "imported" 'on)
  1861. (org-map-drill-entries
  1862. (lambda ()
  1863. (let ((id (org-id-get)))
  1864. (org-drill-strip-entry-data)
  1865. (unless (gethash id *org-drill-dest-id-table*)
  1866. (puthash id (point-marker)
  1867. *org-drill-dest-id-table*))))
  1868. 'tree)))
  1869. (unless path
  1870. (setq path (org-get-outline-path)))
  1871. (org-copy-subtree)
  1872. (switch-to-buffer dest)
  1873. (setq m
  1874. (condition-case nil
  1875. (org-find-olp path t)
  1876. (error ; path does not exist in DEST
  1877. (return-from org-drill-copy-entry-to-other-buffer
  1878. (cond
  1879. ((cdr path)
  1880. (org-drill-copy-entry-to-other-buffer
  1881. dest (butlast path)))
  1882. (t
  1883. ;; We've looked all the way up the path
  1884. ;; Default to appending to the end of DEST
  1885. (goto-char (point-max))
  1886. (newline)
  1887. (paste-tree-here)))))))
  1888. (goto-char m)
  1889. (outline-next-heading)
  1890. (newline)
  1891. (forward-line -1)
  1892. (paste-tree-here (1+ (or (org-current-level) 0)))
  1893. )))))
  1894. (defun org-drill-merge-buffers (src &optional dest)
  1895. "SRC and DEST are two org mode buffers containing drill items.
  1896. For each drill item in DEST that shares an ID with an item in SRC,
  1897. overwrite scheduling data in DEST with data taken from the item in SRC.
  1898. This is intended for use when two people are sharing a set of drill items,
  1899. one person has made some updates to the item set, and the other person
  1900. wants to migrate to the updated set without losing their scheduling data."
  1901. ;; In future could look at what to do if we find an item in SRC whose ID
  1902. ;; is not present in DEST -- copy the whole item to DEST?
  1903. ;; org-copy-subtree --> org-paste-subtree
  1904. ;; could try to put it "near" the closest marker
  1905. (interactive "bImport scheduling info from which buffer?")
  1906. (unless dest
  1907. (setq dest (current-buffer)))
  1908. (setq src (get-buffer src)
  1909. dest (get-buffer dest))
  1910. (when (yes-or-no-p
  1911. (format
  1912. (concat "About to overwrite all scheduling data for drill items in `%s' "
  1913. "with information taken from matching items in `%s'. Proceed? ")
  1914. (buffer-name dest) (buffer-name src)))
  1915. ;; Compile list of all IDs in the destination buffer.
  1916. (clrhash *org-drill-dest-id-table*)
  1917. (with-current-buffer dest
  1918. (org-map-drill-entries
  1919. (lambda ()
  1920. (let ((this-id (org-id-get)))
  1921. (when this-id
  1922. (puthash this-id (point-marker) *org-drill-dest-id-table*))))))
  1923. ;; Look through all entries in source buffer.
  1924. (with-current-buffer src
  1925. (org-map-drill-entries
  1926. (lambda ()
  1927. (let ((id (org-id-get))
  1928. (last-quality nil) (last-reviewed nil)
  1929. (scheduled-time nil))
  1930. (cond
  1931. ((or (null id)
  1932. (not (org-drill-entry-p)))
  1933. nil)
  1934. ((gethash id *org-drill-dest-id-table*)
  1935. ;; This entry matches an entry in dest. Retrieve all its
  1936. ;; scheduling data, then go to the matching location in dest
  1937. ;; and write the data.
  1938. (let ((marker (gethash id *org-drill-dest-id-table*)))
  1939. (destructuring-bind (last-interval repetitions failures
  1940. total-repeats meanq ease)
  1941. (org-drill-get-item-data)
  1942. (setq last-reviewed (org-entry-get (point) "DRILL_LAST_REVIEWED")
  1943. last-quality (org-entry-get (point) "DRILL_LAST_QUALITY")
  1944. scheduled-time (org-get-scheduled-time (point)))
  1945. (save-excursion
  1946. ;; go to matching entry in destination buffer
  1947. (switch-to-buffer (marker-buffer marker))
  1948. (goto-char marker)
  1949. (org-drill-strip-entry-data)
  1950. (unless (zerop total-repeats)
  1951. (org-drill-store-item-data last-interval repetitions failures
  1952. total-repeats meanq ease)
  1953. (org-set-property "LAST_QUALITY" last-quality)
  1954. (org-set-property "LAST_REVIEWED" last-reviewed)
  1955. (if scheduled-time
  1956. (org-schedule nil scheduled-time)))))
  1957. (free-marker marker)))
  1958. (t
  1959. ;; item in SRC has ID, but no matching ID in DEST.
  1960. ;; It must be a new item that does not exist in DEST.
  1961. ;; Copy the entire item to the *end* of DEST.
  1962. (org-drill-copy-entry-to-other-buffer dest)))))))))
  1963. ;;; Card types for learning languages =========================================
  1964. ;;; Get spell-number.el from:
  1965. ;;; http://www.emacswiki.org/emacs/spell-number.el
  1966. (autoload 'spelln-integer-in-words "spell-number")
  1967. ;;; `conjugate' card type =====================================================
  1968. ;;; See spanish.org for usage
  1969. (defvar org-drill-verb-tense-alist
  1970. '(("present" "tomato")
  1971. ("simple present" "tomato")
  1972. ("present indicative" "tomato")
  1973. ;; past tenses
  1974. ("past" "purple")
  1975. ("simple past" "purple")
  1976. ("preterite" "purple")
  1977. ("imperfect" "darkturquoise")
  1978. ("present perfect" "royalblue")
  1979. ;; future tenses
  1980. ("future" "green"))
  1981. "Alist where each entry has the form (TENSE COLOUR), where
  1982. TENSE is a string naming a tense in which verbs can be
  1983. conjugated, and COLOUR is a string specifying a foreground colour
  1984. which will be used by `org-drill-present-verb-conjugation' and
  1985. `org-drill-show-answer-verb-conjugation' to fontify the verb and
  1986. the name of the tense.")
  1987. (defun org-drill-get-verb-conjugation-info ()
  1988. "Auxiliary function used by `org-drill-present-verb-conjugation' and
  1989. `org-drill-show-answer-verb-conjugation'."
  1990. (let ((infinitive (org-entry-get (point) "VERB_INFINITIVE" t))
  1991. (translation (org-entry-get (point) "VERB_TRANSLATION" t))
  1992. (tense (org-entry-get (point) "VERB_TENSE" nil))
  1993. (highlight-face nil))
  1994. (unless (and infinitive translation tense)
  1995. (error "Missing information for verb conjugation card (%s, %s, %s) at %s"
  1996. infinitive translation tense (point)))
  1997. (setq tense (downcase (car (read-from-string tense)))
  1998. infinitive (car (read-from-string infinitive))
  1999. translation (car (read-from-string translation)))
  2000. (setq highlight-face
  2001. (list :foreground
  2002. (or (second (assoc-string tense org-drill-verb-tense-alist t))
  2003. "red")))
  2004. (setq infinitive (propertize infinitive 'face highlight-face))
  2005. (setq translation (propertize translation 'face highlight-face))
  2006. (setq tense (propertize tense 'face highlight-face))
  2007. (list infinitive translation tense)))
  2008. (defun org-drill-present-verb-conjugation ()
  2009. "Present a drill entry whose card type is 'conjugate'."
  2010. (destructuring-bind (infinitive translation tense)
  2011. (org-drill-get-verb-conjugation-info)
  2012. (org-drill-present-card-using-text
  2013. (cond
  2014. ((zerop (random* 2))
  2015. (format "\nTranslate the verb\n\n%s\n\nand conjugate for the %s tense.\n\n"
  2016. infinitive tense))
  2017. (t
  2018. (format "\nGive the verb that means\n\n%s\n\nand conjugate for the %s tense.\n\n"
  2019. translation tense))))))
  2020. (defun org-drill-show-answer-verb-conjugation (reschedule-fn)
  2021. "Show the answer for a drill item whose card type is 'conjugate'.
  2022. RESCHEDULE-FN must be a function that calls `org-drill-reschedule' and
  2023. returns its return value."
  2024. (destructuring-bind (infinitive translation tense)
  2025. (org-drill-get-verb-conjugation-info)
  2026. (with-replaced-entry-heading
  2027. (format "%s tense of %s ==> %s\n\n"
  2028. (capitalize tense)
  2029. infinitive translation)
  2030. (funcall reschedule-fn))))
  2031. ;;; `translate_number' card type ==============================================
  2032. ;;; See spanish.org for usage
  2033. (defvar *drilled-number* 0)
  2034. (defvar *drilled-number-direction* 'to-english)
  2035. (defun org-drill-present-translate-number ()
  2036. (let ((num-min (read (org-entry-get (point) "DRILL_NUMBER_MIN")))
  2037. (num-max (read (org-entry-get (point) "DRILL_NUMBER_MAX")))
  2038. (language (read (org-entry-get (point) "DRILL_LANGUAGE" t)))
  2039. (highlight-face 'font-lock-warning-face))
  2040. (cond
  2041. ((not (fboundp 'spelln-integer-in-words))
  2042. (message "`spell-number.el' not loaded, skipping 'translate_number' card...")
  2043. (sit-for 0.5)
  2044. 'skip)
  2045. ((not (and (numberp num-min) (numberp num-max) language))
  2046. (error "Missing language or minimum or maximum numbers for number card"))
  2047. (t
  2048. (if (> num-min num-max)
  2049. (psetf num-min num-max
  2050. num-max num-min))
  2051. (setq *drilled-number*
  2052. (+ num-min (random* (abs (1+ (- num-max num-min))))))
  2053. (setq *drilled-number-direction*
  2054. (if (zerop (random* 2)) 'from-english 'to-english))
  2055. (org-drill-present-card-using-text
  2056. (if (eql 'to-english *drilled-number-direction*)
  2057. (format "\nTranslate into English:\n\n%s\n"
  2058. (let ((spelln-language language))
  2059. (propertize
  2060. (spelln-integer-in-words *drilled-number*)
  2061. 'face highlight-face)))
  2062. (format "\nTranslate into %s:\n\n%s\n"
  2063. (capitalize (format "%s" language))
  2064. (let ((spelln-language 'english-gb))
  2065. (propertize
  2066. (spelln-integer-in-words *drilled-number*)
  2067. 'face highlight-face)))))))))
  2068. (defun org-drill-show-answer-translate-number (reschedule-fn)
  2069. (let* ((language (read (org-entry-get (point) "DRILL_LANGUAGE" t)))
  2070. (highlight-face 'font-lock-warning-face)
  2071. (non-english
  2072. (let ((spelln-language language))
  2073. (propertize (spelln-integer-in-words *drilled-number*)
  2074. 'face highlight-face)))
  2075. (english
  2076. (let ((spelln-language 'english-gb))
  2077. (propertize (spelln-integer-in-words *drilled-number*)
  2078. 'face 'highlight-face))))
  2079. (with-replaced-entry-text
  2080. (cond
  2081. ((eql 'to-english *drilled-number-direction*)
  2082. (format "\nThe English translation of %s is:\n\n%s\n"
  2083. non-english english))
  2084. (t
  2085. (format "\nThe %s translation of %s is:\n\n%s\n"
  2086. (capitalize (format "%s" language))
  2087. english non-english)))
  2088. (funcall reschedule-fn))))
  2089. ;;; `spanish_verb' card type ==================================================
  2090. ;;; Not very interesting, but included to demonstrate how a presentation
  2091. ;;; function can manipulate which subheading are hidden versus shown.
  2092. (defun org-drill-present-spanish-verb ()
  2093. (let ((prompt nil)
  2094. (reveal-headings nil))
  2095. (with-hidden-comments
  2096. (with-hidden-cloze-hints
  2097. (with-hidden-cloze-text
  2098. (case (random* 6)
  2099. (0
  2100. (org-drill-hide-all-subheadings-except '("Infinitive"))
  2101. (setq prompt
  2102. (concat "Translate this Spanish verb, and conjugate it "
  2103. "for the *present* tense.")
  2104. reveal-headings '("English" "Present Tense" "Notes")))
  2105. (1
  2106. (org-drill-hide-all-subheadings-except '("English"))
  2107. (setq prompt (concat "For the *present* tense, conjugate the "
  2108. "Spanish translation of this English verb.")
  2109. reveal-headings '("Infinitive" "Present Tense" "Notes")))
  2110. (2
  2111. (org-drill-hide-all-subheadings-except '("Infinitive"))
  2112. (setq prompt (concat "Translate this Spanish verb, and "
  2113. "conjugate it for the *past* tense.")
  2114. reveal-headings '("English" "Past Tense" "Notes")))
  2115. (3
  2116. (org-drill-hide-all-subheadings-except '("English"))
  2117. (setq prompt (concat "For the *past* tense, conjugate the "
  2118. "Spanish translation of this English verb.")
  2119. reveal-headings '("Infinitive" "Past Tense" "Notes")))
  2120. (4
  2121. (org-drill-hide-all-subheadings-except '("Infinitive"))
  2122. (setq prompt (concat "Translate this Spanish verb, and "
  2123. "conjugate it for the *future perfect* tense.")
  2124. reveal-headings '("English" "Future Perfect Tense" "Notes")))
  2125. (5
  2126. (org-drill-hide-all-subheadings-except '("English"))
  2127. (setq prompt (concat "For the *future perfect* tense, conjugate the "
  2128. "Spanish translation of this English verb.")
  2129. reveal-headings '("Infinitive" "Future Perfect Tense" "Notes"))))
  2130. (org-cycle-hide-drawers 'all)
  2131. (prog1 (org-drill-presentation-prompt)
  2132. (org-drill-hide-subheadings-if 'org-drill-entry-p)))))))
  2133. (provide 'org-drill)