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