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