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