gnus-score.el 106 KB

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