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