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