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