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