*** empty log message ***
[gnus] / lisp / gnus-score.el
1 ;;; gnus-score.el --- scoring code for Gnus
2 ;; Copyright (C) 1995 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus)
29
30 (defvar gnus-score-expiry-days 7
31   "*Number of days before unused score file entries are expired.")
32
33 (defvar gnus-orphan-score nil
34   "*All orphans get this score added. Set in the score file.")
35
36 (defvar gnus-default-adaptive-score-alist
37   '((gnus-unread-mark)
38     (gnus-ticked-mark (from 4))
39     (gnus-dormant-mark (from 5))
40     (gnus-del-mark (from -4) (subject -1))
41     (gnus-read-mark (from 4) (subject 2))
42     (gnus-expirable-mark (from -1) (subject -1))
43     (gnus-killed-mark (from -1) (subject -3))
44     (gnus-kill-file-mark)
45     (gnus-catchup-mark (from -1) (subject -1)))
46   "*Alist of marks and scores.")
47
48 (defvar gnus-score-mimic-keymap nil
49   "*Have the score entry functions pretend that they are a keymap.")
50
51 (defvar gnus-score-exact-adapt-limit nil
52   "*Number that says how long a match has to be before using substring matching.
53 When doing adaptive scoring, one normally uses substring matching.
54 However, if the header one matches is short, the possibility for false
55 positives is great, so if the length of the match is less than this
56 variable, exact matching will be used.
57
58 If this variable is nil, which it is by default, exact matching will
59 always be used.")
60
61 \f
62
63 ;; Internal variables.
64
65 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
66
67 (defvar gnus-score-trace nil)
68
69 (defvar gnus-score-alist nil
70   "Alist containing score information.
71 The keys can be symbols or strings.  The following symbols are defined. 
72
73 touched: If this alist has been modified.
74 mark:    Automatically mark articles below this.
75 expunge: Automatically expunge articles below this.
76 files:   List of other SCORE files to load when loading this one.
77 eval:    Sexp to be evaluated when the score file is loaded.
78
79 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...) 
80 where HEADER is the header being scored, MATCH is the string we are
81 looking for, TYPE is a flag indicating whether it should use regexp or
82 substring matching, SCORE is the score to add and DATE is the date
83 of the last succesful match.")
84
85 (defvar gnus-score-cache nil)
86 (defvar gnus-scores-articles nil)
87 (defvar gnus-scores-exclude-files nil)
88 (defvar gnus-header-index nil)
89 (defvar gnus-score-index nil)
90
91 (autoload 'gnus-uu-ctl-map "gnus-uu" nil nil 'keymap)
92
93 ;;; Summary mode score maps.
94
95 (defvar gnus-summary-score-map nil)
96
97 (define-prefix-command 'gnus-summary-score-map)
98 (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
99 (define-key gnus-summary-score-map "s" 'gnus-summary-set-score)
100 (define-key gnus-summary-score-map "a" 'gnus-summary-score-entry)
101 (define-key gnus-summary-score-map "S" 'gnus-summary-current-score)
102 (define-key gnus-summary-score-map "c" 'gnus-score-change-score-file)
103 (define-key gnus-summary-score-map "m" 'gnus-score-set-mark-below)
104 (define-key gnus-summary-score-map "x" 'gnus-score-set-expunge-below)
105 (define-key gnus-summary-score-map "e" 'gnus-score-edit-alist)
106 (define-key gnus-summary-score-map "f" 'gnus-score-edit-file)
107 (define-key gnus-summary-score-map "t" 'gnus-score-find-trace)
108 (define-key gnus-summary-score-map "C" 'gnus-score-customize)
109
110
111
112 ;; Summary score file commands
113
114 ;; Much modification of the kill (ahem, score) code and lots of the
115 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
116
117 (defun gnus-summary-lower-score (score)
118   (interactive "P")
119   (gnus-summary-increase-score (- (gnus-score-default score))))
120
121 (defun gnus-summary-increase-score (score)
122   (interactive "P")
123   (gnus-set-global-variables)
124   (let* ((nscore (gnus-score-default score))
125          (prefix (if (< nscore 0) ?L ?I))
126          (increase (> nscore 0))
127          (char-to-header 
128           '((?a "from" nil nil string)
129             (?s "subject" nil nil string)
130             (?b "body" "" nil string)
131             (?h "head" "" nil string)
132             (?i "message-id" nil t string)
133             (?t "references" "message-id" t string)
134             (?x "xref" nil nil string)
135             (?l "lines" nil nil number)
136             (?d "date" nil nil date)
137             (?f "followup" nil nil string)))
138          (char-to-type
139           '((?s s "substring" string)
140             (?e e "exact string" string)
141             (?f f "fuzzy string" string)
142             (?r r "regexp string" string)
143             (?b before "before date" date)
144             (?a at "at date" date) 
145             (?n now "this date" date)
146             (?< < "less than number" number)
147             (?> > "greater than number" number) 
148             (?= = "equal to number" number)))
149          (char-to-perm
150           (list (list ?t (current-time-string) "temporary") 
151                 '(?p perm "permanent") '(?i now "immediate")))
152          (mimic gnus-score-mimic-keymap)
153          hchar entry temporary tchar pchar end type)
154     ;; First we read the header to score.
155     (while (not hchar)
156       (if mimic
157           (progn 
158             (sit-for 1)
159             (message "%c-" prefix))
160         (message "%s header (%s?): " (if increase "Increase" "Lower")
161                  (mapconcat (lambda (s) (char-to-string (car s)))
162                             char-to-header "")))
163       (setq hchar (read-char))
164       (if (not (or (= hchar ??) (= hchar ?\C-h)))
165           ()
166         (setq hchar nil)
167         (gnus-score-insert-help "Match on header" char-to-header 1)))
168
169     (and (get-buffer "*Score Help*")
170          (progn
171            (delete-windows-on "*Score Help*")
172            (kill-buffer "*Score Help*")))
173
174     (or (setq entry (assq (downcase hchar) char-to-header))
175         (progn
176           (ding)
177           (setq end t)
178           (if mimic (message "%c %c" prefix hchar) (message ""))))
179     (if (or end (/= (downcase hchar) hchar))
180         (progn
181           ;; This was a majuscle, so we end reading and set the defaults.
182           (if mimic (message "%c %c" prefix hchar) (message ""))
183           (setq type nil
184                 temporary (current-time-string)))
185
186       ;; We continue reading - the type.
187       (while (not tchar)
188         (if mimic
189             (progn
190               (sit-for 1)
191               (message "%c %c-" prefix hchar))
192           (message "%s header '%s' with match type (%s?): "
193                    (if increase "Increase" "Lower")
194                    (nth 1 entry)
195                    (mapconcat (lambda (s) 
196                                 (if (eq (nth 4 entry) 
197                                         (nth 3 s))
198                                     (char-to-string (car s))
199                                   ""))
200                               char-to-type "")))
201         (setq tchar (read-char))
202         (if (not (or (= tchar ??) (= tchar ?\C-h)))
203             ()
204           (setq tchar nil)
205           (gnus-score-insert-help "Match type" char-to-type 2)))
206
207       (and (get-buffer "*Score Help*")
208            (progn
209              (delete-windows-on "*Score Help*")
210              (kill-buffer "*Score Help*")))
211       
212       (or (setq type (nth 1 (assq (downcase tchar) char-to-type)))
213           (progn
214             (ding)
215             (if mimic (message "%c %c" prefix hchar) (message ""))
216             (setq end t)))
217       (if (or end (/= (downcase tchar) tchar))
218           (progn
219             ;; It was a majuscle, so we end reading and the the default.
220             (if mimic (message "%c %c %c" prefix hchar tchar)
221               (message ""))
222             (setq temporary (current-time-string)))
223
224         ;; We continue reading.
225         (while (not pchar)
226           (if mimic
227               (progn
228                 (sit-for 1)
229                 (message "%c %c %c-" prefix hchar tchar))
230             (message "%s permanence (%s?): " (if increase "Increase" "Lower")
231                      (mapconcat (lambda (s) (char-to-string (car s)))
232                                 char-to-perm "")))
233           (setq pchar (read-char))
234           (if (not (or (= pchar ??) (= pchar ?\C-h)))
235               ()
236             (setq pchar nil)
237             (gnus-score-insert-help "Match permanence" char-to-perm 2)))
238
239         (and (get-buffer "*Score Help*")
240              (progn
241                (delete-windows-on "*Score Help*")
242                (kill-buffer "*Score Help*")))
243
244         (if mimic (message "%c %c %c" prefix hchar tchar pchar)
245           (message ""))
246         (if (setq temporary (nth 1 (assq pchar char-to-perm)))
247             ()
248           (ding)
249           (setq end t)
250           (if mimic 
251               (message "%c %c %c %c" prefix hchar tchar pchar)
252             (message "")))))
253     ;; We have all the data, so we enter this score.
254     (if end
255         ()
256       (gnus-summary-score-entry
257        (nth 1 entry)                    ; Header
258        (gnus-summary-header (or (nth 2 entry) (nth 1 entry))) ; Match
259        type                             ; Type
260        (if (eq 's score) nil score)     ; Score
261        (if (eq 'perm temporary)         ; Temp
262            nil
263          temporary)
264        (not (nth 3 entry)))             ; Prompt
265       )))
266
267 (defun gnus-score-insert-help (string alist idx)
268   (save-excursion
269     (pop-to-buffer "*Score Help*")
270     (buffer-disable-undo (current-buffer))
271     (erase-buffer)
272     (insert string ":\n\n")
273     (while alist
274       (insert (format " %c: %s\n" (car (car alist)) (nth idx (car alist))))
275       (setq alist (cdr alist)))))
276
277 (defun gnus-summary-header (header)
278   ;; Return HEADER for current articles, or error.
279   (let ((article (gnus-summary-article-number)))
280     (if article
281         (aref (gnus-get-header-by-number article)
282               (nth 1 (assoc header gnus-header-index)))
283       (error "No article on current line"))))
284
285 (defun gnus-summary-score-entry 
286   (header match type score date &optional prompt silent)
287   "Enter score file entry.
288 HEADER is the header being scored.
289 MATCH is the string we are looking for.
290 TYPE is a flag indicating if it is a regexp or substring.
291 SCORE is the score to add.
292 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
293 If optional argument `PROMPT' is non-nil, allow user to edit match.
294 If optional argument `SILENT' is nil, show effect of score entry."
295   (interactive
296    (list (completing-read "Header: "
297                           gnus-header-index
298                           (lambda (x) (fboundp (nth 2 x)))
299                           t)
300          (read-string "Match: ")
301          (if (y-or-n-p "Use regexp match? ") 'r 's)
302          (and current-prefix-arg
303              (prefix-numeric-value current-prefix-arg))
304          (cond ((not (y-or-n-p "Add to SCORE file? "))
305                 'now)
306                ((y-or-n-p "Expire kill? ")
307                 (current-time-string))
308                (t nil))))
309   (if (or (eq type 'r) (eq type 's))
310       (setq match (gnus-simplify-subject-re match)))
311   (let ((score (gnus-score-default score))
312         (header (downcase header)))
313     (and prompt (setq match (read-string 
314                              (format "Match %s on %s, %s: " 
315                                      (cond ((eq date 'now)
316                                             "now")
317                                            ((stringp date)
318                                             "temp")
319                                            (t "permanent"))
320                                      header
321                                      (if (< score 0) "lower" "raise"))
322                              (if (numberp match)
323                                  (int-to-string match)
324                                match))))
325     (and (>= (nth 1 (assoc header gnus-header-index)) 0)
326          (eq (nth 2 (assoc header gnus-header-index))
327              'gnus-score-string)
328          (not silent)
329          (gnus-summary-score-effect header match type score))
330     (if (eq date 'now)
331         ()
332       (and (= score gnus-score-interactive-default-score)
333            (setq score nil))
334       (let ((new (cond 
335                   ((eq type 'f)
336                    (list (gnus-simplify-subject-fuzzy match)
337                          score (and date (gnus-day-number date)) type))
338                   (type
339                    (list match score (and date (gnus-day-number date)) type))
340                   (date
341                    (list match score (gnus-day-number date)))
342                   (score
343                    (list match score))
344                   (t
345                    (list match))))
346             (old (gnus-score-get header))
347             elem)
348         ;; We see whether we can collapse some score entries.
349         ;; This isn't quite correct, because there may be more elements
350         ;; later on with the same key that have matching elems... Hm.
351         (if (and old
352                  (setq elem (assoc match old))
353                  (eq (nth 3 elem) (nth 3 new))
354                  (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
355                      (and (not (nth 2 elem)) (not (nth 2 new)))))
356             ;; Yup, we just add this new score to the old elem.
357             (setcar (cdr elem) (+ (or (nth 1 elem) 
358                                       gnus-score-interactive-default-score)
359                                   (or (nth 1 new)
360                                       gnus-score-interactive-default-score)))
361           ;; Nope, we have to add a new elem.
362           (gnus-score-set header (if old (cons new old) (list new)))))
363       (gnus-score-set 'touched '(t)))))
364
365 (defun gnus-summary-score-effect (header match type score)
366   "Simulate the effect of a score file entry.
367 HEADER is the header being scored.
368 MATCH is the string we are looking for.
369 TYPE is a flag indicating if it is a regexp or substring.
370 SCORE is the score to add."
371   (interactive (list (completing-read "Header: "
372                                       gnus-header-index
373                                       (lambda (x) (fboundp (nth 2 x)))
374                                       t)
375                      (read-string "Match: ")
376                      (y-or-n-p "Use regexp match? ")
377                      (prefix-numeric-value current-prefix-arg)))
378   (save-excursion
379     (or (and (stringp match) (> (length match) 0))
380       (error "No match"))
381     (goto-char (point-min))
382     (let ((regexp (cond ((eq type 'f)
383                          (gnus-simplify-subject-fuzzy match))
384                         (type match)
385                         (t (concat "\\`.*" (regexp-quote match) ".*\\'")))))
386       (while (not (eobp))
387         (let ((content (gnus-summary-header header))
388               (case-fold-search t))
389           (and content
390                (if (if (eq type 'f)
391                        (string-equal (gnus-simplify-subject-fuzzy content)
392                                      regexp)
393                      (string-match regexp content))
394                    (gnus-summary-raise-score score))))
395         (beginning-of-line 2)))))
396
397 (defun gnus-summary-score-crossposting (score date)
398    ;; Enter score file entry for current crossposting.
399    ;; SCORE is the score to add.
400    ;; DATE is the expire date.
401    (let ((xref (gnus-summary-header "xref"))
402          (start 0)
403          group)
404      (or xref (error "This article is not crossposted"))
405      (while (string-match " \\([^ \t]+\\):" xref start)
406        (setq start (match-end 0))
407        (if (not (string= 
408                  (setq group 
409                        (substring xref (match-beginning 1) (match-end 1)))
410                  gnus-newsgroup-name))
411            (gnus-summary-score-entry
412             "xref" (concat " " group ":") nil score date t)))))
413
414 \f
415 ;;;
416 ;;; Gnus Score Files
417 ;;;
418
419 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
420
421 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
422 (defun gnus-score-set-mark-below (score)
423   "Automatically mark articles with score below SCORE as read."
424   (interactive 
425    (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
426              (string-to-int (read-string "Mark below: ")))))
427   (setq score (or score gnus-summary-default-score 0))
428   (gnus-score-set 'mark (list score))
429   (gnus-score-set 'touched '(t))
430   (setq gnus-summary-mark-below score)
431   (gnus-summary-update-lines))
432
433 (defun gnus-score-set-expunge-below (score)
434   "Automatically expunge articles with score below SCORE."
435   (interactive 
436    (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
437              (string-to-int (read-string "Expunge below: ")))))
438   (setq score (or score gnus-summary-default-score 0))
439   (gnus-score-set 'expunge (list score))
440   (gnus-score-set 'touched '(t)))
441
442 (defun gnus-score-set (symbol value &optional alist)
443   ;; Set SYMBOL to VALUE in ALIST.
444   (let* ((alist 
445           (or alist 
446               gnus-score-alist
447               (progn
448                 (gnus-score-load (gnus-score-file-name gnus-newsgroup-name))
449                 gnus-score-alist)))
450          (entry (assoc symbol alist)))
451     (cond ((gnus-score-get 'read-only alist)
452            ;; This is a read-only score file, so we do nothing.
453            )
454           (entry
455            (setcdr entry value))
456           ((null alist)
457            (error "Empty alist"))
458           (t
459            (setcdr alist
460                    (cons (cons symbol value) (cdr alist)))))))
461
462 (defun gnus-score-get (symbol &optional alist)
463   ;; Get SYMBOL's definition in ALIST.
464   (cdr (assoc symbol 
465               (or alist 
466                   gnus-score-alist
467                   (progn
468                     (gnus-score-load 
469                      (gnus-score-file-name gnus-newsgroup-name))
470                     gnus-score-alist)))))
471
472 (defun gnus-score-change-score-file (file)
473   "Change current score alist."
474   (interactive (list (completing-read "Score file: " gnus-score-cache)))
475   (gnus-score-load-file file)
476   (gnus-set-mode-line 'summary))
477
478 (defun gnus-score-edit-alist (file)
479   "Edit the current score alist."
480   (interactive (list gnus-current-score-file))
481   (let ((winconf (current-window-configuration)))
482     (and (buffer-name gnus-summary-buffer) (gnus-score-save))
483     (gnus-configure-windows 'article)
484     (pop-to-buffer (find-file-noselect file))
485     (gnus-score-mode)
486     (make-local-variable 'gnus-prev-winconf)
487     (setq gnus-prev-winconf winconf))
488   (gnus-message 
489    4 (substitute-command-keys 
490       "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits")))
491   
492 (defun gnus-score-edit-file (file)
493   "Edit a score file."
494   (interactive 
495    (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
496   (and (buffer-name gnus-summary-buffer) (gnus-score-save))
497   (let ((winconf (current-window-configuration)))
498     (gnus-configure-windows 'article)
499     (pop-to-buffer (find-file-noselect file))
500     (gnus-score-mode)
501     (make-local-variable 'gnus-prev-winconf)
502     (setq gnus-prev-winconf winconf))
503   (gnus-message 
504    4 (substitute-command-keys 
505       "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits")))
506   
507 (defun gnus-score-load-file (file)
508   ;; Load score file FILE.  Returns a list a retrieved score-alists.
509   (setq gnus-kill-files-directory (or gnus-kill-files-directory "~/News/"))
510   (let* ((file (expand-file-name 
511                 (or (and (string-match
512                           (concat "^" (expand-file-name
513                                        gnus-kill-files-directory)) 
514                           (expand-file-name file))
515                          file)
516                     (concat gnus-kill-files-directory file))))
517          (cached (assoc file gnus-score-cache))
518          (global (member file gnus-internal-global-score-files))
519          lists alist)
520     (if cached
521         ;; The score file was already loaded.
522         (setq alist (cdr cached))
523       ;; We load the score file.
524       (setq gnus-score-alist nil)
525       (setq alist (gnus-score-load-score-alist file))
526       ;; We add '(touched) to the alist to signify that it hasn't been
527       ;; touched (yet). 
528       (or (assq 'touched alist) (setq alist (cons (list 'touched nil) alist)))
529       ;; If it is a global score file, we make it read-only.
530       (and global
531            (not (assq 'read-only alist))
532            (setq alist (cons (list 'read-only t) alist)))
533       ;; Update cache.
534       (setq gnus-score-cache
535             (cons (cons file alist) gnus-score-cache)))
536     ;; If there are actual scores in the alist, we add it to the
537     ;; return value of this function.
538     (if (memq t (mapcar (lambda (e) (stringp (car e))) alist))
539         (setq lists (list alist)))
540     ;; Treat the other possible atoms in the score alist.
541     (let ((mark (car (gnus-score-get 'mark alist)))
542           (expunge (car (gnus-score-get 'expunge alist)))
543           (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
544           (read-only (gnus-score-get 'read-only alist))
545           (files (gnus-score-get 'files alist))
546           (exclude-files (gnus-score-get 'exclude-files alist))
547           (orphan (car (gnus-score-get 'orphan alist)))
548           (adapt (gnus-score-get 'adapt alist))
549           (local (gnus-score-get 'local alist))
550           (eval (car (gnus-score-get 'eval alist))))
551       ;; We do not respect eval and files atoms from global score
552       ;; files. 
553       (and files (not global)
554            (setq lists (apply 'append lists
555                               (mapcar (lambda (file)
556                                         (gnus-score-load-file file)) 
557                                       files))))
558       (and eval (not global) (eval eval))
559       (setq gnus-scores-exclude-files exclude-files)
560       (if (not local)
561           ()
562         (save-excursion
563           (set-buffer gnus-summary-buffer)
564           (while local
565             (and (consp (car local))
566                  (symbolp (car (car local)))
567                  (progn
568                    (make-local-variable (car (car local)))
569                    (set (car (car local)) (nth 1 (car local)))))
570             (setq local (cdr local)))))
571       (if orphan (setq gnus-orphan-score orphan))
572       (setq gnus-adaptive-score-alist
573             (cond ((equal adapt '(t))
574                    (setq gnus-newsgroup-adaptive t)
575                    gnus-default-adaptive-score-alist)
576                   ((equal adapt '(ignore))
577                    (setq gnus-newsgroup-adaptive nil))
578                   ((consp adapt)
579                    (setq gnus-newsgroup-adaptive t)
580                    adapt)
581                   (t
582                    (setq gnus-newsgroup-adaptive gnus-use-adaptive-scoring)
583                    gnus-default-adaptive-score-alist)))
584       (setq gnus-summary-mark-below 
585             (or mark mark-and-expunge gnus-summary-mark-below))
586       (setq gnus-summary-expunge-below 
587             (or expunge mark-and-expunge gnus-summary-expunge-below)))
588     (setq gnus-current-score-file file)
589     (setq gnus-score-alist alist)
590     lists))
591
592 (defun gnus-score-load (file)
593   ;; Load score FILE.
594   (let ((cache (assoc file gnus-score-cache)))
595     (if cache
596         (setq gnus-score-alist (cdr cache))
597       (setq gnus-score-alist nil)
598       (gnus-score-load-score-alist file)
599       (or gnus-score-alist
600           (setq gnus-score-alist (copy-alist '((touched nil)))))
601       (setq gnus-score-cache
602             (cons (cons file gnus-score-alist) gnus-score-cache)))))
603
604 (defun gnus-score-remove-from-cache (file)
605   (setq gnus-score-cache 
606         (delq (assoc file gnus-score-cache) gnus-score-cache)))
607
608 (defun gnus-score-load-score-alist (file)
609   (let (alist)
610     (if (file-readable-p file)
611         (progn
612           (save-excursion
613             (gnus-set-work-buffer)
614             (insert-file-contents file)
615             (goto-char (point-min))
616             ;; Only do the loading if the score file isn't empty.
617             (if (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
618                 (setq alist
619                       (condition-case ()
620                           (read (current-buffer))
621                         (error 
622                          (progn
623                            (gnus-message 3 "Problem with score file %s" file)
624                            (ding) 
625                            (sit-for 2)
626                            nil))))))
627           (if (eq (car alist) 'setq)
628               (setq gnus-score-alist (gnus-score-transform-old-to-new alist))
629             (setq gnus-score-alist alist))
630           (setq gnus-score-alist
631                 (gnus-score-check-syntax gnus-score-alist file)))
632       (setq gnus-score-alist nil))))
633
634 (defun gnus-score-check-syntax (alist file)
635   (cond 
636    ((null alist)
637     nil)
638    ((not (consp alist))
639     (gnus-message 1 "Score file is not a list: %s" file)
640     (ding)
641     nil)
642    (t
643     (let ((a alist)
644           err)
645       (while (and a (not err))
646         (cond ((not (listp (car a)))
647                (gnus-message 3 "Illegal score element %s in %s" (car a) file)
648                (setq err t))
649               ((and (stringp (car (car a)))
650                     (not (listp (nth 1 (car a)))))
651                (gnus-message 3 "Illegal header match %s in %s" (nth 1 (car a)) file)
652                (setq err t))
653               (t
654                (setq a (cdr a)))))
655       (if err
656           (progn
657             (ding)
658             nil)
659         alist)))))    
660
661 (defun gnus-score-transform-old-to-new (alist)
662   (let* ((alist (nth 2 alist))
663          out entry)
664     (if (eq (car alist) 'quote)
665         (setq alist (nth 1 alist)))
666     (while alist
667       (setq entry (car alist))
668       (if (stringp (car entry))
669           (let ((scor (cdr entry)))
670             (setq out (cons entry out))
671             (while scor
672               (setcar scor
673                       (list (car (car scor)) (nth 2 (car scor))
674                             (and (nth 3 (car scor))
675                                  (gnus-day-number (nth 3 (car scor))))
676                             (if (nth 1 (car scor)) 'r 's)))
677               (setq scor (cdr scor))))
678         (setq out (cons (if (not (listp (cdr entry))) 
679                             (list (car entry) (cdr entry))
680                           entry)
681                         out)))
682       (setq alist (cdr alist)))
683     (cons (list 'touched t) (nreverse out))))
684   
685 (defun gnus-score-save ()
686   ;; Save all SCORE information.
687   (let ((cache gnus-score-cache))
688     (save-excursion
689       (setq gnus-score-alist nil)
690       (set-buffer (get-buffer-create "*Score*"))
691       (buffer-disable-undo (current-buffer))
692       (let (entry score file)
693         (while cache
694           (setq entry (car cache)
695                 cache (cdr cache)
696                 file (car entry)
697                 score (cdr entry))
698           (if (or (not (equal (gnus-score-get 'touched score) '(t)))
699                   (gnus-score-get 'read-only score)
700                   (and (file-exists-p file)
701                        (not (file-writable-p file))))
702               ()
703             (setq score (setcdr entry (delq (assq 'touched score) score)))
704             (erase-buffer)
705             (let (emacs-lisp-mode-hook)
706               (if (string-match (concat gnus-adaptive-file-suffix "$") file)
707                   ;; This is an adaptive score file, so we do not run
708                   ;; it through `pp'.  These files can get huge, and
709                   ;; are not meant to be edited by human hands.
710                   (insert (format "%S" score))
711                 ;; This is a normal score file, so we print it very
712                 ;; prettily. 
713                 (pp score (current-buffer))))
714             (gnus-make-directory (file-name-directory file))
715             (write-region (point-min) (point-max) file nil 'silent))))
716       (kill-buffer (current-buffer)))))
717   
718 (defun gnus-score-headers (score-files &optional trace)
719   ;; Score `gnus-newsgroup-headers'.
720   (let (scores)
721     ;; PLM: probably this is not the best place to clear orphan-score
722     (setq gnus-orphan-score nil)
723     ;; Load the SCORE files.
724     (while score-files
725       (if (stringp (car score-files))
726           ;; It is a string, which means that it's a score file name,
727           ;; so we load the score file and add the score alist to
728           ;; the list of alists.
729           (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
730         ;; It is an alist, so we just add it to the list directly.
731         (setq scores (nconc (car score-files) scores)))
732       (setq score-files (cdr score-files)))
733     ;; Prune the score files that are to be excluded, if any.
734     (if (not gnus-scores-exclude-files)
735         ()
736       (let ((s scores)
737             c)
738         (while s
739           (and (setq c (rassq (car s) gnus-score-cache))
740                (member (car c) gnus-scores-exclude-files)
741                (setq scores (delq (car s) scores)))
742           (setq s (cdr s)))))
743     (if (not (and gnus-summary-default-score
744                   scores
745                   (> (length gnus-newsgroup-headers)
746                      (length gnus-newsgroup-scored))))
747         ()
748       (let* ((entries gnus-header-index)
749              (now (gnus-day-number (current-time-string)))
750              (expire (- now gnus-score-expiry-days))
751              (headers gnus-newsgroup-headers)
752              (current-score-file gnus-current-score-file)
753              entry header)
754         (gnus-message 5 "Scoring...")
755         ;; Create articles, an alist of the form `(HEADER . SCORE)'.
756         (while headers
757           (setq header (car headers)
758                 headers (cdr headers))
759           ;; WARNING: The assq makes the function O(N*S) while it could
760           ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
761           ;; and S is (length gnus-newsgroup-scored).
762           (or (assq (header-number header) gnus-newsgroup-scored)
763               (setq gnus-scores-articles       ;Total of 2 * N cons-cells used.
764                     (cons (cons header (or gnus-summary-default-score 0))
765                           gnus-scores-articles))))
766
767         (save-excursion
768           (set-buffer (get-buffer-create "*Headers*"))
769           (buffer-disable-undo (current-buffer))
770
771           ;; Set the global variant of this variable.
772           (setq gnus-current-score-file current-score-file)
773           ;; score orphans
774           (if gnus-orphan-score 
775               (progn
776                 (setq gnus-score-index 
777                       (nth 1 (assoc "references" gnus-header-index)))
778                 (gnus-score-orphans gnus-orphan-score)))
779           ;; Run each header through the score process.
780           (while entries
781             (setq entry (car entries)
782                   header (nth 0 entry)
783                   entries (cdr entries))
784             (setq gnus-score-index (nth 1 (assoc header gnus-header-index)))
785             (if (< 0 (apply 'max (mapcar
786                                   (lambda (score)
787                                     (length (gnus-score-get header score)))
788                                   scores)))
789                 (funcall (nth 2 entry) scores header now expire trace)))
790           ;; Remove the buffer.
791           (kill-buffer (current-buffer)))
792
793         ;; Add articles to `gnus-newsgroup-scored'.
794         (while gnus-scores-articles
795           (or (= gnus-summary-default-score (cdr (car gnus-scores-articles)))
796               (setq gnus-newsgroup-scored
797                     (cons (cons (header-number 
798                                  (car (car gnus-scores-articles)))
799                                 (cdr (car gnus-scores-articles)))
800                           gnus-newsgroup-scored)))
801           (setq gnus-scores-articles (cdr gnus-scores-articles)))
802
803         (gnus-message 5 "Scoring...done")))))
804
805
806 (defun gnus-get-new-thread-ids (articles)
807   (let ((index (nth 1 (assoc "message-id" gnus-header-index)))
808         (refind gnus-score-index)
809         id-list art this tref)
810     (while articles
811       (setq art (car articles)
812             this (aref (car art) index)
813             tref (aref (car art) refind)
814             articles (cdr articles))
815       (if (string-equal tref "")        ;no references line
816           (setq id-list (cons this id-list))))
817     id-list))
818
819 ;; Orphan functions written by plm@atcmp.nl (Peter Mutsaers).
820 (defun gnus-score-orphans (score)
821   (let ((new-thread-ids (gnus-get-new-thread-ids gnus-scores-articles))
822         (index (nth 1 (assoc "references" gnus-header-index)))
823         alike articles art arts this last this-id)
824     
825     (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
826           articles gnus-scores-articles)
827
828     ;;more or less the same as in gnus-score-string
829     (erase-buffer)
830     (while articles
831       (setq art (car articles)
832             this (aref (car art) gnus-score-index)
833             articles (cdr articles))
834       ;;completely skip if this is empty (not a child, so not an orphan)
835       (if (not (string= this ""))
836           (if (equal last this)
837               ;; O(N*H) cons-cells used here, where H is the number of
838               ;; headers.
839               (setq alike (cons art alike))
840             (if last
841                 (progn
842                   ;; Insert the line, with a text property on the
843                   ;; terminating newline refering to the articles with
844                   ;; this line.
845                   (insert last ?\n)
846                   (put-text-property (1- (point)) (point) 'articles alike)))
847             (setq alike (list art)
848                   last this))))
849     (and last                           ; Bwadr, duplicate code.
850          (progn
851            (insert last ?\n)                    
852            (put-text-property (1- (point)) (point) 'articles alike)))
853
854     ;; PLM: now delete those lines that contain an entry from new-thread-ids
855     (while new-thread-ids
856       (setq this-id (car new-thread-ids)
857             new-thread-ids (cdr new-thread-ids))
858       (goto-char (point-min))
859       (while (search-forward this-id nil t)
860         ;; found a match. remove this line
861         (beginning-of-line)
862         (kill-line 1)))
863
864     ;; now for each line: update its articles with score by moving to
865     ;; every end-of-line in the buffer and read the articles property
866     (goto-char (point-min))
867     (while (eq 0 (progn
868                    (end-of-line)
869                    (setq arts (get-text-property (point) 'articles))
870                    (while arts
871                      (setq art (car arts)
872                            arts (cdr arts))
873                      (setcdr art (+ score (cdr art))))
874                    (forward-line))))))
875              
876
877 (defun gnus-score-integer (scores header now expire &optional trace)
878   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
879         alike last this art entries alist articles)
880
881     ;; Find matches.
882     (while scores
883       (setq alist (car scores)
884             scores (cdr scores)
885             entries (assoc header alist))
886       (while (cdr entries)              ;First entry is the header index.
887         (let* ((rest (cdr entries))             
888                (kill (car rest))
889                (match (nth 0 kill))
890                (type (or (nth 3 kill) '>))
891                (score (or (nth 1 kill) gnus-score-interactive-default-score))
892                (date (nth 2 kill))
893                (found nil)
894                (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
895                                    (eq type '>=) (eq type '=))
896                                type
897                              (error "Illegal match type: %s" type)))
898                (articles gnus-scores-articles)
899                arts art)
900           ;; Instead of doing all the clever stuff that
901           ;; `gnus-score-string' does to minimize searches and stuff,
902           ;; I will assume that people generally will put so few
903           ;; matches on numbers that any cleverness will take more
904           ;; time than one would gain.
905           (while articles
906             (and (funcall match-func 
907                           (or (aref (car (car articles)) gnus-score-index) 0)
908                           match)
909                  (progn
910                    (and trace (setq gnus-score-trace 
911                                     (cons (cons (car (car articles)) kill)
912                                           gnus-score-trace)))
913                    (setq found t)
914                    (setcdr (car articles) (+ score (cdr (car articles))))))
915             (setq articles (cdr articles)))
916           ;; Update expire date
917           (cond ((null date))           ;Permanent entry.
918                 (found                  ;Match, update date.
919                  (gnus-score-set 'touched '(t) alist)
920                  (setcar (nthcdr 2 kill) now))
921                 ((< date expire) ;Old entry, remove.
922                  (gnus-score-set 'touched '(t) alist)
923                  (setcdr entries (cdr rest))
924                  (setq rest entries)))
925           (setq entries rest))))))
926
927 (defun gnus-score-date (scores header now expire &optional trace)
928   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
929         alike last this art entries alist articles)
930
931     ;; Find matches.
932     (while scores
933       (setq alist (car scores)
934             scores (cdr scores)
935             entries (assoc header alist))
936       (while (cdr entries)              ;First entry is the header index.
937         (let* ((rest (cdr entries))             
938                (kill (car rest))
939                (match (timezone-make-date-sortable (nth 0 kill)))
940                (type (or (nth 3 kill) 'before))
941                (score (or (nth 1 kill) gnus-score-interactive-default-score))
942                (date (nth 2 kill))
943                (found nil)
944                (match-func 
945                 (cond ((eq type 'after) 'string<)
946                       ((eq type 'before) 'gnus-string>)
947                       ((eq type 'at) 'string=)
948                       (t (error "Illegal match type: %s" type))))
949                (articles gnus-scores-articles)
950                arts art l)
951           ;; Instead of doing all the clever stuff that
952           ;; `gnus-score-string' does to minimize searches and stuff,
953           ;; I will assume that people generally will put so few
954           ;; matches on numbers that any cleverness will take more
955           ;; time than one would gain.
956           (while articles
957             (and
958              (setq l (aref (car (car articles)) gnus-score-index))
959              (funcall match-func match (timezone-make-date-sortable l))
960              (progn
961                (and trace (setq gnus-score-trace 
962                                 (cons (cons (car (car articles)) kill)
963                                       gnus-score-trace)))
964                (setq found t)
965                (setcdr (car articles) (+ score (cdr (car articles))))))
966             (setq articles (cdr articles)))
967           ;; Update expire date
968           (cond ((null date))           ;Permanent entry.
969                 (found                  ;Match, update date.
970                  (gnus-score-set 'touched '(t) alist)
971                  (setcar (nthcdr 2 kill) now))
972                 ((< date expire) ;Old entry, remove.
973                  (gnus-score-set 'touched '(t) alist)
974                  (setcdr entries (cdr rest))
975                  (setq rest entries)))
976           (setq entries rest))))))
977
978 (defun gnus-score-body (scores header now expire &optional trace)
979   (save-excursion
980     (set-buffer nntp-server-buffer)
981     (save-restriction
982       (let* ((buffer-read-only nil)
983              (articles gnus-scores-articles)
984              (last (header-number 
985                     (gnus-last-element (car gnus-scores-articles))))
986              (all-scores scores)
987              (request-func (cond ((string= "head" (downcase header))
988                                   'gnus-request-head)
989                                  ((string= "body" (downcase header))
990                                   'gnus-request-body)
991                                  (t 'gnus-request-article)))
992              alike last this art entries alist ofunc article)
993         ;; Not all backends support partial fetching.  In that case,
994         ;; we just fetch the entire article.
995         (or (gnus-check-backend-function request-func gnus-newsgroup-name)
996             (progn
997               (setq ofunc request-func)
998               (setq request-func 'gnus-request-article)))
999         (while articles
1000           (setq article (header-number (car (car articles))))
1001           (gnus-message 7 "Scoring on article %s of %s..." article last)
1002           (if (not (funcall request-func article gnus-newsgroup-name))
1003               ()
1004             (widen)
1005             (goto-char (point-min))
1006             ;; If just parts of the article is to be searched, but the
1007             ;; backend didn't support partial fetching, we just narrow
1008             ;; to the relevant parts.
1009             (if ofunc
1010                 (if (eq ofunc 'gnus-request-head)
1011                     (narrow-to-region
1012                      (point)
1013                      (or (search-forward "\n\n" nil t) (point-max)))
1014                   (narrow-to-region
1015                    (or (search-forward "\n\n" nil t) (point))
1016                    (point-max))))
1017             (setq scores all-scores)
1018             ;; Find matches.
1019             (while scores
1020               (setq alist (car scores)
1021                     scores (cdr scores)
1022                     entries (assoc header alist))
1023               (while (cdr entries)      ;First entry is the header index.
1024                 (let* ((rest (cdr entries))             
1025                        (kill (car rest))
1026                        (match (nth 0 kill))
1027                        (type (or (nth 3 kill) 's))
1028                        (score (or (nth 1 kill) 
1029                                   gnus-score-interactive-default-score))
1030                        (date (nth 2 kill))
1031                        (found nil)
1032                        (case-fold-search 
1033                         (not (or (eq type 'R) (eq type 'S)
1034                                  (eq type 'Regexp) (eq type 'String))))
1035                        (search-func 
1036                         (cond ((or (eq type 'r) (eq type 'R)
1037                                    (eq type 'regexp) (eq type 'Regexp))
1038                                're-search-forward)
1039                               ((or (eq type 's) (eq type 'S)
1040                                    (eq type 'string) (eq type 'String))
1041                                'search-forward)
1042                               (t
1043                                (error "Illegal match type: %s" type))))
1044                        arts art)
1045                   (goto-char (point-min))
1046                   (if (funcall search-func match nil t)
1047                       ;; Found a match, update scores.
1048                       (progn
1049                         (setcdr (car articles) (+ score (cdr (car articles))))
1050                         (setq found t)
1051                         (and trace (setq gnus-score-trace 
1052                                          (cons (cons (car (car articles)) kill)
1053                                                gnus-score-trace)))))
1054                   ;; Update expire date
1055                   (cond ((null date))   ;Permanent entry.
1056                         (found          ;Match, update date.
1057                          (gnus-score-set 'touched '(t) alist)
1058                          (setcar (nthcdr 2 kill) now))
1059                         ((< date expire) ;Old entry, remove.
1060                          (gnus-score-set 'touched '(t) alist)
1061                          (setcdr entries (cdr rest))
1062                          (setq rest entries)))
1063                   (setq entries rest)))))
1064           (setq articles (cdr articles)))))))
1065
1066
1067
1068 (defun gnus-score-followup (scores header now expire &optional trace)
1069   ;; Insert the unique article headers in the buffer.
1070   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1071         (current-score-file gnus-current-score-file)
1072         ;; gnus-score-index is used as a free variable.
1073         alike last this art entries alist articles)
1074
1075     ;; Change score file to the adaptive score file.  All entries that
1076     ;; this function makes will be put into this file.
1077     (gnus-score-load-file (gnus-score-file-name 
1078                            gnus-newsgroup-name gnus-adaptive-file-suffix))
1079
1080     (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1081           articles gnus-scores-articles)
1082
1083     (erase-buffer)
1084     (while articles
1085       (setq art (car articles)
1086             this (aref (car art) gnus-score-index)
1087             articles (cdr articles))
1088       (if (equal last this)
1089           (setq alike (cons art alike))
1090         (if last
1091             (progn
1092               (insert last ?\n)
1093               (put-text-property (1- (point)) (point) 'articles alike)))
1094         (setq alike (list art)
1095               last this)))
1096     (and last                           ; Bwadr, duplicate code.
1097          (progn
1098            (insert last ?\n)                    
1099            (put-text-property (1- (point)) (point) 'articles alike)))
1100   
1101     ;; Find matches.
1102     (while scores
1103       (setq alist (car scores)
1104             scores (cdr scores)
1105             entries (assoc header alist))
1106       (while (cdr entries)              ;First entry is the header index.
1107         (let* ((rest (cdr entries))             
1108                (kill (car rest))
1109                (match (nth 0 kill))
1110                (type (or (nth 3 kill) 's))
1111                (score (or (nth 1 kill) gnus-score-interactive-default-score))
1112                (date (nth 2 kill))
1113                (found nil)
1114                (mt (aref (symbol-name type) 0))
1115                (case-fold-search 
1116                 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1117                (dmt (downcase mt))
1118                (search-func 
1119                 (cond ((= dmt ?r) 're-search-forward)
1120                       ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1121                       (t (error "Illegal match type: %s" type))))
1122                arts art)
1123           (goto-char (point-min))
1124           (if (= dmt ?e)
1125               (while (funcall search-func match nil t)
1126                 (and (= (progn (beginning-of-line) (point))
1127                         (match-beginning 0))
1128                      (= (progn (end-of-line) (point))
1129                         (match-end 0))
1130                      (progn
1131                        (setq found (setq arts (get-text-property 
1132                                                (point) 'articles)))
1133                        ;; Found a match, update scores.
1134                        (while arts
1135                          (setq art (car arts)
1136                                arts (cdr arts))
1137                          (gnus-score-add-followups (car art) score)))))
1138             (while (funcall search-func match nil t)
1139               (end-of-line)
1140               (setq found (setq arts (get-text-property (point) 'articles)))
1141               ;; Found a match, update scores.
1142               (while arts
1143                 (setq art (car arts)
1144                       arts (cdr arts))
1145                 (gnus-score-add-followups (car art) score))))
1146           ;; Update expire date
1147           (cond ((null date))           ;Permanent entry.
1148                 (found                  ;Match, update date.
1149                  (gnus-score-set 'touched '(t) alist)
1150                  (setcar (nthcdr 2 kill) now))
1151                 ((< date expire) ;Old entry, remove.
1152                  (gnus-score-set 'touched '(t) alist)
1153                  (setcdr entries (cdr rest))
1154                  (setq rest entries)))
1155           (setq entries rest))))
1156     ;; We change the score file back to the previous one.
1157     (gnus-score-load-file current-score-file)))
1158
1159 (defun gnus-score-add-followups (header score)
1160   (save-excursion
1161     (set-buffer gnus-summary-buffer)
1162     (let ((id (header-id header))
1163           (score gnus-score-alist)
1164           dont)
1165       ;; Don't enter a score if there already is one.
1166       (while score
1167         (and (equal "references" (car (car score)))
1168              (or (null (nth 3 (car score)))
1169                  (eq 's (nth 3 (car score))))
1170              (progn
1171                (or (assoc id (car score))
1172                    (setq dont t))
1173                (setq score nil)))
1174         (setq score (cdr score)))
1175       (or dont
1176           (gnus-summary-score-entry 
1177            "references" id 's score (current-time-string) nil t)))))
1178
1179
1180 (defun gnus-score-string (score-list header now expire &optional trace)
1181   ;; Score ARTICLES according to HEADER in SCORE-LIST.
1182   ;; Update matches entries to NOW and remove unmatched entried older
1183   ;; than EXPIRE.
1184   
1185   ;; Insert the unique article headers in the buffer.
1186   (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1187         ;; gnus-score-index is used as a free variable.
1188         alike last this art entries alist articles scores fuzzy)
1189
1190     ;; Sorting the articles costs os O(N*log N) but will allow us to
1191     ;; only match with each unique header.  Thus the actual matching
1192     ;; will be O(M*U) where M is the number of strings to match with,
1193     ;; and U is the number of unique headers.  It is assumed (but
1194     ;; untested) this will be a net win because of the large constant
1195     ;; factor involved with string matching.
1196     (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1197           articles gnus-scores-articles)
1198
1199     (erase-buffer)
1200     (while articles
1201       (setq art (car articles)
1202             this (aref (car art) gnus-score-index)
1203             articles (cdr articles))
1204       (if (equal last this)
1205           ;; O(N*H) cons-cells used here, where H is the number of
1206           ;; headers.
1207           (setq alike (cons art alike))
1208         (if last
1209             (progn
1210               ;; Insert the line, with a text property on the
1211               ;; terminating newline refering to the articles with
1212               ;; this line.
1213               (insert last ?\n)
1214               (put-text-property (1- (point)) (point) 'articles alike)))
1215         (setq alike (list art)
1216               last this)))
1217     (and last                           ; Bwadr, duplicate code.
1218          (progn
1219            (insert last ?\n)                    
1220            (put-text-property (1- (point)) (point) 'articles alike)))
1221   
1222     ;; Find ordinary matches.
1223     (setq scores score-list) 
1224     (while scores
1225       (setq alist (car scores)
1226             scores (cdr scores)
1227             entries (assoc header alist))
1228       (while (cdr entries)              ;First entry is the header index.
1229         (let* ((rest (cdr entries))             
1230                (kill (car rest))
1231                (match (nth 0 kill))
1232                (type (or (nth 3 kill) 's))
1233                (score (or (nth 1 kill) gnus-score-interactive-default-score))
1234                (date (nth 2 kill))
1235                (found nil)
1236                (mt (aref (symbol-name type) 0))
1237                (case-fold-search 
1238                 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1239                (dmt (downcase mt))
1240                (search-func 
1241                 (cond ((= dmt ?r) 're-search-forward)
1242                       ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1243                       (t (error "Illegal match type: %s" type))))
1244                arts art)
1245           (if (= dmt ?f)
1246               (setq fuzzy t)
1247             (goto-char (point-min))
1248             (if (= dmt ?e)
1249                 (while (and (not (eobp)) 
1250                             (funcall search-func match nil t))
1251                   (and (= (progn (beginning-of-line) (point))
1252                           (match-beginning 0))
1253                        (= (progn (end-of-line) (point))
1254                           (match-end 0))
1255                        (progn
1256                          (setq found (setq arts (get-text-property 
1257                                                  (point) 'articles)))
1258                          ;; Found a match, update scores.
1259                          (if trace
1260                              (while arts
1261                                (setq art (car arts)
1262                                      arts (cdr arts))
1263                                (setcdr art (+ score (cdr art)))
1264                                (setq gnus-score-trace 
1265                                      (cons (cons (header-number
1266                                                   (car art)) kill)
1267                                            gnus-score-trace)))
1268                            (while arts
1269                              (setq art (car arts)
1270                                    arts (cdr arts))
1271                              (setcdr art (+ score (cdr art)))))))
1272                   (forward-line 1))
1273               (and (string= match "") (setq match "\n"))
1274               (while (funcall search-func match nil t)
1275                 (end-of-line)
1276                 (setq found (setq arts (get-text-property (point) 'articles)))
1277                 ;; Found a match, update scores.
1278                 (if trace
1279                     (while arts
1280                       (setq art (car arts)
1281                             arts (cdr arts))
1282                       (setcdr art (+ score (cdr art)))
1283                       (setq gnus-score-trace 
1284                             (cons (cons (header-number (car art)) kill)
1285                                   gnus-score-trace)))
1286                   (while arts
1287                     (setq art (car arts)
1288                           arts (cdr arts))
1289                     (setcdr art (+ score (cdr art)))))))
1290             ;; Update expire date
1291             (cond ((null date))         ;Permanent entry.
1292                   (found                ;Match, update date.
1293                    (gnus-score-set 'touched '(t) alist)
1294                    (setcar (nthcdr 2 kill) now))
1295                   ((< date expire)      ;Old entry, remove.
1296                    (gnus-score-set 'touched '(t) alist)
1297                    (setcdr entries (cdr rest))
1298                    (setq rest entries))))
1299           (setq entries rest))))
1300   
1301     ;; Find fuzzy matches.
1302     (setq scores (and fuzzy score-list))
1303     (if fuzzy (gnus-simplify-buffer-fuzzy))
1304     (while scores
1305       (setq alist (car scores)
1306             scores (cdr scores)
1307             entries (assoc header alist))
1308       (while (cdr entries)              ;First entry is the header index.
1309         (let* ((rest (cdr entries))             
1310                (kill (car rest))
1311                (match (nth 0 kill))
1312                (type (or (nth 3 kill) 's))
1313                (score (or (nth 1 kill) gnus-score-interactive-default-score))
1314                (date (nth 2 kill))
1315                (found nil)
1316                (mt (aref (symbol-name type) 0))
1317                (case-fold-search 
1318                 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1319                (dmt (downcase mt))
1320                (search-func 
1321                 (cond ((= dmt ?r) 're-search-forward)
1322                       ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1323                       (t (error "Illegal match type: %s" type))))
1324                arts art)
1325           (if (/= dmt ?f)
1326               ()
1327             (goto-char (point-min))
1328             (while (and (not (eobp)) 
1329                         (funcall search-func match nil t))
1330               (and (= (progn (beginning-of-line) (point))
1331                       (match-beginning 0))
1332                    (= (progn (end-of-line) (point))
1333                       (match-end 0))
1334                    (progn
1335                      (setq found (setq arts (get-text-property 
1336                                              (point) 'articles)))
1337                      ;; Found a match, update scores.
1338                      (while arts
1339                        (setq art (car arts)
1340                              arts (cdr arts))
1341                        (setcdr art (+ score (cdr art))))))
1342               (forward-line 1))
1343             ;; Update expire date
1344             (cond ((null date))         ;Permanent entry.
1345                   (found                ;Match, update date.
1346                    (gnus-score-set 'touched '(t) alist)
1347                    (setcar (nthcdr 2 kill) now))
1348                   ((< date expire)      ;Old entry, remove.
1349                    (gnus-score-set 'touched '(t) alist)
1350                    (setcdr entries (cdr rest))
1351                    (setq rest entries))))
1352           (setq entries rest))))))
1353
1354 (defun gnus-score-string< (a1 a2)
1355   ;; Compare headers in articles A2 and A2.
1356   ;; The header index used is the free variable `gnus-score-index'.
1357   (string-lessp (aref (car a1) gnus-score-index)
1358                 (aref (car a2) gnus-score-index)))
1359
1360 (defun gnus-score-build-cons (article)
1361   ;; Build a `gnus-newsgroup-scored' type cons from ARTICLE.
1362   (cons (header-number (car article)) (cdr article)))
1363
1364 (defconst gnus-header-index
1365   ;; Name to index alist.
1366   '(("number" 0 gnus-score-integer)
1367     ("subject" 1 gnus-score-string)
1368     ("from" 2 gnus-score-string)
1369     ("date" 3 gnus-score-date)
1370     ("message-id" 4 gnus-score-string) 
1371     ("references" 5 gnus-score-string) 
1372     ("chars" 6 gnus-score-integer) 
1373     ("lines" 7 gnus-score-integer) 
1374     ("xref" 8 gnus-score-string)
1375     ("head" -1 gnus-score-body)
1376     ("body" -1 gnus-score-body)
1377     ("all" -1 gnus-score-body)
1378     ("followup" 2 gnus-score-followup)))
1379
1380 (defun gnus-current-score-file-nondirectory (&optional score-file)
1381   (let ((score-file (or score-file gnus-current-score-file)))
1382     (if score-file 
1383         (gnus-short-group-name (file-name-nondirectory score-file))
1384       "none")))
1385
1386 (defun gnus-score-adaptive ()
1387   (save-excursion
1388     (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
1389            (alist malist)
1390            (date (current-time-string)) 
1391            elem headers match)
1392       ;; First we transform the adaptive rule alist into something
1393       ;; that's faster to process.
1394       (while malist
1395         (setq elem (car malist))
1396         (if (symbolp (car elem))
1397             (setcar elem (symbol-value (car elem))))
1398         (setq elem (cdr elem))
1399         (while elem
1400           (setcdr (car elem) 
1401                   (cons (symbol-name (car (car elem))) (cdr (car elem))))
1402           (setcar (car elem) 
1403                   (intern 
1404                    (concat "gnus-header-" 
1405                            (downcase (symbol-name (car (car elem)))))))
1406           (setq elem (cdr elem)))
1407         (setq malist (cdr malist)))
1408       ;; We change the score file to the adaptive score file.
1409       (gnus-score-load-file (gnus-score-file-name 
1410                              gnus-newsgroup-name gnus-adaptive-file-suffix))
1411       ;; The we score away.
1412       (goto-char (point-min))
1413       (while (not (eobp))
1414         (setq elem (cdr (assq (gnus-summary-article-mark) alist)))
1415         (if (or (not elem)
1416                 (get-text-property (point) 'gnus-pseudo))
1417             ()
1418           (setq headers (gnus-get-header-by-number 
1419                          (gnus-summary-article-number)))
1420           (while (and elem headers)
1421             (setq match (funcall (car (car elem)) headers))
1422             (gnus-summary-score-entry 
1423              (nth 1 (car elem)) match
1424              ;; Whether we use substring or exact matches are controlled
1425              ;; here.  
1426              (if (or (not gnus-score-exact-adapt-limit)
1427                      (< (length match) gnus-score-exact-adapt-limit))
1428                  'e 's) 
1429              (nth 2 (car elem)) date nil t)
1430             (setq elem (cdr elem))))
1431         (forward-line 1)))))
1432
1433 (defun gnus-score-remove-lines-adaptive (marks)
1434   (save-excursion
1435     (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
1436            (alist malist)
1437            (date (current-time-string)) 
1438            (cur-score gnus-current-score-file)
1439            elem headers match)
1440       ;; First we transform the adaptive rule alist into something
1441       ;; that's faster to process.
1442       (while malist
1443         (setq elem (car malist))
1444         (if (symbolp (car elem))
1445             (setcar elem (symbol-value (car elem))))
1446         (setq elem (cdr elem))
1447         (while elem
1448           (setcdr (car elem) 
1449                   (cons (symbol-name (car (car elem))) (cdr (car elem))))
1450           (setcar (car elem) 
1451                   (intern 
1452                    (concat "gnus-header-" 
1453                            (downcase (symbol-name (car (car elem)))))))
1454           (setq elem (cdr elem)))
1455         (setq malist (cdr malist)))
1456       ;; The we score away.
1457       (goto-char (point-min))
1458       ;; We change the score file to the adaptive score file.
1459       (gnus-score-load-file (gnus-score-file-name 
1460                              gnus-newsgroup-name gnus-adaptive-file-suffix))
1461       (while (re-search-forward marks nil t)
1462         (beginning-of-line)
1463         (setq elem (cdr (assq (gnus-summary-article-mark) alist)))
1464         (if (or (not elem)
1465                 (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
1466             ()
1467           (setq headers (gnus-get-header-by-number 
1468                          (gnus-summary-article-number)))
1469           (while elem
1470             (setq match (funcall (car (car elem)) headers))
1471             (gnus-summary-score-entry 
1472              (nth 1 (car elem)) match
1473              (if (or (not gnus-score-exact-adapt-limit)
1474                      (< (length match) gnus-score-exact-adapt-limit))
1475                  'e 's) 
1476              (nth 2 (car elem)) date nil t)
1477             (setq elem (cdr elem))))
1478         (delete-region (point) (progn (forward-line 1) (point))))
1479       ;; Switch back to the old score file.
1480       (gnus-score-load-file cur-score))))
1481
1482 ;;;
1483 ;;; Score mode.
1484 ;;;
1485
1486 (defvar gnus-score-mode-map nil)
1487 (defvar gnus-score-mode-hook nil)
1488
1489 (if gnus-score-mode-map
1490     ()
1491   (setq gnus-score-mode-map (copy-keymap emacs-lisp-mode-map))
1492   (define-key gnus-score-mode-map "\C-c\C-c" 'gnus-score-edit-done)
1493   (define-key gnus-score-mode-map "\C-c\C-d" 'gnus-score-edit-insert-date))
1494
1495 (defun gnus-score-mode ()
1496   "Mode for editing score files.
1497 This mode is an extended emacs-lisp mode.
1498
1499 \\{gnus-score-mode-map}"
1500   (interactive)
1501   (kill-all-local-variables)
1502   (use-local-map gnus-score-mode-map)
1503   (set-syntax-table emacs-lisp-mode-syntax-table)
1504   (setq major-mode 'gnus-score-mode)
1505   (setq mode-name "Score")
1506   (lisp-mode-variables nil)
1507   (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
1508
1509 (defun gnus-score-edit-insert-date ()
1510   "Insert date in numerical format."
1511   (interactive)
1512   (insert (int-to-string (gnus-day-number (current-time-string)))))
1513
1514 (defun gnus-score-edit-done ()
1515   "Save the score file and return to the summary buffer."
1516   (interactive)
1517   (let ((bufnam (buffer-file-name (current-buffer)))
1518         (winconf gnus-prev-winconf))
1519     (gnus-make-directory (file-name-directory (buffer-file-name)))
1520     (save-buffer)
1521     (kill-buffer (current-buffer))
1522     (gnus-score-remove-from-cache bufnam)
1523     (gnus-score-load-file bufnam)
1524     (and winconf (set-window-configuration winconf))))
1525
1526 (defun gnus-score-find-trace ()
1527   "Find all score rules applied to this article."
1528   (interactive)
1529   (let ((gnus-newsgroup-headers
1530          (list (gnus-get-header-by-number (gnus-summary-article-number))))
1531         (gnus-newsgroup-scored nil)
1532         (buf (current-buffer))
1533         trace)
1534     (setq gnus-score-trace nil)
1535     (gnus-possibly-score-headers 'trace)
1536     (pop-to-buffer "*Gnus Scores*")
1537     (erase-buffer)
1538     (setq trace gnus-score-trace)
1539     (or trace
1540         (error "No score rules apply to the current article."))
1541     (while trace
1542       (insert (format "%S\n" (cdr (car trace))))
1543       (setq trace (cdr trace)))
1544     (goto-char (point-min))
1545     (pop-to-buffer buf)))
1546   
1547
1548 (provide 'gnus-score)
1549
1550 ;;; gnus-score.el ends here