]> code.delx.au - gnu-emacs/blob - lisp/gnus/gnus-score.el
(mail-reply-buffer): Add defvar.
[gnu-emacs] / lisp / gnus / gnus-score.el
1 ;;; gnus-score.el --- scoring code for Gnus
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
5
6 ;; Author: Per Abrahamsen <amanda@iesd.auc.dk>
7 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; Keywords: news
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 (require 'gnus)
34 (require 'gnus-sum)
35 (require 'gnus-range)
36 (require 'gnus-win)
37 (require 'message)
38 (require 'score-mode)
39
40 (autoload 'ffap-string-at-point "ffap")
41
42 (defcustom gnus-global-score-files nil
43 "List of global score files and directories.
44 Set this variable if you want to use people's score files. One entry
45 for each score file or each score file directory. Gnus will decide
46 by itself what score files are applicable to which group.
47
48 Say you want to use the single score file
49 \"/ftp.gnus.org@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
50 score files in the \"/ftp.some-where:/pub/score\" directory.
51
52 (setq gnus-global-score-files
53 '(\"/ftp.gnus.org:/pub/larsi/ding/score/soc.motss.SCORE\"
54 \"/ftp.some-where:/pub/score\"))"
55 :group 'gnus-score-files
56 :type '(repeat file))
57
58 (defcustom gnus-score-file-single-match-alist nil
59 "Alist mapping regexps to lists of score files.
60 Each element of this alist should be of the form
61 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
62
63 If the name of a group is matched by REGEXP, the corresponding scorefiles
64 will be used for that group.
65 The first match found is used, subsequent matching entries are ignored (to
66 use multiple matches, see `gnus-score-file-multiple-match-alist').
67
68 These score files are loaded in addition to any files returned by
69 `gnus-score-find-score-files-function'."
70 :group 'gnus-score-files
71 :type '(repeat (cons regexp (repeat file))))
72
73 (defcustom gnus-score-file-multiple-match-alist nil
74 "Alist mapping regexps to lists of score files.
75 Each element of this alist should be of the form
76 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
77
78 If the name of a group is matched by REGEXP, the corresponding scorefiles
79 will be used for that group.
80 If multiple REGEXPs match a group, the score files corresponding to each
81 match will be used (for only one match to be used, see
82 `gnus-score-file-single-match-alist').
83
84 These score files are loaded in addition to any files returned by
85 `gnus-score-find-score-files-function'."
86 :group 'gnus-score-files
87 :type '(repeat (cons regexp (repeat file))))
88
89 (defcustom gnus-score-file-suffix "SCORE"
90 "Suffix of the score files."
91 :group 'gnus-score-files
92 :type 'string)
93
94 (defcustom gnus-adaptive-file-suffix "ADAPT"
95 "Suffix of the adaptive score files."
96 :group 'gnus-score-files
97 :group 'gnus-score-adapt
98 :type 'string)
99
100 (defcustom gnus-score-find-score-files-function 'gnus-score-find-bnews
101 "Function used to find score files.
102 The function will be called with the group name as the argument, and
103 should return a list of score files to apply to that group. The score
104 files do not actually have to exist.
105
106 Predefined values are:
107
108 `gnus-score-find-single': Only apply the group's own score file.
109 `gnus-score-find-hierarchical': Also apply score files from parent groups.
110 `gnus-score-find-bnews': Apply score files whose names matches.
111
112 See the documentation to these functions for more information.
113
114 This variable can also be a list of functions to be called. Each
115 function is given the group name as argument and should either return
116 a list of score files, or a list of score alists.
117
118 If functions other than these pre-defined functions are used,
119 the `a' symbolic prefix to the score commands will always use
120 \"all.SCORE\"."
121 :group 'gnus-score-files
122 :type '(radio (function-item gnus-score-find-single)
123 (function-item gnus-score-find-hierarchical)
124 (function-item gnus-score-find-bnews)
125 (repeat :tag "List of functions"
126 (choice (function :tag "Other" :value 'ignore)
127 (function-item gnus-score-find-single)
128 (function-item gnus-score-find-hierarchical)
129 (function-item gnus-score-find-bnews)))
130 (function :tag "Other" :value 'ignore)))
131
132 (defcustom gnus-score-interactive-default-score 1000
133 "*Scoring commands will raise/lower the score with this number as the default."
134 :group 'gnus-score-default
135 :type 'integer)
136
137 (defcustom gnus-score-expiry-days 7
138 "*Number of days before unused score file entries are expired.
139 If this variable is nil, no score file entries will be expired."
140 :group 'gnus-score-expire
141 :type '(choice (const :tag "never" nil)
142 number))
143
144 (defcustom gnus-update-score-entry-dates t
145 "*In non-nil, update matching score entry dates.
146 If this variable is nil, then score entries that provide matches
147 will be expired along with non-matching score entries."
148 :group 'gnus-score-expire
149 :type 'boolean)
150
151 (defcustom gnus-decay-scores nil
152 "*If non-nil, decay non-permanent scores."
153 :group 'gnus-score-decay
154 :type 'boolean)
155
156 (defcustom gnus-decay-score-function 'gnus-decay-score
157 "*Function called to decay a score.
158 It is called with one parameter -- the score to be decayed."
159 :group 'gnus-score-decay
160 :type '(radio (function-item gnus-decay-score)
161 (function :tag "Other")))
162
163 (defcustom gnus-score-decay-constant 3
164 "*Decay all \"small\" scores with this amount."
165 :group 'gnus-score-decay
166 :type 'integer)
167
168 (defcustom gnus-score-decay-scale .05
169 "*Decay all \"big\" scores with this factor."
170 :group 'gnus-score-decay
171 :type 'number)
172
173 (defcustom gnus-home-score-file nil
174 "Variable to control where interactive score entries are to go.
175 It can be:
176
177 * A string
178 This file file will be used as the home score file.
179
180 * A function
181 The result of this function will be used as the home score file.
182 The function will be passed the name of the group as its
183 parameter.
184
185 * A list
186 The elements in this list can be:
187
188 * `(regexp file-name ...)'
189 If the `regexp' matches the group name, the first `file-name' will
190 will be used as the home score file. (Multiple filenames are
191 allowed so that one may use gnus-score-file-single-match-alist to
192 set this variable.)
193
194 * A function.
195 If the function returns non-nil, the result will be used
196 as the home score file. The function will be passed the
197 name of the group as its parameter.
198
199 * A string. Use the string as the home score file.
200
201 The list will be traversed from the beginning towards the end looking
202 for matches."
203 :group 'gnus-score-files
204 :type '(choice string
205 (repeat (choice string
206 (cons regexp (repeat file))
207 (function :value fun)))
208 (function-item gnus-hierarchial-home-score-file)
209 (function-item gnus-current-home-score-file)
210 (function :value fun)))
211
212 (defcustom gnus-home-adapt-file nil
213 "Variable to control where new adaptive score entries are to go.
214 This variable allows the same syntax as `gnus-home-score-file'."
215 :group 'gnus-score-adapt
216 :group 'gnus-score-files
217 :type '(choice string
218 (repeat (choice string
219 (cons regexp (repeat file))
220 (function :value fun)))
221 (function :value fun)))
222
223 (defcustom gnus-default-adaptive-score-alist
224 '((gnus-kill-file-mark)
225 (gnus-unread-mark)
226 (gnus-read-mark (from 3) (subject 30))
227 (gnus-catchup-mark (subject -10))
228 (gnus-killed-mark (from -1) (subject -20))
229 (gnus-del-mark (from -2) (subject -15)))
230 "*Alist of marks and scores."
231 :group 'gnus-score-adapt
232 :type '(repeat (cons (symbol :tag "Mark")
233 (repeat (list (choice :tag "Header"
234 (const from)
235 (const subject)
236 (symbol :tag "other"))
237 (integer :tag "Score"))))))
238
239 (defcustom gnus-adaptive-word-length-limit nil
240 "*Words of a length lesser than this limit will be ignored when doing adaptive scoring."
241 :version "22.1"
242 :group 'gnus-score-adapt
243 :type '(radio (const :format "Unlimited " nil)
244 (integer :format "Maximum length: %v")))
245
246 (defcustom gnus-ignored-adaptive-words nil
247 "List of words to be ignored when doing adaptive word scoring."
248 :group 'gnus-score-adapt
249 :type '(repeat string))
250
251 (defcustom gnus-default-ignored-adaptive-words
252 '("a" "i" "the" "to" "of" "and" "in" "is" "it" "for" "that" "if" "you"
253 "this" "be" "on" "with" "not" "have" "are" "or" "as" "from" "can"
254 "but" "by" "at" "an" "will" "no" "all" "was" "do" "there" "my" "one"
255 "so" "we" "they" "what" "would" "any" "which" "about" "get" "your"
256 "use" "some" "me" "then" "name" "like" "out" "when" "up" "time"
257 "other" "more" "only" "just" "end" "also" "know" "how" "new" "should"
258 "been" "than" "them" "he" "who" "make" "may" "people" "these" "now"
259 "their" "here" "into" "first" "could" "way" "had" "see" "work" "well"
260 "were" "two" "very" "where" "while" "us" "because" "good" "same"
261 "even" "much" "most" "many" "such" "long" "his" "over" "last" "since"
262 "right" "before" "our" "without" "too" "those" "why" "must" "part"
263 "being" "current" "back" "still" "go" "point" "value" "each" "did"
264 "both" "true" "off" "say" "another" "state" "might" "under" "start"
265 "try" "re")
266 "*Default list of words to be ignored when doing adaptive word scoring."
267 :group 'gnus-score-adapt
268 :type '(repeat string))
269
270 (defcustom gnus-default-adaptive-word-score-alist
271 `((,gnus-read-mark . 30)
272 (,gnus-catchup-mark . -10)
273 (,gnus-killed-mark . -20)
274 (,gnus-del-mark . -15))
275 "*Alist of marks and scores."
276 :group 'gnus-score-adapt
277 :type '(repeat (cons (character :tag "Mark")
278 (integer :tag "Score"))))
279
280 (defcustom gnus-adaptive-word-minimum nil
281 "If a number, this is the minimum score value that can be assigned to a word."
282 :group 'gnus-score-adapt
283 :type '(choice (const nil) integer))
284
285 (defcustom gnus-adaptive-word-no-group-words nil
286 "If t, don't adaptively score words included in the group name."
287 :group 'gnus-score-adapt
288 :type 'boolean)
289
290 (defcustom gnus-score-mimic-keymap nil
291 "*Have the score entry functions pretend that they are a keymap."
292 :group 'gnus-score-default
293 :type 'boolean)
294
295 (defcustom gnus-score-exact-adapt-limit 10
296 "*Number that says how long a match has to be before using substring matching.
297 When doing adaptive scoring, one normally uses fuzzy or substring
298 matching. However, if the header one matches is short, the possibility
299 for false positives is great, so if the length of the match is less
300 than this variable, exact matching will be used.
301
302 If this variable is nil, exact matching will always be used."
303 :group 'gnus-score-adapt
304 :type '(choice (const nil) integer))
305
306 (defcustom gnus-score-uncacheable-files "ADAPT$"
307 "All score files that match this regexp will not be cached."
308 :group 'gnus-score-adapt
309 :group 'gnus-score-files
310 :type 'regexp)
311
312 (defcustom gnus-score-default-header nil
313 "Default header when entering new scores.
314
315 Should be one of the following symbols.
316
317 a: from
318 s: subject
319 b: body
320 h: head
321 i: message-id
322 t: references
323 x: xref
324 e: `extra' (non-standard overview)
325 l: lines
326 d: date
327 f: followup
328
329 If nil, the user will be asked for a header."
330 :group 'gnus-score-default
331 :type '(choice (const :tag "from" a)
332 (const :tag "subject" s)
333 (const :tag "body" b)
334 (const :tag "head" h)
335 (const :tag "message-id" i)
336 (const :tag "references" t)
337 (const :tag "xref" x)
338 (const :tag "extra" e)
339 (const :tag "lines" l)
340 (const :tag "date" d)
341 (const :tag "followup" f)
342 (const :tag "ask" nil)))
343
344 (defcustom gnus-score-default-type nil
345 "Default match type when entering new scores.
346
347 Should be one of the following symbols.
348
349 s: substring
350 e: exact string
351 f: fuzzy string
352 r: regexp string
353 b: before date
354 a: after date
355 n: this date
356 <: less than number
357 >: greater than number
358 =: equal to number
359
360 If nil, the user will be asked for a match type."
361 :group 'gnus-score-default
362 :type '(choice (const :tag "substring" s)
363 (const :tag "exact string" e)
364 (const :tag "fuzzy string" f)
365 (const :tag "regexp string" r)
366 (const :tag "before date" b)
367 (const :tag "after date" a)
368 (const :tag "this date" n)
369 (const :tag "less than number" <)
370 (const :tag "greater than number" >)
371 (const :tag "equal than number" =)
372 (const :tag "ask" nil)))
373
374 (defcustom gnus-score-default-fold nil
375 "Use case folding for new score file entries iff not nil."
376 :group 'gnus-score-default
377 :type 'boolean)
378
379 (defcustom gnus-score-default-duration nil
380 "Default duration of effect when entering new scores.
381
382 Should be one of the following symbols.
383
384 t: temporary
385 p: permanent
386 i: immediate
387
388 If nil, the user will be asked for a duration."
389 :group 'gnus-score-default
390 :type '(choice (const :tag "temporary" t)
391 (const :tag "permanent" p)
392 (const :tag "immediate" i)
393 (const :tag "ask" nil)))
394
395 (defcustom gnus-score-after-write-file-function nil
396 "Function called with the name of the score file just written to disk."
397 :group 'gnus-score-files
398 :type '(choice (const nil) function))
399
400 (defcustom gnus-score-thread-simplify nil
401 "If non-nil, subjects will simplified as in threading."
402 :group 'gnus-score-various
403 :type 'boolean)
404
405 \f
406
407 ;; Internal variables.
408
409 (defvar gnus-score-use-all-scores t
410 "If nil, only `gnus-score-find-score-files-function' is used.")
411
412 (defvar gnus-adaptive-word-syntax-table
413 (let ((table (copy-syntax-table (standard-syntax-table)))
414 (numbers '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)))
415 (while numbers
416 (modify-syntax-entry (pop numbers) " " table))
417 (modify-syntax-entry ?' "w" table)
418 table)
419 "Syntax table used when doing adaptive word scoring.")
420
421 (defvar gnus-scores-exclude-files nil)
422 (defvar gnus-internal-global-score-files nil)
423 (defvar gnus-score-file-list nil)
424
425 (defvar gnus-short-name-score-file-cache nil)
426
427 (defvar gnus-score-help-winconf nil)
428 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
429 (defvar gnus-adaptive-word-score-alist gnus-default-adaptive-word-score-alist)
430 (defvar gnus-score-trace nil)
431 (defvar gnus-score-edit-buffer nil)
432
433 (defvar gnus-score-alist nil
434 "Alist containing score information.
435 The keys can be symbols or strings. The following symbols are defined.
436
437 touched: If this alist has been modified.
438 mark: Automatically mark articles below this.
439 expunge: Automatically expunge articles below this.
440 files: List of other score files to load when loading this one.
441 eval: Sexp to be evaluated when the score file is loaded.
442
443 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
444 where HEADER is the header being scored, MATCH is the string we are
445 looking for, TYPE is a flag indicating whether it should use regexp or
446 substring matching, SCORE is the score to add and DATE is the date
447 of the last successful match.")
448
449 (defvar gnus-score-cache nil)
450 (defvar gnus-scores-articles nil)
451 (defvar gnus-score-index nil)
452
453
454 (defconst gnus-header-index
455 ;; Name to index alist.
456 '(("number" 0 gnus-score-integer)
457 ("subject" 1 gnus-score-string)
458 ("from" 2 gnus-score-string)
459 ("date" 3 gnus-score-date)
460 ("message-id" 4 gnus-score-string)
461 ("references" 5 gnus-score-string)
462 ("chars" 6 gnus-score-integer)
463 ("lines" 7 gnus-score-integer)
464 ("xref" 8 gnus-score-string)
465 ("extra" 9 gnus-score-string)
466 ("head" -1 gnus-score-body)
467 ("body" -1 gnus-score-body)
468 ("all" -1 gnus-score-body)
469 ("followup" 2 gnus-score-followup)
470 ("thread" 5 gnus-score-thread)))
471
472 ;;; Summary mode score maps.
473
474 (gnus-define-keys (gnus-summary-score-map "V" gnus-summary-mode-map)
475 "s" gnus-summary-set-score
476 "S" gnus-summary-current-score
477 "c" gnus-score-change-score-file
478 "C" gnus-score-customize
479 "m" gnus-score-set-mark-below
480 "x" gnus-score-set-expunge-below
481 "R" gnus-summary-rescore
482 "e" gnus-score-edit-current-scores
483 "f" gnus-score-edit-file
484 "F" gnus-score-flush-cache
485 "t" gnus-score-find-trace
486 "w" gnus-score-find-favourite-words)
487
488 ;; Summary score file commands
489
490 ;; Much modification of the kill (ahem, score) code and lots of the
491 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
492
493 (defun gnus-summary-lower-score (&optional score symp)
494 "Make a score entry based on the current article.
495 The user will be prompted for header to score on, match type,
496 permanence, and the string to be used. The numerical prefix will be
497 used as score. A symbolic prefix of `a' says to use the `all.SCORE'
498 file for the command instead of the current score file."
499 (interactive (gnus-interactive "P\ny"))
500 (gnus-summary-increase-score (- (gnus-score-delta-default score)) symp))
501
502 (defun gnus-score-kill-help-buffer ()
503 (when (get-buffer "*Score Help*")
504 (kill-buffer "*Score Help*")
505 (when gnus-score-help-winconf
506 (set-window-configuration gnus-score-help-winconf))))
507
508 (defun gnus-summary-increase-score (&optional score symp)
509 "Make a score entry based on the current article.
510 The user will be prompted for header to score on, match type,
511 permanence, and the string to be used. The numerical prefix will be
512 used as score. A symbolic prefix of `a' says to use the `all.SCORE'
513 file for the command instead of the current score file."
514 (interactive (gnus-interactive "P\ny"))
515 (let* ((nscore (gnus-score-delta-default score))
516 (prefix (if (< nscore 0) ?L ?I))
517 (increase (> nscore 0))
518 (char-to-header
519 '((?a "from" nil nil string)
520 (?s "subject" nil nil string)
521 (?b "body" "" nil body-string)
522 (?h "head" "" nil body-string)
523 (?i "message-id" nil nil string)
524 (?r "references" "message-id" nil string)
525 (?x "xref" nil nil string)
526 (?e "extra" nil nil string)
527 (?l "lines" nil nil number)
528 (?d "date" nil nil date)
529 (?f "followup" nil nil string)
530 (?t "thread" "message-id" nil string)))
531 (char-to-type
532 '((?s s "substring" string)
533 (?e e "exact string" string)
534 (?f f "fuzzy string" string)
535 (?r r "regexp string" string)
536 (?z s "substring" body-string)
537 (?p r "regexp string" body-string)
538 (?b before "before date" date)
539 (?a after "after date" date)
540 (?n at "this date" date)
541 (?< < "less than number" number)
542 (?> > "greater than number" number)
543 (?= = "equal to number" number)))
544 (current-score-file gnus-current-score-file)
545 (char-to-perm
546 (list (list ?t (current-time-string) "temporary")
547 '(?p perm "permanent") '(?i now "immediate")))
548 (mimic gnus-score-mimic-keymap)
549 (hchar (and gnus-score-default-header
550 (aref (symbol-name gnus-score-default-header) 0)))
551 (tchar (and gnus-score-default-type
552 (aref (symbol-name gnus-score-default-type) 0)))
553 (pchar (and gnus-score-default-duration
554 (aref (symbol-name gnus-score-default-duration) 0)))
555 entry temporary type match extra)
556
557 (unwind-protect
558 (progn
559
560 ;; First we read the header to score.
561 (while (not hchar)
562 (if mimic
563 (progn
564 (sit-for 1)
565 (message "%c-" prefix))
566 (message "%s header (%s?): " (if increase "Increase" "Lower")
567 (mapconcat (lambda (s) (char-to-string (car s)))
568 char-to-header "")))
569 (setq hchar (read-char))
570 (when (or (= hchar ??) (= hchar ?\C-h))
571 (setq hchar nil)
572 (gnus-score-insert-help "Match on header" char-to-header 1)))
573
574 (gnus-score-kill-help-buffer)
575 (unless (setq entry (assq (downcase hchar) char-to-header))
576 (if mimic (error "%c %c" prefix hchar)
577 (error "Invalid header type")))
578
579 (when (/= (downcase hchar) hchar)
580 ;; This was a majuscule, so we end reading and set the defaults.
581 (if mimic (message "%c %c" prefix hchar) (message ""))
582 (setq tchar (or tchar ?s)
583 pchar (or pchar ?t)))
584
585 (let ((legal-types
586 (delq nil
587 (mapcar (lambda (s)
588 (if (eq (nth 4 entry)
589 (nth 3 s))
590 s nil))
591 char-to-type))))
592 ;; We continue reading - the type.
593 (while (not tchar)
594 (if mimic
595 (progn
596 (sit-for 1) (message "%c %c-" prefix hchar))
597 (message "%s header '%s' with match type (%s?): "
598 (if increase "Increase" "Lower")
599 (nth 1 entry)
600 (mapconcat (lambda (s) (char-to-string (car s)))
601 legal-types "")))
602 (setq tchar (read-char))
603 (when (or (= tchar ??) (= tchar ?\C-h))
604 (setq tchar nil)
605 (gnus-score-insert-help "Match type" legal-types 2)))
606
607 (gnus-score-kill-help-buffer)
608 (unless (setq type (nth 1 (assq (downcase tchar) legal-types)))
609 (if mimic (error "%c %c" prefix hchar)
610 (error "Invalid match type"))))
611
612 (when (/= (downcase tchar) tchar)
613 ;; It was a majuscule, so we end reading and use the default.
614 (if mimic (message "%c %c %c" prefix hchar tchar)
615 (message ""))
616 (setq pchar (or pchar ?t)))
617
618 ;; We continue reading.
619 (while (not pchar)
620 (if mimic
621 (progn
622 (sit-for 1) (message "%c %c %c-" prefix hchar tchar))
623 (message "%s permanence (%s?): " (if increase "Increase" "Lower")
624 (mapconcat (lambda (s) (char-to-string (car s)))
625 char-to-perm "")))
626 (setq pchar (read-char))
627 (when (or (= pchar ??) (= pchar ?\C-h))
628 (setq pchar nil)
629 (gnus-score-insert-help "Match permanence" char-to-perm 2)))
630
631 (gnus-score-kill-help-buffer)
632 (if mimic (message "%c %c %c %c" prefix hchar tchar pchar)
633 (message ""))
634 (unless (setq temporary (cadr (assq pchar char-to-perm)))
635 ;; Deal with der(r)ided superannuated paradigms.
636 (when (and (eq (1+ prefix) 77)
637 (eq (+ hchar 12) 109)
638 (eq (1- tchar) 113)
639 (eq (- pchar 4) 111))
640 (error "You rang?"))
641 (if mimic
642 (error "%c %c %c %c" prefix hchar tchar pchar)
643 (error "Invalid match duration"))))
644 ;; Always kill the score help buffer.
645 (gnus-score-kill-help-buffer))
646
647 ;; If scoring an extra (non-standard overview) header,
648 ;; we must find out which header is in question.
649 (setq extra
650 (and gnus-extra-headers
651 (equal (nth 1 entry) "extra")
652 (intern ; need symbol
653 (gnus-completing-read-with-default
654 (symbol-name (car gnus-extra-headers)) ; default response
655 "Score extra header:" ; prompt
656 (mapcar (lambda (x) ; completion list
657 (cons (symbol-name x) x))
658 gnus-extra-headers)
659 nil ; no completion limit
660 t)))) ; require match
661 ;; extra is now nil or a symbol.
662
663 ;; We have all the data, so we enter this score.
664 (setq match (if (string= (nth 2 entry) "") ""
665 (gnus-summary-header (or (nth 2 entry) (nth 1 entry))
666 nil extra)))
667
668 ;; Modify the match, perhaps.
669 (cond
670 ((equal (nth 1 entry) "xref")
671 (when (string-match "^Xref: *" match)
672 (setq match (substring match (match-end 0))))
673 (when (string-match "^[^:]* +" match)
674 (setq match (substring match (match-end 0))))))
675
676 (when (memq type '(r R regexp Regexp))
677 (setq match (regexp-quote match)))
678
679 ;; Change score file to the "all.SCORE" file.
680 (when (eq symp 'a)
681 (save-excursion
682 (set-buffer gnus-summary-buffer)
683 (gnus-score-load-file
684 ;; This is a kludge; yes...
685 (cond
686 ((eq gnus-score-find-score-files-function
687 'gnus-score-find-hierarchical)
688 (gnus-score-file-name ""))
689 ((eq gnus-score-find-score-files-function 'gnus-score-find-single)
690 current-score-file)
691 (t
692 (gnus-score-file-name "all"))))))
693
694 (gnus-summary-score-entry
695 (nth 1 entry) ; Header
696 match ; Match
697 type ; Type
698 (if (eq score 's) nil score) ; Score
699 (if (eq temporary 'perm) ; Temp
700 nil
701 temporary)
702 (not (nth 3 entry)) ; Prompt
703 nil ; not silent
704 extra) ; non-standard overview.
705
706 (when (eq symp 'a)
707 ;; We change the score file back to the previous one.
708 (save-excursion
709 (set-buffer gnus-summary-buffer)
710 (gnus-score-load-file current-score-file)))))
711
712 (defun gnus-score-insert-help (string alist idx)
713 (setq gnus-score-help-winconf (current-window-configuration))
714 (save-excursion
715 (set-buffer (gnus-get-buffer-create "*Score Help*"))
716 (buffer-disable-undo)
717 (delete-windows-on (current-buffer))
718 (erase-buffer)
719 (insert string ":\n\n")
720 (let ((max -1)
721 (list alist)
722 (i 0)
723 n width pad format)
724 ;; find the longest string to display
725 (while list
726 (setq n (length (nth idx (car list))))
727 (unless (> max n)
728 (setq max n))
729 (setq list (cdr list)))
730 (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
731 (setq n (/ (1- (window-width)) max)) ; items per line
732 (setq width (/ (1- (window-width)) n)) ; width of each item
733 ;; insert `n' items, each in a field of width `width'
734 (while alist
735 (if (< i n)
736 ()
737 (setq i 0)
738 (delete-char -1) ; the `\n' takes a char
739 (insert "\n"))
740 (setq pad (- width 3))
741 (setq format (concat "%c: %-" (int-to-string pad) "s"))
742 (insert (format format (caar alist) (nth idx (car alist))))
743 (setq alist (cdr alist))
744 (setq i (1+ i))))
745 (goto-char (point-min))
746 ;; display ourselves in a small window at the bottom
747 (gnus-appt-select-lowest-window)
748 (if (< (/ (window-height) 2) window-min-height)
749 (switch-to-buffer "*Score Help*")
750 (split-window)
751 (pop-to-buffer "*Score Help*"))
752 (let ((window-min-height 1))
753 (shrink-window-if-larger-than-buffer))
754 (select-window (gnus-get-buffer-window gnus-summary-buffer t))))
755
756 (defun gnus-summary-header (header &optional no-err extra)
757 ;; Return HEADER for current articles, or error.
758 (let ((article (gnus-summary-article-number))
759 headers)
760 (if article
761 (if (and (setq headers (gnus-summary-article-header article))
762 (vectorp headers))
763 (if extra ; `header' must be "extra"
764 (or (cdr (assq extra (mail-header-extra headers))) "")
765 (aref headers (nth 1 (assoc header gnus-header-index))))
766 (if no-err
767 nil
768 (error "Pseudo-articles can't be scored")))
769 (if no-err
770 (error "No article on current line")
771 nil))))
772
773 (defun gnus-newsgroup-score-alist ()
774 (or
775 (let ((param-file (gnus-group-find-parameter
776 gnus-newsgroup-name 'score-file)))
777 (when param-file
778 (gnus-score-load param-file)))
779 (gnus-score-load
780 (gnus-score-file-name gnus-newsgroup-name)))
781 gnus-score-alist)
782
783 (defsubst gnus-score-get (symbol &optional alist)
784 ;; Get SYMBOL's definition in ALIST.
785 (cdr (assoc symbol
786 (or alist
787 gnus-score-alist
788 (gnus-newsgroup-score-alist)))))
789
790 (defun gnus-summary-score-entry (header match type score date
791 &optional prompt silent extra)
792 "Enter score file entry.
793 HEADER is the header being scored.
794 MATCH is the string we are looking for.
795 TYPE is the match type: substring, regexp, exact, fuzzy.
796 SCORE is the score to add.
797 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
798 If optional argument `PROMPT' is non-nil, allow user to edit match.
799 If optional argument `SILENT' is nil, show effect of score entry.
800 If optional argument `EXTRA' is non-nil, it's a non-standard overview header."
801 ;; Regexp is the default type.
802 (when (eq type t)
803 (setq type 'r))
804 ;; Simplify matches...
805 (cond ((or (eq type 'r) (eq type 's) (eq type nil))
806 (setq match (if match (gnus-simplify-subject-re match) "")))
807 ((eq type 'f)
808 (setq match (gnus-simplify-subject-fuzzy match))))
809 (let ((score (gnus-score-delta-default score))
810 (header (downcase header))
811 new)
812 (set-text-properties 0 (length header) nil header)
813 (when prompt
814 (setq match (read-string
815 (format "Match %s on %s, %s: "
816 (cond ((eq date 'now)
817 "now")
818 ((stringp date)
819 "temp")
820 (t "permanent"))
821 header
822 (if (< score 0) "lower" "raise"))
823 (if (numberp match)
824 (int-to-string match)
825 match))))
826
827 ;; If this is an integer comparison, we transform from string to int.
828 (if (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
829 (if (stringp match)
830 (setq match (string-to-number match)))
831 (set-text-properties 0 (length match) nil match))
832
833 (unless (eq date 'now)
834 ;; Add the score entry to the score file.
835 (when (= score gnus-score-interactive-default-score)
836 (setq score nil))
837 (let ((old (gnus-score-get header))
838 elem)
839 (setq new
840 (cond
841 (extra
842 (list match score
843 (and date (if (numberp date) date
844 (date-to-day date)))
845 type (symbol-name extra)))
846 (type
847 (list match score
848 (and date (if (numberp date) date
849 (date-to-day date)))
850 type))
851 (date (list match score (date-to-day date)))
852 (score (list match score))
853 (t (list match))))
854 ;; We see whether we can collapse some score entries.
855 ;; This isn't quite correct, because there may be more elements
856 ;; later on with the same key that have matching elems... Hm.
857 (if (and old
858 (setq elem (assoc match old))
859 (eq (nth 3 elem) (nth 3 new))
860 (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
861 (and (not (nth 2 elem)) (not (nth 2 new)))))
862 ;; Yup, we just add this new score to the old elem.
863 (setcar (cdr elem) (+ (or (nth 1 elem)
864 gnus-score-interactive-default-score)
865 (or (nth 1 new)
866 gnus-score-interactive-default-score)))
867 ;; Nope, we have to add a new elem.
868 (gnus-score-set header (if old (cons new old) (list new)) nil t))
869 (gnus-score-set 'touched '(t))))
870
871 ;; Score the current buffer.
872 (unless silent
873 (if (and (>= (nth 1 (assoc header gnus-header-index)) 0)
874 (eq (nth 2 (assoc header gnus-header-index))
875 'gnus-score-string))
876 (gnus-summary-score-effect header match type score extra)
877 (gnus-summary-rescore)))
878
879 ;; Return the new scoring rule.
880 new))
881
882 (defun gnus-summary-score-effect (header match type score &optional extra)
883 "Simulate the effect of a score file entry.
884 HEADER is the header being scored.
885 MATCH is the string we are looking for.
886 TYPE is the score type.
887 SCORE is the score to add.
888 EXTRA is the possible non-standard header."
889 (interactive (list (completing-read "Header: "
890 gnus-header-index
891 (lambda (x) (fboundp (nth 2 x)))
892 t)
893 (read-string "Match: ")
894 (if (y-or-n-p "Use regexp match? ") 'r 's)
895 (string-to-number (read-string "Score: "))))
896 (save-excursion
897 (unless (and (stringp match) (> (length match) 0))
898 (error "No match"))
899 (goto-char (point-min))
900 (let ((regexp (cond ((eq type 'f)
901 (gnus-simplify-subject-fuzzy match))
902 ((eq type 'r)
903 match)
904 ((eq type 'e)
905 (concat "\\`" (regexp-quote match) "\\'"))
906 (t
907 (regexp-quote match)))))
908 (while (not (eobp))
909 (let ((content (gnus-summary-header header 'noerr extra))
910 (case-fold-search t))
911 (and content
912 (when (if (eq type 'f)
913 (string-equal (gnus-simplify-subject-fuzzy content)
914 regexp)
915 (string-match regexp content))
916 (gnus-summary-raise-score score))))
917 (beginning-of-line 2))))
918 (gnus-set-mode-line 'summary))
919
920 (defun gnus-summary-score-crossposting (score date)
921 ;; Enter score file entry for current crossposting.
922 ;; SCORE is the score to add.
923 ;; DATE is the expire date.
924 (let ((xref (gnus-summary-header "xref"))
925 (start 0)
926 group)
927 (unless xref
928 (error "This article is not crossposted"))
929 (while (string-match " \\([^ \t]+\\):" xref start)
930 (setq start (match-end 0))
931 (when (not (string=
932 (setq group
933 (substring xref (match-beginning 1) (match-end 1)))
934 gnus-newsgroup-name))
935 (gnus-summary-score-entry
936 "xref" (concat " " group ":") nil score date t)))))
937
938 \f
939 ;;;
940 ;;; Gnus Score Files
941 ;;;
942
943 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
944
945 (defun gnus-score-set-mark-below (score)
946 "Automatically mark articles with score below SCORE as read."
947 (interactive
948 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
949 (string-to-number (read-string "Mark below: ")))))
950 (setq score (or score gnus-summary-default-score 0))
951 (gnus-score-set 'mark (list score))
952 (gnus-score-set 'touched '(t))
953 (setq gnus-summary-mark-below score)
954 (gnus-score-update-lines))
955
956 (defun gnus-score-update-lines ()
957 "Update all lines in the summary buffer."
958 (save-excursion
959 (goto-char (point-min))
960 (while (not (eobp))
961 (gnus-summary-update-line)
962 (forward-line 1))))
963
964 (defun gnus-score-update-all-lines ()
965 "Update all lines in the summary buffer, even the hidden ones."
966 (save-excursion
967 (goto-char (point-min))
968 (let (hidden)
969 (while (not (eobp))
970 (when (gnus-summary-show-thread)
971 (push (point) hidden))
972 (gnus-summary-update-line)
973 (forward-line 1))
974 ;; Re-hide the hidden threads.
975 (while hidden
976 (goto-char (pop hidden))
977 (gnus-summary-hide-thread)))))
978
979 (defun gnus-score-set-expunge-below (score)
980 "Automatically expunge articles with score below SCORE."
981 (interactive
982 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
983 (string-to-number (read-string "Set expunge below: ")))))
984 (setq score (or score gnus-summary-default-score 0))
985 (gnus-score-set 'expunge (list score))
986 (gnus-score-set 'touched '(t)))
987
988 (defun gnus-score-followup-article (&optional score)
989 "Add SCORE to all followups to the article in the current buffer."
990 (interactive "P")
991 (setq score (gnus-score-delta-default score))
992 (when (gnus-buffer-live-p gnus-summary-buffer)
993 (save-excursion
994 (save-restriction
995 (message-narrow-to-headers)
996 (let ((id (mail-fetch-field "message-id")))
997 (when id
998 (set-buffer gnus-summary-buffer)
999 (gnus-summary-score-entry
1000 "references" (concat id "[ \t]*$") 'r
1001 score (current-time-string) nil t)))))))
1002
1003 (defun gnus-score-followup-thread (&optional score)
1004 "Add SCORE to all later articles in the thread the current buffer is part of."
1005 (interactive "P")
1006 (setq score (gnus-score-delta-default score))
1007 (when (gnus-buffer-live-p gnus-summary-buffer)
1008 (save-excursion
1009 (save-restriction
1010 (goto-char (point-min))
1011 (let ((id (mail-fetch-field "message-id")))
1012 (when id
1013 (set-buffer gnus-summary-buffer)
1014 (gnus-summary-score-entry
1015 "references" id 's
1016 score (current-time-string))))))))
1017
1018 (defun gnus-score-set (symbol value &optional alist warn)
1019 ;; Set SYMBOL to VALUE in ALIST.
1020 (let* ((alist
1021 (or alist
1022 gnus-score-alist
1023 (gnus-newsgroup-score-alist)))
1024 (entry (assoc symbol alist)))
1025 (cond ((gnus-score-get 'read-only alist)
1026 ;; This is a read-only score file, so we do nothing.
1027 (when warn
1028 (gnus-message 4 "Note: read-only score file; entry discarded")))
1029 (entry
1030 (setcdr entry value))
1031 ((null alist)
1032 (error "Empty alist"))
1033 (t
1034 (setcdr alist
1035 (cons (cons symbol value) (cdr alist)))))))
1036
1037 (defun gnus-summary-raise-score (n)
1038 "Raise the score of the current article by N."
1039 (interactive "p")
1040 (gnus-summary-set-score (+ (gnus-summary-article-score)
1041 (or n gnus-score-interactive-default-score ))))
1042
1043 (defun gnus-summary-set-score (n)
1044 "Set the score of the current article to N."
1045 (interactive "p")
1046 (save-excursion
1047 (gnus-summary-show-thread)
1048 (let ((buffer-read-only nil))
1049 ;; Set score.
1050 (gnus-summary-update-mark
1051 (if (= n (or gnus-summary-default-score 0)) ? ;Whitespace
1052 (if (< n (or gnus-summary-default-score 0))
1053 gnus-score-below-mark gnus-score-over-mark))
1054 'score))
1055 (let* ((article (gnus-summary-article-number))
1056 (score (assq article gnus-newsgroup-scored)))
1057 (if score (setcdr score n)
1058 (push (cons article n) gnus-newsgroup-scored)))
1059 (gnus-summary-update-line)))
1060
1061 (defun gnus-summary-current-score ()
1062 "Return the score of the current article."
1063 (interactive)
1064 (gnus-message 1 "%s" (gnus-summary-article-score)))
1065
1066 (defun gnus-score-change-score-file (file)
1067 "Change current score alist."
1068 (interactive
1069 (list (read-file-name "Change to score file: " gnus-kill-files-directory)))
1070 (gnus-score-load-file file)
1071 (gnus-set-mode-line 'summary))
1072
1073 (defvar gnus-score-edit-exit-function)
1074 (defun gnus-score-edit-current-scores (file)
1075 "Edit the current score alist."
1076 (interactive (list gnus-current-score-file))
1077 (if (not gnus-current-score-file)
1078 (error "No current score file")
1079 (let ((winconf (current-window-configuration)))
1080 (when (buffer-name gnus-summary-buffer)
1081 (gnus-score-save))
1082 (gnus-make-directory (file-name-directory file))
1083 (setq gnus-score-edit-buffer (find-file-noselect file))
1084 (gnus-configure-windows 'edit-score)
1085 (gnus-score-mode)
1086 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1087 (make-local-variable 'gnus-prev-winconf)
1088 (setq gnus-prev-winconf winconf))
1089 (gnus-message
1090 4 (substitute-command-keys
1091 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits"))))
1092
1093 (defun gnus-score-edit-file (file)
1094 "Edit a score file."
1095 (interactive
1096 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
1097 (gnus-make-directory (file-name-directory file))
1098 (when (buffer-name gnus-summary-buffer)
1099 (gnus-score-save))
1100 (let ((winconf (current-window-configuration)))
1101 (setq gnus-score-edit-buffer (find-file-noselect file))
1102 (gnus-configure-windows 'edit-score)
1103 (gnus-score-mode)
1104 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1105 (make-local-variable 'gnus-prev-winconf)
1106 (setq gnus-prev-winconf winconf))
1107 (gnus-message
1108 4 (substitute-command-keys
1109 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
1110
1111 (defun gnus-score-edit-file-at-point (&optional format)
1112 "Edit score file at point in Score Trace buffers.
1113 If FORMAT, also format the current score file."
1114 (let* ((rule (save-excursion
1115 (beginning-of-line)
1116 (read (current-buffer))))
1117 (sep "[ \n\r\t]*")
1118 ;; Must be synced with `gnus-score-find-trace':
1119 (reg " -> +")
1120 (file (save-excursion
1121 (end-of-line)
1122 (if (and (re-search-backward reg (gnus-point-at-bol) t)
1123 (re-search-forward reg (gnus-point-at-eol) t))
1124 (buffer-substring (point) (gnus-point-at-eol))
1125 nil))))
1126 (if (or (not file)
1127 (string-match "\\<\\(non-file rule\\|A file\\)\\>" file)
1128 ;; (see `gnus-score-find-trace' and `gnus-score-advanced')
1129 (string= "" file))
1130 (gnus-error 3 "Can't find a score file in current line.")
1131 (gnus-score-edit-file file)
1132 (when format
1133 (gnus-score-pretty-print))
1134 (when (consp rule) ;; the rule exists
1135 (setq rule (mapconcat #'(lambda (obj)
1136 (regexp-quote (format "%S" obj)))
1137 rule
1138 sep))
1139 (goto-char (point-min))
1140 (re-search-forward rule nil t)
1141 ;; make it easy to use `kill-sexp':
1142 (goto-char (1- (match-beginning 0)))))))
1143
1144 (defun gnus-score-load-file (file)
1145 ;; Load score file FILE. Returns a list a retrieved score-alists.
1146 (let* ((file (expand-file-name
1147 (or (and (string-match
1148 (concat "^" (regexp-quote
1149 (expand-file-name
1150 gnus-kill-files-directory)))
1151 (expand-file-name file))
1152 file)
1153 (expand-file-name file gnus-kill-files-directory))))
1154 (cached (assoc file gnus-score-cache))
1155 (global (member file gnus-internal-global-score-files))
1156 lists alist)
1157 (if cached
1158 ;; The score file was already loaded.
1159 (setq alist (cdr cached))
1160 ;; We load the score file.
1161 (setq gnus-score-alist nil)
1162 (setq alist (gnus-score-load-score-alist file))
1163 ;; We add '(touched) to the alist to signify that it hasn't been
1164 ;; touched (yet).
1165 (unless (assq 'touched alist)
1166 (push (list 'touched nil) alist))
1167 ;; If it is a global score file, we make it read-only.
1168 (and global
1169 (not (assq 'read-only alist))
1170 (push (list 'read-only t) alist))
1171 (push (cons file alist) gnus-score-cache))
1172 (let ((a alist)
1173 found)
1174 (while a
1175 ;; Downcase all header names.
1176 (cond
1177 ((stringp (caar a))
1178 (setcar (car a) (downcase (caar a)))
1179 (setq found t))
1180 ;; Advanced scoring.
1181 ((consp (caar a))
1182 (setq found t)))
1183 (pop a))
1184 ;; If there are actual scores in the alist, we add it to the
1185 ;; return value of this function.
1186 (when found
1187 (setq lists (list alist))))
1188 ;; Treat the other possible atoms in the score alist.
1189 (let ((mark (car (gnus-score-get 'mark alist)))
1190 (expunge (car (gnus-score-get 'expunge alist)))
1191 (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
1192 (files (gnus-score-get 'files alist))
1193 (exclude-files (gnus-score-get 'exclude-files alist))
1194 (orphan (car (gnus-score-get 'orphan alist)))
1195 (adapt (gnus-score-get 'adapt alist))
1196 (thread-mark-and-expunge
1197 (car (gnus-score-get 'thread-mark-and-expunge alist)))
1198 (adapt-file (car (gnus-score-get 'adapt-file alist)))
1199 (local (gnus-score-get 'local alist))
1200 (decay (car (gnus-score-get 'decay alist)))
1201 (eval (car (gnus-score-get 'eval alist))))
1202 ;; Perform possible decays.
1203 (when (and gnus-decay-scores
1204 (or cached (file-exists-p file))
1205 (or (not decay)
1206 (gnus-decay-scores alist decay)))
1207 (gnus-score-set 'touched '(t) alist)
1208 (gnus-score-set 'decay (list (time-to-days (current-time))) alist))
1209 ;; We do not respect eval and files atoms from global score
1210 ;; files.
1211 (when (and files (not global))
1212 (setq lists (apply 'append lists
1213 (mapcar (lambda (file)
1214 (gnus-score-load-file file))
1215 (if adapt-file (cons adapt-file files)
1216 files)))))
1217 (when (and eval (not global))
1218 (eval eval))
1219 ;; We then expand any exclude-file directives.
1220 (setq gnus-scores-exclude-files
1221 (nconc
1222 (apply
1223 'nconc
1224 (mapcar
1225 (lambda (sfile)
1226 (list
1227 (expand-file-name sfile (file-name-directory file))
1228 (expand-file-name sfile gnus-kill-files-directory)))
1229 exclude-files))
1230 gnus-scores-exclude-files))
1231 (when local
1232 (save-excursion
1233 (set-buffer gnus-summary-buffer)
1234 (while local
1235 (and (consp (car local))
1236 (symbolp (caar local))
1237 (progn
1238 (make-local-variable (caar local))
1239 (set (caar local) (nth 1 (car local)))))
1240 (setq local (cdr local)))))
1241 (when orphan
1242 (setq gnus-orphan-score orphan))
1243 (setq gnus-adaptive-score-alist
1244 (cond ((equal adapt '(t))
1245 (setq gnus-newsgroup-adaptive t)
1246 gnus-default-adaptive-score-alist)
1247 ((equal adapt '(ignore))
1248 (setq gnus-newsgroup-adaptive nil))
1249 ((consp adapt)
1250 (setq gnus-newsgroup-adaptive t)
1251 adapt)
1252 (t
1253 gnus-default-adaptive-score-alist)))
1254 (setq gnus-thread-expunge-below
1255 (or thread-mark-and-expunge gnus-thread-expunge-below))
1256 (setq gnus-summary-mark-below
1257 (or mark mark-and-expunge gnus-summary-mark-below))
1258 (setq gnus-summary-expunge-below
1259 (or expunge mark-and-expunge gnus-summary-expunge-below))
1260 (setq gnus-newsgroup-adaptive-score-file
1261 (or adapt-file gnus-newsgroup-adaptive-score-file)))
1262 (setq gnus-current-score-file file)
1263 (setq gnus-score-alist alist)
1264 lists))
1265
1266 (defun gnus-score-load (file)
1267 ;; Load score FILE.
1268 (let ((cache (assoc file gnus-score-cache)))
1269 (if cache
1270 (setq gnus-score-alist (cdr cache))
1271 (setq gnus-score-alist nil)
1272 (gnus-score-load-score-alist file)
1273 (unless gnus-score-alist
1274 (setq gnus-score-alist (copy-alist '((touched nil)))))
1275 (push (cons file gnus-score-alist) gnus-score-cache))))
1276
1277 (defun gnus-score-remove-from-cache (file)
1278 (setq gnus-score-cache
1279 (delq (assoc file gnus-score-cache) gnus-score-cache)))
1280
1281 (defun gnus-score-load-score-alist (file)
1282 "Read score FILE."
1283 (let (alist)
1284 (if (not (file-readable-p file))
1285 ;; Couldn't read file.
1286 (setq gnus-score-alist nil)
1287 ;; Read file.
1288 (with-temp-buffer
1289 (let ((coding-system-for-read score-mode-coding-system))
1290 (insert-file-contents file))
1291 (goto-char (point-min))
1292 ;; Only do the loading if the score file isn't empty.
1293 (when (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
1294 (setq alist
1295 (condition-case ()
1296 (read (current-buffer))
1297 (error
1298 (gnus-error 3.2 "Problem with score file %s" file))))))
1299 (cond
1300 ((and alist
1301 (atom alist))
1302 ;; Bogus score file.
1303 (error "Invalid syntax with score file %s" file))
1304 ((eq (car alist) 'setq)
1305 ;; This is an old-style score file.
1306 (setq gnus-score-alist (gnus-score-transform-old-to-new alist)))
1307 (t
1308 (setq gnus-score-alist alist)))
1309 ;; Check the syntax of the score file.
1310 (setq gnus-score-alist
1311 (gnus-score-check-syntax gnus-score-alist file)))))
1312
1313 (defun gnus-score-check-syntax (alist file)
1314 "Check the syntax of the score ALIST."
1315 (cond
1316 ((null alist)
1317 nil)
1318 ((not (consp alist))
1319 (gnus-message 1 "Score file is not a list: %s" file)
1320 (ding)
1321 nil)
1322 (t
1323 (let ((a alist)
1324 sr err s type)
1325 (while (and a (not err))
1326 (setq
1327 err
1328 (cond
1329 ((not (listp (car a)))
1330 (format "Invalid score element %s in %s" (car a) file))
1331 ((stringp (caar a))
1332 (cond
1333 ((not (listp (setq sr (cdar a))))
1334 (format "Invalid header match %s in %s" (nth 1 (car a)) file))
1335 (t
1336 (setq type (caar a))
1337 (while (and sr (not err))
1338 (setq s (pop sr))
1339 (setq
1340 err
1341 (cond
1342 ((if (member (downcase type) '("lines" "chars"))
1343 (not (numberp (car s)))
1344 (not (stringp (car s))))
1345 (format "Invalid match %s in %s" (car s) file))
1346 ((and (cadr s) (not (integerp (cadr s))))
1347 (format "Non-integer score %s in %s" (cadr s) file))
1348 ((and (caddr s) (not (integerp (caddr s))))
1349 (format "Non-integer date %s in %s" (caddr s) file))
1350 ((and (cadddr s) (not (symbolp (cadddr s))))
1351 (format "Non-symbol match type %s in %s" (cadddr s) file)))))
1352 err)))))
1353 (setq a (cdr a)))
1354 (if err
1355 (progn
1356 (ding)
1357 (gnus-message 3 err)
1358 (sit-for 2)
1359 nil)
1360 alist)))))
1361
1362 (defun gnus-score-transform-old-to-new (alist)
1363 (let* ((alist (nth 2 alist))
1364 out entry)
1365 (when (eq (car alist) 'quote)
1366 (setq alist (nth 1 alist)))
1367 (while alist
1368 (setq entry (car alist))
1369 (if (stringp (car entry))
1370 (let ((scor (cdr entry)))
1371 (push entry out)
1372 (while scor
1373 (setcar scor
1374 (list (caar scor) (nth 2 (car scor))
1375 (and (nth 3 (car scor))
1376 (date-to-day (nth 3 (car scor))))
1377 (if (nth 1 (car scor)) 'r 's)))
1378 (setq scor (cdr scor))))
1379 (push (if (not (listp (cdr entry)))
1380 (list (car entry) (cdr entry))
1381 entry)
1382 out))
1383 (setq alist (cdr alist)))
1384 (cons (list 'touched t) (nreverse out))))
1385
1386 (defun gnus-score-save ()
1387 ;; Save all score information.
1388 (let ((cache gnus-score-cache)
1389 entry score file)
1390 (save-excursion
1391 (setq gnus-score-alist nil)
1392 (nnheader-set-temp-buffer " *Gnus Scores*")
1393 (while cache
1394 (current-buffer)
1395 (setq entry (pop cache)
1396 file (nnheader-translate-file-chars (car entry) t)
1397 score (cdr entry))
1398 (if (or (not (equal (gnus-score-get 'touched score) '(t)))
1399 (gnus-score-get 'read-only score)
1400 (and (file-exists-p file)
1401 (not (file-writable-p file))))
1402 ()
1403 (setq score (setcdr entry (gnus-delete-alist 'touched score)))
1404 (erase-buffer)
1405 (let (emacs-lisp-mode-hook)
1406 (if (string-match
1407 (concat (regexp-quote gnus-adaptive-file-suffix) "$")
1408 file)
1409 ;; This is an adaptive score file, so we do not run
1410 ;; it through `pp'. These files can get huge, and
1411 ;; are not meant to be edited by human hands.
1412 (gnus-prin1 score)
1413 ;; This is a normal score file, so we print it very
1414 ;; prettily.
1415 (let ((lisp-mode-syntax-table score-mode-syntax-table))
1416 (gnus-pp score))))
1417 (gnus-make-directory (file-name-directory file))
1418 ;; If the score file is empty, we delete it.
1419 (if (zerop (buffer-size))
1420 (delete-file file)
1421 ;; There are scores, so we write the file.
1422 (when (file-writable-p file)
1423 (let ((coding-system-for-write score-mode-coding-system))
1424 (gnus-write-buffer file))
1425 (when gnus-score-after-write-file-function
1426 (funcall gnus-score-after-write-file-function file)))))
1427 (and gnus-score-uncacheable-files
1428 (string-match gnus-score-uncacheable-files file)
1429 (gnus-score-remove-from-cache file)))
1430 (kill-buffer (current-buffer)))))
1431
1432 (defun gnus-score-load-files (score-files)
1433 "Load all score files in SCORE-FILES."
1434 ;; Load the score files.
1435 (let (scores)
1436 (while score-files
1437 (if (stringp (car score-files))
1438 ;; It is a string, which means that it's a score file name,
1439 ;; so we load the score file and add the score alist to
1440 ;; the list of alists.
1441 (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
1442 ;; It is an alist, so we just add it to the list directly.
1443 (setq scores (nconc (car score-files) scores)))
1444 (setq score-files (cdr score-files)))
1445 ;; Prune the score files that are to be excluded, if any.
1446 (when gnus-scores-exclude-files
1447 (let ((s scores)
1448 c)
1449 (while s
1450 (and (setq c (rassq (car s) gnus-score-cache))
1451 (member (car c) gnus-scores-exclude-files)
1452 (setq scores (delq (car s) scores)))
1453 (setq s (cdr s)))))
1454 scores))
1455
1456 (defun gnus-score-headers (score-files &optional trace)
1457 ;; Score `gnus-newsgroup-headers'.
1458 (let (scores news)
1459 ;; PLM: probably this is not the best place to clear orphan-score
1460 (setq gnus-orphan-score nil
1461 gnus-scores-articles nil
1462 gnus-scores-exclude-files nil
1463 scores (gnus-score-load-files score-files))
1464 (setq news scores)
1465 ;; Do the scoring.
1466 (while news
1467 (setq scores news
1468 news nil)
1469 (when (and gnus-summary-default-score
1470 scores)
1471 (let* ((entries gnus-header-index)
1472 (now (date-to-day (current-time-string)))
1473 (expire (and gnus-score-expiry-days
1474 (- now gnus-score-expiry-days)))
1475 (headers gnus-newsgroup-headers)
1476 (current-score-file gnus-current-score-file)
1477 entry header new)
1478 (gnus-message 7 "Scoring...")
1479 ;; Create articles, an alist of the form `(HEADER . SCORE)'.
1480 (while (setq header (pop headers))
1481 ;; WARNING: The assq makes the function O(N*S) while it could
1482 ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
1483 ;; and S is (length gnus-newsgroup-scored).
1484 (unless (assq (mail-header-number header) gnus-newsgroup-scored)
1485 (setq gnus-scores-articles ;Total of 2 * N cons-cells used.
1486 (cons (cons header (or gnus-summary-default-score 0))
1487 gnus-scores-articles))))
1488
1489 (save-excursion
1490 (set-buffer (gnus-get-buffer-create "*Headers*"))
1491 (buffer-disable-undo)
1492 (when (gnus-buffer-live-p gnus-summary-buffer)
1493 (message-clone-locals gnus-summary-buffer))
1494
1495 ;; Set the global variant of this variable.
1496 (setq gnus-current-score-file current-score-file)
1497 ;; score orphans
1498 (when gnus-orphan-score
1499 (setq gnus-score-index
1500 (nth 1 (assoc "references" gnus-header-index)))
1501 (gnus-score-orphans gnus-orphan-score))
1502 ;; Run each header through the score process.
1503 (while entries
1504 (setq entry (pop entries)
1505 header (nth 0 entry)
1506 gnus-score-index (nth 1 (assoc header gnus-header-index)))
1507 (when (< 0 (apply 'max (mapcar
1508 (lambda (score)
1509 (length (gnus-score-get header score)))
1510 scores)))
1511 ;; Call the scoring function for this type of "header".
1512 (when (setq new (funcall (nth 2 entry) scores header
1513 now expire trace))
1514 (push new news))))
1515 (when (gnus-buffer-live-p gnus-summary-buffer)
1516 (let ((scored gnus-newsgroup-scored))
1517 (with-current-buffer gnus-summary-buffer
1518 (setq gnus-newsgroup-scored scored))))
1519 ;; Remove the buffer.
1520 (gnus-kill-buffer (current-buffer)))
1521
1522 ;; Add articles to `gnus-newsgroup-scored'.
1523 (while gnus-scores-articles
1524 (when (or (/= gnus-summary-default-score
1525 (cdar gnus-scores-articles))
1526 gnus-save-score)
1527 (push (cons (mail-header-number (caar gnus-scores-articles))
1528 (cdar gnus-scores-articles))
1529 gnus-newsgroup-scored))
1530 (setq gnus-scores-articles (cdr gnus-scores-articles)))
1531
1532 (let (score)
1533 (while (setq score (pop scores))
1534 (while score
1535 (when (consp (caar score))
1536 (gnus-score-advanced (car score) trace))
1537 (pop score))))
1538
1539 (gnus-message 7 "Scoring...done"))))))
1540
1541 (defun gnus-score-lower-thread (thread score-adjust)
1542 "Lower the score on THREAD with SCORE-ADJUST.
1543 THREAD is expected to contain a list of the form `(PARENT [CHILD1
1544 CHILD2 ...])' where PARENT is a header array and each CHILD is a list
1545 of the same form as THREAD. The empty list nil is valid. For each
1546 article in the tree, the score of the corresponding entry in
1547 `gnus-newsgroup-scored' is adjusted by SCORE-ADJUST."
1548 (while thread
1549 (let ((head (car thread)))
1550 (if (listp head)
1551 ;; handle a child and its descendants
1552 (gnus-score-lower-thread head score-adjust)
1553 ;; handle the parent
1554 (let* ((article (mail-header-number head))
1555 (score (assq article gnus-newsgroup-scored)))
1556 (if score (setcdr score (+ (cdr score) score-adjust))
1557 (push (cons article score-adjust) gnus-newsgroup-scored)))))
1558 (setq thread (cdr thread))))
1559
1560 (defun gnus-score-orphans (score)
1561 "Score orphans.
1562 A root is an article with no references. An orphan is an article
1563 which has references, but is not connected via its references to a
1564 root article. This function finds all the orphans, and adjusts their
1565 score in `gnus-newsgroup-scored' by SCORE."
1566 ;; gnus-make-threads produces a list, where each entry is a "thread"
1567 ;; as described in the gnus-score-lower-thread docs. This function
1568 ;; will be called again (after limiting has been done) if the display
1569 ;; is threaded. It would be nice to somehow save this info and use
1570 ;; it later.
1571 (dolist (thread (gnus-make-threads))
1572 (let ((id (aref (car thread) gnus-score-index)))
1573 ;; If the parent of the thread is not a root, lower the score of
1574 ;; it and its descendants. Note that some roots seem to satisfy
1575 ;; (eq id nil) and some (eq id ""); not sure why.
1576 (when (and id
1577 (not (string= id "")))
1578 (gnus-score-lower-thread thread score)))))
1579
1580 (defun gnus-score-integer (scores header now expire &optional trace)
1581 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1582 entries alist)
1583 ;; Find matches.
1584 (while scores
1585 (setq alist (car scores)
1586 scores (cdr scores)
1587 entries (assoc header alist))
1588 (while (cdr entries) ;First entry is the header index.
1589 (let* ((rest (cdr entries))
1590 (kill (car rest))
1591 (match (nth 0 kill))
1592 (type (or (nth 3 kill) '>))
1593 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1594 (date (nth 2 kill))
1595 (found nil)
1596 (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
1597 (eq type '>=) (eq type '=))
1598 type
1599 (error "Invalid match type: %s" type)))
1600 (articles gnus-scores-articles))
1601 ;; Instead of doing all the clever stuff that
1602 ;; `gnus-score-string' does to minimize searches and stuff,
1603 ;; I will assume that people generally will put so few
1604 ;; matches on numbers that any cleverness will take more
1605 ;; time than one would gain.
1606 (while articles
1607 (when (funcall match-func
1608 (or (aref (caar articles) gnus-score-index) 0)
1609 match)
1610 (when trace
1611 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1612 gnus-score-trace))
1613 (setq found t)
1614 (setcdr (car articles) (+ score (cdar articles))))
1615 (setq articles (cdr articles)))
1616 ;; Update expire date
1617 (cond ((null date)) ;Permanent entry.
1618 ((and found gnus-update-score-entry-dates) ;Match, update date.
1619 (gnus-score-set 'touched '(t) alist)
1620 (setcar (nthcdr 2 kill) now))
1621 ((and expire (< date expire)) ;Old entry, remove.
1622 (gnus-score-set 'touched '(t) alist)
1623 (setcdr entries (cdr rest))
1624 (setq rest entries)))
1625 (setq entries rest)))))
1626 nil)
1627
1628 (defun gnus-score-date (scores header now expire &optional trace)
1629 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1630 entries alist match match-func article)
1631 ;; Find matches.
1632 (while scores
1633 (setq alist (car scores)
1634 scores (cdr scores)
1635 entries (assoc header alist))
1636 (while (cdr entries) ;First entry is the header index.
1637 (let* ((rest (cdr entries))
1638 (kill (car rest))
1639 (type (or (nth 3 kill) 'before))
1640 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1641 (date (nth 2 kill))
1642 (found nil)
1643 (articles gnus-scores-articles)
1644 l)
1645 (cond
1646 ((eq type 'after)
1647 (setq match-func 'string<
1648 match (gnus-date-iso8601 (nth 0 kill))))
1649 ((eq type 'before)
1650 (setq match-func 'gnus-string>
1651 match (gnus-date-iso8601 (nth 0 kill))))
1652 ((eq type 'at)
1653 (setq match-func 'string=
1654 match (gnus-date-iso8601 (nth 0 kill))))
1655 ((eq type 'regexp)
1656 (setq match-func 'string-match
1657 match (nth 0 kill)))
1658 (t (error "Invalid match type: %s" type)))
1659 ;; Instead of doing all the clever stuff that
1660 ;; `gnus-score-string' does to minimize searches and stuff,
1661 ;; I will assume that people generally will put so few
1662 ;; matches on numbers that any cleverness will take more
1663 ;; time than one would gain.
1664 (while (setq article (pop articles))
1665 (when (and
1666 (setq l (aref (car article) gnus-score-index))
1667 (funcall match-func match (gnus-date-iso8601 l)))
1668 (when trace
1669 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1670 gnus-score-trace))
1671 (setq found t)
1672 (setcdr article (+ score (cdr article)))))
1673 ;; Update expire date
1674 (cond ((null date)) ;Permanent entry.
1675 ((and found gnus-update-score-entry-dates) ;Match, update date.
1676 (gnus-score-set 'touched '(t) alist)
1677 (setcar (nthcdr 2 kill) now))
1678 ((and expire (< date expire)) ;Old entry, remove.
1679 (gnus-score-set 'touched '(t) alist)
1680 (setcdr entries (cdr rest))
1681 (setq rest entries)))
1682 (setq entries rest)))))
1683 nil)
1684
1685 (defun gnus-score-body (scores header now expire &optional trace)
1686 (if gnus-agent-fetching
1687 nil
1688 (save-excursion
1689 (setq gnus-scores-articles
1690 (sort gnus-scores-articles
1691 (lambda (a1 a2)
1692 (< (mail-header-number (car a1))
1693 (mail-header-number (car a2))))))
1694 (set-buffer nntp-server-buffer)
1695 (save-restriction
1696 (let* ((buffer-read-only nil)
1697 (articles gnus-scores-articles)
1698 (all-scores scores)
1699 (request-func (cond ((string= "head" header)
1700 'gnus-request-head)
1701 ((string= "body" header)
1702 'gnus-request-body)
1703 (t 'gnus-request-article)))
1704 entries alist ofunc article last)
1705 (when articles
1706 (setq last (mail-header-number (caar (last articles))))
1707 ;; Not all backends support partial fetching. In that case,
1708 ;; we just fetch the entire article.
1709 (unless (gnus-check-backend-function
1710 (and (string-match "^gnus-" (symbol-name request-func))
1711 (intern (substring (symbol-name request-func)
1712 (match-end 0))))
1713 gnus-newsgroup-name)
1714 (setq ofunc request-func)
1715 (setq request-func 'gnus-request-article))
1716 (while articles
1717 (setq article (mail-header-number (caar articles)))
1718 (gnus-message 7 "Scoring article %s of %s..." article last)
1719 (widen)
1720 (when (funcall request-func article gnus-newsgroup-name)
1721 (goto-char (point-min))
1722 ;; If just parts of the article is to be searched, but the
1723 ;; backend didn't support partial fetching, we just narrow
1724 ;; to the relevant parts.
1725 (when ofunc
1726 (if (eq ofunc 'gnus-request-head)
1727 (narrow-to-region
1728 (point)
1729 (or (search-forward "\n\n" nil t) (point-max)))
1730 (narrow-to-region
1731 (or (search-forward "\n\n" nil t) (point))
1732 (point-max))))
1733 (setq scores all-scores)
1734 ;; Find matches.
1735 (while scores
1736 (setq alist (pop scores)
1737 entries (assoc header alist))
1738 (while (cdr entries) ;First entry is the header index.
1739 (let* ((rest (cdr entries))
1740 (kill (car rest))
1741 (match (nth 0 kill))
1742 (type (or (nth 3 kill) 's))
1743 (score (or (nth 1 kill)
1744 gnus-score-interactive-default-score))
1745 (date (nth 2 kill))
1746 (found nil)
1747 (case-fold-search
1748 (not (or (eq type 'R) (eq type 'S)
1749 (eq type 'Regexp) (eq type 'String))))
1750 (search-func
1751 (cond ((or (eq type 'r) (eq type 'R)
1752 (eq type 'regexp) (eq type 'Regexp))
1753 're-search-forward)
1754 ((or (eq type 's) (eq type 'S)
1755 (eq type 'string) (eq type 'String))
1756 'search-forward)
1757 (t
1758 (error "Invalid match type: %s" type)))))
1759 (goto-char (point-min))
1760 (when (funcall search-func match nil t)
1761 ;; Found a match, update scores.
1762 (setcdr (car articles) (+ score (cdar articles)))
1763 (setq found t)
1764 (when trace
1765 (push
1766 (cons (car-safe (rassq alist gnus-score-cache))
1767 kill)
1768 gnus-score-trace)))
1769 ;; Update expire date
1770 (unless trace
1771 (cond
1772 ((null date)) ;Permanent entry.
1773 ((and found gnus-update-score-entry-dates)
1774 ;; Match, update date.
1775 (gnus-score-set 'touched '(t) alist)
1776 (setcar (nthcdr 2 kill) now))
1777 ((and expire (< date expire)) ;Old entry, remove.
1778 (gnus-score-set 'touched '(t) alist)
1779 (setcdr entries (cdr rest))
1780 (setq rest entries))))
1781 (setq entries rest)))))
1782 (setq articles (cdr articles)))))))
1783 nil))
1784
1785 (defun gnus-score-thread (scores header now expire &optional trace)
1786 (gnus-score-followup scores header now expire trace t))
1787
1788 (defun gnus-score-followup (scores header now expire &optional trace thread)
1789 (if gnus-agent-fetching
1790 ;; FIXME: It seems doable in fetching mode.
1791 nil
1792 ;; Insert the unique article headers in the buffer.
1793 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1794 (current-score-file gnus-current-score-file)
1795 (all-scores scores)
1796 ;; gnus-score-index is used as a free variable.
1797 alike last this art entries alist articles
1798 new news)
1799
1800 ;; Change score file to the adaptive score file. All entries that
1801 ;; this function makes will be put into this file.
1802 (save-excursion
1803 (set-buffer gnus-summary-buffer)
1804 (gnus-score-load-file
1805 (or gnus-newsgroup-adaptive-score-file
1806 (gnus-score-file-name
1807 gnus-newsgroup-name gnus-adaptive-file-suffix))))
1808
1809 (setq gnus-scores-articles (sort gnus-scores-articles
1810 'gnus-score-string<)
1811 articles gnus-scores-articles)
1812
1813 (erase-buffer)
1814 (while articles
1815 (setq art (car articles)
1816 this (aref (car art) gnus-score-index)
1817 articles (cdr articles))
1818 (if (equal last this)
1819 (push art alike)
1820 (when last
1821 (insert last ?\n)
1822 (put-text-property (1- (point)) (point) 'articles alike))
1823 (setq alike (list art)
1824 last this)))
1825 (when last ; Bwadr, duplicate code.
1826 (insert last ?\n)
1827 (put-text-property (1- (point)) (point) 'articles alike))
1828
1829 ;; Find matches.
1830 (while scores
1831 (setq alist (car scores)
1832 scores (cdr scores)
1833 entries (assoc header alist))
1834 (while (cdr entries) ;First entry is the header index.
1835 (let* ((rest (cdr entries))
1836 (kill (car rest))
1837 (match (nth 0 kill))
1838 (type (or (nth 3 kill) 's))
1839 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1840 (date (nth 2 kill))
1841 (found nil)
1842 (mt (aref (symbol-name type) 0))
1843 (case-fold-search
1844 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1845 (dmt (downcase mt))
1846 (search-func
1847 (cond ((= dmt ?r) 're-search-forward)
1848 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1849 (t (error "Invalid match type: %s" type))))
1850 arts art)
1851 (goto-char (point-min))
1852 (if (= dmt ?e)
1853 (while (funcall search-func match nil t)
1854 (and (= (gnus-point-at-bol)
1855 (match-beginning 0))
1856 (= (progn (end-of-line) (point))
1857 (match-end 0))
1858 (progn
1859 (setq found (setq arts (get-text-property
1860 (point) 'articles)))
1861 ;; Found a match, update scores.
1862 (while arts
1863 (setq art (car arts)
1864 arts (cdr arts))
1865 (gnus-score-add-followups
1866 (car art) score all-scores thread))))
1867 (end-of-line))
1868 (while (funcall search-func match nil t)
1869 (end-of-line)
1870 (setq found (setq arts (get-text-property (point) 'articles)))
1871 ;; Found a match, update scores.
1872 (while (setq art (pop arts))
1873 (setcdr art (+ score (cdr art)))
1874 (when trace
1875 (push (cons
1876 (car-safe (rassq alist gnus-score-cache))
1877 kill)
1878 gnus-score-trace))
1879 (when (setq new (gnus-score-add-followups
1880 (car art) score all-scores thread))
1881 (push new news)))))
1882 ;; Update expire date
1883 (cond ((null date)) ;Permanent entry.
1884 ((and found gnus-update-score-entry-dates)
1885 ;Match, update date.
1886 (gnus-score-set 'touched '(t) alist)
1887 (setcar (nthcdr 2 kill) now))
1888 ((and expire (< date expire)) ;Old entry, remove.
1889 (gnus-score-set 'touched '(t) alist)
1890 (setcdr entries (cdr rest))
1891 (setq rest entries)))
1892 (setq entries rest))))
1893 ;; We change the score file back to the previous one.
1894 (save-excursion
1895 (set-buffer gnus-summary-buffer)
1896 (gnus-score-load-file current-score-file))
1897 (list (cons "references" news)))))
1898
1899 (defun gnus-score-add-followups (header score scores &optional thread)
1900 "Add a score entry to the adapt file."
1901 (save-excursion
1902 (set-buffer gnus-summary-buffer)
1903 (let* ((id (mail-header-id header))
1904 (scores (car scores))
1905 entry dont)
1906 ;; Don't enter a score if there already is one.
1907 (while (setq entry (pop scores))
1908 (and (equal "references" (car entry))
1909 (or (null (nth 3 (cadr entry)))
1910 (eq 's (nth 3 (cadr entry))))
1911 (assoc id entry)
1912 (setq dont t)))
1913 (unless dont
1914 (gnus-summary-score-entry
1915 (if thread "thread" "references")
1916 id 's score (current-time-string) nil t)))))
1917
1918 (defun gnus-score-string (score-list header now expire &optional trace)
1919 ;; Score ARTICLES according to HEADER in SCORE-LIST.
1920 ;; Update matching entries to NOW and remove unmatched entries older
1921 ;; than EXPIRE.
1922
1923 ;; Insert the unique article headers in the buffer.
1924 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1925 ;; gnus-score-index is used as a free variable.
1926 (simplify (and gnus-score-thread-simplify
1927 (string= "subject" header)))
1928 alike last this art entries alist articles
1929 fuzzies arts words kill)
1930
1931 ;; Sorting the articles costs os O(N*log N) but will allow us to
1932 ;; only match with each unique header. Thus the actual matching
1933 ;; will be O(M*U) where M is the number of strings to match with,
1934 ;; and U is the number of unique headers. It is assumed (but
1935 ;; untested) this will be a net win because of the large constant
1936 ;; factor involved with string matching.
1937 (setq gnus-scores-articles
1938 ;; We cannot string-sort the extra headers list. *sigh*
1939 (if (= gnus-score-index 9)
1940 gnus-scores-articles
1941 (sort gnus-scores-articles 'gnus-score-string<))
1942 articles gnus-scores-articles)
1943
1944 (erase-buffer)
1945 (while (setq art (pop articles))
1946 (setq this (aref (car art) gnus-score-index))
1947
1948 ;; If we're working with non-standard headers, we are stuck
1949 ;; with working on them as a group. What a hassle.
1950 ;; Just wait 'til you see what horrors we commit against `match'...
1951 (if (= gnus-score-index 9)
1952 (setq this (gnus-prin1-to-string this))) ; ick.
1953
1954 (if simplify
1955 (setq this (gnus-map-function gnus-simplify-subject-functions this)))
1956 (if (equal last this)
1957 ;; O(N*H) cons-cells used here, where H is the number of
1958 ;; headers.
1959 (push art alike)
1960 (when last
1961 ;; Insert the line, with a text property on the
1962 ;; terminating newline referring to the articles with
1963 ;; this line.
1964 (insert last ?\n)
1965 (put-text-property (1- (point)) (point) 'articles alike))
1966 (setq alike (list art)
1967 last this)))
1968 (when last ; Bwadr, duplicate code.
1969 (insert last ?\n)
1970 (put-text-property (1- (point)) (point) 'articles alike))
1971
1972 ;; Go through all the score alists and pick out the entries
1973 ;; for this header.
1974 (while score-list
1975 (setq alist (pop score-list)
1976 ;; There's only one instance of this header for
1977 ;; each score alist.
1978 entries (assoc header alist))
1979 (while (cdr entries) ;First entry is the header index.
1980 (let* ((kill (cadr entries))
1981 (type (or (nth 3 kill) 's))
1982 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1983 (date (nth 2 kill))
1984 (extra (nth 4 kill)) ; non-standard header; string.
1985 (found nil)
1986 (mt (aref (symbol-name type) 0))
1987 (case-fold-search (not (memq mt '(?R ?S ?E ?F))))
1988 (dmt (downcase mt))
1989 ;; Assume user already simplified regexp and fuzzies
1990 (match (if (and simplify (not (memq dmt '(?f ?r))))
1991 (gnus-map-function
1992 gnus-simplify-subject-functions
1993 (nth 0 kill))
1994 (nth 0 kill)))
1995 (search-func
1996 (cond ((= dmt ?r) 're-search-forward)
1997 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1998 ((= dmt ?w) nil)
1999 (t (error "Invalid match type: %s" type)))))
2000
2001 ;; Evil hackery to make match usable in non-standard headers.
2002 (when extra
2003 (setq match (concat "[ (](" extra " \\. \"[^)]*"
2004 match "[^\"]*\")[ )]")
2005 search-func 're-search-forward)) ; XXX danger?!?
2006
2007 (cond
2008 ;; Fuzzy matches. We save these for later.
2009 ((= dmt ?f)
2010 (push (cons entries alist) fuzzies)
2011 (setq entries (cdr entries)))
2012 ;; Word matches. Save these for even later.
2013 ((= dmt ?w)
2014 (push (cons entries alist) words)
2015 (setq entries (cdr entries)))
2016 ;; Exact matches.
2017 ((= dmt ?e)
2018 ;; Do exact matching.
2019 (goto-char (point-min))
2020 (while (and (not (eobp))
2021 (funcall search-func match nil t))
2022 ;; Is it really exact?
2023 (and (eolp)
2024 (= (gnus-point-at-bol) (match-beginning 0))
2025 ;; Yup.
2026 (progn
2027 (setq found (setq arts (get-text-property
2028 (point) 'articles)))
2029 ;; Found a match, update scores.
2030 (if trace
2031 (while (setq art (pop arts))
2032 (setcdr art (+ score (cdr art)))
2033 (push
2034 (cons
2035 (car-safe (rassq alist gnus-score-cache))
2036 kill)
2037 gnus-score-trace))
2038 (while (setq art (pop arts))
2039 (setcdr art (+ score (cdr art)))))))
2040 (forward-line 1))
2041 ;; Update expiry date
2042 (if trace
2043 (setq entries (cdr entries))
2044 (cond
2045 ;; Permanent entry.
2046 ((null date)
2047 (setq entries (cdr entries)))
2048 ;; We have a match, so we update the date.
2049 ((and found gnus-update-score-entry-dates)
2050 (gnus-score-set 'touched '(t) alist)
2051 (setcar (nthcdr 2 kill) now)
2052 (setq entries (cdr entries)))
2053 ;; This entry has expired, so we remove it.
2054 ((and expire (< date expire))
2055 (gnus-score-set 'touched '(t) alist)
2056 (setcdr entries (cddr entries)))
2057 ;; No match; go to next entry.
2058 (t
2059 (setq entries (cdr entries))))))
2060 ;; Regexp and substring matching.
2061 (t
2062 (goto-char (point-min))
2063 (when (string= match "")
2064 (setq match "\n"))
2065 (while (and (not (eobp))
2066 (funcall search-func match nil t))
2067 (goto-char (match-beginning 0))
2068 (end-of-line)
2069 (setq found (setq arts (get-text-property (point) 'articles)))
2070 ;; Found a match, update scores.
2071 (if trace
2072 (while (setq art (pop arts))
2073 (setcdr art (+ score (cdr art)))
2074 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
2075 gnus-score-trace))
2076 (while (setq art (pop arts))
2077 (setcdr art (+ score (cdr art)))))
2078 (forward-line 1))
2079 ;; Update expiry date
2080 (if trace
2081 (setq entries (cdr entries))
2082 (cond
2083 ;; Permanent entry.
2084 ((null date)
2085 (setq entries (cdr entries)))
2086 ;; We have a match, so we update the date.
2087 ((and found gnus-update-score-entry-dates)
2088 (gnus-score-set 'touched '(t) alist)
2089 (setcar (nthcdr 2 kill) now)
2090 (setq entries (cdr entries)))
2091 ;; This entry has expired, so we remove it.
2092 ((and expire (< date expire))
2093 (gnus-score-set 'touched '(t) alist)
2094 (setcdr entries (cddr entries)))
2095 ;; No match; go to next entry.
2096 (t
2097 (setq entries (cdr entries))))))))))
2098
2099 ;; Find fuzzy matches.
2100 (when fuzzies
2101 ;; Simplify the entire buffer for easy matching.
2102 (gnus-simplify-buffer-fuzzy)
2103 (while (setq kill (cadaar fuzzies))
2104 (let* ((match (nth 0 kill))
2105 (type (nth 3 kill))
2106 (score (or (nth 1 kill) gnus-score-interactive-default-score))
2107 (date (nth 2 kill))
2108 (mt (aref (symbol-name type) 0))
2109 (case-fold-search (not (= mt ?F)))
2110 found)
2111 (goto-char (point-min))
2112 (while (and (not (eobp))
2113 (search-forward match nil t))
2114 (when (and (= (gnus-point-at-bol) (match-beginning 0))
2115 (eolp))
2116 (setq found (setq arts (get-text-property (point) 'articles)))
2117 (if trace
2118 (while (setq art (pop arts))
2119 (setcdr art (+ score (cdr art)))
2120 (push (cons
2121 (car-safe (rassq (cdar fuzzies) gnus-score-cache))
2122 kill)
2123 gnus-score-trace))
2124 ;; Found a match, update scores.
2125 (while (setq art (pop arts))
2126 (setcdr art (+ score (cdr art))))))
2127 (forward-line 1))
2128 ;; Update expiry date
2129 (if (not trace)
2130 (cond
2131 ;; Permanent.
2132 ((null date)
2133 ;; Do nothing.
2134 )
2135 ;; Match, update date.
2136 ((and found gnus-update-score-entry-dates)
2137 (gnus-score-set 'touched '(t) (cdar fuzzies))
2138 (setcar (nthcdr 2 kill) now))
2139 ;; Old entry, remove.
2140 ((and expire (< date expire))
2141 (gnus-score-set 'touched '(t) (cdar fuzzies))
2142 (setcdr (caar fuzzies) (cddaar fuzzies)))))
2143 (setq fuzzies (cdr fuzzies)))))
2144
2145 (when words
2146 ;; Enter all words into the hashtb.
2147 (let ((hashtb (gnus-make-hashtable
2148 (* 10 (count-lines (point-min) (point-max))))))
2149 (gnus-enter-score-words-into-hashtb hashtb)
2150 (while (setq kill (cadaar words))
2151 (let* ((score (or (nth 1 kill) gnus-score-interactive-default-score))
2152 (date (nth 2 kill))
2153 found)
2154 (when (setq arts (intern-soft (nth 0 kill) hashtb))
2155 (setq arts (symbol-value arts))
2156 (setq found t)
2157 (if trace
2158 (while (setq art (pop arts))
2159 (setcdr art (+ score (cdr art)))
2160 (push (cons
2161 (car-safe (rassq (cdar words) gnus-score-cache))
2162 kill)
2163 gnus-score-trace))
2164 ;; Found a match, update scores.
2165 (while (setq art (pop arts))
2166 (setcdr art (+ score (cdr art))))))
2167 ;; Update expiry date
2168 (if (not trace)
2169 (cond
2170 ;; Permanent.
2171 ((null date)
2172 ;; Do nothing.
2173 )
2174 ;; Match, update date.
2175 ((and found gnus-update-score-entry-dates)
2176 (gnus-score-set 'touched '(t) (cdar words))
2177 (setcar (nthcdr 2 kill) now))
2178 ;; Old entry, remove.
2179 ((and expire (< date expire))
2180 (gnus-score-set 'touched '(t) (cdar words))
2181 (setcdr (caar words) (cddaar words)))))
2182 (setq words (cdr words))))))
2183 nil))
2184
2185 (defun gnus-enter-score-words-into-hashtb (hashtb)
2186 ;; Find all the words in the buffer and enter them into
2187 ;; the hashtable.
2188 (let ((syntab (syntax-table))
2189 word val)
2190 (goto-char (point-min))
2191 (unwind-protect
2192 (progn
2193 (set-syntax-table gnus-adaptive-word-syntax-table)
2194 (while (re-search-forward "\\b\\w+\\b" nil t)
2195 (setq val
2196 (gnus-gethash
2197 (setq word (downcase (buffer-substring
2198 (match-beginning 0) (match-end 0))))
2199 hashtb))
2200 (gnus-sethash
2201 word
2202 (append (get-text-property (gnus-point-at-eol) 'articles) val)
2203 hashtb)))
2204 (set-syntax-table syntab))
2205 ;; Make all the ignorable words ignored.
2206 (let ((ignored (append gnus-ignored-adaptive-words
2207 (if gnus-adaptive-word-no-group-words
2208 (message-tokenize-header
2209 (gnus-group-real-name gnus-newsgroup-name)
2210 "."))
2211 gnus-default-ignored-adaptive-words)))
2212 (while ignored
2213 (gnus-sethash (pop ignored) nil hashtb)))))
2214
2215 (defun gnus-score-string< (a1 a2)
2216 ;; Compare headers in articles A2 and A2.
2217 ;; The header index used is the free variable `gnus-score-index'.
2218 (string-lessp (aref (car a1) gnus-score-index)
2219 (aref (car a2) gnus-score-index)))
2220
2221 (defun gnus-current-score-file-nondirectory (&optional score-file)
2222 (let ((score-file (or score-file gnus-current-score-file)))
2223 (if score-file
2224 (gnus-short-group-name (file-name-nondirectory score-file))
2225 "none")))
2226
2227 (defun gnus-score-adaptive ()
2228 "Create adaptive score rules for this newsgroup."
2229 (when gnus-newsgroup-adaptive
2230 ;; We change the score file to the adaptive score file.
2231 (save-excursion
2232 (set-buffer gnus-summary-buffer)
2233 (gnus-score-load-file
2234 (or gnus-newsgroup-adaptive-score-file
2235 (gnus-home-score-file gnus-newsgroup-name t)
2236 (gnus-score-file-name
2237 gnus-newsgroup-name gnus-adaptive-file-suffix))))
2238 ;; Perform ordinary line scoring.
2239 (when (or (not (listp gnus-newsgroup-adaptive))
2240 (memq 'line gnus-newsgroup-adaptive))
2241 (save-excursion
2242 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
2243 (alist malist)
2244 (date (current-time-string))
2245 (data gnus-newsgroup-data)
2246 elem headers match func)
2247 ;; First we transform the adaptive rule alist into something
2248 ;; that's faster to process.
2249 (while malist
2250 (setq elem (car malist))
2251 (when (symbolp (car elem))
2252 (setcar elem (symbol-value (car elem))))
2253 (setq elem (cdr elem))
2254 (while elem
2255 (when (fboundp
2256 (setq func
2257 (intern
2258 (concat "mail-header-"
2259 (if (eq (caar elem) 'followup)
2260 "message-id"
2261 (downcase (symbol-name (caar elem))))))))
2262 (setcdr (car elem)
2263 (cons (if (eq (caar elem) 'followup)
2264 "references"
2265 (symbol-name (caar elem)))
2266 (cdar elem)))
2267 (setcar (car elem)
2268 `(lambda (h)
2269 (,func h))))
2270 (setq elem (cdr elem)))
2271 (setq malist (cdr malist)))
2272 ;; Then we score away.
2273 (while data
2274 (setq elem (cdr (assq (gnus-data-mark (car data)) alist)))
2275 (if (or (not elem)
2276 (gnus-data-pseudo-p (car data)))
2277 ()
2278 (when (setq headers (gnus-data-header (car data)))
2279 (while elem
2280 (setq match (funcall (caar elem) headers))
2281 (gnus-summary-score-entry
2282 (nth 1 (car elem)) match
2283 (cond
2284 ((numberp match)
2285 '=)
2286 ((equal (nth 1 (car elem)) "date")
2287 'a)
2288 (t
2289 ;; Whether we use substring or exact matches is
2290 ;; controlled here.
2291 (if (or (not gnus-score-exact-adapt-limit)
2292 (< (length match) gnus-score-exact-adapt-limit))
2293 'e
2294 (if (equal (nth 1 (car elem)) "subject")
2295 'f 's))))
2296 (nth 2 (car elem)) date nil t)
2297 (setq elem (cdr elem)))))
2298 (setq data (cdr data))))))
2299
2300 ;; Perform adaptive word scoring.
2301 (when (and (listp gnus-newsgroup-adaptive)
2302 (memq 'word gnus-newsgroup-adaptive))
2303 (with-temp-buffer
2304 (let* ((hashtb (gnus-make-hashtable 1000))
2305 (date (date-to-day (current-time-string)))
2306 (data gnus-newsgroup-data)
2307 (syntab (syntax-table))
2308 word d score val)
2309 (unwind-protect
2310 (progn
2311 (set-syntax-table gnus-adaptive-word-syntax-table)
2312 ;; Go through all articles.
2313 (while (setq d (pop data))
2314 (when (and
2315 (not (gnus-data-pseudo-p d))
2316 (setq score
2317 (cdr (assq
2318 (gnus-data-mark d)
2319 gnus-adaptive-word-score-alist))))
2320 ;; This article has a mark that should lead to
2321 ;; adaptive word rules, so we insert the subject
2322 ;; and find all words in that string.
2323 (insert (mail-header-subject (gnus-data-header d)))
2324 (downcase-region (point-min) (point-max))
2325 (goto-char (point-min))
2326 (while (re-search-forward "\\b\\w+\\b" nil t)
2327 ;; Put the word and score into the hashtb.
2328 (setq val (gnus-gethash (setq word (match-string 0))
2329 hashtb))
2330 (when (or (not gnus-adaptive-word-length-limit)
2331 (> (length word)
2332 gnus-adaptive-word-length-limit))
2333 (setq val (+ score (or val 0)))
2334 (if (and gnus-adaptive-word-minimum
2335 (< val gnus-adaptive-word-minimum))
2336 (setq val gnus-adaptive-word-minimum))
2337 (gnus-sethash word val hashtb)))
2338 (erase-buffer))))
2339 (set-syntax-table syntab))
2340 ;; Make all the ignorable words ignored.
2341 (let ((ignored (append gnus-ignored-adaptive-words
2342 (if gnus-adaptive-word-no-group-words
2343 (message-tokenize-header
2344 (gnus-group-real-name
2345 gnus-newsgroup-name)
2346 "."))
2347 gnus-default-ignored-adaptive-words)))
2348 (while ignored
2349 (gnus-sethash (pop ignored) nil hashtb)))
2350 ;; Now we have all the words and scores, so we
2351 ;; add these rules to the ADAPT file.
2352 (set-buffer gnus-summary-buffer)
2353 (mapatoms
2354 (lambda (word)
2355 (when (symbol-value word)
2356 (gnus-summary-score-entry
2357 "subject" (symbol-name word) 'w (symbol-value word)
2358 date nil t)))
2359 hashtb))))))
2360
2361 (defun gnus-score-edit-done ()
2362 (let ((bufnam (buffer-file-name (current-buffer)))
2363 (winconf gnus-prev-winconf))
2364 (when winconf
2365 (set-window-configuration winconf))
2366 (gnus-score-remove-from-cache bufnam)
2367 (gnus-score-load-file bufnam)))
2368
2369 (defun gnus-score-find-trace ()
2370 "Find all score rules that applies to the current article."
2371 (interactive)
2372 (let ((old-scored gnus-newsgroup-scored))
2373 (let ((gnus-newsgroup-headers
2374 (list (gnus-summary-article-header)))
2375 (gnus-newsgroup-scored nil)
2376 ;; Must be synced with `gnus-score-edit-file-at-point':
2377 (frmt "%S [%s] -> %s\n")
2378 trace
2379 file)
2380 (save-excursion
2381 (nnheader-set-temp-buffer "*Score Trace*"))
2382 (setq gnus-score-trace nil)
2383 (gnus-possibly-score-headers 'trace)
2384 (if (not (setq trace gnus-score-trace))
2385 (gnus-error
2386 1 "No score rules apply to the current article (default score %d)."
2387 gnus-summary-default-score)
2388 (set-buffer "*Score Trace*")
2389 ;; Use a keymap instead?
2390 (local-set-key "q"
2391 (lambda ()
2392 (interactive)
2393 (bury-buffer nil)
2394 (gnus-summary-expand-window)))
2395 (local-set-key "e" (lambda ()
2396 "Run `gnus-score-edit-file-at-point'."
2397 (interactive)
2398 (gnus-score-edit-file-at-point)))
2399 (local-set-key "f" (lambda ()
2400 "Run `gnus-score-edit-file-at-point'."
2401 (interactive)
2402 (gnus-score-edit-file-at-point 'format)))
2403 (local-set-key "t" 'toggle-truncate-lines)
2404 (setq truncate-lines t)
2405 (dolist (entry trace)
2406 (setq file (or (car entry)
2407 ;; Must be synced with
2408 ;; `gnus-score-edit-file-at-point':
2409 "(non-file rule)"))
2410 (insert
2411 (format frmt
2412 (cdr entry)
2413 ;; Don't use `file-name-sans-extension' to see .SCORE and
2414 ;; .ADAPT directly:
2415 (file-name-nondirectory file)
2416 (abbreviate-file-name file))))
2417 (insert
2418 "\n\nQuick help:
2419
2420 Type `e' to edit score file corresponding to the score rule on current line,
2421 `f' to format (pretty print) the score file and edit it,
2422 `t' toggle to truncate long lines in this buffer,
2423 `q' to quit.
2424
2425 The first sexp on each line is the score rule, followed by the file name of
2426 the score file and its full name, including the directory.")
2427 (goto-char (point-min))
2428 (gnus-configure-windows 'score-trace)))
2429 (set-buffer gnus-summary-buffer)
2430 (setq gnus-newsgroup-scored old-scored)))
2431
2432 (defun gnus-score-find-favourite-words ()
2433 "List words used in scoring."
2434 (interactive)
2435 (let ((alists (gnus-score-load-files (gnus-all-score-files)))
2436 alist rule rules kill)
2437 ;; Go through all the score alists for this group
2438 ;; and find all `w' rules.
2439 (while (setq alist (pop alists))
2440 (while (setq rule (pop alist))
2441 (when (and (stringp (car rule))
2442 (equal "subject" (downcase (pop rule))))
2443 (while (setq kill (pop rule))
2444 (when (memq (nth 3 kill) '(w W word Word))
2445 (push (cons (or (nth 1 kill)
2446 gnus-score-interactive-default-score)
2447 (car kill))
2448 rules))))))
2449 (setq rules (sort rules (lambda (r1 r2)
2450 (string-lessp (cdr r1) (cdr r2)))))
2451 ;; Add up words that have appeared several times.
2452 (let ((r rules))
2453 (while (cdr r)
2454 (if (equal (cdar r) (cdadr r))
2455 (progn
2456 (setcar (car r) (+ (caar r) (caadr r)))
2457 (setcdr r (cddr r)))
2458 (pop r))))
2459 ;; Insert the words.
2460 (nnheader-set-temp-buffer "*Score Words*")
2461 (if (not (setq rules (sort rules (lambda (r1 r2) (> (car r1) (car r2))))))
2462 (gnus-error 3 "No word score rules")
2463 (while rules
2464 (insert (format "%-5d: %s\n" (caar rules) (cdar rules)))
2465 (pop rules))
2466 (goto-char (point-min))
2467 (gnus-configure-windows 'score-words))))
2468
2469 (defun gnus-summary-rescore ()
2470 "Redo the entire scoring process in the current summary."
2471 (interactive)
2472 (gnus-score-save)
2473 (setq gnus-score-cache nil)
2474 (setq gnus-newsgroup-scored nil)
2475 (gnus-possibly-score-headers)
2476 (gnus-score-update-all-lines))
2477
2478 (defun gnus-score-flush-cache ()
2479 "Flush the cache of score files."
2480 (interactive)
2481 (gnus-score-save)
2482 (setq gnus-score-cache nil
2483 gnus-score-alist nil
2484 gnus-short-name-score-file-cache nil)
2485 (gnus-message 6 "The score cache is now flushed"))
2486
2487 (gnus-add-shutdown 'gnus-score-close 'gnus)
2488
2489 (defvar gnus-score-file-alist-cache nil)
2490
2491 (defun gnus-score-close ()
2492 "Clear all internal score variables."
2493 (setq gnus-score-cache nil
2494 gnus-internal-global-score-files nil
2495 gnus-score-file-list nil
2496 gnus-score-file-alist-cache nil))
2497
2498 ;; Summary score marking commands.
2499
2500 (defun gnus-summary-raise-same-subject-and-select (score)
2501 "Raise articles which has the same subject with SCORE and select the next."
2502 (interactive "p")
2503 (let ((subject (gnus-summary-article-subject)))
2504 (gnus-summary-raise-score score)
2505 (while (gnus-summary-find-subject subject)
2506 (gnus-summary-raise-score score))
2507 (gnus-summary-next-article t)))
2508
2509 (defun gnus-summary-raise-same-subject (score)
2510 "Raise articles which has the same subject with SCORE."
2511 (interactive "p")
2512 (let ((subject (gnus-summary-article-subject)))
2513 (gnus-summary-raise-score score)
2514 (while (gnus-summary-find-subject subject)
2515 (gnus-summary-raise-score score))
2516 (gnus-summary-next-subject 1 t)))
2517
2518 (defun gnus-score-delta-default (level)
2519 (if level (prefix-numeric-value level)
2520 gnus-score-interactive-default-score))
2521
2522 (defun gnus-summary-raise-thread (&optional score)
2523 "Raise the score of the articles in the current thread with SCORE."
2524 (interactive "P")
2525 (setq score (gnus-score-delta-default score))
2526 (let (e)
2527 (save-excursion
2528 (let ((articles (gnus-summary-articles-in-thread)))
2529 (while articles
2530 (gnus-summary-goto-subject (car articles))
2531 (gnus-summary-raise-score score)
2532 (setq articles (cdr articles))))
2533 (setq e (point)))
2534 (let ((gnus-summary-check-current t))
2535 (unless (zerop (gnus-summary-next-subject 1 t))
2536 (goto-char e))))
2537 (gnus-summary-recenter)
2538 (gnus-summary-position-point)
2539 (gnus-set-mode-line 'summary))
2540
2541 (defun gnus-summary-lower-same-subject-and-select (score)
2542 "Raise articles which has the same subject with SCORE and select the next."
2543 (interactive "p")
2544 (gnus-summary-raise-same-subject-and-select (- score)))
2545
2546 (defun gnus-summary-lower-same-subject (score)
2547 "Raise articles which has the same subject with SCORE."
2548 (interactive "p")
2549 (gnus-summary-raise-same-subject (- score)))
2550
2551 (defun gnus-summary-lower-thread (&optional score)
2552 "Lower score of articles in the current thread with SCORE."
2553 (interactive "P")
2554 (gnus-summary-raise-thread (- (gnus-score-delta-default score))))
2555
2556 ;;; Finding score files.
2557
2558 (defun gnus-score-score-files (group)
2559 "Return a list of all possible score files."
2560 ;; Search and set any global score files.
2561 (when gnus-global-score-files
2562 (unless gnus-internal-global-score-files
2563 (gnus-score-search-global-directories gnus-global-score-files)))
2564 ;; Fix the kill-file dir variable.
2565 (setq gnus-kill-files-directory
2566 (file-name-as-directory gnus-kill-files-directory))
2567 ;; If we can't read it, there are no score files.
2568 (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
2569 (setq gnus-score-file-list nil)
2570 (if (not (gnus-use-long-file-name 'not-score))
2571 ;; We do not use long file names, so we have to do some
2572 ;; directory traversing.
2573 (setq gnus-score-file-list
2574 (cons nil
2575 (or gnus-short-name-score-file-cache
2576 (prog2
2577 (gnus-message 6 "Finding all score files...")
2578 (setq gnus-short-name-score-file-cache
2579 (gnus-score-score-files-1
2580 gnus-kill-files-directory))
2581 (gnus-message 6 "Finding all score files...done")))))
2582 ;; We want long file names.
2583 (when (or (not gnus-score-file-list)
2584 (not (car gnus-score-file-list))
2585 (gnus-file-newer-than gnus-kill-files-directory
2586 (car gnus-score-file-list)))
2587 (setq gnus-score-file-list
2588 (cons (nth 5 (file-attributes gnus-kill-files-directory))
2589 (nreverse
2590 (directory-files
2591 gnus-kill-files-directory t
2592 (gnus-score-file-regexp)))))))
2593 (cdr gnus-score-file-list)))
2594
2595 (defun gnus-score-score-files-1 (dir)
2596 "Return all possible score files under DIR."
2597 (let ((files (list (expand-file-name dir)))
2598 (regexp (gnus-score-file-regexp))
2599 (case-fold-search nil)
2600 seen out file)
2601 (while (setq file (pop files))
2602 (cond
2603 ;; Ignore files that start with a dot.
2604 ((string-match "^\\." (file-name-nondirectory file))
2605 nil)
2606 ;; Add subtrees of directory to also be searched.
2607 ((and (file-directory-p file)
2608 (not (member (file-truename file) seen)))
2609 (push (file-truename file) seen)
2610 (setq files (nconc (directory-files file t nil t) files)))
2611 ;; Add files to the list of score files.
2612 ((string-match regexp file)
2613 (push file out))))
2614 (or out
2615 ;; Return a dummy value.
2616 (list (expand-file-name "this.file.does.not.exist.SCORE"
2617 gnus-kill-files-directory)))))
2618
2619 (defun gnus-score-file-regexp ()
2620 "Return a regexp that match all score files."
2621 (concat "\\(" (regexp-quote gnus-score-file-suffix )
2622 "\\|" (regexp-quote gnus-adaptive-file-suffix) "\\)\\'"))
2623
2624 (defun gnus-score-find-bnews (group)
2625 "Return a list of score files for GROUP.
2626 The score files are those files in the ~/News/ directory which matches
2627 GROUP using BNews sys file syntax."
2628 (let* ((sfiles (append (gnus-score-score-files group)
2629 gnus-internal-global-score-files))
2630 (kill-dir (file-name-as-directory
2631 (expand-file-name gnus-kill-files-directory)))
2632 (klen (length kill-dir))
2633 (score-regexp (gnus-score-file-regexp))
2634 (trans (cdr (assq ?: nnheader-file-name-translation-alist)))
2635 (group-trans (nnheader-translate-file-chars group t))
2636 ofiles not-match regexp)
2637 (save-excursion
2638 (set-buffer (gnus-get-buffer-create "*gnus score files*"))
2639 (buffer-disable-undo)
2640 ;; Go through all score file names and create regexp with them
2641 ;; as the source.
2642 (while sfiles
2643 (erase-buffer)
2644 (insert (car sfiles))
2645 (goto-char (point-min))
2646 ;; First remove the suffix itself.
2647 (when (re-search-forward (concat "." score-regexp) nil t)
2648 (replace-match "" t t)
2649 (goto-char (point-min))
2650 (if (looking-at (regexp-quote kill-dir))
2651 ;; If the file name was just "SCORE", `klen' is one character
2652 ;; too much.
2653 (delete-char (min (1- (point-max)) klen))
2654 (goto-char (point-max))
2655 (if (re-search-backward gnus-directory-sep-char-regexp nil t)
2656 (delete-region (1+ (point)) (point-min))
2657 (gnus-message 1 "Can't find directory separator in %s"
2658 (car sfiles))))
2659 ;; If short file names were used, we have to translate slashes.
2660 (goto-char (point-min))
2661 (let ((regexp (concat
2662 "[/:" (if trans (char-to-string trans)) "]")))
2663 (while (re-search-forward regexp nil t)
2664 (replace-match "." t t)))
2665 ;; Kludge to get rid of "nntp+" problems.
2666 (goto-char (point-min))
2667 (when (looking-at "nn[a-z]+\\+")
2668 (search-forward "+")
2669 (forward-char -1)
2670 (insert "\\")
2671 (forward-char 1))
2672 ;; Kludge to deal with "++".
2673 (while (search-forward "+" nil t)
2674 (replace-match "\\+" t t))
2675 ;; Translate "all" to ".*".
2676 (goto-char (point-min))
2677 (while (search-forward "all" nil t)
2678 (replace-match ".*" t t))
2679 (goto-char (point-min))
2680 ;; Deal with "not."s.
2681 (if (looking-at "not.")
2682 (progn
2683 (setq not-match t)
2684 (setq regexp
2685 (concat "^" (buffer-substring 5 (point-max)) "$")))
2686 (setq regexp (concat "^" (buffer-substring 1 (point-max)) "$"))
2687 (setq not-match nil))
2688 ;; Finally - if this resulting regexp matches the group name,
2689 ;; we add this score file to the list of score files
2690 ;; applicable to this group.
2691 (when (or (and not-match
2692 (ignore-errors
2693 (not (string-match regexp group-trans))))
2694 (and (not not-match)
2695 (ignore-errors (string-match regexp group-trans))))
2696 (push (car sfiles) ofiles)))
2697 (setq sfiles (cdr sfiles)))
2698 (gnus-kill-buffer (current-buffer))
2699 ;; Slight kludge here - the last score file returned should be
2700 ;; the local score file, whether it exists or not. This is so
2701 ;; that any score commands the user enters will go to the right
2702 ;; file, and not end up in some global score file.
2703 (let ((localscore (gnus-score-file-name group)))
2704 (setq ofiles (cons localscore (delete localscore ofiles))))
2705 (gnus-sort-score-files (nreverse ofiles)))))
2706
2707 (defun gnus-score-find-single (group)
2708 "Return list containing the score file for GROUP."
2709 (list (or gnus-newsgroup-adaptive-score-file
2710 (gnus-score-file-name group gnus-adaptive-file-suffix))
2711 (gnus-score-file-name group)))
2712
2713 (defun gnus-score-find-hierarchical (group)
2714 "Return list of score files for GROUP.
2715 This includes the score file for the group and all its parents."
2716 (let* ((prefix (gnus-group-real-prefix group))
2717 (all (list nil))
2718 (group (gnus-group-real-name group))
2719 (start 0))
2720 (while (string-match "\\." group (1+ start))
2721 (setq start (match-beginning 0))
2722 (push (substring group 0 start) all))
2723 (push group all)
2724 (setq all
2725 (nconc
2726 (mapcar (lambda (group)
2727 (gnus-score-file-name group gnus-adaptive-file-suffix))
2728 (setq all (nreverse all)))
2729 (mapcar 'gnus-score-file-name all)))
2730 (if (equal prefix "")
2731 all
2732 (mapcar
2733 (lambda (file)
2734 (nnheader-translate-file-chars
2735 (concat (file-name-directory file) prefix
2736 (file-name-nondirectory file))))
2737 all))))
2738
2739 (defun gnus-score-file-rank (file)
2740 "Return a number that says how specific score FILE is.
2741 Destroys the current buffer."
2742 (if (member file gnus-internal-global-score-files)
2743 0
2744 (when (string-match
2745 (concat "^" (regexp-quote
2746 (expand-file-name
2747 (file-name-as-directory gnus-kill-files-directory))))
2748 file)
2749 (setq file (substring file (match-end 0))))
2750 (insert file)
2751 (goto-char (point-min))
2752 (let ((beg (point))
2753 elems)
2754 (while (re-search-forward "[./]" nil t)
2755 (push (buffer-substring beg (1- (point)))
2756 elems))
2757 (erase-buffer)
2758 (setq elems (delete "all" elems))
2759 (length elems))))
2760
2761 (defun gnus-sort-score-files (files)
2762 "Sort FILES so that the most general files come first."
2763 (with-temp-buffer
2764 (let ((alist
2765 (mapcar
2766 (lambda (file)
2767 (cons (inline (gnus-score-file-rank file)) file))
2768 files)))
2769 (mapcar
2770 (lambda (f) (cdr f))
2771 (sort alist 'car-less-than-car)))))
2772
2773 (defun gnus-score-find-alist (group)
2774 "Return list of score files for GROUP.
2775 The list is determined from the variable `gnus-score-file-alist'."
2776 (let ((alist gnus-score-file-multiple-match-alist)
2777 score-files)
2778 ;; if this group has been seen before, return the cached entry
2779 (if (setq score-files (assoc group gnus-score-file-alist-cache))
2780 (cdr score-files) ;ensures caching groups with no matches
2781 ;; handle the multiple match alist
2782 (while alist
2783 (when (string-match (caar alist) group)
2784 (setq score-files
2785 (nconc score-files (copy-sequence (cdar alist)))))
2786 (setq alist (cdr alist)))
2787 (setq alist gnus-score-file-single-match-alist)
2788 ;; handle the single match alist
2789 (while alist
2790 (when (string-match (caar alist) group)
2791 ;; progn used just in case ("regexp") has no files
2792 ;; and score-files is still nil. -sj
2793 ;; this can be construed as a "stop searching here" feature :>
2794 ;; and used to simplify regexps in the single-alist
2795 (setq score-files
2796 (nconc score-files (copy-sequence (cdar alist))))
2797 (setq alist nil))
2798 (setq alist (cdr alist)))
2799 ;; cache the score files
2800 (push (cons group score-files) gnus-score-file-alist-cache)
2801 score-files)))
2802
2803 (defun gnus-all-score-files (&optional group)
2804 "Return a list of all score files for the current group."
2805 (let ((funcs gnus-score-find-score-files-function)
2806 (group (or group gnus-newsgroup-name))
2807 score-files)
2808 (when group
2809 ;; Make sure funcs is a list.
2810 (and funcs
2811 (not (listp funcs))
2812 (setq funcs (list funcs)))
2813 (when gnus-score-use-all-scores
2814 ;; Get the initial score files for this group.
2815 (when funcs
2816 (setq score-files (nreverse (gnus-score-find-alist group))))
2817 ;; Add any home adapt files.
2818 (let ((home (gnus-home-score-file group t)))
2819 (when home
2820 (push home score-files)
2821 (setq gnus-newsgroup-adaptive-score-file home)))
2822 ;; Check whether there is a `adapt-file' group parameter.
2823 (let ((param-file (gnus-group-find-parameter group 'adapt-file)))
2824 (when param-file
2825 (push param-file score-files)
2826 (setq gnus-newsgroup-adaptive-score-file param-file))))
2827 ;; Go through all the functions for finding score files (or actual
2828 ;; scores) and add them to a list.
2829 (while funcs
2830 (when (functionp (car funcs))
2831 (setq score-files
2832 (append score-files
2833 (nreverse (funcall (car funcs) group)))))
2834 (setq funcs (cdr funcs)))
2835 (when gnus-score-use-all-scores
2836 ;; Add any home score files.
2837 (let ((home (gnus-home-score-file group)))
2838 (when home
2839 (push home score-files)))
2840 ;; Check whether there is a `score-file' group parameter.
2841 (let ((param-file (gnus-group-find-parameter group 'score-file)))
2842 (when param-file
2843 (push param-file score-files))))
2844 ;; Expand all files names.
2845 (let ((files score-files))
2846 (while files
2847 (when (stringp (car files))
2848 (setcar files (expand-file-name
2849 (car files) gnus-kill-files-directory)))
2850 (pop files)))
2851 (setq score-files (nreverse score-files))
2852 ;; Remove any duplicate score files.
2853 (while (and score-files
2854 (member (car score-files) (cdr score-files)))
2855 (pop score-files))
2856 (let ((files score-files))
2857 (while (cdr files)
2858 (if (member (cadr files) (cddr files))
2859 (setcdr files (cddr files))
2860 (pop files))))
2861 ;; Do the scoring if there are any score files for this group.
2862 score-files)))
2863
2864 (defun gnus-possibly-score-headers (&optional trace)
2865 "Do scoring if scoring is required."
2866 (let ((score-files (gnus-all-score-files)))
2867 (when score-files
2868 (gnus-score-headers score-files trace))))
2869
2870 (defun gnus-score-file-name (newsgroup &optional suffix)
2871 "Return the name of a score file for NEWSGROUP."
2872 (let ((suffix (or suffix gnus-score-file-suffix)))
2873 (nnheader-translate-file-chars
2874 (cond
2875 ((or (null newsgroup)
2876 (string-equal newsgroup ""))
2877 ;; The global score file is placed at top of the directory.
2878 (expand-file-name suffix gnus-kill-files-directory))
2879 ((gnus-use-long-file-name 'not-score)
2880 ;; Append ".SCORE" to newsgroup name.
2881 (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
2882 "." suffix)
2883 gnus-kill-files-directory))
2884 (t
2885 ;; Place "SCORE" under the hierarchical directory.
2886 (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
2887 "/" suffix)
2888 gnus-kill-files-directory))))))
2889
2890 (defun gnus-score-search-global-directories (files)
2891 "Scan all global score directories for score files."
2892 ;; Set the variable `gnus-internal-global-score-files' to all
2893 ;; available global score files.
2894 (interactive (list gnus-global-score-files))
2895 (let (out)
2896 (while files
2897 ;; #### /$ Unix-specific?
2898 (if (file-directory-p (car files))
2899 (setq out (nconc (directory-files
2900 (car files) t
2901 (concat (gnus-score-file-regexp) "$"))))
2902 (push (car files) out))
2903 (setq files (cdr files)))
2904 (setq gnus-internal-global-score-files out)))
2905
2906 (defun gnus-score-default-fold-toggle ()
2907 "Toggle folding for new score file entries."
2908 (interactive)
2909 (setq gnus-score-default-fold (not gnus-score-default-fold))
2910 (if gnus-score-default-fold
2911 (gnus-message 1 "New score file entries will be case insensitive.")
2912 (gnus-message 1 "New score file entries will be case sensitive.")))
2913
2914 ;;; Home score file.
2915
2916 (defun gnus-home-score-file (group &optional adapt)
2917 "Return the home score file for GROUP.
2918 If ADAPT, return the home adaptive file instead."
2919 (let ((list (if adapt gnus-home-adapt-file gnus-home-score-file))
2920 elem found)
2921 ;; Make sure we have a list.
2922 (unless (listp list)
2923 (setq list (list list)))
2924 ;; Go through the list and look for matches.
2925 (while (and (not found)
2926 (setq elem (pop list)))
2927 (setq found
2928 (cond
2929 ;; Simple string.
2930 ((stringp elem)
2931 elem)
2932 ;; Function.
2933 ((functionp elem)
2934 (funcall elem group))
2935 ;; Regexp-file cons.
2936 ((consp elem)
2937 (when (string-match (gnus-globalify-regexp (car elem)) group)
2938 (replace-match (cadr elem) t nil group))))))
2939 (when found
2940 (setq found (nnheader-translate-file-chars found))
2941 (if (file-name-absolute-p found)
2942 found
2943 (nnheader-concat gnus-kill-files-directory found)))))
2944
2945 (defun gnus-hierarchial-home-score-file (group)
2946 "Return the score file of the top-level hierarchy of GROUP."
2947 (if (string-match "^[^.]+\\." group)
2948 (concat (match-string 0 group) gnus-score-file-suffix)
2949 ;; Group name without any dots.
2950 (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
2951 gnus-score-file-suffix)))
2952
2953 (defun gnus-hierarchial-home-adapt-file (group)
2954 "Return the adapt file of the top-level hierarchy of GROUP."
2955 (if (string-match "^[^.]+\\." group)
2956 (concat (match-string 0 group) gnus-adaptive-file-suffix)
2957 ;; Group name without any dots.
2958 (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
2959 gnus-adaptive-file-suffix)))
2960
2961 (defun gnus-current-home-score-file (group)
2962 "Return the \"current\" regular score file."
2963 (car (nreverse (gnus-score-find-alist group))))
2964
2965 ;;;
2966 ;;; Score decays
2967 ;;;
2968
2969 (defun gnus-decay-score (score)
2970 "Decay SCORE according to `gnus-score-decay-constant' and `gnus-score-decay-scale'."
2971 (let ((n (- score
2972 (* (if (< score 0) -1 1)
2973 (min (abs score)
2974 (max gnus-score-decay-constant
2975 (* (abs score)
2976 gnus-score-decay-scale)))))))
2977 (if (and (featurep 'xemacs)
2978 ;; XEmacs' floor can handle only the floating point
2979 ;; number below the half of the maximum integer.
2980 (> (abs n) (lsh -1 -2)))
2981 (string-to-number
2982 (car (split-string (number-to-string n) "\\.")))
2983 (floor n))))
2984
2985 (defun gnus-decay-scores (alist day)
2986 "Decay non-permanent scores in ALIST."
2987 (let ((times (- (time-to-days (current-time)) day))
2988 kill entry updated score n)
2989 (unless (zerop times) ;Done decays today already?
2990 (while (setq entry (pop alist))
2991 (when (stringp (car entry))
2992 (setq entry (cdr entry))
2993 (while (setq kill (pop entry))
2994 (when (nth 2 kill)
2995 (setq updated t)
2996 (setq score (or (nth 1 kill)
2997 gnus-score-interactive-default-score)
2998 n times)
2999 (while (natnump (decf n))
3000 (setq score (funcall gnus-decay-score-function score)))
3001 (setcdr kill (cons score
3002 (cdr (cdr kill)))))))))
3003 ;; Return whether this score file needs to be saved. By Je-haysuss!
3004 updated))
3005
3006 (defun gnus-score-regexp-bad-p (regexp)
3007 "Test whether REGEXP is safe for Gnus scoring.
3008 A regexp is unsafe if it matches newline or a buffer boundary.
3009
3010 If the regexp is good, return nil. If the regexp is bad, return a
3011 cons cell (SYM . STRING), where the symbol SYM is `new' or `bad'.
3012 In the `new' case, the string is a safe replacement for REGEXP.
3013 In the `bad' case, the string is a unsafe subexpression of REGEXP,
3014 and we do not have a simple replacement to suggest.
3015
3016 See Info node `(gnus)Scoring Tips' for examples of good regular expressions."
3017 (let (case-fold-search)
3018 (and
3019 ;; First, try a relatively fast necessary condition.
3020 ;; Notice ranges (like [^:] or [\t-\r]), \s>, \Sw, \W, \', \`:
3021 (string-match "\n\\|\\\\[SsW`']\\|\\[\\^\\|[\0-\n]-" regexp)
3022 ;; Now break the regexp into tokens, and check each:
3023 (let ((tail regexp) ; remaining regexp to check
3024 tok ; current token
3025 bad ; nil, or bad subexpression
3026 new ; nil, or replacement regexp so far
3027 end) ; length of current token
3028 (while (and (not bad)
3029 (string-match
3030 "\\`\\(\\\\[sS]?.\\|\\[\\^?]?[^]]*]\\|[^\\]\\)"
3031 tail))
3032 (setq end (match-end 0)
3033 tok (substring tail 0 end)
3034 tail (substring tail end))
3035 (if;; Is token `bad' (matching newline or buffer ends)?
3036 (or (member tok '("\n" "\\W" "\\`" "\\'"))
3037 ;; This next handles "[...]", "\\s.", and "\\S.":
3038 (and (> end 2) (string-match tok "\n")))
3039 (let ((newtok
3040 ;; Try to suggest a replacement for tok ...
3041 (cond ((string-equal tok "\\`") "^") ; or "\\(^\\)"
3042 ((string-equal tok "\\'") "$") ; or "\\($\\)"
3043 ((string-match "\\[\\^" tok) ; very common
3044 (concat (substring tok 0 -1) "\n]")))))
3045 (if newtok
3046 (setq new
3047 (concat
3048 (or new
3049 ;; good prefix so far:
3050 (substring regexp 0 (- (+ (length tail) end))))
3051 newtok))
3052 ;; No replacement idea, so give up:
3053 (setq bad tok)))
3054 ;; tok is good, may need to extend new
3055 (and new (setq new (concat new tok)))))
3056 ;; Now return a value:
3057 (cond
3058 (bad (cons 'bad bad))
3059 (new (cons 'new new))
3060 (t nil))))))
3061
3062 (provide 'gnus-score)
3063
3064 ;;; arch-tag: d3922589-764d-46ae-9954-9330fd192634
3065 ;;; gnus-score.el ends here