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