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