org-drill.el 130 KB

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