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