gnus-score.el 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089
  1. ;;; gnus-score.el --- scoring code for Gnus
  2. ;; Copyright (C) 1995-2015 Free Software Foundation, Inc.
  3. ;; Author: Per Abrahamsen <amanda@iesd.auc.dk>
  4. ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
  5. ;; Keywords: news
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (eval-when-compile (require 'cl))
  20. (require 'gnus)
  21. (require 'gnus-sum)
  22. (require 'gnus-range)
  23. (require 'gnus-win)
  24. (require 'message)
  25. (require 'score-mode)
  26. (require 'gmm-utils)
  27. (defcustom gnus-global-score-files nil
  28. "List of global score files and directories.
  29. Set this variable if you want to use people's score files. One entry
  30. for each score file or each score file directory. Gnus will decide
  31. by itself what score files are applicable to which group.
  32. Say you want to use the single score file
  33. \"/ftp.gnus.org@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
  34. score files in the \"/ftp.some-where:/pub/score\" directory.
  35. (setq gnus-global-score-files
  36. '(\"/ftp.gnus.org:/pub/larsi/ding/score/soc.motss.SCORE\"
  37. \"/ftp.some-where:/pub/score\"))"
  38. :group 'gnus-score-files
  39. :type '(repeat file))
  40. (defcustom gnus-score-file-single-match-alist nil
  41. "Alist mapping regexps to lists of score files.
  42. Each element of this alist should be of the form
  43. (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
  44. If the name of a group is matched by REGEXP, the corresponding scorefiles
  45. will be used for that group.
  46. The first match found is used, subsequent matching entries are ignored (to
  47. use multiple matches, see `gnus-score-file-multiple-match-alist').
  48. These score files are loaded in addition to any files returned by
  49. `gnus-score-find-score-files-function'."
  50. :group 'gnus-score-files
  51. :type '(repeat (cons regexp (repeat file))))
  52. (defcustom gnus-score-file-multiple-match-alist nil
  53. "Alist mapping regexps to lists of score files.
  54. Each element of this alist should be of the form
  55. (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
  56. If the name of a group is matched by REGEXP, the corresponding scorefiles
  57. will be used for that group.
  58. If multiple REGEXPs match a group, the score files corresponding to each
  59. match will be used (for only one match to be used, see
  60. `gnus-score-file-single-match-alist').
  61. These score files are loaded in addition to any files returned by
  62. `gnus-score-find-score-files-function'."
  63. :group 'gnus-score-files
  64. :type '(repeat (cons regexp (repeat file))))
  65. (defcustom gnus-score-file-suffix "SCORE"
  66. "Suffix of the score files."
  67. :group 'gnus-score-files
  68. :type 'string)
  69. (defcustom gnus-adaptive-file-suffix "ADAPT"
  70. "Suffix of the adaptive score files."
  71. :group 'gnus-score-files
  72. :group 'gnus-score-adapt
  73. :type 'string)
  74. (defcustom gnus-score-find-score-files-function 'gnus-score-find-bnews
  75. "Function used to find score files.
  76. The function will be called with the group name as the argument, and
  77. should return a list of score files to apply to that group. The score
  78. files do not actually have to exist.
  79. Predefined values are:
  80. `gnus-score-find-single': Only apply the group's own score file.
  81. `gnus-score-find-hierarchical': Also apply score files from parent groups.
  82. `gnus-score-find-bnews': Apply score files whose names matches.
  83. See the documentation to these functions for more information.
  84. This variable can also be a list of functions to be called. Each
  85. function is given the group name as argument and should either return
  86. a list of score files, or a list of score alists.
  87. If functions other than these pre-defined functions are used,
  88. the `a' symbolic prefix to the score commands will always use
  89. \"all.SCORE\"."
  90. :group 'gnus-score-files
  91. :type '(radio (function-item gnus-score-find-single)
  92. (function-item gnus-score-find-hierarchical)
  93. (function-item gnus-score-find-bnews)
  94. (repeat :tag "List of functions"
  95. (choice (function :tag "Other" :value 'ignore)
  96. (function-item gnus-score-find-single)
  97. (function-item gnus-score-find-hierarchical)
  98. (function-item gnus-score-find-bnews)))
  99. (function :tag "Other" :value 'ignore)))
  100. (defcustom gnus-score-interactive-default-score 1000
  101. "*Scoring commands will raise/lower the score with this number as the default."
  102. :group 'gnus-score-default
  103. :type 'integer)
  104. (defcustom gnus-score-expiry-days 7
  105. "*Number of days before unused score file entries are expired.
  106. If this variable is nil, no score file entries will be expired."
  107. :group 'gnus-score-expire
  108. :type '(choice (const :tag "never" nil)
  109. number))
  110. (defcustom gnus-update-score-entry-dates t
  111. "*If non-nil, update matching score entry dates.
  112. If this variable is nil, then score entries that provide matches
  113. will be expired along with non-matching score entries."
  114. :group 'gnus-score-expire
  115. :type 'boolean)
  116. (defcustom gnus-decay-scores nil
  117. "*If non-nil, decay non-permanent scores.
  118. If it is a regexp, only decay score files matching regexp."
  119. :group 'gnus-score-decay
  120. :type `(choice (const :tag "never" nil)
  121. (const :tag "always" t)
  122. (const :tag "adaptive score files"
  123. ,(concat "\\." gnus-adaptive-file-suffix "\\'"))
  124. (regexp)))
  125. (defcustom gnus-decay-score-function 'gnus-decay-score
  126. "*Function called to decay a score.
  127. It is called with one parameter -- the score to be decayed."
  128. :group 'gnus-score-decay
  129. :type '(radio (function-item gnus-decay-score)
  130. (function :tag "Other")))
  131. (defcustom gnus-score-decay-constant 3
  132. "*Decay all \"small\" scores with this amount."
  133. :group 'gnus-score-decay
  134. :type 'integer)
  135. (defcustom gnus-score-decay-scale .05
  136. "*Decay all \"big\" scores with this factor."
  137. :group 'gnus-score-decay
  138. :type 'number)
  139. (defcustom gnus-home-score-file nil
  140. "Variable to control where interactive score entries are to go.
  141. It can be:
  142. * A string
  143. This file will be used as the home score file.
  144. * A function
  145. The result of this function will be used as the home score file.
  146. The function will be passed the name of the group as its
  147. parameter.
  148. * A list
  149. The elements in this list can be:
  150. * `(regexp file-name ...)'
  151. If the `regexp' matches the group name, the first `file-name'
  152. will be used as the home score file. (Multiple filenames are
  153. allowed so that one may use gnus-score-file-single-match-alist to
  154. set this variable.)
  155. * A function.
  156. If the function returns non-nil, the result will be used
  157. as the home score file. The function will be passed the
  158. name of the group as its parameter.
  159. * A string. Use the string as the home score file.
  160. The list will be traversed from the beginning towards the end looking
  161. for matches."
  162. :group 'gnus-score-files
  163. :type '(choice string
  164. (repeat (choice string
  165. (cons regexp (repeat file))
  166. function))
  167. (function-item gnus-hierarchial-home-score-file)
  168. (function-item gnus-current-home-score-file)
  169. function))
  170. (defcustom gnus-home-adapt-file nil
  171. "Variable to control where new adaptive score entries are to go.
  172. This variable allows the same syntax as `gnus-home-score-file'."
  173. :group 'gnus-score-adapt
  174. :group 'gnus-score-files
  175. :type '(choice string
  176. (repeat (choice string
  177. (cons regexp (repeat file))
  178. function))
  179. function))
  180. (defcustom gnus-default-adaptive-score-alist
  181. `((gnus-kill-file-mark)
  182. (gnus-unread-mark)
  183. (gnus-read-mark
  184. (from , (+ 2 gnus-score-decay-constant))
  185. (subject , (+ 27 gnus-score-decay-constant)))
  186. (gnus-catchup-mark
  187. (subject , (+ -7 (* -1 gnus-score-decay-constant))))
  188. (gnus-killed-mark
  189. (from , (- -1 gnus-score-decay-constant))
  190. (subject , (+ -17 (* -1 gnus-score-decay-constant))))
  191. (gnus-del-mark
  192. (from , (- -1 gnus-score-decay-constant))
  193. (subject , (+ -12 (* -1 gnus-score-decay-constant)))))
  194. "Alist of marks and scores.
  195. If you use score decays, you might want to set values higher than
  196. `gnus-score-decay-constant'."
  197. :group 'gnus-score-adapt
  198. :type '(repeat (cons (symbol :tag "Mark")
  199. (repeat (list (choice :tag "Header"
  200. (const from)
  201. (const subject)
  202. (symbol :tag "other"))
  203. (integer :tag "Score"))))))
  204. (defcustom gnus-adaptive-word-length-limit nil
  205. "*Words of a length lesser than this limit will be ignored when doing adaptive scoring."
  206. :version "22.1"
  207. :group 'gnus-score-adapt
  208. :type '(radio (const :format "Unlimited " nil)
  209. (integer :format "Maximum length: %v")))
  210. (defcustom gnus-ignored-adaptive-words nil
  211. "List of words to be ignored when doing adaptive word scoring."
  212. :group 'gnus-score-adapt
  213. :type '(repeat string))
  214. (defcustom gnus-default-ignored-adaptive-words
  215. '("a" "i" "the" "to" "of" "and" "in" "is" "it" "for" "that" "if" "you"
  216. "this" "be" "on" "with" "not" "have" "are" "or" "as" "from" "can"
  217. "but" "by" "at" "an" "will" "no" "all" "was" "do" "there" "my" "one"
  218. "so" "we" "they" "what" "would" "any" "which" "about" "get" "your"
  219. "use" "some" "me" "then" "name" "like" "out" "when" "up" "time"
  220. "other" "more" "only" "just" "end" "also" "know" "how" "new" "should"
  221. "been" "than" "them" "he" "who" "make" "may" "people" "these" "now"
  222. "their" "here" "into" "first" "could" "way" "had" "see" "work" "well"
  223. "were" "two" "very" "where" "while" "us" "because" "good" "same"
  224. "even" "much" "most" "many" "such" "long" "his" "over" "last" "since"
  225. "right" "before" "our" "without" "too" "those" "why" "must" "part"
  226. "being" "current" "back" "still" "go" "point" "value" "each" "did"
  227. "both" "true" "off" "say" "another" "state" "might" "under" "start"
  228. "try" "re")
  229. "*Default list of words to be ignored when doing adaptive word scoring."
  230. :group 'gnus-score-adapt
  231. :type '(repeat string))
  232. (defcustom gnus-default-adaptive-word-score-alist
  233. `((,gnus-read-mark . 30)
  234. (,gnus-catchup-mark . -10)
  235. (,gnus-killed-mark . -20)
  236. (,gnus-del-mark . -15))
  237. "*Alist of marks and scores."
  238. :group 'gnus-score-adapt
  239. :type '(repeat (cons (character :tag "Mark")
  240. (integer :tag "Score"))))
  241. (defcustom gnus-adaptive-word-minimum nil
  242. "If a number, this is the minimum score value that can be assigned to a word."
  243. :group 'gnus-score-adapt
  244. :type '(choice (const nil) integer))
  245. (defcustom gnus-adaptive-word-no-group-words nil
  246. "If t, don't adaptively score words included in the group name."
  247. :group 'gnus-score-adapt
  248. :type 'boolean)
  249. (defcustom gnus-score-mimic-keymap nil
  250. "*Have the score entry functions pretend that they are a keymap."
  251. :group 'gnus-score-default
  252. :type 'boolean)
  253. (defcustom gnus-score-exact-adapt-limit 10
  254. "*Number that says how long a match has to be before using substring matching.
  255. When doing adaptive scoring, one normally uses fuzzy or substring
  256. matching. However, if the header one matches is short, the possibility
  257. for false positives is great, so if the length of the match is less
  258. than this variable, exact matching will be used.
  259. If this variable is nil, exact matching will always be used."
  260. :group 'gnus-score-adapt
  261. :type '(choice (const nil) integer))
  262. (defcustom gnus-score-uncacheable-files "ADAPT$"
  263. "All score files that match this regexp will not be cached."
  264. :group 'gnus-score-adapt
  265. :group 'gnus-score-files
  266. :type 'regexp)
  267. (defcustom gnus-adaptive-pretty-print nil
  268. "If non-nil, adaptive score files fill are pretty printed."
  269. :group 'gnus-score-files
  270. :group 'gnus-score-adapt
  271. :version "23.1" ;; No Gnus
  272. :type 'boolean)
  273. (defcustom gnus-score-default-header nil
  274. "Default header when entering new scores.
  275. Should be one of the following symbols.
  276. a: from
  277. s: subject
  278. b: body
  279. h: head
  280. i: message-id
  281. t: references
  282. x: xref
  283. e: `extra' (non-standard overview)
  284. l: lines
  285. d: date
  286. f: followup
  287. If nil, the user will be asked for a header."
  288. :group 'gnus-score-default
  289. :type '(choice (const :tag "from" a)
  290. (const :tag "subject" s)
  291. (const :tag "body" b)
  292. (const :tag "head" h)
  293. (const :tag "message-id" i)
  294. (const :tag "references" t)
  295. (const :tag "xref" x)
  296. (const :tag "extra" e)
  297. (const :tag "lines" l)
  298. (const :tag "date" d)
  299. (const :tag "followup" f)
  300. (const :tag "ask" nil)))
  301. (defcustom gnus-score-default-type nil
  302. "Default match type when entering new scores.
  303. Should be one of the following symbols.
  304. s: substring
  305. e: exact string
  306. f: fuzzy string
  307. r: regexp string
  308. b: before date
  309. a: after date
  310. n: this date
  311. <: less than number
  312. >: greater than number
  313. =: equal to number
  314. If nil, the user will be asked for a match type."
  315. :group 'gnus-score-default
  316. :type '(choice (const :tag "substring" s)
  317. (const :tag "exact string" e)
  318. (const :tag "fuzzy string" f)
  319. (const :tag "regexp string" r)
  320. (const :tag "before date" b)
  321. (const :tag "after date" a)
  322. (const :tag "this date" n)
  323. (const :tag "less than number" <)
  324. (const :tag "greater than number" >)
  325. (const :tag "equal than number" =)
  326. (const :tag "ask" nil)))
  327. (defcustom gnus-score-default-fold nil
  328. "Non-nil means use case folding for new score file entries."
  329. :group 'gnus-score-default
  330. :type 'boolean)
  331. (defcustom gnus-score-default-duration nil
  332. "Default duration of effect when entering new scores.
  333. Should be one of the following symbols.
  334. t: temporary
  335. p: permanent
  336. i: immediate
  337. If nil, the user will be asked for a duration."
  338. :group 'gnus-score-default
  339. :type '(choice (const :tag "temporary" t)
  340. (const :tag "permanent" p)
  341. (const :tag "immediate" i)
  342. (const :tag "ask" nil)))
  343. (defcustom gnus-score-after-write-file-function nil
  344. "Function called with the name of the score file just written to disk."
  345. :group 'gnus-score-files
  346. :type '(choice (const nil) function))
  347. (defcustom gnus-score-thread-simplify nil
  348. "If non-nil, subjects will simplified as in threading."
  349. :group 'gnus-score-various
  350. :type 'boolean)
  351. (defcustom gnus-inhibit-slow-scoring nil
  352. "Inhibit slow scoring, e.g. scoring on headers or body.
  353. If a regexp, scoring on headers or body is inhibited if the group
  354. matches the regexp. If it is t, scoring on headers or body is
  355. inhibited for all groups."
  356. :group 'gnus-score-various
  357. :version "23.1" ;; No Gnus
  358. :type '(choice (const :tag "All" nil)
  359. (const :tag "None" t)
  360. regexp))
  361. ;; Internal variables.
  362. (defvar gnus-score-use-all-scores t
  363. "If nil, only `gnus-score-find-score-files-function' is used.")
  364. (defvar gnus-adaptive-word-syntax-table
  365. (let ((table (copy-syntax-table (standard-syntax-table)))
  366. (numbers '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)))
  367. (while numbers
  368. (modify-syntax-entry (pop numbers) " " table))
  369. (modify-syntax-entry ?' "w" table)
  370. table)
  371. "Syntax table used when doing adaptive word scoring.")
  372. (defvar gnus-scores-exclude-files nil)
  373. (defvar gnus-internal-global-score-files nil)
  374. (defvar gnus-score-file-list nil)
  375. (defvar gnus-short-name-score-file-cache nil)
  376. (defvar gnus-score-help-winconf nil)
  377. (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
  378. (defvar gnus-adaptive-word-score-alist gnus-default-adaptive-word-score-alist)
  379. (defvar gnus-score-trace nil)
  380. (defvar gnus-score-edit-buffer nil)
  381. (defvar gnus-score-alist nil
  382. "Alist containing score information.
  383. The keys can be symbols or strings. The following symbols are defined.
  384. touched: If this alist has been modified.
  385. mark: Automatically mark articles below this.
  386. expunge: Automatically expunge articles below this.
  387. files: List of other score files to load when loading this one.
  388. eval: Sexp to be evaluated when the score file is loaded.
  389. String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
  390. where HEADER is the header being scored, MATCH is the string we are
  391. looking for, TYPE is a flag indicating whether it should use regexp or
  392. substring matching, SCORE is the score to add and DATE is the date
  393. of the last successful match.")
  394. (defvar gnus-score-cache nil)
  395. (defvar gnus-scores-articles nil)
  396. (defvar gnus-score-index nil)
  397. (defconst gnus-header-index
  398. ;; Name to index alist.
  399. '(("number" 0 gnus-score-integer)
  400. ("subject" 1 gnus-score-string)
  401. ("from" 2 gnus-score-string)
  402. ("date" 3 gnus-score-date)
  403. ("message-id" 4 gnus-score-string)
  404. ("references" 5 gnus-score-string)
  405. ("chars" 6 gnus-score-integer)
  406. ("lines" 7 gnus-score-integer)
  407. ("xref" 8 gnus-score-string)
  408. ("extra" 9 gnus-score-string)
  409. ("head" -1 gnus-score-body)
  410. ("body" -1 gnus-score-body)
  411. ("all" -1 gnus-score-body)
  412. ("followup" 2 gnus-score-followup)
  413. ("thread" 5 gnus-score-thread)))
  414. ;;; Summary mode score maps.
  415. (gnus-define-keys (gnus-summary-score-map "V" gnus-summary-mode-map)
  416. "s" gnus-summary-set-score
  417. "S" gnus-summary-current-score
  418. "c" gnus-score-change-score-file
  419. "C" gnus-score-customize
  420. "m" gnus-score-set-mark-below
  421. "x" gnus-score-set-expunge-below
  422. "R" gnus-summary-rescore
  423. "e" gnus-score-edit-current-scores
  424. "f" gnus-score-edit-file
  425. "F" gnus-score-flush-cache
  426. "t" gnus-score-find-trace
  427. "w" gnus-score-find-favourite-words)
  428. ;; Summary score file commands
  429. ;; Much modification of the kill (ahem, score) code and lots of the
  430. ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
  431. (defun gnus-summary-lower-score (&optional score symp)
  432. "Make a score entry based on the current article.
  433. The user will be prompted for header to score on, match type,
  434. permanence, and the string to be used. The numerical prefix will
  435. be used as SCORE. A symbolic prefix of `a' (the SYMP parameter)
  436. says to use the `all.SCORE' file for the command instead of the
  437. current score file."
  438. (interactive (gnus-interactive "P\ny"))
  439. (gnus-summary-increase-score (- (gnus-score-delta-default score)) symp))
  440. (defun gnus-score-kill-help-buffer ()
  441. (when (get-buffer "*Score Help*")
  442. (kill-buffer "*Score Help*")
  443. (when gnus-score-help-winconf
  444. (set-window-configuration gnus-score-help-winconf))))
  445. (defun gnus-summary-increase-score (&optional score symp)
  446. "Make a score entry based on the current article.
  447. The user will be prompted for header to score on, match type,
  448. permanence, and the string to be used. The numerical prefix will
  449. be used as SCORE. A symbolic prefix of `a' (the SYMP parameter)
  450. says to use the `all.SCORE' file for the command instead of the
  451. current score file."
  452. (interactive (gnus-interactive "P\ny"))
  453. (let* ((nscore (gnus-score-delta-default score))
  454. (prefix (if (< nscore 0) ?L ?I))
  455. (increase (> nscore 0))
  456. (char-to-header
  457. '((?a "from" nil nil string)
  458. (?s "subject" nil nil string)
  459. (?b "body" "" nil body-string)
  460. (?h "head" "" nil body-string)
  461. (?i "message-id" nil nil string)
  462. (?r "references" "message-id" nil string)
  463. (?x "xref" nil nil string)
  464. (?e "extra" nil nil string)
  465. (?l "lines" nil nil number)
  466. (?d "date" nil nil date)
  467. (?f "followup" nil nil string)
  468. (?t "thread" "message-id" nil string)))
  469. (char-to-type
  470. '((?s s "substring" string)
  471. (?e e "exact string" string)
  472. (?f f "fuzzy string" string)
  473. (?r r "regexp string" string)
  474. (?z s "substring" body-string)
  475. (?p r "regexp string" body-string)
  476. (?b before "before date" date)
  477. (?a after "after date" date)
  478. (?n at "this date" date)
  479. (?< < "less than number" number)
  480. (?> > "greater than number" number)
  481. (?= = "equal to number" number)))
  482. (current-score-file gnus-current-score-file)
  483. (char-to-perm
  484. (list (list ?t (current-time-string) "temporary")
  485. '(?p perm "permanent") '(?i now "immediate")))
  486. (mimic gnus-score-mimic-keymap)
  487. (hchar (and gnus-score-default-header
  488. (aref (symbol-name gnus-score-default-header) 0)))
  489. (tchar (and gnus-score-default-type
  490. (aref (symbol-name gnus-score-default-type) 0)))
  491. (pchar (and gnus-score-default-duration
  492. (aref (symbol-name gnus-score-default-duration) 0)))
  493. entry temporary type match extra)
  494. (unwind-protect
  495. (progn
  496. ;; First we read the header to score.
  497. (while (not hchar)
  498. (if mimic
  499. (progn
  500. (sit-for 1)
  501. (message "%c-" prefix))
  502. (message "%s header (%s?): " (if increase "Increase" "Lower")
  503. (mapconcat (lambda (s) (char-to-string (car s)))
  504. char-to-header "")))
  505. (setq hchar (read-char))
  506. (when (or (= hchar ??) (= hchar ?\C-h))
  507. (setq hchar nil)
  508. (gnus-score-insert-help "Match on header" char-to-header 1)))
  509. (gnus-score-kill-help-buffer)
  510. (unless (setq entry (assq (downcase hchar) char-to-header))
  511. (if mimic (error "%c %c" prefix hchar)
  512. (error "Invalid header type")))
  513. (when (/= (downcase hchar) hchar)
  514. ;; This was a majuscule, so we end reading and set the defaults.
  515. (if mimic (message "%c %c" prefix hchar) (message ""))
  516. (setq tchar (or tchar ?s)
  517. pchar (or pchar ?t)))
  518. (let ((legal-types
  519. (delq nil
  520. (mapcar (lambda (s)
  521. (if (eq (nth 4 entry)
  522. (nth 3 s))
  523. s nil))
  524. char-to-type))))
  525. ;; We continue reading - the type.
  526. (while (not tchar)
  527. (if mimic
  528. (progn
  529. (sit-for 1) (message "%c %c-" prefix hchar))
  530. (message "%s header `%s' with match type (%s?): "
  531. (if increase "Increase" "Lower")
  532. (nth 1 entry)
  533. (mapconcat (lambda (s) (char-to-string (car s)))
  534. legal-types "")))
  535. (setq tchar (read-char))
  536. (when (or (= tchar ??) (= tchar ?\C-h))
  537. (setq tchar nil)
  538. (gnus-score-insert-help "Match type" legal-types 2)))
  539. (gnus-score-kill-help-buffer)
  540. (unless (setq type (nth 1 (assq (downcase tchar) legal-types)))
  541. (if mimic (error "%c %c" prefix hchar)
  542. (error "Invalid match type"))))
  543. (when (/= (downcase tchar) tchar)
  544. ;; It was a majuscule, so we end reading and use the default.
  545. (if mimic (message "%c %c %c" prefix hchar tchar)
  546. (message ""))
  547. (setq pchar (or pchar ?t)))
  548. ;; We continue reading.
  549. (while (not pchar)
  550. (if mimic
  551. (progn
  552. (sit-for 1) (message "%c %c %c-" prefix hchar tchar))
  553. (message "%s permanence (%s?): " (if increase "Increase" "Lower")
  554. (mapconcat (lambda (s) (char-to-string (car s)))
  555. char-to-perm "")))
  556. (setq pchar (read-char))
  557. (when (or (= pchar ??) (= pchar ?\C-h))
  558. (setq pchar nil)
  559. (gnus-score-insert-help "Match permanence" char-to-perm 2)))
  560. (gnus-score-kill-help-buffer)
  561. (if mimic (message "%c %c %c %c" prefix hchar tchar pchar)
  562. (message ""))
  563. (unless (setq temporary (cadr (assq pchar char-to-perm)))
  564. ;; Deal with der(r)ided superannuated paradigms.
  565. (when (and (eq (1+ prefix) 77)
  566. (eq (+ hchar 12) 109)
  567. (eq (1- tchar) 113)
  568. (eq (- pchar 4) 111))
  569. (error "You rang?"))
  570. (if mimic
  571. (error "%c %c %c %c" prefix hchar tchar pchar)
  572. (error "Invalid match duration"))))
  573. ;; Always kill the score help buffer.
  574. (gnus-score-kill-help-buffer))
  575. ;; If scoring an extra (non-standard overview) header,
  576. ;; we must find out which header is in question.
  577. (setq extra
  578. (and gnus-extra-headers
  579. (equal (nth 1 entry) "extra")
  580. (intern ; need symbol
  581. (let ((collection (mapcar 'symbol-name gnus-extra-headers)))
  582. (gnus-completing-read
  583. "Score extra header" ; prompt
  584. collection ; completion list
  585. t ; require match
  586. nil ; no history
  587. nil ; no initial-input
  588. (car collection)))))) ; default value
  589. ;; extra is now nil or a symbol.
  590. ;; We have all the data, so we enter this score.
  591. (setq match (if (string= (nth 2 entry) "") ""
  592. (gnus-summary-header (or (nth 2 entry) (nth 1 entry))
  593. nil extra)))
  594. ;; Modify the match, perhaps.
  595. (cond
  596. ((equal (nth 1 entry) "xref")
  597. (when (string-match "^Xref: *" match)
  598. (setq match (substring match (match-end 0))))
  599. (when (string-match "^[^:]* +" match)
  600. (setq match (substring match (match-end 0))))))
  601. (when (memq type '(r R regexp Regexp))
  602. (setq match (regexp-quote match)))
  603. ;; Change score file to the "all.SCORE" file.
  604. (when (eq symp 'a)
  605. (with-current-buffer gnus-summary-buffer
  606. (gnus-score-load-file
  607. ;; This is a kludge; yes...
  608. (cond
  609. ((eq gnus-score-find-score-files-function
  610. 'gnus-score-find-hierarchical)
  611. (gnus-score-file-name ""))
  612. ((eq gnus-score-find-score-files-function 'gnus-score-find-single)
  613. current-score-file)
  614. (t
  615. (gnus-score-file-name "all"))))))
  616. (gnus-summary-score-entry
  617. (nth 1 entry) ; Header
  618. match ; Match
  619. type ; Type
  620. (if (eq score 's) nil score) ; Score
  621. (if (eq temporary 'perm) ; Temp
  622. nil
  623. temporary)
  624. (not (nth 3 entry)) ; Prompt
  625. nil ; not silent
  626. extra) ; non-standard overview.
  627. (when (eq symp 'a)
  628. ;; We change the score file back to the previous one.
  629. (with-current-buffer gnus-summary-buffer
  630. (gnus-score-load-file current-score-file)))))
  631. (defun gnus-score-insert-help (string alist idx)
  632. (setq gnus-score-help-winconf (current-window-configuration))
  633. (with-current-buffer (gnus-get-buffer-create "*Score Help*")
  634. (buffer-disable-undo)
  635. (delete-windows-on (current-buffer))
  636. (erase-buffer)
  637. (insert string ":\n\n")
  638. (let ((max -1)
  639. (list alist)
  640. (i 0)
  641. n width pad format)
  642. ;; find the longest string to display
  643. (while list
  644. (setq n (length (nth idx (car list))))
  645. (unless (> max n)
  646. (setq max n))
  647. (setq list (cdr list)))
  648. (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
  649. (setq n (/ (1- (window-width)) max)) ; items per line
  650. (setq width (/ (1- (window-width)) n)) ; width of each item
  651. ;; insert `n' items, each in a field of width `width'
  652. (while alist
  653. (if (< i n)
  654. ()
  655. (setq i 0)
  656. (delete-char -1) ; the `\n' takes a char
  657. (insert "\n"))
  658. (setq pad (- width 3))
  659. (setq format (concat "%c: %-" (int-to-string pad) "s"))
  660. (insert (format format (caar alist) (nth idx (car alist))))
  661. (setq alist (cdr alist))
  662. (setq i (1+ i))))
  663. (goto-char (point-min))
  664. ;; display ourselves in a small window at the bottom
  665. (gnus-select-lowest-window)
  666. (if (< (/ (window-height) 2) window-min-height)
  667. (switch-to-buffer "*Score Help*")
  668. (split-window)
  669. (pop-to-buffer "*Score Help*"))
  670. (let ((window-min-height 1))
  671. (shrink-window-if-larger-than-buffer))
  672. (select-window (gnus-get-buffer-window gnus-summary-buffer t))))
  673. (defun gnus-summary-header (header &optional no-err extra)
  674. ;; Return HEADER for current articles, or error.
  675. (let ((article (gnus-summary-article-number))
  676. headers)
  677. (if article
  678. (if (and (setq headers (gnus-summary-article-header article))
  679. (vectorp headers))
  680. (if extra ; `header' must be "extra"
  681. (or (cdr (assq extra (mail-header-extra headers))) "")
  682. (aref headers (nth 1 (assoc header gnus-header-index))))
  683. (if no-err
  684. nil
  685. (error "Pseudo-articles can't be scored")))
  686. (if no-err
  687. (error "No article on current line")
  688. nil))))
  689. (defun gnus-newsgroup-score-alist ()
  690. (or
  691. (let ((param-file (gnus-group-find-parameter
  692. gnus-newsgroup-name 'score-file)))
  693. (when param-file
  694. (gnus-score-load param-file)))
  695. (gnus-score-load
  696. (gnus-score-file-name gnus-newsgroup-name)))
  697. gnus-score-alist)
  698. (defsubst gnus-score-get (symbol &optional alist)
  699. ;; Get SYMBOL's definition in ALIST.
  700. (cdr (assoc symbol
  701. (or alist
  702. gnus-score-alist
  703. (gnus-newsgroup-score-alist)))))
  704. (defun gnus-summary-score-entry (header match type score date
  705. &optional prompt silent extra)
  706. "Enter score file entry.
  707. HEADER is the header being scored.
  708. MATCH is the string we are looking for.
  709. TYPE is the match type: substring, regexp, exact, fuzzy.
  710. SCORE is the score to add.
  711. DATE is the expire date, or nil for no expire, or 'now for immediate expire.
  712. If optional argument `PROMPT' is non-nil, allow user to edit match.
  713. If optional argument `SILENT' is nil, show effect of score entry.
  714. If optional argument `EXTRA' is non-nil, it's a non-standard overview header."
  715. ;; Regexp is the default type.
  716. (when (eq type t)
  717. (setq type 'r))
  718. ;; Simplify matches...
  719. (cond ((or (eq type 'r) (eq type 's) (eq type nil))
  720. (setq match (if match (gnus-simplify-subject-re match) "")))
  721. ((eq type 'f)
  722. (setq match (gnus-simplify-subject-fuzzy match))))
  723. (let ((score (gnus-score-delta-default score))
  724. (header (downcase header))
  725. new)
  726. (set-text-properties 0 (length header) nil header)
  727. (when prompt
  728. (setq match (read-string
  729. (format "Match %s on %s, %s: "
  730. (cond ((eq date 'now)
  731. "now")
  732. ((stringp date)
  733. "temp")
  734. (t "permanent"))
  735. header
  736. (if (< score 0) "lower" "raise"))
  737. (if (numberp match)
  738. (int-to-string match)
  739. match))))
  740. ;; If this is an integer comparison, we transform from string to int.
  741. (if (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
  742. (if (stringp match)
  743. (setq match (string-to-number match)))
  744. (set-text-properties 0 (length match) nil match))
  745. (unless (eq date 'now)
  746. ;; Add the score entry to the score file.
  747. (when (= score gnus-score-interactive-default-score)
  748. (setq score nil))
  749. (let ((old (gnus-score-get header))
  750. elem)
  751. (setq new
  752. (cond
  753. (extra
  754. (list match score
  755. (and date (if (numberp date) date
  756. (date-to-day date)))
  757. type (symbol-name extra)))
  758. (type
  759. (list match score
  760. (and date (if (numberp date) date
  761. (date-to-day date)))
  762. type))
  763. (date (list match score (date-to-day date)))
  764. (score (list match score))
  765. (t (list match))))
  766. ;; We see whether we can collapse some score entries.
  767. ;; This isn't quite correct, because there may be more elements
  768. ;; later on with the same key that have matching elems... Hm.
  769. (if (and old
  770. (setq elem (assoc match old))
  771. (eq (nth 3 elem) (nth 3 new))
  772. (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
  773. (and (not (nth 2 elem)) (not (nth 2 new)))))
  774. ;; Yup, we just add this new score to the old elem.
  775. (setcar (cdr elem) (+ (or (nth 1 elem)
  776. gnus-score-interactive-default-score)
  777. (or (nth 1 new)
  778. gnus-score-interactive-default-score)))
  779. ;; Nope, we have to add a new elem.
  780. (gnus-score-set header (if old (cons new old) (list new)) nil t))
  781. (gnus-score-set 'touched '(t))))
  782. ;; Score the current buffer.
  783. (unless silent
  784. (if (and (>= (nth 1 (assoc header gnus-header-index)) 0)
  785. (eq (nth 2 (assoc header gnus-header-index))
  786. 'gnus-score-string))
  787. (gnus-summary-score-effect header match type score extra)
  788. (gnus-summary-rescore)))
  789. ;; Return the new scoring rule.
  790. new))
  791. (defun gnus-summary-score-effect (header match type score &optional extra)
  792. "Simulate the effect of a score file entry.
  793. HEADER is the header being scored.
  794. MATCH is the string we are looking for.
  795. TYPE is the score type.
  796. SCORE is the score to add.
  797. EXTRA is the possible non-standard header."
  798. (interactive (list (gnus-completing-read "Header"
  799. (mapcar
  800. 'car
  801. (gnus-remove-if-not
  802. (lambda (x) (fboundp (nth 2 x)))
  803. gnus-header-index))
  804. t)
  805. (read-string "Match: ")
  806. (if (y-or-n-p "Use regexp match? ") 'r 's)
  807. (string-to-number (read-string "Score: "))))
  808. (save-excursion
  809. (unless (and (stringp match) (> (length match) 0))
  810. (error "No match"))
  811. (goto-char (point-min))
  812. (let ((regexp (cond ((eq type 'f)
  813. (gnus-simplify-subject-fuzzy match))
  814. ((eq type 'r)
  815. match)
  816. ((eq type 'e)
  817. (concat "\\`" (regexp-quote match) "\\'"))
  818. (t
  819. (regexp-quote match)))))
  820. (while (not (eobp))
  821. (let ((content (gnus-summary-header header 'noerr extra))
  822. (case-fold-search t))
  823. (and content
  824. (when (if (eq type 'f)
  825. (string-equal (gnus-simplify-subject-fuzzy content)
  826. regexp)
  827. (string-match regexp content))
  828. (gnus-summary-raise-score score))))
  829. (beginning-of-line 2))))
  830. (gnus-set-mode-line 'summary))
  831. ;;;
  832. ;;; Gnus Score Files
  833. ;;;
  834. ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
  835. (defun gnus-score-set-mark-below (score)
  836. "Automatically mark articles with score below SCORE as read."
  837. (interactive
  838. (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
  839. (string-to-number (read-string "Mark below: ")))))
  840. (setq score (or score gnus-summary-default-score 0))
  841. (gnus-score-set 'mark (list score))
  842. (gnus-score-set 'touched '(t))
  843. (setq gnus-summary-mark-below score)
  844. (gnus-score-update-lines))
  845. (defun gnus-score-update-lines ()
  846. "Update all lines in the summary buffer."
  847. (save-excursion
  848. (goto-char (point-min))
  849. (while (not (eobp))
  850. (gnus-summary-update-line)
  851. (forward-line 1))))
  852. (defun gnus-score-update-all-lines ()
  853. "Update all lines in the summary buffer, even the hidden ones."
  854. (save-excursion
  855. (goto-char (point-min))
  856. (let (hidden)
  857. (while (not (eobp))
  858. (when (gnus-summary-show-thread)
  859. (push (point) hidden))
  860. (gnus-summary-update-line)
  861. (forward-line 1))
  862. ;; Re-hide the hidden threads.
  863. (while hidden
  864. (goto-char (pop hidden))
  865. (gnus-summary-hide-thread)))))
  866. (defun gnus-score-set-expunge-below (score)
  867. "Automatically expunge articles with score below SCORE."
  868. (interactive
  869. (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
  870. (string-to-number (read-string "Set expunge below: ")))))
  871. (setq score (or score gnus-summary-default-score 0))
  872. (gnus-score-set 'expunge (list score))
  873. (gnus-score-set 'touched '(t)))
  874. (defun gnus-score-followup-article (&optional score)
  875. "Add SCORE to all followups to the article in the current buffer."
  876. (interactive "P")
  877. (setq score (gnus-score-delta-default score))
  878. (when (gnus-buffer-live-p gnus-summary-buffer)
  879. (save-excursion
  880. (save-restriction
  881. (message-narrow-to-headers)
  882. (let ((id (mail-fetch-field "message-id")))
  883. (when id
  884. (set-buffer gnus-summary-buffer)
  885. (gnus-summary-score-entry
  886. "references" (concat id "[ \t]*$") 'r
  887. score (current-time-string) nil t)))))))
  888. (defun gnus-score-followup-thread (&optional score)
  889. "Add SCORE to all later articles in the thread the current buffer is part of."
  890. (interactive "P")
  891. (setq score (gnus-score-delta-default score))
  892. (when (gnus-buffer-live-p gnus-summary-buffer)
  893. (save-excursion
  894. (save-restriction
  895. (goto-char (point-min))
  896. (let ((id (mail-fetch-field "message-id")))
  897. (when id
  898. (set-buffer gnus-summary-buffer)
  899. (gnus-summary-score-entry
  900. "references" id 's
  901. score (current-time-string))))))))
  902. (defun gnus-score-set (symbol value &optional alist warn)
  903. ;; Set SYMBOL to VALUE in ALIST.
  904. (let* ((alist
  905. (or alist
  906. gnus-score-alist
  907. (gnus-newsgroup-score-alist)))
  908. (entry (assoc symbol alist)))
  909. (cond ((gnus-score-get 'read-only alist)
  910. ;; This is a read-only score file, so we do nothing.
  911. (when warn
  912. (gnus-message 4 "Note: read-only score file; entry discarded")))
  913. (entry
  914. (setcdr entry value))
  915. ((null alist)
  916. (error "Empty alist"))
  917. (t
  918. (setcdr alist
  919. (cons (cons symbol value) (cdr alist)))))))
  920. (defun gnus-summary-raise-score (n)
  921. "Raise the score of the current article by N."
  922. (interactive "p")
  923. (gnus-summary-set-score (+ (gnus-summary-article-score)
  924. (or n gnus-score-interactive-default-score ))))
  925. (defun gnus-summary-set-score (n)
  926. "Set the score of the current article to N."
  927. (interactive "p")
  928. (save-excursion
  929. (gnus-summary-show-thread)
  930. (let ((buffer-read-only nil))
  931. ;; Set score.
  932. (gnus-summary-update-mark
  933. (if (= n (or gnus-summary-default-score 0)) ? ;Whitespace
  934. (if (< n (or gnus-summary-default-score 0))
  935. gnus-score-below-mark gnus-score-over-mark))
  936. 'score))
  937. (let* ((article (gnus-summary-article-number))
  938. (score (assq article gnus-newsgroup-scored)))
  939. (if score (setcdr score n)
  940. (push (cons article n) gnus-newsgroup-scored)))
  941. (gnus-summary-update-line)))
  942. (defun gnus-summary-current-score (arg)
  943. "Return the score of the current article.
  944. With prefix ARG, return the total score of the current (sub)thread."
  945. (interactive "P")
  946. (gnus-message 1 "%s" (if arg
  947. (gnus-thread-total-score
  948. (gnus-id-to-thread
  949. (mail-header-id (gnus-summary-article-header))))
  950. (gnus-summary-article-score))))
  951. (defun gnus-score-change-score-file (file)
  952. "Change current score alist."
  953. (interactive
  954. (list (read-file-name "Change to score file: " gnus-kill-files-directory)))
  955. (gnus-score-load-file file)
  956. (gnus-set-mode-line 'summary))
  957. (defvar gnus-score-edit-exit-function)
  958. (defun gnus-score-edit-current-scores (file)
  959. "Edit the current score alist."
  960. (interactive (list gnus-current-score-file))
  961. (if (not gnus-current-score-file)
  962. (error "No current score file")
  963. (let ((winconf (current-window-configuration)))
  964. (when (buffer-name gnus-summary-buffer)
  965. (gnus-score-save))
  966. (gnus-make-directory (file-name-directory file))
  967. (setq gnus-score-edit-buffer (find-file-noselect file))
  968. (gnus-configure-windows 'edit-score)
  969. (gnus-score-mode)
  970. (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
  971. (make-local-variable 'gnus-prev-winconf)
  972. (setq gnus-prev-winconf winconf))
  973. (gnus-message
  974. 4 "%s" (substitute-command-keys
  975. "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits"))))
  976. (defun gnus-score-edit-all-score ()
  977. "Edit the all.SCORE file."
  978. (interactive)
  979. (find-file (gnus-score-file-name "all"))
  980. (gnus-score-mode)
  981. (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
  982. (gnus-message
  983. 4 (substitute-command-keys
  984. "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
  985. (defun gnus-score-edit-file (file)
  986. "Edit a score file."
  987. (interactive
  988. (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
  989. (gnus-make-directory (file-name-directory file))
  990. (when (buffer-name gnus-summary-buffer)
  991. (gnus-score-save))
  992. (let ((winconf (current-window-configuration)))
  993. (setq gnus-score-edit-buffer (find-file-noselect file))
  994. (gnus-configure-windows 'edit-score)
  995. (gnus-score-mode)
  996. (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
  997. (make-local-variable 'gnus-prev-winconf)
  998. (setq gnus-prev-winconf winconf))
  999. (gnus-message
  1000. 4 "%s" (substitute-command-keys
  1001. "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
  1002. (defun gnus-score-edit-file-at-point (&optional format)
  1003. "Edit score file at point in Score Trace buffers.
  1004. If FORMAT, also format the current score file."
  1005. (let* ((rule (save-excursion
  1006. (beginning-of-line)
  1007. (read (current-buffer))))
  1008. (sep "[ \n\r\t]*")
  1009. ;; Must be synced with `gnus-score-find-trace':
  1010. (reg " -> +")
  1011. (file (save-excursion
  1012. (end-of-line)
  1013. (if (and (re-search-backward reg (point-at-bol) t)
  1014. (re-search-forward reg (point-at-eol) t))
  1015. (buffer-substring (point) (point-at-eol))
  1016. nil))))
  1017. (if (or (not file)
  1018. (string-match "\\<\\(non-file rule\\|A file\\)\\>" file)
  1019. ;; (see `gnus-score-find-trace' and `gnus-score-advanced')
  1020. (string= "" file))
  1021. (gnus-error 3 "Can't find a score file in current line.")
  1022. (gnus-score-edit-file file)
  1023. (when format
  1024. (gnus-score-pretty-print))
  1025. (when (consp rule) ;; the rule exists
  1026. (setq rule (mapconcat #'(lambda (obj)
  1027. (regexp-quote (format "%S" obj)))
  1028. rule
  1029. sep))
  1030. (goto-char (point-min))
  1031. (re-search-forward rule nil t)
  1032. ;; make it easy to use `kill-sexp':
  1033. (goto-char (1- (match-beginning 0)))))))
  1034. (defun gnus-score-load-file (file)
  1035. ;; Load score file FILE. Returns a list a retrieved score-alists.
  1036. (let* ((file (expand-file-name
  1037. (or (and (string-match
  1038. (concat "^" (regexp-quote
  1039. (expand-file-name
  1040. gnus-kill-files-directory)))
  1041. (expand-file-name file))
  1042. file)
  1043. (expand-file-name file gnus-kill-files-directory))))
  1044. (cached (assoc file gnus-score-cache))
  1045. (global (member file gnus-internal-global-score-files))
  1046. lists alist)
  1047. (if cached
  1048. ;; The score file was already loaded.
  1049. (setq alist (cdr cached))
  1050. ;; We load the score file.
  1051. (setq gnus-score-alist nil)
  1052. (setq alist (gnus-score-load-score-alist file))
  1053. ;; We add '(touched) to the alist to signify that it hasn't been
  1054. ;; touched (yet).
  1055. (unless (assq 'touched alist)
  1056. (push (list 'touched nil) alist))
  1057. ;; If it is a global score file, we make it read-only.
  1058. (and global
  1059. (not (assq 'read-only alist))
  1060. (push (list 'read-only t) alist))
  1061. (push (cons file alist) gnus-score-cache))
  1062. (let ((a alist)
  1063. found)
  1064. (while a
  1065. ;; Downcase all header names.
  1066. (cond
  1067. ((stringp (caar a))
  1068. (setcar (car a) (downcase (caar a)))
  1069. (setq found t))
  1070. ;; Advanced scoring.
  1071. ((consp (caar a))
  1072. (setq found t)))
  1073. (pop a))
  1074. ;; If there are actual scores in the alist, we add it to the
  1075. ;; return value of this function.
  1076. (when found
  1077. (setq lists (list alist))))
  1078. ;; Treat the other possible atoms in the score alist.
  1079. (let ((mark (car (gnus-score-get 'mark alist)))
  1080. (expunge (car (gnus-score-get 'expunge alist)))
  1081. (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
  1082. (files (gnus-score-get 'files alist))
  1083. (exclude-files (gnus-score-get 'exclude-files alist))
  1084. (orphan (car (gnus-score-get 'orphan alist)))
  1085. (adapt (gnus-score-get 'adapt alist))
  1086. (thread-mark-and-expunge
  1087. (car (gnus-score-get 'thread-mark-and-expunge alist)))
  1088. (adapt-file (car (gnus-score-get 'adapt-file alist)))
  1089. (local (gnus-score-get 'local alist))
  1090. (decay (car (gnus-score-get 'decay alist)))
  1091. (eval (car (gnus-score-get 'eval alist))))
  1092. ;; Perform possible decays.
  1093. (when (and (if (stringp gnus-decay-scores)
  1094. (string-match gnus-decay-scores file)
  1095. gnus-decay-scores)
  1096. (or cached (file-exists-p file))
  1097. (or (not decay)
  1098. (gnus-decay-scores alist decay)))
  1099. (gnus-score-set 'touched '(t) alist)
  1100. (gnus-score-set 'decay (list (time-to-days (current-time))) alist))
  1101. ;; We do not respect eval and files atoms from global score
  1102. ;; files.
  1103. (when (and files (not global))
  1104. (setq lists (apply 'append lists
  1105. (mapcar 'gnus-score-load-file
  1106. (if adapt-file (cons adapt-file files)
  1107. files)))))
  1108. (when (and eval (not global))
  1109. (eval eval))
  1110. ;; We then expand any exclude-file directives.
  1111. (setq gnus-scores-exclude-files
  1112. (nconc
  1113. (apply
  1114. 'nconc
  1115. (mapcar
  1116. (lambda (sfile)
  1117. (list
  1118. (expand-file-name sfile (file-name-directory file))
  1119. (expand-file-name sfile gnus-kill-files-directory)))
  1120. exclude-files))
  1121. gnus-scores-exclude-files))
  1122. (when local
  1123. (with-current-buffer gnus-summary-buffer
  1124. (while local
  1125. (and (consp (car local))
  1126. (symbolp (caar local))
  1127. (progn
  1128. (make-local-variable (caar local))
  1129. (set (caar local) (nth 1 (car local)))))
  1130. (setq local (cdr local)))))
  1131. (when orphan
  1132. (setq gnus-orphan-score orphan))
  1133. (setq gnus-adaptive-score-alist
  1134. (cond ((equal adapt '(t))
  1135. (setq gnus-newsgroup-adaptive t)
  1136. gnus-default-adaptive-score-alist)
  1137. ((equal adapt '(ignore))
  1138. (setq gnus-newsgroup-adaptive nil))
  1139. ((consp adapt)
  1140. (setq gnus-newsgroup-adaptive t)
  1141. adapt)
  1142. (t
  1143. gnus-default-adaptive-score-alist)))
  1144. (setq gnus-thread-expunge-below
  1145. (or thread-mark-and-expunge gnus-thread-expunge-below))
  1146. (setq gnus-summary-mark-below
  1147. (or mark mark-and-expunge gnus-summary-mark-below))
  1148. (setq gnus-summary-expunge-below
  1149. (or expunge mark-and-expunge gnus-summary-expunge-below))
  1150. (setq gnus-newsgroup-adaptive-score-file
  1151. (or adapt-file gnus-newsgroup-adaptive-score-file)))
  1152. (setq gnus-current-score-file file)
  1153. (setq gnus-score-alist alist)
  1154. lists))
  1155. (defun gnus-score-load (file)
  1156. ;; Load score FILE.
  1157. (let ((cache (assoc file gnus-score-cache)))
  1158. (if cache
  1159. (setq gnus-score-alist (cdr cache))
  1160. (setq gnus-score-alist nil)
  1161. (gnus-score-load-score-alist file)
  1162. (unless gnus-score-alist
  1163. (setq gnus-score-alist (copy-alist '((touched nil)))))
  1164. (push (cons file gnus-score-alist) gnus-score-cache))))
  1165. (defun gnus-score-remove-from-cache (file)
  1166. (setq gnus-score-cache
  1167. (delq (assoc file gnus-score-cache) gnus-score-cache)))
  1168. (defun gnus-score-load-score-alist (file)
  1169. "Read score FILE."
  1170. (let (alist)
  1171. (if (not (file-readable-p file))
  1172. ;; Couldn't read file.
  1173. (setq gnus-score-alist nil)
  1174. ;; Read file.
  1175. (with-temp-buffer
  1176. (let ((coding-system-for-read score-mode-coding-system))
  1177. (insert-file-contents file))
  1178. (goto-char (point-min))
  1179. ;; Only do the loading if the score file isn't empty.
  1180. (when (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
  1181. (setq alist
  1182. (condition-case ()
  1183. (read (current-buffer))
  1184. (error
  1185. (gnus-error 3.2 "Problem with score file %s" file))))))
  1186. (cond
  1187. ((and alist
  1188. (atom alist))
  1189. ;; Bogus score file.
  1190. (error "Invalid syntax with score file %s" file))
  1191. ((eq (car alist) 'setq)
  1192. ;; This is an old-style score file.
  1193. (setq gnus-score-alist (gnus-score-transform-old-to-new alist)))
  1194. (t
  1195. (setq gnus-score-alist alist)))
  1196. ;; Check the syntax of the score file.
  1197. (setq gnus-score-alist
  1198. (gnus-score-check-syntax gnus-score-alist file)))))
  1199. (defun gnus-score-check-syntax (alist file)
  1200. "Check the syntax of the score ALIST."
  1201. (cond
  1202. ((null alist)
  1203. nil)
  1204. ((not (consp alist))
  1205. (gnus-message 1 "Score file is not a list: %s" file)
  1206. (ding)
  1207. nil)
  1208. (t
  1209. (let ((a alist)
  1210. sr err s type)
  1211. (while (and a (not err))
  1212. (setq
  1213. err
  1214. (cond
  1215. ((not (listp (car a)))
  1216. (format "Invalid score element %s in %s" (car a) file))
  1217. ((stringp (caar a))
  1218. (cond
  1219. ((not (listp (setq sr (cdar a))))
  1220. (format "Invalid header match %s in %s" (nth 1 (car a)) file))
  1221. (t
  1222. (setq type (caar a))
  1223. (while (and sr (not err))
  1224. (setq s (pop sr))
  1225. (setq
  1226. err
  1227. (cond
  1228. ((if (member (downcase type) '("lines" "chars"))
  1229. (not (numberp (car s)))
  1230. (not (stringp (car s))))
  1231. (format "Invalid match %s in %s" (car s) file))
  1232. ((and (cadr s) (not (integerp (cadr s))))
  1233. (format "Non-integer score %s in %s" (cadr s) file))
  1234. ((and (caddr s) (not (integerp (caddr s))))
  1235. (format "Non-integer date %s in %s" (caddr s) file))
  1236. ((and (cadddr s) (not (symbolp (cadddr s))))
  1237. (format "Non-symbol match type %s in %s" (cadddr s) file)))))
  1238. err)))))
  1239. (setq a (cdr a)))
  1240. (if err
  1241. (progn
  1242. (ding)
  1243. (gnus-message 3 "%s" err)
  1244. (sit-for 2)
  1245. nil)
  1246. alist)))))
  1247. (defun gnus-score-transform-old-to-new (alist)
  1248. (let* ((alist (nth 2 alist))
  1249. out entry)
  1250. (when (eq (car alist) 'quote)
  1251. (setq alist (nth 1 alist)))
  1252. (while alist
  1253. (setq entry (car alist))
  1254. (if (stringp (car entry))
  1255. (let ((scor (cdr entry)))
  1256. (push entry out)
  1257. (while scor
  1258. (setcar scor
  1259. (list (caar scor) (nth 2 (car scor))
  1260. (and (nth 3 (car scor))
  1261. (date-to-day (nth 3 (car scor))))
  1262. (if (nth 1 (car scor)) 'r 's)))
  1263. (setq scor (cdr scor))))
  1264. (push (if (not (listp (cdr entry)))
  1265. (list (car entry) (cdr entry))
  1266. entry)
  1267. out))
  1268. (setq alist (cdr alist)))
  1269. (cons (list 'touched t) (nreverse out))))
  1270. (defun gnus-score-save ()
  1271. ;; Save all score information.
  1272. (let ((cache gnus-score-cache)
  1273. entry score file)
  1274. (save-excursion
  1275. (setq gnus-score-alist nil)
  1276. (nnheader-set-temp-buffer " *Gnus Scores*")
  1277. (while cache
  1278. (current-buffer)
  1279. (setq entry (pop cache)
  1280. file (nnheader-translate-file-chars (car entry) t)
  1281. score (cdr entry))
  1282. (if (or (not (equal (gnus-score-get 'touched score) '(t)))
  1283. (gnus-score-get 'read-only score)
  1284. (and (file-exists-p file)
  1285. (not (file-writable-p file))))
  1286. ()
  1287. (setq score (setcdr entry (gnus-delete-alist 'touched score)))
  1288. (erase-buffer)
  1289. (let (emacs-lisp-mode-hook)
  1290. (if (and (not gnus-adaptive-pretty-print)
  1291. (string-match
  1292. (concat (regexp-quote gnus-adaptive-file-suffix) "$")
  1293. file))
  1294. ;; This is an adaptive score file, so we do not run it through
  1295. ;; `pp' unless requested. These files can get huge, and are
  1296. ;; not meant to be edited by human hands.
  1297. (gnus-prin1 score)
  1298. ;; This is a normal score file, so we print it very
  1299. ;; prettily.
  1300. (let ((lisp-mode-syntax-table score-mode-syntax-table))
  1301. (gnus-pp score))))
  1302. (gnus-make-directory (file-name-directory file))
  1303. ;; If the score file is empty, we delete it.
  1304. (if (zerop (buffer-size))
  1305. (delete-file file)
  1306. ;; There are scores, so we write the file.
  1307. (when (file-writable-p file)
  1308. (let ((coding-system-for-write score-mode-coding-system))
  1309. (gnus-write-buffer file))
  1310. (when gnus-score-after-write-file-function
  1311. (funcall gnus-score-after-write-file-function file)))))
  1312. (and gnus-score-uncacheable-files
  1313. (string-match gnus-score-uncacheable-files file)
  1314. (gnus-score-remove-from-cache file)))
  1315. (kill-buffer (current-buffer)))))
  1316. (defun gnus-score-load-files (score-files)
  1317. "Load all score files in SCORE-FILES."
  1318. ;; Load the score files.
  1319. (let (scores)
  1320. (while score-files
  1321. (if (stringp (car score-files))
  1322. ;; It is a string, which means that it's a score file name,
  1323. ;; so we load the score file and add the score alist to
  1324. ;; the list of alists.
  1325. (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
  1326. ;; It is an alist, so we just add it to the list directly.
  1327. (setq scores (nconc (car score-files) scores)))
  1328. (setq score-files (cdr score-files)))
  1329. ;; Prune the score files that are to be excluded, if any.
  1330. (when gnus-scores-exclude-files
  1331. (let ((s scores)
  1332. c)
  1333. (while s
  1334. (and (setq c (rassq (car s) gnus-score-cache))
  1335. (member (car c) gnus-scores-exclude-files)
  1336. (setq scores (delq (car s) scores)))
  1337. (setq s (cdr s)))))
  1338. scores))
  1339. (defun gnus-score-headers (score-files &optional trace)
  1340. ;; Score `gnus-newsgroup-headers'.
  1341. (let (scores news)
  1342. ;; PLM: probably this is not the best place to clear orphan-score
  1343. (setq gnus-orphan-score nil
  1344. gnus-scores-articles nil
  1345. gnus-scores-exclude-files nil
  1346. scores (gnus-score-load-files score-files))
  1347. (setq news scores)
  1348. ;; Do the scoring.
  1349. (while news
  1350. (setq scores news
  1351. news nil)
  1352. (when (and gnus-summary-default-score
  1353. scores)
  1354. (let* ((entries gnus-header-index)
  1355. (now (date-to-day (current-time-string)))
  1356. (expire (and gnus-score-expiry-days
  1357. (- now gnus-score-expiry-days)))
  1358. (headers gnus-newsgroup-headers)
  1359. (current-score-file gnus-current-score-file)
  1360. entry header new)
  1361. (gnus-message 7 "Scoring...")
  1362. ;; Create articles, an alist of the form `(HEADER . SCORE)'.
  1363. (while (setq header (pop headers))
  1364. ;; WARNING: The assq makes the function O(N*S) while it could
  1365. ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
  1366. ;; and S is (length gnus-newsgroup-scored).
  1367. (unless (assq (mail-header-number header) gnus-newsgroup-scored)
  1368. (setq gnus-scores-articles ;Total of 2 * N cons-cells used.
  1369. (cons (cons header (or gnus-summary-default-score 0))
  1370. gnus-scores-articles))))
  1371. (with-current-buffer (gnus-get-buffer-create "*Headers*")
  1372. (buffer-disable-undo)
  1373. (when (gnus-buffer-live-p gnus-summary-buffer)
  1374. (message-clone-locals gnus-summary-buffer))
  1375. ;; Set the global variant of this variable.
  1376. (setq gnus-current-score-file current-score-file)
  1377. ;; score orphans
  1378. (when gnus-orphan-score
  1379. (setq gnus-score-index
  1380. (nth 1 (assoc "references" gnus-header-index)))
  1381. (gnus-score-orphans gnus-orphan-score))
  1382. ;; Run each header through the score process.
  1383. (while entries
  1384. (setq entry (pop entries)
  1385. header (nth 0 entry)
  1386. gnus-score-index (nth 1 (assoc header gnus-header-index)))
  1387. (when (< 0 (apply 'max (mapcar
  1388. (lambda (score)
  1389. (length (gnus-score-get header score)))
  1390. scores)))
  1391. (when (if (and gnus-inhibit-slow-scoring
  1392. (or (eq gnus-inhibit-slow-scoring t)
  1393. (and (stringp gnus-inhibit-slow-scoring)
  1394. ;; Always true here?
  1395. ;; (stringp gnus-newsgroup-name)
  1396. (string-match
  1397. gnus-inhibit-slow-scoring
  1398. gnus-newsgroup-name)))
  1399. (> 0 (nth 1 (assoc header gnus-header-index))))
  1400. (progn
  1401. (gnus-message
  1402. 7 "Scoring on headers or body skipped.")
  1403. nil)
  1404. ;; Call the scoring function for this type of "header".
  1405. (setq new (funcall (nth 2 entry) scores header
  1406. now expire trace)))
  1407. (push new news))))
  1408. (when (gnus-buffer-live-p gnus-summary-buffer)
  1409. (let ((scored gnus-newsgroup-scored))
  1410. (with-current-buffer gnus-summary-buffer
  1411. (setq gnus-newsgroup-scored scored))))
  1412. ;; Remove the buffer.
  1413. (gnus-kill-buffer (current-buffer)))
  1414. ;; Add articles to `gnus-newsgroup-scored'.
  1415. (while gnus-scores-articles
  1416. (when (or (/= gnus-summary-default-score
  1417. (cdar gnus-scores-articles))
  1418. gnus-save-score)
  1419. (push (cons (mail-header-number (caar gnus-scores-articles))
  1420. (cdar gnus-scores-articles))
  1421. gnus-newsgroup-scored))
  1422. (setq gnus-scores-articles (cdr gnus-scores-articles)))
  1423. (let (score)
  1424. (while (setq score (pop scores))
  1425. (while score
  1426. (when (consp (caar score))
  1427. (gnus-score-advanced (car score) trace))
  1428. (pop score))))
  1429. (gnus-message 7 "Scoring...done"))))))
  1430. (defun gnus-score-lower-thread (thread score-adjust)
  1431. "Lower the score on THREAD with SCORE-ADJUST.
  1432. THREAD is expected to contain a list of the form `(PARENT [CHILD1
  1433. CHILD2 ...])' where PARENT is a header array and each CHILD is a list
  1434. of the same form as THREAD. The empty list nil is valid. For each
  1435. article in the tree, the score of the corresponding entry in
  1436. `gnus-newsgroup-scored' is adjusted by SCORE-ADJUST."
  1437. (while thread
  1438. (let ((head (car thread)))
  1439. (if (listp head)
  1440. ;; handle a child and its descendants
  1441. (gnus-score-lower-thread head score-adjust)
  1442. ;; handle the parent
  1443. (let* ((article (mail-header-number head))
  1444. (score (assq article gnus-newsgroup-scored)))
  1445. (if score (setcdr score (+ (cdr score) score-adjust))
  1446. (push (cons article score-adjust) gnus-newsgroup-scored)))))
  1447. (setq thread (cdr thread))))
  1448. (defun gnus-score-orphans (score)
  1449. "Score orphans.
  1450. A root is an article with no references. An orphan is an article
  1451. which has references, but is not connected via its references to a
  1452. root article. This function finds all the orphans, and adjusts their
  1453. score in `gnus-newsgroup-scored' by SCORE."
  1454. ;; gnus-make-threads produces a list, where each entry is a "thread"
  1455. ;; as described in the gnus-score-lower-thread docs. This function
  1456. ;; will be called again (after limiting has been done) if the display
  1457. ;; is threaded. It would be nice to somehow save this info and use
  1458. ;; it later.
  1459. (dolist (thread (gnus-make-threads))
  1460. (let ((id (aref (car thread) gnus-score-index)))
  1461. ;; If the parent of the thread is not a root, lower the score of
  1462. ;; it and its descendants. Note that some roots seem to satisfy
  1463. ;; (eq id nil) and some (eq id ""); not sure why.
  1464. (when (and id
  1465. (not (string= id "")))
  1466. (gnus-score-lower-thread thread score)))))
  1467. (defun gnus-score-integer (scores header now expire &optional trace)
  1468. (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
  1469. entries alist)
  1470. ;; Find matches.
  1471. (while scores
  1472. (setq alist (car scores)
  1473. scores (cdr scores)
  1474. entries (assoc header alist))
  1475. (while (cdr entries) ;First entry is the header index.
  1476. (let* ((rest (cdr entries))
  1477. (kill (car rest))
  1478. (match (nth 0 kill))
  1479. (type (or (nth 3 kill) '>))
  1480. (score (or (nth 1 kill) gnus-score-interactive-default-score))
  1481. (date (nth 2 kill))
  1482. (found nil)
  1483. (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
  1484. (eq type '>=) (eq type '=))
  1485. type
  1486. (error "Invalid match type: %s" type)))
  1487. (articles gnus-scores-articles))
  1488. ;; Instead of doing all the clever stuff that
  1489. ;; `gnus-score-string' does to minimize searches and stuff,
  1490. ;; I will assume that people generally will put so few
  1491. ;; matches on numbers that any cleverness will take more
  1492. ;; time than one would gain.
  1493. (while articles
  1494. (when (funcall match-func
  1495. (or (aref (caar articles) gnus-score-index) 0)
  1496. match)
  1497. (when trace
  1498. (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
  1499. gnus-score-trace))
  1500. (setq found t)
  1501. (setcdr (car articles) (+ score (cdar articles))))
  1502. (setq articles (cdr articles)))
  1503. ;; Update expire date
  1504. (cond ((null date)) ;Permanent entry.
  1505. ((and found gnus-update-score-entry-dates) ;Match, update date.
  1506. (gnus-score-set 'touched '(t) alist)
  1507. (setcar (nthcdr 2 kill) now))
  1508. ((and expire (< date expire)) ;Old entry, remove.
  1509. (gnus-score-set 'touched '(t) alist)
  1510. (setcdr entries (cdr rest))
  1511. (setq rest entries)))
  1512. (setq entries rest)))))
  1513. nil)
  1514. (defun gnus-score-date (scores header now expire &optional trace)
  1515. (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
  1516. entries alist match match-func article)
  1517. ;; Find matches.
  1518. (while scores
  1519. (setq alist (car scores)
  1520. scores (cdr scores)
  1521. entries (assoc header alist))
  1522. (while (cdr entries) ;First entry is the header index.
  1523. (let* ((rest (cdr entries))
  1524. (kill (car rest))
  1525. (type (or (nth 3 kill) 'before))
  1526. (score (or (nth 1 kill) gnus-score-interactive-default-score))
  1527. (date (nth 2 kill))
  1528. (found nil)
  1529. (articles gnus-scores-articles)
  1530. l)
  1531. (cond
  1532. ((eq type 'after)
  1533. (setq match-func 'string<
  1534. match (gnus-date-iso8601 (nth 0 kill))))
  1535. ((eq type 'before)
  1536. (setq match-func 'gnus-string>
  1537. match (gnus-date-iso8601 (nth 0 kill))))
  1538. ((eq type 'at)
  1539. (setq match-func 'string=
  1540. match (gnus-date-iso8601 (nth 0 kill))))
  1541. ((eq type 'regexp)
  1542. (setq match-func 'string-match
  1543. match (nth 0 kill)))
  1544. (t (error "Invalid match type: %s" type)))
  1545. ;; Instead of doing all the clever stuff that
  1546. ;; `gnus-score-string' does to minimize searches and stuff,
  1547. ;; I will assume that people generally will put so few
  1548. ;; matches on numbers that any cleverness will take more
  1549. ;; time than one would gain.
  1550. (while (setq article (pop articles))
  1551. (when (and
  1552. (setq l (aref (car article) gnus-score-index))
  1553. (funcall match-func match (gnus-date-iso8601 l)))
  1554. (when trace
  1555. (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
  1556. gnus-score-trace))
  1557. (setq found t)
  1558. (setcdr article (+ score (cdr article)))))
  1559. ;; Update expire date
  1560. (cond ((null date)) ;Permanent entry.
  1561. ((and found gnus-update-score-entry-dates) ;Match, update date.
  1562. (gnus-score-set 'touched '(t) alist)
  1563. (setcar (nthcdr 2 kill) now))
  1564. ((and expire (< date expire)) ;Old entry, remove.
  1565. (gnus-score-set 'touched '(t) alist)
  1566. (setcdr entries (cdr rest))
  1567. (setq rest entries)))
  1568. (setq entries rest)))))
  1569. nil)
  1570. (defun gnus-score-decode-text-parts ()
  1571. (gmm-labels
  1572. ((mm-text-parts
  1573. (handle)
  1574. (cond ((stringp (car handle))
  1575. (let ((parts (apply #'append
  1576. (mapcar #'mm-text-parts (cdr handle)))))
  1577. (if (equal "multipart/alternative" (car handle))
  1578. ;; pick the first supported alternative
  1579. (list (car parts))
  1580. parts)))
  1581. ((bufferp (car handle))
  1582. (when (string-match "^text/" (mm-handle-media-type handle))
  1583. (list handle)))
  1584. (t (apply #'append (mapcar #'mm-text-parts handle)))))
  1585. (my-mm-display-part
  1586. (handle)
  1587. (when handle
  1588. (save-restriction
  1589. (narrow-to-region (point) (point))
  1590. (mm-display-inline handle)
  1591. (goto-char (point-max))))))
  1592. (let (;(mm-text-html-renderer 'w3m-standalone)
  1593. (handles (mm-dissect-buffer t)))
  1594. (save-excursion
  1595. (article-goto-body)
  1596. (delete-region (point) (point-max))
  1597. (mapc #'my-mm-display-part (mm-text-parts handles))
  1598. handles))))
  1599. (defun gnus-score-body (scores header now expire &optional trace)
  1600. (if gnus-agent-fetching
  1601. nil
  1602. (save-excursion
  1603. (setq gnus-scores-articles
  1604. (sort gnus-scores-articles
  1605. (lambda (a1 a2)
  1606. (< (mail-header-number (car a1))
  1607. (mail-header-number (car a2))))))
  1608. (set-buffer nntp-server-buffer)
  1609. (save-restriction
  1610. (let* ((buffer-read-only nil)
  1611. (articles gnus-scores-articles)
  1612. (all-scores scores)
  1613. (request-func (cond ((string= "head" header)
  1614. 'gnus-request-head)
  1615. ((string= "body" header)
  1616. 'gnus-request-body)
  1617. (t 'gnus-request-article)))
  1618. entries alist ofunc article last)
  1619. (when articles
  1620. (setq last (mail-header-number (caar (last articles))))
  1621. ;; Not all backends support partial fetching. In that case,
  1622. ;; we just fetch the entire article.
  1623. ;; When scoring by body, we need to peek at the headers to detect
  1624. ;; the content encoding
  1625. (unless (or (gnus-check-backend-function
  1626. (and (string-match "^gnus-" (symbol-name request-func))
  1627. (intern (substring (symbol-name request-func)
  1628. (match-end 0))))
  1629. gnus-newsgroup-name)
  1630. (string= "body" header))
  1631. (setq ofunc request-func)
  1632. (setq request-func 'gnus-request-article))
  1633. (while articles
  1634. (setq article (mail-header-number (caar articles)))
  1635. (gnus-message 7 "Scoring article %s of %s..." article last)
  1636. (widen)
  1637. (let (handles)
  1638. (when (funcall request-func article gnus-newsgroup-name)
  1639. (when (string= "body" header)
  1640. (setq handles (gnus-score-decode-text-parts)))
  1641. (goto-char (point-min))
  1642. ;; If just parts of the article is to be searched, but the
  1643. ;; backend didn't support partial fetching, we just narrow
  1644. ;; to the relevant parts.
  1645. (when ofunc
  1646. (if (eq ofunc 'gnus-request-head)
  1647. (narrow-to-region
  1648. (point)
  1649. (or (search-forward "\n\n" nil t) (point-max)))
  1650. (narrow-to-region
  1651. (or (search-forward "\n\n" nil t) (point))
  1652. (point-max))))
  1653. (setq scores all-scores)
  1654. ;; Find matches.
  1655. (while scores
  1656. (setq alist (pop scores)
  1657. entries (assoc header alist))
  1658. (while (cdr entries) ;First entry is the header index.
  1659. (let* ((rest (cdr entries))
  1660. (kill (car rest))
  1661. (match (nth 0 kill))
  1662. (type (or (nth 3 kill) 's))
  1663. (score (or (nth 1 kill)
  1664. gnus-score-interactive-default-score))
  1665. (date (nth 2 kill))
  1666. (found nil)
  1667. (case-fold-search
  1668. (not (or (eq type 'R) (eq type 'S)
  1669. (eq type 'Regexp) (eq type 'String))))
  1670. (search-func
  1671. (cond ((or (eq type 'r) (eq type 'R)
  1672. (eq type 'regexp) (eq type 'Regexp))
  1673. 're-search-forward)
  1674. ((or (eq type 's) (eq type 'S)
  1675. (eq type 'string) (eq type 'String))
  1676. 'search-forward)
  1677. (t
  1678. (error "Invalid match type: %s" type)))))
  1679. (goto-char (point-min))
  1680. (when (funcall search-func match nil t)
  1681. ;; Found a match, update scores.
  1682. (setcdr (car articles) (+ score (cdar articles)))
  1683. (setq found t)
  1684. (when trace
  1685. (push
  1686. (cons (car-safe (rassq alist gnus-score-cache))
  1687. kill)
  1688. gnus-score-trace)))
  1689. ;; Update expire date
  1690. (unless trace
  1691. (cond
  1692. ((null date)) ;Permanent entry.
  1693. ((and found gnus-update-score-entry-dates)
  1694. ;; Match, update date.
  1695. (gnus-score-set 'touched '(t) alist)
  1696. (setcar (nthcdr 2 kill) now))
  1697. ((and expire (< date expire)) ;Old entry, remove.
  1698. (gnus-score-set 'touched '(t) alist)
  1699. (setcdr entries (cdr rest))
  1700. (setq rest entries))))
  1701. (setq entries rest))))
  1702. (when handles (mm-destroy-parts handles))))
  1703. (setq articles (cdr articles)))))))
  1704. nil))
  1705. (defun gnus-score-thread (scores header now expire &optional trace)
  1706. (gnus-score-followup scores header now expire trace t))
  1707. (defun gnus-score-followup (scores header now expire &optional trace thread)
  1708. (if gnus-agent-fetching
  1709. ;; FIXME: It seems doable in fetching mode.
  1710. nil
  1711. ;; Insert the unique article headers in the buffer.
  1712. (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
  1713. (current-score-file gnus-current-score-file)
  1714. (all-scores scores)
  1715. ;; gnus-score-index is used as a free variable.
  1716. alike last this art entries alist articles
  1717. new news)
  1718. ;; Change score file to the adaptive score file. All entries that
  1719. ;; this function makes will be put into this file.
  1720. (with-current-buffer gnus-summary-buffer
  1721. (gnus-score-load-file
  1722. (or gnus-newsgroup-adaptive-score-file
  1723. (gnus-score-file-name
  1724. gnus-newsgroup-name gnus-adaptive-file-suffix))))
  1725. (setq gnus-scores-articles (sort gnus-scores-articles
  1726. 'gnus-score-string<)
  1727. articles gnus-scores-articles)
  1728. (erase-buffer)
  1729. (while articles
  1730. (setq art (car articles)
  1731. this (aref (car art) gnus-score-index)
  1732. articles (cdr articles))
  1733. (if (equal last this)
  1734. (push art alike)
  1735. (when last
  1736. (insert last ?\n)
  1737. (put-text-property (1- (point)) (point) 'articles alike))
  1738. (setq alike (list art)
  1739. last this)))
  1740. (when last ; Bwadr, duplicate code.
  1741. (insert last ?\n)
  1742. (put-text-property (1- (point)) (point) 'articles alike))
  1743. ;; Find matches.
  1744. (while scores
  1745. (setq alist (car scores)
  1746. scores (cdr scores)
  1747. entries (assoc header alist))
  1748. (while (cdr entries) ;First entry is the header index.
  1749. (let* ((rest (cdr entries))
  1750. (kill (car rest))
  1751. (match (nth 0 kill))
  1752. (type (or (nth 3 kill) 's))
  1753. (score (or (nth 1 kill) gnus-score-interactive-default-score))
  1754. (date (nth 2 kill))
  1755. (found nil)
  1756. (mt (aref (symbol-name type) 0))
  1757. (case-fold-search
  1758. (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
  1759. (dmt (downcase mt))
  1760. (search-func
  1761. (cond ((= dmt ?r) 're-search-forward)
  1762. ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
  1763. (t (error "Invalid match type: %s" type))))
  1764. arts art)
  1765. (goto-char (point-min))
  1766. (if (= dmt ?e)
  1767. (while (funcall search-func match nil t)
  1768. (and (= (point-at-bol)
  1769. (match-beginning 0))
  1770. (= (progn (end-of-line) (point))
  1771. (match-end 0))
  1772. (progn
  1773. (setq found (setq arts (get-text-property
  1774. (point) 'articles)))
  1775. ;; Found a match, update scores.
  1776. (while arts
  1777. (setq art (car arts)
  1778. arts (cdr arts))
  1779. (gnus-score-add-followups
  1780. (car art) score all-scores thread))))
  1781. (end-of-line))
  1782. (while (funcall search-func match nil t)
  1783. (end-of-line)
  1784. (setq found (setq arts (get-text-property (point) 'articles)))
  1785. ;; Found a match, update scores.
  1786. (while (setq art (pop arts))
  1787. (setcdr art (+ score (cdr art)))
  1788. (when trace
  1789. (push (cons
  1790. (car-safe (rassq alist gnus-score-cache))
  1791. kill)
  1792. gnus-score-trace))
  1793. (when (setq new (gnus-score-add-followups
  1794. (car art) score all-scores thread))
  1795. (push new news)))))
  1796. ;; Update expire date
  1797. (cond ((null date)) ;Permanent entry.
  1798. ((and found gnus-update-score-entry-dates)
  1799. ;Match, update date.
  1800. (gnus-score-set 'touched '(t) alist)
  1801. (setcar (nthcdr 2 kill) now))
  1802. ((and expire (< date expire)) ;Old entry, remove.
  1803. (gnus-score-set 'touched '(t) alist)
  1804. (setcdr entries (cdr rest))
  1805. (setq rest entries)))
  1806. (setq entries rest))))
  1807. ;; We change the score file back to the previous one.
  1808. (with-current-buffer gnus-summary-buffer
  1809. (gnus-score-load-file current-score-file))
  1810. (list (cons "references" news)))))
  1811. (defun gnus-score-add-followups (header score scores &optional thread)
  1812. "Add a score entry to the adapt file."
  1813. (with-current-buffer gnus-summary-buffer
  1814. (let* ((id (mail-header-id header))
  1815. (scores (car scores))
  1816. entry dont)
  1817. ;; Don't enter a score if there already is one.
  1818. (while (setq entry (pop scores))
  1819. (and (equal "references" (car entry))
  1820. (or (null (nth 3 (cadr entry)))
  1821. (eq 's (nth 3 (cadr entry))))
  1822. (assoc id entry)
  1823. (setq dont t)))
  1824. (unless dont
  1825. (gnus-summary-score-entry
  1826. (if thread "thread" "references")
  1827. id 's score (current-time-string) nil t)))))
  1828. (defun gnus-score-string (score-list header now expire &optional trace)
  1829. ;; Score ARTICLES according to HEADER in SCORE-LIST.
  1830. ;; Update matching entries to NOW and remove unmatched entries older
  1831. ;; than EXPIRE.
  1832. ;; Insert the unique article headers in the buffer.
  1833. (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
  1834. ;; gnus-score-index is used as a free variable.
  1835. (simplify (and gnus-score-thread-simplify
  1836. (string= "subject" header)))
  1837. alike last this art entries alist articles
  1838. fuzzies arts words kill)
  1839. ;; Sorting the articles costs os O(N*log N) but will allow us to
  1840. ;; only match with each unique header. Thus the actual matching
  1841. ;; will be O(M*U) where M is the number of strings to match with,
  1842. ;; and U is the number of unique headers. It is assumed (but
  1843. ;; untested) this will be a net win because of the large constant
  1844. ;; factor involved with string matching.
  1845. (setq gnus-scores-articles
  1846. ;; We cannot string-sort the extra headers list. *sigh*
  1847. (if (= gnus-score-index 9)
  1848. gnus-scores-articles
  1849. (sort gnus-scores-articles 'gnus-score-string<))
  1850. articles gnus-scores-articles)
  1851. (erase-buffer)
  1852. (while (setq art (pop articles))
  1853. (setq this (aref (car art) gnus-score-index))
  1854. ;; If we're working with non-standard headers, we are stuck
  1855. ;; with working on them as a group. What a hassle.
  1856. ;; Just wait 'til you see what horrors we commit against `match'...
  1857. (if (= gnus-score-index 9)
  1858. (setq this (gnus-prin1-to-string this))) ; ick.
  1859. (if simplify
  1860. (setq this (gnus-map-function gnus-simplify-subject-functions this)))
  1861. (if (equal last this)
  1862. ;; O(N*H) cons-cells used here, where H is the number of
  1863. ;; headers.
  1864. (push art alike)
  1865. (when last
  1866. ;; Insert the line, with a text property on the
  1867. ;; terminating newline referring to the articles with
  1868. ;; this line.
  1869. (insert last ?\n)
  1870. (put-text-property (1- (point)) (point) 'articles alike))
  1871. (setq alike (list art)
  1872. last this)))
  1873. (when last ; Bwadr, duplicate code.
  1874. (insert last ?\n)
  1875. (put-text-property (1- (point)) (point) 'articles alike))
  1876. ;; Go through all the score alists and pick out the entries
  1877. ;; for this header.
  1878. (while score-list
  1879. (setq alist (pop score-list)
  1880. ;; There's only one instance of this header for
  1881. ;; each score alist.
  1882. entries (assoc header alist))
  1883. (while (cdr entries) ;First entry is the header index.
  1884. (let* ((kill (cadr entries))
  1885. (type (or (nth 3 kill) 's))
  1886. (score (or (nth 1 kill) gnus-score-interactive-default-score))
  1887. (date (nth 2 kill))
  1888. (extra (nth 4 kill)) ; non-standard header; string.
  1889. (found nil)
  1890. (mt (aref (symbol-name type) 0))
  1891. (case-fold-search (not (memq mt '(?R ?S ?E ?F))))
  1892. (dmt (downcase mt))
  1893. ;; Assume user already simplified regexp and fuzzies
  1894. (match (if (and simplify (not (memq dmt '(?f ?r))))
  1895. (gnus-map-function
  1896. gnus-simplify-subject-functions
  1897. (nth 0 kill))
  1898. (nth 0 kill)))
  1899. (search-func
  1900. (cond ((= dmt ?r) 're-search-forward)
  1901. ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
  1902. ((= dmt ?w) nil)
  1903. (t (error "Invalid match type: %s" type)))))
  1904. ;; Evil hackery to make match usable in non-standard headers.
  1905. (when extra
  1906. (setq match (concat "[ (](" extra " \\. \"\\([^\"]*\\\\\"\\)*[^\"]*"
  1907. (if (eq search-func 're-search-forward)
  1908. match
  1909. (regexp-quote match))
  1910. "\\([^\"]*\\\\\"\\)*[^\"]*\")[ )]")
  1911. search-func 're-search-forward)) ; XXX danger?!?
  1912. (cond
  1913. ;; Fuzzy matches. We save these for later.
  1914. ((= dmt ?f)
  1915. (push (cons entries alist) fuzzies)
  1916. (setq entries (cdr entries)))
  1917. ;; Word matches. Save these for even later.
  1918. ((= dmt ?w)
  1919. (push (cons entries alist) words)
  1920. (setq entries (cdr entries)))
  1921. ;; Exact matches.
  1922. ((= dmt ?e)
  1923. ;; Do exact matching.
  1924. (goto-char (point-min))
  1925. (while (and (not (eobp))
  1926. (funcall search-func match nil t))
  1927. ;; Is it really exact?
  1928. (and (eolp)
  1929. (= (point-at-bol) (match-beginning 0))
  1930. ;; Yup.
  1931. (progn
  1932. (setq found (setq arts (get-text-property
  1933. (point) 'articles)))
  1934. ;; Found a match, update scores.
  1935. (if trace
  1936. (while (setq art (pop arts))
  1937. (setcdr art (+ score (cdr art)))
  1938. (push
  1939. (cons
  1940. (car-safe (rassq alist gnus-score-cache))
  1941. kill)
  1942. gnus-score-trace))
  1943. (while (setq art (pop arts))
  1944. (setcdr art (+ score (cdr art)))))))
  1945. (forward-line 1))
  1946. ;; Update expiry date
  1947. (if trace
  1948. (setq entries (cdr entries))
  1949. (cond
  1950. ;; Permanent entry.
  1951. ((null date)
  1952. (setq entries (cdr entries)))
  1953. ;; We have a match, so we update the date.
  1954. ((and found gnus-update-score-entry-dates)
  1955. (gnus-score-set 'touched '(t) alist)
  1956. (setcar (nthcdr 2 kill) now)
  1957. (setq entries (cdr entries)))
  1958. ;; This entry has expired, so we remove it.
  1959. ((and expire (< date expire))
  1960. (gnus-score-set 'touched '(t) alist)
  1961. (setcdr entries (cddr entries)))
  1962. ;; No match; go to next entry.
  1963. (t
  1964. (setq entries (cdr entries))))))
  1965. ;; Regexp and substring matching.
  1966. (t
  1967. (goto-char (point-min))
  1968. (when (string= match "")
  1969. (setq match "\n"))
  1970. (while (and (not (eobp))
  1971. (funcall search-func match nil t))
  1972. (goto-char (match-beginning 0))
  1973. (end-of-line)
  1974. (setq found (setq arts (get-text-property (point) 'articles)))
  1975. ;; Found a match, update scores.
  1976. (if trace
  1977. (while (setq art (pop arts))
  1978. (setcdr art (+ score (cdr art)))
  1979. (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
  1980. gnus-score-trace))
  1981. (while (setq art (pop arts))
  1982. (setcdr art (+ score (cdr art)))))
  1983. (forward-line 1))
  1984. ;; Update expiry date
  1985. (if trace
  1986. (setq entries (cdr entries))
  1987. (cond
  1988. ;; Permanent entry.
  1989. ((null date)
  1990. (setq entries (cdr entries)))
  1991. ;; We have a match, so we update the date.
  1992. ((and found gnus-update-score-entry-dates)
  1993. (gnus-score-set 'touched '(t) alist)
  1994. (setcar (nthcdr 2 kill) now)
  1995. (setq entries (cdr entries)))
  1996. ;; This entry has expired, so we remove it.
  1997. ((and expire (< date expire))
  1998. (gnus-score-set 'touched '(t) alist)
  1999. (setcdr entries (cddr entries)))
  2000. ;; No match; go to next entry.
  2001. (t
  2002. (setq entries (cdr entries))))))))))
  2003. ;; Find fuzzy matches.
  2004. (when fuzzies
  2005. ;; Simplify the entire buffer for easy matching.
  2006. (gnus-simplify-buffer-fuzzy gnus-simplify-subject-fuzzy-regexp)
  2007. (while (setq kill (cadaar fuzzies))
  2008. (let* ((match (nth 0 kill))
  2009. (type (nth 3 kill))
  2010. (score (or (nth 1 kill) gnus-score-interactive-default-score))
  2011. (date (nth 2 kill))
  2012. (mt (aref (symbol-name type) 0))
  2013. (case-fold-search (not (= mt ?F)))
  2014. found)
  2015. (goto-char (point-min))
  2016. (while (and (not (eobp))
  2017. (search-forward match nil t))
  2018. (when (and (= (point-at-bol) (match-beginning 0))
  2019. (eolp))
  2020. (setq found (setq arts (get-text-property (point) 'articles)))
  2021. (if trace
  2022. (while (setq art (pop arts))
  2023. (setcdr art (+ score (cdr art)))
  2024. (push (cons
  2025. (car-safe (rassq (cdar fuzzies) gnus-score-cache))
  2026. kill)
  2027. gnus-score-trace))
  2028. ;; Found a match, update scores.
  2029. (while (setq art (pop arts))
  2030. (setcdr art (+ score (cdr art))))))
  2031. (forward-line 1))
  2032. ;; Update expiry date
  2033. (if (not trace)
  2034. (cond
  2035. ;; Permanent.
  2036. ((null date)
  2037. ;; Do nothing.
  2038. )
  2039. ;; Match, update date.
  2040. ((and found gnus-update-score-entry-dates)
  2041. (gnus-score-set 'touched '(t) (cdar fuzzies))
  2042. (setcar (nthcdr 2 kill) now))
  2043. ;; Old entry, remove.
  2044. ((and expire (< date expire))
  2045. (gnus-score-set 'touched '(t) (cdar fuzzies))
  2046. (setcdr (caar fuzzies) (cddaar fuzzies)))))
  2047. (setq fuzzies (cdr fuzzies)))))
  2048. (when words
  2049. ;; Enter all words into the hashtb.
  2050. (let ((hashtb (gnus-make-hashtable
  2051. (* 10 (count-lines (point-min) (point-max))))))
  2052. (gnus-enter-score-words-into-hashtb hashtb)
  2053. (while (setq kill (cadaar words))
  2054. (let* ((score (or (nth 1 kill) gnus-score-interactive-default-score))
  2055. (date (nth 2 kill))
  2056. found)
  2057. (when (setq arts (intern-soft (nth 0 kill) hashtb))
  2058. (setq arts (symbol-value arts))
  2059. (setq found t)
  2060. (if trace
  2061. (while (setq art (pop arts))
  2062. (setcdr art (+ score (cdr art)))
  2063. (push (cons
  2064. (car-safe (rassq (cdar words) gnus-score-cache))
  2065. kill)
  2066. gnus-score-trace))
  2067. ;; Found a match, update scores.
  2068. (while (setq art (pop arts))
  2069. (setcdr art (+ score (cdr art))))))
  2070. ;; Update expiry date
  2071. (if (not trace)
  2072. (cond
  2073. ;; Permanent.
  2074. ((null date)
  2075. ;; Do nothing.
  2076. )
  2077. ;; Match, update date.
  2078. ((and found gnus-update-score-entry-dates)
  2079. (gnus-score-set 'touched '(t) (cdar words))
  2080. (setcar (nthcdr 2 kill) now))
  2081. ;; Old entry, remove.
  2082. ((and expire (< date expire))
  2083. (gnus-score-set 'touched '(t) (cdar words))
  2084. (setcdr (caar words) (cddaar words)))))
  2085. (setq words (cdr words))))))
  2086. nil))
  2087. (defun gnus-enter-score-words-into-hashtb (hashtb)
  2088. ;; Find all the words in the buffer and enter them into
  2089. ;; the hashtable.
  2090. (let (word val)
  2091. (goto-char (point-min))
  2092. (with-syntax-table gnus-adaptive-word-syntax-table
  2093. (while (re-search-forward "\\b\\w+\\b" nil t)
  2094. (setq val
  2095. (gnus-gethash
  2096. (setq word (downcase (buffer-substring
  2097. (match-beginning 0) (match-end 0))))
  2098. hashtb))
  2099. (gnus-sethash
  2100. word
  2101. (append (get-text-property (point-at-eol) 'articles) val)
  2102. hashtb)))
  2103. ;; Make all the ignorable words ignored.
  2104. (let ((ignored (append gnus-ignored-adaptive-words
  2105. (if gnus-adaptive-word-no-group-words
  2106. (message-tokenize-header
  2107. (gnus-group-real-name gnus-newsgroup-name)
  2108. "."))
  2109. gnus-default-ignored-adaptive-words)))
  2110. (while ignored
  2111. (gnus-sethash (pop ignored) nil hashtb)))))
  2112. (defun gnus-score-string< (a1 a2)
  2113. ;; Compare headers in articles A2 and A2.
  2114. ;; The header index used is the free variable `gnus-score-index'.
  2115. (string-lessp (aref (car a1) gnus-score-index)
  2116. (aref (car a2) gnus-score-index)))
  2117. (defun gnus-current-score-file-nondirectory (&optional score-file)
  2118. (let ((score-file (or score-file gnus-current-score-file)))
  2119. (if score-file
  2120. (gnus-short-group-name (file-name-nondirectory score-file))
  2121. "none")))
  2122. (defun gnus-score-adaptive ()
  2123. "Create adaptive score rules for this newsgroup."
  2124. (when gnus-newsgroup-adaptive
  2125. ;; We change the score file to the adaptive score file.
  2126. (with-current-buffer gnus-summary-buffer
  2127. (gnus-score-load-file
  2128. (or gnus-newsgroup-adaptive-score-file
  2129. (gnus-home-score-file gnus-newsgroup-name t)
  2130. (gnus-score-file-name
  2131. gnus-newsgroup-name gnus-adaptive-file-suffix))))
  2132. ;; Perform ordinary line scoring.
  2133. (when (or (not (listp gnus-newsgroup-adaptive))
  2134. (memq 'line gnus-newsgroup-adaptive))
  2135. (save-excursion
  2136. (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
  2137. (alist malist)
  2138. (date (current-time-string))
  2139. (data gnus-newsgroup-data)
  2140. elem headers match func)
  2141. ;; First we transform the adaptive rule alist into something
  2142. ;; that's faster to process.
  2143. (while malist
  2144. (setq elem (car malist))
  2145. (when (symbolp (car elem))
  2146. (setcar elem (symbol-value (car elem))))
  2147. (setq elem (cdr elem))
  2148. (while elem
  2149. (when (fboundp
  2150. (setq func
  2151. (intern
  2152. (concat "mail-header-"
  2153. (if (eq (caar elem) 'followup)
  2154. "message-id"
  2155. (downcase (symbol-name (caar elem))))))))
  2156. (setcdr (car elem)
  2157. (cons (if (eq (caar elem) 'followup)
  2158. "references"
  2159. (symbol-name (caar elem)))
  2160. (cdar elem)))
  2161. (setcar (car elem)
  2162. `(lambda (h)
  2163. (,func h))))
  2164. (setq elem (cdr elem)))
  2165. (setq malist (cdr malist)))
  2166. ;; Then we score away.
  2167. (while data
  2168. (setq elem (cdr (assq (gnus-data-mark (car data)) alist)))
  2169. (if (or (not elem)
  2170. (gnus-data-pseudo-p (car data)))
  2171. ()
  2172. (when (setq headers (gnus-data-header (car data)))
  2173. (while elem
  2174. (setq match (funcall (caar elem) headers))
  2175. (gnus-summary-score-entry
  2176. (nth 1 (car elem)) match
  2177. (cond
  2178. ((numberp match)
  2179. '=)
  2180. ((equal (nth 1 (car elem)) "date")
  2181. 'a)
  2182. (t
  2183. ;; Whether we use substring or exact matches is
  2184. ;; controlled here.
  2185. (if (or (not gnus-score-exact-adapt-limit)
  2186. (< (length match) gnus-score-exact-adapt-limit))
  2187. 'e
  2188. (if (equal (nth 1 (car elem)) "subject")
  2189. 'f 's))))
  2190. (nth 2 (car elem)) date nil t)
  2191. (setq elem (cdr elem)))))
  2192. (setq data (cdr data))))))
  2193. ;; Perform adaptive word scoring.
  2194. (when (and (listp gnus-newsgroup-adaptive)
  2195. (memq 'word gnus-newsgroup-adaptive))
  2196. (with-temp-buffer
  2197. (let* ((hashtb (gnus-make-hashtable 1000))
  2198. (date (date-to-day (current-time-string)))
  2199. (data gnus-newsgroup-data)
  2200. word d score val)
  2201. (with-syntax-table gnus-adaptive-word-syntax-table
  2202. ;; Go through all articles.
  2203. (while (setq d (pop data))
  2204. (when (and
  2205. (not (gnus-data-pseudo-p d))
  2206. (setq score
  2207. (cdr (assq
  2208. (gnus-data-mark d)
  2209. gnus-adaptive-word-score-alist))))
  2210. ;; This article has a mark that should lead to
  2211. ;; adaptive word rules, so we insert the subject
  2212. ;; and find all words in that string.
  2213. (insert (mail-header-subject (gnus-data-header d)))
  2214. (downcase-region (point-min) (point-max))
  2215. (goto-char (point-min))
  2216. (while (re-search-forward "\\b\\w+\\b" nil t)
  2217. ;; Put the word and score into the hashtb.
  2218. (setq val (gnus-gethash (setq word (match-string 0))
  2219. hashtb))
  2220. (when (or (not gnus-adaptive-word-length-limit)
  2221. (> (length word)
  2222. gnus-adaptive-word-length-limit))
  2223. (setq val (+ score (or val 0)))
  2224. (if (and gnus-adaptive-word-minimum
  2225. (< val gnus-adaptive-word-minimum))
  2226. (setq val gnus-adaptive-word-minimum))
  2227. (gnus-sethash word val hashtb)))
  2228. (erase-buffer))))
  2229. ;; Make all the ignorable words ignored.
  2230. (let ((ignored (append gnus-ignored-adaptive-words
  2231. (if gnus-adaptive-word-no-group-words
  2232. (message-tokenize-header
  2233. (gnus-group-real-name
  2234. gnus-newsgroup-name)
  2235. "."))
  2236. gnus-default-ignored-adaptive-words)))
  2237. (while ignored
  2238. (gnus-sethash (pop ignored) nil hashtb)))
  2239. ;; Now we have all the words and scores, so we
  2240. ;; add these rules to the ADAPT file.
  2241. (set-buffer gnus-summary-buffer)
  2242. (mapatoms
  2243. (lambda (word)
  2244. (when (symbol-value word)
  2245. (gnus-summary-score-entry
  2246. "subject" (symbol-name word) 'w (symbol-value word)
  2247. date nil t)))
  2248. hashtb))))))
  2249. (defun gnus-score-edit-done ()
  2250. (let ((bufnam (buffer-file-name (current-buffer)))
  2251. (winconf gnus-prev-winconf))
  2252. (when winconf
  2253. (set-window-configuration winconf))
  2254. (gnus-score-remove-from-cache bufnam)
  2255. (gnus-score-load-file bufnam)
  2256. (run-hooks 'gnus-score-edit-done-hook)))
  2257. (defun gnus-score-find-trace ()
  2258. "Find all score rules that applies to the current article."
  2259. (interactive)
  2260. (let ((old-scored gnus-newsgroup-scored))
  2261. (let ((gnus-newsgroup-headers
  2262. (list (gnus-summary-article-header)))
  2263. (gnus-newsgroup-scored nil)
  2264. ;; Must be synced with `gnus-score-edit-file-at-point':
  2265. (frmt "%S [%s] -> %s\n")
  2266. trace
  2267. file)
  2268. (save-excursion
  2269. (nnheader-set-temp-buffer "*Score Trace*"))
  2270. (setq gnus-score-trace nil)
  2271. (gnus-possibly-score-headers 'trace)
  2272. (if (not (setq trace gnus-score-trace))
  2273. (gnus-error
  2274. 1 "No score rules apply to the current article (default score %d)."
  2275. gnus-summary-default-score)
  2276. (set-buffer "*Score Trace*")
  2277. ;; Use a keymap instead?
  2278. (local-set-key "q"
  2279. (lambda ()
  2280. (interactive)
  2281. (bury-buffer nil)
  2282. (gnus-summary-expand-window)))
  2283. (local-set-key "k"
  2284. (lambda ()
  2285. (interactive)
  2286. (kill-buffer (current-buffer))
  2287. (gnus-summary-expand-window)))
  2288. (local-set-key "e" (lambda ()
  2289. "Run `gnus-score-edit-file-at-point'."
  2290. (interactive)
  2291. (gnus-score-edit-file-at-point)))
  2292. (local-set-key "f" (lambda ()
  2293. "Run `gnus-score-edit-file-at-point'."
  2294. (interactive)
  2295. (gnus-score-edit-file-at-point 'format)))
  2296. (local-set-key "t" 'toggle-truncate-lines)
  2297. (setq truncate-lines t)
  2298. (dolist (entry trace)
  2299. (setq file (or (car entry)
  2300. ;; Must be synced with
  2301. ;; `gnus-score-edit-file-at-point':
  2302. "(non-file rule)"))
  2303. (insert
  2304. (format frmt
  2305. (cdr entry)
  2306. ;; Don't use `file-name-sans-extension' to see .SCORE and
  2307. ;; .ADAPT directly:
  2308. (file-name-nondirectory file)
  2309. (abbreviate-file-name file))))
  2310. (insert
  2311. (format "\nTotal score: %d"
  2312. (apply '+ (mapcar
  2313. (lambda (s)
  2314. (or (caddr s)
  2315. gnus-score-interactive-default-score))
  2316. trace))))
  2317. (insert
  2318. "\n\nQuick help:
  2319. Type `e' to edit score file corresponding to the score rule on current line,
  2320. `f' to format (pretty print) the score file and edit it,
  2321. `t' toggle to truncate long lines in this buffer,
  2322. `q' to quit, `k' to kill score trace buffer.
  2323. The first sexp on each line is the score rule, followed by the file name of
  2324. the score file and its full name, including the directory.")
  2325. (goto-char (point-min))
  2326. (gnus-configure-windows 'score-trace)))
  2327. (set-buffer gnus-summary-buffer)
  2328. (setq gnus-newsgroup-scored old-scored)))
  2329. (defun gnus-score-find-favourite-words ()
  2330. "List words used in scoring."
  2331. (interactive)
  2332. (let ((alists (gnus-score-load-files (gnus-all-score-files)))
  2333. alist rule rules kill)
  2334. ;; Go through all the score alists for this group
  2335. ;; and find all `w' rules.
  2336. (while (setq alist (pop alists))
  2337. (while (setq rule (pop alist))
  2338. (when (and (stringp (car rule))
  2339. (equal "subject" (downcase (pop rule))))
  2340. (while (setq kill (pop rule))
  2341. (when (memq (nth 3 kill) '(w W word Word))
  2342. (push (cons (or (nth 1 kill)
  2343. gnus-score-interactive-default-score)
  2344. (car kill))
  2345. rules))))))
  2346. (setq rules (sort rules (lambda (r1 r2)
  2347. (string-lessp (cdr r1) (cdr r2)))))
  2348. ;; Add up words that have appeared several times.
  2349. (let ((r rules))
  2350. (while (cdr r)
  2351. (if (equal (cdar r) (cdadr r))
  2352. (progn
  2353. (setcar (car r) (+ (caar r) (caadr r)))
  2354. (setcdr r (cddr r)))
  2355. (pop r))))
  2356. ;; Insert the words.
  2357. (nnheader-set-temp-buffer "*Score Words*")
  2358. (if (not (setq rules (sort rules (lambda (r1 r2) (> (car r1) (car r2))))))
  2359. (gnus-error 3 "No word score rules")
  2360. (while rules
  2361. (insert (format "%-5d: %s\n" (caar rules) (cdar rules)))
  2362. (pop rules))
  2363. (goto-char (point-min))
  2364. (gnus-configure-windows 'score-words))))
  2365. (defun gnus-summary-rescore ()
  2366. "Redo the entire scoring process in the current summary."
  2367. (interactive)
  2368. (gnus-score-save)
  2369. (setq gnus-score-cache nil)
  2370. (setq gnus-newsgroup-scored nil)
  2371. (gnus-possibly-score-headers)
  2372. (gnus-score-update-all-lines))
  2373. (defun gnus-score-flush-cache ()
  2374. "Flush the cache of score files."
  2375. (interactive)
  2376. (gnus-score-save)
  2377. (setq gnus-score-cache nil
  2378. gnus-score-alist nil
  2379. gnus-short-name-score-file-cache nil)
  2380. (gnus-message 6 "The score cache is now flushed"))
  2381. (gnus-add-shutdown 'gnus-score-close 'gnus)
  2382. (defvar gnus-score-file-alist-cache nil)
  2383. (defun gnus-score-close ()
  2384. "Clear all internal score variables."
  2385. (setq gnus-score-cache nil
  2386. gnus-internal-global-score-files nil
  2387. gnus-score-file-list nil
  2388. gnus-score-file-alist-cache nil))
  2389. ;; Summary score marking commands.
  2390. (defun gnus-summary-raise-same-subject-and-select (score)
  2391. "Raise articles which has the same subject with SCORE and select the next."
  2392. (interactive "p")
  2393. (let ((subject (gnus-summary-article-subject)))
  2394. (gnus-summary-raise-score score)
  2395. (while (gnus-summary-find-subject subject)
  2396. (gnus-summary-raise-score score))
  2397. (gnus-summary-next-article t)))
  2398. (defun gnus-summary-raise-same-subject (score)
  2399. "Raise articles which has the same subject with SCORE."
  2400. (interactive "p")
  2401. (let ((subject (gnus-summary-article-subject)))
  2402. (gnus-summary-raise-score score)
  2403. (while (gnus-summary-find-subject subject)
  2404. (gnus-summary-raise-score score))
  2405. (gnus-summary-next-subject 1 t)))
  2406. (defun gnus-score-delta-default (level)
  2407. (if level (prefix-numeric-value level)
  2408. gnus-score-interactive-default-score))
  2409. (defun gnus-summary-raise-thread (&optional score)
  2410. "Raise the score of the articles in the current thread with SCORE."
  2411. (interactive "P")
  2412. (setq score (gnus-score-delta-default score))
  2413. (let (e)
  2414. (save-excursion
  2415. (let ((articles (gnus-summary-articles-in-thread)))
  2416. (while articles
  2417. (gnus-summary-goto-subject (car articles))
  2418. (gnus-summary-raise-score score)
  2419. (setq articles (cdr articles))))
  2420. (setq e (point)))
  2421. (let ((gnus-summary-check-current t))
  2422. (unless (zerop (gnus-summary-next-subject 1 t))
  2423. (goto-char e))))
  2424. (gnus-summary-recenter)
  2425. (gnus-summary-position-point)
  2426. (gnus-set-mode-line 'summary))
  2427. (defun gnus-summary-lower-same-subject-and-select (score)
  2428. "Raise articles which has the same subject with SCORE and select the next."
  2429. (interactive "p")
  2430. (gnus-summary-raise-same-subject-and-select (- score)))
  2431. (defun gnus-summary-lower-same-subject (score)
  2432. "Raise articles which has the same subject with SCORE."
  2433. (interactive "p")
  2434. (gnus-summary-raise-same-subject (- score)))
  2435. (defun gnus-summary-lower-thread (&optional score)
  2436. "Lower score of articles in the current thread with SCORE."
  2437. (interactive "P")
  2438. (gnus-summary-raise-thread (- (gnus-score-delta-default score))))
  2439. ;;; Finding score files.
  2440. (defun gnus-score-score-files (group)
  2441. "Return a list of all possible score files."
  2442. ;; Search and set any global score files.
  2443. (when gnus-global-score-files
  2444. (unless gnus-internal-global-score-files
  2445. (gnus-score-search-global-directories gnus-global-score-files)))
  2446. ;; Fix the kill-file dir variable.
  2447. (setq gnus-kill-files-directory
  2448. (file-name-as-directory gnus-kill-files-directory))
  2449. ;; If we can't read it, there are no score files.
  2450. (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
  2451. (setq gnus-score-file-list nil)
  2452. (if (not (gnus-use-long-file-name 'not-score))
  2453. ;; We do not use long file names, so we have to do some
  2454. ;; directory traversing.
  2455. (setq gnus-score-file-list
  2456. (cons nil
  2457. (or gnus-short-name-score-file-cache
  2458. (prog2
  2459. (gnus-message 6 "Finding all score files...")
  2460. (setq gnus-short-name-score-file-cache
  2461. (gnus-score-score-files-1
  2462. gnus-kill-files-directory))
  2463. (gnus-message 6 "Finding all score files...done")))))
  2464. ;; We want long file names.
  2465. (when (or (not gnus-score-file-list)
  2466. (not (car gnus-score-file-list))
  2467. (gnus-file-newer-than gnus-kill-files-directory
  2468. (car gnus-score-file-list)))
  2469. (setq gnus-score-file-list
  2470. (cons (nth 5 (file-attributes gnus-kill-files-directory))
  2471. (nreverse
  2472. (directory-files
  2473. gnus-kill-files-directory t
  2474. (gnus-score-file-regexp)))))))
  2475. (cdr gnus-score-file-list)))
  2476. (defun gnus-score-score-files-1 (dir)
  2477. "Return all possible score files under DIR."
  2478. (let ((files (list (expand-file-name dir)))
  2479. (regexp (gnus-score-file-regexp))
  2480. (case-fold-search nil)
  2481. seen out file)
  2482. (while (setq file (pop files))
  2483. (cond
  2484. ;; Ignore files that start with a dot.
  2485. ((string-match "^\\." (file-name-nondirectory file))
  2486. nil)
  2487. ;; Add subtrees of directory to also be searched.
  2488. ((and (file-directory-p file)
  2489. (not (member (file-truename file) seen)))
  2490. (push (file-truename file) seen)
  2491. (setq files (nconc (directory-files file t nil t) files)))
  2492. ;; Add files to the list of score files.
  2493. ((string-match regexp file)
  2494. (push file out))))
  2495. (or out
  2496. ;; Return a dummy value.
  2497. (list (expand-file-name "this.file.does.not.exist.SCORE"
  2498. gnus-kill-files-directory)))))
  2499. (defun gnus-score-file-regexp ()
  2500. "Return a regexp that match all score files."
  2501. (concat "\\(" (regexp-quote gnus-score-file-suffix )
  2502. "\\|" (regexp-quote gnus-adaptive-file-suffix) "\\)\\'"))
  2503. (defun gnus-score-find-bnews (group)
  2504. "Return a list of score files for GROUP.
  2505. The score files are those files in the ~/News/ directory which matches
  2506. GROUP using BNews sys file syntax."
  2507. (let* ((sfiles (append (gnus-score-score-files group)
  2508. gnus-internal-global-score-files))
  2509. (kill-dir (file-name-as-directory
  2510. (expand-file-name gnus-kill-files-directory)))
  2511. (klen (length kill-dir))
  2512. (score-regexp (gnus-score-file-regexp))
  2513. (trans (cdr (assq ?: nnheader-file-name-translation-alist)))
  2514. (group-trans (nnheader-translate-file-chars group t))
  2515. ofiles not-match regexp)
  2516. (with-current-buffer (gnus-get-buffer-create "*gnus score files*")
  2517. (buffer-disable-undo)
  2518. ;; Go through all score file names and create regexp with them
  2519. ;; as the source.
  2520. (while sfiles
  2521. (erase-buffer)
  2522. (insert (car sfiles))
  2523. (goto-char (point-min))
  2524. ;; First remove the suffix itself.
  2525. (when (re-search-forward (concat "." score-regexp) nil t)
  2526. (replace-match "" t t)
  2527. (goto-char (point-min))
  2528. (if (looking-at (regexp-quote kill-dir))
  2529. ;; If the file name was just "SCORE", `klen' is one character
  2530. ;; too much.
  2531. (delete-char (min (1- (point-max)) klen))
  2532. (goto-char (point-max))
  2533. (if (re-search-backward gnus-directory-sep-char-regexp nil t)
  2534. (delete-region (1+ (point)) (point-min))
  2535. (gnus-message 1 "Can't find directory separator in %s"
  2536. (car sfiles))))
  2537. ;; If short file names were used, we have to translate slashes.
  2538. (goto-char (point-min))
  2539. (let ((regexp (concat
  2540. "[/:" (if trans (char-to-string trans)) "]")))
  2541. (while (re-search-forward regexp nil t)
  2542. (replace-match "." t t)))
  2543. ;; Kludge to get rid of "nntp+" problems.
  2544. (goto-char (point-min))
  2545. (when (looking-at "nn[a-z]+\\+")
  2546. (search-forward "+")
  2547. (forward-char -1)
  2548. (insert "\\")
  2549. (forward-char 1))
  2550. ;; Kludge to deal with "++".
  2551. (while (search-forward "+" nil t)
  2552. (replace-match "\\+" t t))
  2553. ;; Translate "all" to ".*".
  2554. (goto-char (point-min))
  2555. (while (search-forward "all" nil t)
  2556. (replace-match ".*" t t))
  2557. (goto-char (point-min))
  2558. ;; Deal with "not."s.
  2559. (if (looking-at "not.")
  2560. (progn
  2561. (setq not-match t)
  2562. (setq regexp
  2563. (concat "^" (buffer-substring 5 (point-max)) "$")))
  2564. (setq regexp (concat "^" (buffer-substring 1 (point-max)) "$"))
  2565. (setq not-match nil))
  2566. ;; Finally - if this resulting regexp matches the group name,
  2567. ;; we add this score file to the list of score files
  2568. ;; applicable to this group.
  2569. (when (or (and not-match
  2570. (ignore-errors
  2571. (not (string-match regexp group-trans))))
  2572. (and (not not-match)
  2573. (ignore-errors (string-match regexp group-trans))))
  2574. (push (car sfiles) ofiles)))
  2575. (setq sfiles (cdr sfiles)))
  2576. (gnus-kill-buffer (current-buffer))
  2577. ;; Slight kludge here - the last score file returned should be
  2578. ;; the local score file, whether it exists or not. This is so
  2579. ;; that any score commands the user enters will go to the right
  2580. ;; file, and not end up in some global score file.
  2581. (let ((localscore (gnus-score-file-name group)))
  2582. (setq ofiles (cons localscore (delete localscore ofiles))))
  2583. (gnus-sort-score-files (nreverse ofiles)))))
  2584. (defun gnus-score-find-single (group)
  2585. "Return list containing the score file for GROUP."
  2586. (list (or gnus-newsgroup-adaptive-score-file
  2587. (gnus-score-file-name group gnus-adaptive-file-suffix))
  2588. (gnus-score-file-name group)))
  2589. (defun gnus-score-find-hierarchical (group)
  2590. "Return list of score files for GROUP.
  2591. This includes the score file for the group and all its parents."
  2592. (let* ((prefix (gnus-group-real-prefix group))
  2593. (all (list nil))
  2594. (group (gnus-group-real-name group))
  2595. (start 0))
  2596. (while (string-match "\\." group (1+ start))
  2597. (setq start (match-beginning 0))
  2598. (push (substring group 0 start) all))
  2599. (push group all)
  2600. (setq all
  2601. (nconc
  2602. (mapcar (lambda (group)
  2603. (gnus-score-file-name group gnus-adaptive-file-suffix))
  2604. (setq all (nreverse all)))
  2605. (mapcar 'gnus-score-file-name all)))
  2606. (if (equal prefix "")
  2607. all
  2608. (mapcar
  2609. (lambda (file)
  2610. (nnheader-translate-file-chars
  2611. (concat (file-name-directory file) prefix
  2612. (file-name-nondirectory file))))
  2613. all))))
  2614. (defun gnus-score-file-rank (file)
  2615. "Return a number that says how specific score FILE is.
  2616. Destroys the current buffer."
  2617. (if (member file gnus-internal-global-score-files)
  2618. 0
  2619. (when (string-match
  2620. (concat "^" (regexp-quote
  2621. (expand-file-name
  2622. (file-name-as-directory gnus-kill-files-directory))))
  2623. file)
  2624. (setq file (substring file (match-end 0))))
  2625. (insert file)
  2626. (goto-char (point-min))
  2627. (let ((beg (point))
  2628. elems)
  2629. (while (re-search-forward "[./]" nil t)
  2630. (push (buffer-substring beg (1- (point)))
  2631. elems))
  2632. (erase-buffer)
  2633. (setq elems (delete "all" elems))
  2634. (length elems))))
  2635. (defun gnus-sort-score-files (files)
  2636. "Sort FILES so that the most general files come first."
  2637. (with-temp-buffer
  2638. (let ((alist
  2639. (mapcar
  2640. (lambda (file)
  2641. (cons (inline (gnus-score-file-rank file)) file))
  2642. files)))
  2643. (mapcar 'cdr (sort alist 'car-less-than-car)))))
  2644. (defun gnus-score-find-alist (group)
  2645. "Return list of score files for GROUP.
  2646. The list is determined from the variable `gnus-score-file-alist'."
  2647. (let ((alist gnus-score-file-multiple-match-alist)
  2648. score-files)
  2649. ;; if this group has been seen before, return the cached entry
  2650. (if (setq score-files (assoc group gnus-score-file-alist-cache))
  2651. (cdr score-files) ;ensures caching groups with no matches
  2652. ;; handle the multiple match alist
  2653. (while alist
  2654. (when (string-match (caar alist) group)
  2655. (setq score-files (append (cdar alist) score-files)))
  2656. (setq alist (cdr alist)))
  2657. (setq alist gnus-score-file-single-match-alist)
  2658. ;; handle the single match alist
  2659. (while alist
  2660. (when (string-match (caar alist) group)
  2661. ;; progn used just in case ("regexp") has no files
  2662. ;; and score-files is still nil. -sj
  2663. ;; this can be construed as a "stop searching here" feature :>
  2664. ;; and used to simplify regexps in the single-alist
  2665. (setq score-files (append (cdar alist) score-files))
  2666. (setq alist nil))
  2667. (setq alist (cdr alist)))
  2668. ;; cache the score files
  2669. (push (cons group score-files) gnus-score-file-alist-cache)
  2670. score-files)))
  2671. (defun gnus-all-score-files (&optional group)
  2672. "Return a list of all score files for the current group."
  2673. (let ((funcs gnus-score-find-score-files-function)
  2674. (group (or group gnus-newsgroup-name))
  2675. score-files)
  2676. (when group
  2677. ;; Make sure funcs is a list.
  2678. (and funcs
  2679. (not (listp funcs))
  2680. (setq funcs (list funcs)))
  2681. (when gnus-score-use-all-scores
  2682. ;; Get the initial score files for this group.
  2683. (when funcs
  2684. (setq score-files (copy-sequence (gnus-score-find-alist group))))
  2685. ;; Add any home adapt files.
  2686. (let ((home (gnus-home-score-file group t)))
  2687. (when home
  2688. (push home score-files)
  2689. (setq gnus-newsgroup-adaptive-score-file home)))
  2690. ;; Check whether there is a `adapt-file' group parameter.
  2691. (let ((param-file (gnus-group-find-parameter group 'adapt-file)))
  2692. (when param-file
  2693. (push param-file score-files)
  2694. (setq gnus-newsgroup-adaptive-score-file param-file))))
  2695. ;; Go through all the functions for finding score files (or actual
  2696. ;; scores) and add them to a list.
  2697. (while funcs
  2698. (when (functionp (car funcs))
  2699. (setq score-files
  2700. (append score-files
  2701. (nreverse (funcall (car funcs) group)))))
  2702. (setq funcs (cdr funcs)))
  2703. (when gnus-score-use-all-scores
  2704. ;; Add any home score files.
  2705. (let ((home (gnus-home-score-file group)))
  2706. (when home
  2707. (push home score-files)))
  2708. ;; Check whether there is a `score-file' group parameter.
  2709. (let ((param-file (gnus-group-find-parameter group 'score-file)))
  2710. (when param-file
  2711. (push param-file score-files))))
  2712. ;; Expand all files names.
  2713. (let ((files score-files))
  2714. (while files
  2715. (when (stringp (car files))
  2716. (setcar files (expand-file-name
  2717. (car files) gnus-kill-files-directory)))
  2718. (pop files)))
  2719. (setq score-files (nreverse score-files))
  2720. ;; Remove any duplicate score files.
  2721. (while (and score-files
  2722. (member (car score-files) (cdr score-files)))
  2723. (pop score-files))
  2724. (let ((files score-files))
  2725. (while (cdr files)
  2726. (if (member (cadr files) (cddr files))
  2727. (setcdr files (cddr files))
  2728. (pop files))))
  2729. ;; Do the scoring if there are any score files for this group.
  2730. score-files)))
  2731. (defun gnus-possibly-score-headers (&optional trace)
  2732. "Do scoring if scoring is required."
  2733. (let ((score-files (gnus-all-score-files)))
  2734. (when score-files
  2735. (gnus-score-headers score-files trace))))
  2736. (defun gnus-score-file-name (newsgroup &optional suffix)
  2737. "Return the name of a score file for NEWSGROUP."
  2738. (let ((suffix (or suffix gnus-score-file-suffix)))
  2739. (nnheader-translate-file-chars
  2740. (cond
  2741. ((or (null newsgroup)
  2742. (string-equal newsgroup ""))
  2743. ;; The global score file is placed at top of the directory.
  2744. (expand-file-name suffix gnus-kill-files-directory))
  2745. ((gnus-use-long-file-name 'not-score)
  2746. ;; Append ".SCORE" to newsgroup name.
  2747. (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
  2748. "." suffix)
  2749. gnus-kill-files-directory))
  2750. (t
  2751. ;; Place "SCORE" under the hierarchical directory.
  2752. (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
  2753. "/" suffix)
  2754. gnus-kill-files-directory))))))
  2755. (defun gnus-score-search-global-directories (files)
  2756. "Scan all global score directories for score files."
  2757. ;; Set the variable `gnus-internal-global-score-files' to all
  2758. ;; available global score files.
  2759. (interactive (list gnus-global-score-files))
  2760. (let (out)
  2761. (while files
  2762. ;; #### /$ Unix-specific?
  2763. (if (file-directory-p (car files))
  2764. (setq out (nconc (directory-files
  2765. (car files) t
  2766. (concat (gnus-score-file-regexp) "$"))))
  2767. (push (car files) out))
  2768. (setq files (cdr files)))
  2769. (setq gnus-internal-global-score-files out)))
  2770. (defun gnus-score-default-fold-toggle ()
  2771. "Toggle folding for new score file entries."
  2772. (interactive)
  2773. (setq gnus-score-default-fold (not gnus-score-default-fold))
  2774. (if gnus-score-default-fold
  2775. (gnus-message 1 "New score file entries will be case insensitive.")
  2776. (gnus-message 1 "New score file entries will be case sensitive.")))
  2777. ;;; Home score file.
  2778. (defun gnus-home-score-file (group &optional adapt)
  2779. "Return the home score file for GROUP.
  2780. If ADAPT, return the home adaptive file instead."
  2781. (let ((list (if adapt gnus-home-adapt-file gnus-home-score-file))
  2782. elem found)
  2783. ;; Make sure we have a list.
  2784. (unless (listp list)
  2785. (setq list (list list)))
  2786. ;; Go through the list and look for matches.
  2787. (while (and (not found)
  2788. (setq elem (pop list)))
  2789. (setq found
  2790. (cond
  2791. ;; Simple string.
  2792. ((stringp elem)
  2793. elem)
  2794. ;; Function.
  2795. ((functionp elem)
  2796. (funcall elem group))
  2797. ;; Regexp-file cons.
  2798. ((consp elem)
  2799. (when (string-match (gnus-globalify-regexp (car elem)) group)
  2800. (replace-match (cadr elem) t nil group))))))
  2801. (when found
  2802. (setq found (nnheader-translate-file-chars found))
  2803. (if (file-name-absolute-p found)
  2804. found
  2805. (nnheader-concat gnus-kill-files-directory found)))))
  2806. (defun gnus-hierarchial-home-score-file (group)
  2807. "Return the score file of the top-level hierarchy of GROUP."
  2808. (if (string-match "^[^.]+\\." group)
  2809. (concat (match-string 0 group) gnus-score-file-suffix)
  2810. ;; Group name without any dots.
  2811. (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
  2812. gnus-score-file-suffix)))
  2813. (defun gnus-hierarchial-home-adapt-file (group)
  2814. "Return the adapt file of the top-level hierarchy of GROUP."
  2815. (if (string-match "^[^.]+\\." group)
  2816. (concat (match-string 0 group) gnus-adaptive-file-suffix)
  2817. ;; Group name without any dots.
  2818. (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
  2819. gnus-adaptive-file-suffix)))
  2820. (defun gnus-current-home-score-file (group)
  2821. "Return the \"current\" regular score file."
  2822. (car (gnus-score-find-alist group)))
  2823. ;;;
  2824. ;;; Score decays
  2825. ;;;
  2826. (defun gnus-decay-score (score)
  2827. "Decay SCORE according to `gnus-score-decay-constant' and `gnus-score-decay-scale'."
  2828. (let ((n (- score
  2829. (* (if (< score 0) -1 1)
  2830. (min (abs score)
  2831. (max gnus-score-decay-constant
  2832. (* (abs score)
  2833. gnus-score-decay-scale)))))))
  2834. (if (and (featurep 'xemacs)
  2835. ;; XEmacs's floor can handle only the floating point
  2836. ;; number below the half of the maximum integer.
  2837. (> (abs n) (lsh -1 -2)))
  2838. (string-to-number
  2839. (car (split-string (number-to-string n) "\\.")))
  2840. (floor n))))
  2841. (defun gnus-decay-scores (alist day)
  2842. "Decay non-permanent scores in ALIST."
  2843. (let ((times (- (time-to-days (current-time)) day))
  2844. kill entry updated score n)
  2845. (unless (zerop times) ;Done decays today already?
  2846. (while (setq entry (pop alist))
  2847. (when (stringp (car entry))
  2848. (setq entry (cdr entry))
  2849. (while (setq kill (pop entry))
  2850. (when (nth 2 kill)
  2851. (setq updated t)
  2852. (setq score (or (nth 1 kill)
  2853. gnus-score-interactive-default-score)
  2854. n times)
  2855. (while (natnump (decf n))
  2856. (setq score (funcall gnus-decay-score-function score)))
  2857. (setcdr kill (cons score
  2858. (cdr (cdr kill)))))))))
  2859. ;; Return whether this score file needs to be saved. By Je-haysuss!
  2860. updated))
  2861. (provide 'gnus-score)
  2862. ;;; gnus-score.el ends here