1 1;;; gnus-score.el --- scoring code for Gnus
2 ;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
4 ;; Author: Per Abrahamsen <amanda@iesd.auc.dk>
5 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (eval-when-compile (require 'cl))
36 (defcustom gnus-global-score-files nil
37 "List of global score files and directories.
38 Set this variable if you want to use people's score files. One entry
39 for each score file or each score file directory. Gnus will decide
40 by itself what score files are applicable to which group.
42 Say you want to use the single score file
43 \"/ftp.gnus.org@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
44 score files in the \"/ftp.some-where:/pub/score\" directory.
46 (setq gnus-global-score-files
47 '(\"/ftp.gnus.org:/pub/larsi/ding/score/soc.motss.SCORE\"
48 \"/ftp.some-where:/pub/score\"))"
49 :group 'gnus-score-files
52 (defcustom gnus-score-file-single-match-alist nil
53 "Alist mapping regexps to lists of score files.
54 Each element of this alist should be of the form
55 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
57 If the name of a group is matched by REGEXP, the corresponding scorefiles
58 will be used for that group.
59 The first match found is used, subsequent matching entries are ignored (to
60 use multiple matches, see gnus-score-file-multiple-match-alist).
62 These score files are loaded in addition to any files returned by
63 gnus-score-find-score-files-function (which see)."
64 :group 'gnus-score-files
65 :type '(repeat (cons regexp (repeat file))))
67 (defcustom gnus-score-file-multiple-match-alist nil
68 "Alist mapping regexps to lists of score files.
69 Each element of this alist should be of the form
70 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
72 If the name of a group is matched by REGEXP, the corresponding scorefiles
73 will be used for that group.
74 If multiple REGEXPs match a group, the score files corresponding to each
75 match will be used (for only one match to be used, see
76 gnus-score-file-single-match-alist).
78 These score files are loaded in addition to any files returned by
79 gnus-score-find-score-files-function (which see)."
80 :group 'gnus-score-files
81 :type '(repeat (cons regexp (repeat file))))
83 (defcustom gnus-score-file-suffix "SCORE"
84 "Suffix of the score files."
85 :group 'gnus-score-files
88 (defcustom gnus-adaptive-file-suffix "ADAPT"
89 "Suffix of the adaptive score files."
90 :group 'gnus-score-files
91 :group 'gnus-score-adapt
94 (defcustom gnus-score-find-score-files-function 'gnus-score-find-bnews
95 "Function used to find score files.
96 The function will be called with the group name as the argument, and
97 should return a list of score files to apply to that group. The score
98 files do not actually have to exist.
100 Predefined values are:
102 gnus-score-find-single: Only apply the group's own score file.
103 gnus-score-find-hierarchical: Also apply score files from parent groups.
104 gnus-score-find-bnews: Apply score files whose names matches.
106 See the documentation to these functions for more information.
108 This variable can also be a list of functions to be called. Each
109 function should either return a list of score files, or a list of
111 :group 'gnus-score-files
112 :type '(radio (function-item gnus-score-find-single)
113 (function-item gnus-score-find-hierarchical)
114 (function-item gnus-score-find-bnews)
115 (function :tag "Other")))
117 (defcustom gnus-score-interactive-default-score 1000
118 "*Scoring commands will raise/lower the score with this number as the default."
119 :group 'gnus-score-default
122 (defcustom gnus-score-expiry-days 7
123 "*Number of days before unused score file entries are expired.
124 If this variable is nil, no score file entries will be expired."
125 :group 'gnus-score-expire
126 :type '(choice (const :tag "never" nil)
129 (defcustom gnus-update-score-entry-dates t
130 "*In non-nil, update matching score entry dates.
131 If this variable is nil, then score entries that provide matches
132 will be expired along with non-matching score entries."
133 :group 'gnus-score-expire
136 (defcustom gnus-orphan-score nil
137 "*All orphans get this score added. Set in the score file."
138 :group 'gnus-score-default
141 (defcustom gnus-decay-scores nil
142 "*If non-nil, decay non-permanent scores."
143 :group 'gnus-score-decay
146 (defcustom gnus-decay-score-function 'gnus-decay-score
147 "*Function called to decay a score.
148 It is called with one parameter -- the score to be decayed."
149 :group 'gnus-score-decay
150 :type '(radio (function-item gnus-decay-score)
151 (function :tag "Other")))
153 (defcustom gnus-score-decay-constant 3
154 "*Decay all \"small\" scores with this amount."
155 :group 'gnus-score-decay
158 (defcustom gnus-score-decay-scale .05
159 "*Decay all \"big\" scores with this factor."
160 :group 'gnus-score-decay
163 (defcustom gnus-home-score-file nil
164 "Variable to control where interactive score entries are to go.
168 This file file will be used as the home score file.
171 The result of this function will be used as the home score file.
172 The function will be passed the name of the group as its
176 The elements in this list can be:
178 * `(regexp file-name ...)'
179 If the `regexp' matches the group name, the first `file-name' will
180 will be used as the home score file. (Multiple filenames are
181 allowed so that one may use gnus-score-file-single-match-alist to
185 If the function returns non-nil, the result will be used
186 as the home score file. The function will be passed the
187 name of the group as its parameter.
189 * A string. Use the string as the home score file.
191 The list will be traversed from the beginning towards the end looking
193 :group 'gnus-score-files
194 :type '(choice string
195 (repeat (choice string
196 (cons regexp (repeat file))
200 (defcustom gnus-home-adapt-file nil
201 "Variable to control where new adaptive score entries are to go.
202 This variable allows the same syntax as `gnus-home-score-file'."
203 :group 'gnus-score-adapt
204 :group 'gnus-score-files
205 :type '(choice string
206 (repeat (choice string
207 (cons regexp (repeat file))
211 (defcustom gnus-default-adaptive-score-alist
212 '((gnus-kill-file-mark)
214 (gnus-read-mark (from 3) (subject 30))
215 (gnus-catchup-mark (subject -10))
216 (gnus-killed-mark (from -1) (subject -20))
217 (gnus-del-mark (from -2) (subject -15)))
218 "Alist of marks and scores."
219 :group 'gnus-score-adapt
220 :type '(repeat (cons (symbol :tag "Mark")
221 (repeat (list (choice :tag "Header"
224 (symbol :tag "other"))
225 (integer :tag "Score"))))))
227 (defcustom gnus-ignored-adaptive-words nil
228 "List of words to be ignored when doing adaptive word scoring."
229 :group 'gnus-score-adapt
230 :type '(repeat string))
232 (defcustom gnus-default-ignored-adaptive-words
233 '("a" "i" "the" "to" "of" "and" "in" "is" "it" "for" "that" "if" "you"
234 "this" "be" "on" "with" "not" "have" "are" "or" "as" "from" "can"
235 "but" "by" "at" "an" "will" "no" "all" "was" "do" "there" "my" "one"
236 "so" "we" "they" "what" "would" "any" "which" "about" "get" "your"
237 "use" "some" "me" "then" "name" "like" "out" "when" "up" "time"
238 "other" "more" "only" "just" "end" "also" "know" "how" "new" "should"
239 "been" "than" "them" "he" "who" "make" "may" "people" "these" "now"
240 "their" "here" "into" "first" "could" "way" "had" "see" "work" "well"
241 "were" "two" "very" "where" "while" "us" "because" "good" "same"
242 "even" "much" "most" "many" "such" "long" "his" "over" "last" "since"
243 "right" "before" "our" "without" "too" "those" "why" "must" "part"
244 "being" "current" "back" "still" "go" "point" "value" "each" "did"
245 "both" "true" "off" "say" "another" "state" "might" "under" "start"
247 "Default list of words to be ignored when doing adaptive word scoring."
248 :group 'gnus-score-adapt
249 :type '(repeat string))
251 (defcustom gnus-default-adaptive-word-score-alist
252 `((,gnus-read-mark . 30)
253 (,gnus-catchup-mark . -10)
254 (,gnus-killed-mark . -20)
255 (,gnus-del-mark . -15))
256 "Alist of marks and scores."
257 :group 'gnus-score-adapt
258 :type '(repeat (cons (character :tag "Mark")
259 (integer :tag "Score"))))
261 (defcustom gnus-score-mimic-keymap nil
262 "*Have the score entry functions pretend that they are a keymap."
263 :group 'gnus-score-default
266 (defcustom gnus-score-exact-adapt-limit 10
267 "*Number that says how long a match has to be before using substring matching.
268 When doing adaptive scoring, one normally uses fuzzy or substring
269 matching. However, if the header one matches is short, the possibility
270 for false positives is great, so if the length of the match is less
271 than this variable, exact matching will be used.
273 If this variable is nil, exact matching will always be used."
274 :group 'gnus-score-adapt
275 :type '(choice (const nil) integer))
277 (defcustom gnus-score-uncacheable-files "ADAPT$"
278 "All score files that match this regexp will not be cached."
279 :group 'gnus-score-adapt
280 :group 'gnus-score-files
283 (defcustom gnus-score-default-header nil
284 "Default header when entering new scores.
286 Should be one of the following symbols.
299 If nil, the user will be asked for a header."
300 :group 'gnus-score-default
301 :type '(choice (const :tag "from" a)
302 (const :tag "subject" s)
303 (const :tag "body" b)
304 (const :tag "head" h)
305 (const :tag "message-id" i)
306 (const :tag "references" t)
307 (const :tag "xref" x)
308 (const :tag "lines" l)
309 (const :tag "date" d)
310 (const :tag "followup" f)))
312 (defcustom gnus-score-default-type nil
313 "Default match type when entering new scores.
315 Should be one of the following symbols.
325 >: greater than number
328 If nil, the user will be asked for a match type."
329 :group 'gnus-score-default
330 :type '(choice (const :tag "substring" s)
331 (const :tag "exact string" e)
332 (const :tag "fuzzy string" f)
333 (const :tag "regexp string" r)
334 (const :tag "before date" b)
335 (const :tag "at date" a)
336 (const :tag "this date" n)
337 (const :tag "less than number" <)
338 (const :tag "greater than number" >)
339 (const :tag "equal than number" =)))
341 (defcustom gnus-score-default-fold nil
342 "Use case folding for new score file entries iff not nil."
343 :group 'gnus-score-default
346 (defcustom gnus-score-default-duration nil
347 "Default duration of effect when entering new scores.
349 Should be one of the following symbols.
355 If nil, the user will be asked for a duration."
356 :group 'gnus-score-default
357 :type '(choice (const :tag "temporary" t)
358 (const :tag "permanent" p)
359 (const :tag "immediate" i)
360 (const :tag "ask" nil)))
362 (defcustom gnus-score-after-write-file-function nil
363 "Function called with the name of the score file just written to disk."
364 :group 'gnus-score-files
369 ;; Internal variables.
371 (defvar gnus-adaptive-word-syntax-table
372 (let ((table (copy-syntax-table (standard-syntax-table)))
373 (numbers '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)))
375 (modify-syntax-entry (pop numbers) " " table))
376 (modify-syntax-entry ?' "w" table)
378 "Syntax table used when doing adaptive word scoring.")
380 (defvar gnus-scores-exclude-files nil)
381 (defvar gnus-internal-global-score-files nil)
382 (defvar gnus-score-file-list nil)
384 (defvar gnus-short-name-score-file-cache nil)
386 (defvar gnus-score-help-winconf nil)
387 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
388 (defvar gnus-adaptive-word-score-alist gnus-default-adaptive-word-score-alist)
389 (defvar gnus-score-trace nil)
390 (defvar gnus-score-edit-buffer nil)
392 (defvar gnus-score-alist nil
393 "Alist containing score information.
394 The keys can be symbols or strings. The following symbols are defined.
396 touched: If this alist has been modified.
397 mark: Automatically mark articles below this.
398 expunge: Automatically expunge articles below this.
399 files: List of other score files to load when loading this one.
400 eval: Sexp to be evaluated when the score file is loaded.
402 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
403 where HEADER is the header being scored, MATCH is the string we are
404 looking for, TYPE is a flag indicating whether it should use regexp or
405 substring matching, SCORE is the score to add and DATE is the date
406 of the last successful match.")
408 (defvar gnus-score-cache nil)
409 (defvar gnus-scores-articles nil)
410 (defvar gnus-score-index nil)
413 (defconst gnus-header-index
414 ;; Name to index alist.
415 '(("number" 0 gnus-score-integer)
416 ("subject" 1 gnus-score-string)
417 ("from" 2 gnus-score-string)
418 ("date" 3 gnus-score-date)
419 ("message-id" 4 gnus-score-string)
420 ("references" 5 gnus-score-string)
421 ("chars" 6 gnus-score-integer)
422 ("lines" 7 gnus-score-integer)
423 ("xref" 8 gnus-score-string)
424 ("head" -1 gnus-score-body)
425 ("body" -1 gnus-score-body)
426 ("all" -1 gnus-score-body)
427 ("followup" 2 gnus-score-followup)
428 ("thread" 5 gnus-score-thread)))
430 ;;; Summary mode score maps.
432 (gnus-define-keys (gnus-summary-score-map "V" gnus-summary-mode-map)
433 "s" gnus-summary-set-score
434 "a" gnus-summary-score-entry
435 "S" gnus-summary-current-score
436 "c" gnus-score-change-score-file
437 "C" gnus-score-customize
438 "m" gnus-score-set-mark-below
439 "x" gnus-score-set-expunge-below
440 "R" gnus-summary-rescore
441 "e" gnus-score-edit-current-scores
442 "f" gnus-score-edit-file
443 "F" gnus-score-flush-cache
444 "t" gnus-score-find-trace
445 "w" gnus-score-find-favourite-words)
447 ;; Summary score file commands
449 ;; Much modification of the kill (ahem, score) code and lots of the
450 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
452 (defun gnus-summary-lower-score (&optional score)
453 "Make a score entry based on the current article.
454 The user will be prompted for header to score on, match type,
455 permanence, and the string to be used. The numerical prefix will be
458 (gnus-summary-increase-score (- (gnus-score-default score))))
460 (defun gnus-score-kill-help-buffer ()
461 (when (get-buffer "*Score Help*")
462 (kill-buffer "*Score Help*")
463 (when gnus-score-help-winconf
464 (set-window-configuration gnus-score-help-winconf))))
466 (defun gnus-summary-increase-score (&optional score)
467 "Make a score entry based on the current article.
468 The user will be prompted for header to score on, match type,
469 permanence, and the string to be used. The numerical prefix will be
472 (gnus-set-global-variables)
473 (let* ((nscore (gnus-score-default score))
474 (prefix (if (< nscore 0) ?L ?I))
475 (increase (> nscore 0))
477 '((?a "from" nil nil string)
478 (?s "subject" nil nil string)
479 (?b "body" "" nil body-string)
480 (?h "head" "" nil body-string)
481 (?i "message-id" nil t string)
482 (?t "references" "message-id" nil string)
483 (?x "xref" nil nil string)
484 (?l "lines" nil nil number)
485 (?d "date" nil nil date)
486 (?f "followup" nil nil string)
487 (?T "thread" nil nil string)))
489 '((?s s "substring" string)
490 (?e e "exact string" string)
491 (?f f "fuzzy string" string)
492 (?r r "regexp string" string)
493 (?z s "substring" body-string)
494 (?p r "regexp string" body-string)
495 (?b before "before date" date)
496 (?a at "at date" date)
497 (?n now "this date" date)
498 (?< < "less than number" number)
499 (?> > "greater than number" number)
500 (?= = "equal to number" number)))
502 (list (list ?t (current-time-string) "temporary")
503 '(?p perm "permanent") '(?i now "immediate")))
504 (mimic gnus-score-mimic-keymap)
505 (hchar (and gnus-score-default-header
506 (aref (symbol-name gnus-score-default-header) 0)))
507 (tchar (and gnus-score-default-type
508 (aref (symbol-name gnus-score-default-type) 0)))
509 (pchar (and gnus-score-default-duration
510 (aref (symbol-name gnus-score-default-duration) 0)))
511 entry temporary type match)
516 ;; First we read the header to score.
521 (message "%c-" prefix))
522 (message "%s header (%s?): " (if increase "Increase" "Lower")
523 (mapconcat (lambda (s) (char-to-string (car s)))
525 (setq hchar (read-char))
526 (when (or (= hchar ??) (= hchar ?\C-h))
528 (gnus-score-insert-help "Match on header" char-to-header 1)))
530 (gnus-score-kill-help-buffer)
531 (unless (setq entry (assq (downcase hchar) char-to-header))
532 (if mimic (error "%c %c" prefix hchar)
533 (error "Illegal header type")))
535 (when (/= (downcase hchar) hchar)
536 ;; This was a majuscule, so we end reading and set the defaults.
537 (if mimic (message "%c %c" prefix hchar) (message ""))
538 (setq tchar (or tchar ?s)
539 pchar (or pchar ?t)))
544 (if (eq (nth 4 entry)
548 ;; We continue reading - the type.
552 (sit-for 1) (message "%c %c-" prefix hchar))
553 (message "%s header '%s' with match type (%s?): "
554 (if increase "Increase" "Lower")
556 (mapconcat (lambda (s) (char-to-string (car s)))
558 (setq tchar (read-char))
559 (when (or (= tchar ??) (= tchar ?\C-h))
561 (gnus-score-insert-help "Match type" legal-types 2)))
563 (gnus-score-kill-help-buffer)
564 (unless (setq type (nth 1 (assq (downcase tchar) legal-types)))
565 (if mimic (error "%c %c" prefix hchar)
566 (error "Illegal match type"))))
568 (when (/= (downcase tchar) tchar)
569 ;; It was a majuscule, so we end reading and use the default.
570 (if mimic (message "%c %c %c" prefix hchar tchar)
572 (setq pchar (or pchar ?p)))
574 ;; We continue reading.
578 (sit-for 1) (message "%c %c %c-" prefix hchar tchar))
579 (message "%s permanence (%s?): " (if increase "Increase" "Lower")
580 (mapconcat (lambda (s) (char-to-string (car s)))
582 (setq pchar (read-char))
583 (when (or (= pchar ??) (= pchar ?\C-h))
585 (gnus-score-insert-help "Match permanence" char-to-perm 2)))
587 (gnus-score-kill-help-buffer)
588 (if mimic (message "%c %c %c" prefix hchar tchar pchar)
590 (unless (setq temporary (cadr (assq pchar char-to-perm)))
591 ;; Deal with der(r)ided superannuated paradigms.
592 (when (and (eq (1+ prefix) 77)
593 (eq (+ hchar 12) 109)
595 (eq (- pchar 4) 111))
598 (error "%c %c %c %c" prefix hchar tchar pchar)
599 (error "Illegal match duration"))))
600 ;; Always kill the score help buffer.
601 (gnus-score-kill-help-buffer))
603 ;; We have all the data, so we enter this score.
604 (setq match (if (string= (nth 2 entry) "") ""
605 (gnus-summary-header (or (nth 2 entry) (nth 1 entry)))))
607 ;; Modify the match, perhaps.
609 ((equal (nth 1 entry) "xref")
610 (when (string-match "^Xref: *" match)
611 (setq match (substring match (match-end 0))))
612 (when (string-match "^[^:]* +" match)
613 (setq match (substring match (match-end 0))))))
615 (when (memq type '(r R regexp Regexp))
616 (setq match (regexp-quote match)))
618 (gnus-summary-score-entry
619 (nth 1 entry) ; Header
622 (if (eq score 's) nil score) ; Score
623 (if (eq temporary 'perm) ; Temp
626 (not (nth 3 entry))) ; Prompt
629 (defun gnus-score-insert-help (string alist idx)
630 (setq gnus-score-help-winconf (current-window-configuration))
632 (set-buffer (get-buffer-create "*Score Help*"))
633 (buffer-disable-undo (current-buffer))
634 (delete-windows-on (current-buffer))
636 (insert string ":\n\n")
641 ;; find the longest string to display
643 (setq n (length (nth idx (car list))))
646 (setq list (cdr list)))
647 (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
648 (setq n (/ (1- (window-width)) max)) ; items per line
649 (setq width (/ (1- (window-width)) n)) ; width of each item
650 ;; insert `n' items, each in a field of width `width'
655 (delete-char -1) ; the `\n' takes a char
657 (setq pad (- width 3))
658 (setq format (concat "%c: %-" (int-to-string pad) "s"))
659 (insert (format format (caar alist) (nth idx (car alist))))
660 (setq alist (cdr alist))
662 ;; display ourselves in a small window at the bottom
663 (gnus-appt-select-lowest-window)
665 (pop-to-buffer "*Score Help*")
666 (let ((window-min-height 1))
667 (shrink-window-if-larger-than-buffer))
668 (select-window (get-buffer-window gnus-summary-buffer))))
670 (defun gnus-summary-header (header &optional no-err)
671 ;; Return HEADER for current articles, or error.
672 (let ((article (gnus-summary-article-number))
675 (if (and (setq headers (gnus-summary-article-header article))
677 (aref headers (nth 1 (assoc header gnus-header-index)))
680 (error "Pseudo-articles can't be scored")))
682 (error "No article on current line")
685 (defun gnus-newsgroup-score-alist ()
687 (let ((param-file (gnus-group-find-parameter
688 gnus-newsgroup-name 'score-file)))
690 (gnus-score-load param-file)))
692 (gnus-score-file-name gnus-newsgroup-name)))
695 (defsubst gnus-score-get (symbol &optional alist)
696 ;; Get SYMBOL's definition in ALIST.
700 (gnus-newsgroup-score-alist)))))
702 (defun gnus-summary-score-entry (header match type score date
703 &optional prompt silent)
704 "Enter score file entry.
705 HEADER is the header being scored.
706 MATCH is the string we are looking for.
707 TYPE is the match type: substring, regexp, exact, fuzzy.
708 SCORE is the score to add.
709 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
710 If optional argument `PROMPT' is non-nil, allow user to edit match.
711 If optional argument `SILENT' is nil, show effect of score entry."
713 (list (completing-read "Header: "
715 (lambda (x) (fboundp (nth 2 x)))
717 (read-string "Match: ")
718 (if (y-or-n-p "Use regexp match? ") 'r 's)
719 (and current-prefix-arg
720 (prefix-numeric-value current-prefix-arg))
721 (cond ((not (y-or-n-p "Add to score file? "))
723 ((y-or-n-p "Expire kill? ")
724 (current-time-string))
726 ;; Regexp is the default type.
729 ;; Simplify matches...
730 (cond ((or (eq type 'r) (eq type 's) (eq type nil))
731 (setq match (if match (gnus-simplify-subject-re match) "")))
733 (setq match (gnus-simplify-subject-fuzzy match))))
734 (let ((score (gnus-score-default score))
735 (header (format "%s" (downcase header)))
738 (setq match (read-string
739 (format "Match %s on %s, %s: "
740 (cond ((eq date 'now)
746 (if (< score 0) "lower" "raise"))
748 (int-to-string match)
751 ;; Get rid of string props.
752 (setq match (format "%s" match))
754 ;; If this is an integer comparison, we transform from string to int.
755 (when (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
756 (setq match (string-to-int match)))
758 (unless (eq date 'now)
759 ;; Add the score entry to the score file.
760 (when (= score gnus-score-interactive-default-score)
762 (let ((old (gnus-score-get header))
768 (and date (if (numberp date) date
769 (gnus-day-number date)))
771 (date (list match score (gnus-day-number date)))
772 (score (list match score))
774 ;; We see whether we can collapse some score entries.
775 ;; This isn't quite correct, because there may be more elements
776 ;; later on with the same key that have matching elems... Hm.
778 (setq elem (assoc match old))
779 (eq (nth 3 elem) (nth 3 new))
780 (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
781 (and (not (nth 2 elem)) (not (nth 2 new)))))
782 ;; Yup, we just add this new score to the old elem.
783 (setcar (cdr elem) (+ (or (nth 1 elem)
784 gnus-score-interactive-default-score)
786 gnus-score-interactive-default-score)))
787 ;; Nope, we have to add a new elem.
788 (gnus-score-set header (if old (cons new old) (list new))))
789 (gnus-score-set 'touched '(t))))
791 ;; Score the current buffer.
793 (if (and (>= (nth 1 (assoc header gnus-header-index)) 0)
794 (eq (nth 2 (assoc header gnus-header-index))
796 (gnus-summary-score-effect header match type score)
797 (gnus-summary-rescore)))
799 ;; Return the new scoring rule.
802 (defun gnus-summary-score-effect (header match type score)
803 "Simulate the effect of a score file entry.
804 HEADER is the header being scored.
805 MATCH is the string we are looking for.
806 TYPE is the score type.
807 SCORE is the score to add."
808 (interactive (list (completing-read "Header: "
810 (lambda (x) (fboundp (nth 2 x)))
812 (read-string "Match: ")
813 (y-or-n-p "Use regexp match? ")
814 (prefix-numeric-value current-prefix-arg)))
816 (unless (and (stringp match) (> (length match) 0))
818 (goto-char (point-min))
819 (let ((regexp (cond ((eq type 'f)
820 (gnus-simplify-subject-fuzzy match))
824 (concat "\\`" (regexp-quote match) "\\'"))
826 (regexp-quote match)))))
828 (let ((content (gnus-summary-header header 'noerr))
829 (case-fold-search t))
831 (when (if (eq type 'f)
832 (string-equal (gnus-simplify-subject-fuzzy content)
834 (string-match regexp content))
835 (gnus-summary-raise-score score))))
836 (beginning-of-line 2))))
837 (gnus-set-mode-line 'summary))
839 (defun gnus-summary-score-crossposting (score date)
840 ;; Enter score file entry for current crossposting.
841 ;; SCORE is the score to add.
842 ;; DATE is the expire date.
843 (let ((xref (gnus-summary-header "xref"))
847 (error "This article is not crossposted"))
848 (while (string-match " \\([^ \t]+\\):" xref start)
849 (setq start (match-end 0))
852 (substring xref (match-beginning 1) (match-end 1)))
853 gnus-newsgroup-name))
854 (gnus-summary-score-entry
855 "xref" (concat " " group ":") nil score date t)))))
862 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
864 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
865 (defun gnus-score-set-mark-below (score)
866 "Automatically mark articles with score below SCORE as read."
868 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
869 (string-to-int (read-string "Mark below: ")))))
870 (setq score (or score gnus-summary-default-score 0))
871 (gnus-score-set 'mark (list score))
872 (gnus-score-set 'touched '(t))
873 (setq gnus-summary-mark-below score)
874 (gnus-score-update-lines))
876 (defun gnus-score-update-lines ()
877 "Update all lines in the summary buffer."
879 (goto-char (point-min))
881 (gnus-summary-update-line)
884 (defun gnus-score-update-all-lines ()
885 "Update all lines in the summary buffer, even the hidden ones."
887 (goto-char (point-min))
890 (when (gnus-summary-show-thread)
891 (push (point) hidden))
892 (gnus-summary-update-line)
894 ;; Re-hide the hidden threads.
896 (goto-char (pop hidden))
897 (gnus-summary-hide-thread)))))
899 (defun gnus-score-set-expunge-below (score)
900 "Automatically expunge articles with score below SCORE."
902 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
903 (string-to-int (read-string "Set expunge below: ")))))
904 (setq score (or score gnus-summary-default-score 0))
905 (gnus-score-set 'expunge (list score))
906 (gnus-score-set 'touched '(t)))
908 (defun gnus-score-followup-article (&optional score)
909 "Add SCORE to all followups to the article in the current buffer."
911 (setq score (gnus-score-default score))
912 (when (gnus-buffer-live-p gnus-summary-buffer)
915 (message-narrow-to-headers)
916 (let ((id (mail-fetch-field "message-id")))
918 (set-buffer gnus-summary-buffer)
919 (gnus-summary-score-entry
920 "references" (concat id "[ \t]*$") 'r
921 score (current-time-string) nil t)))))))
923 (defun gnus-score-followup-thread (&optional score)
924 "Add SCORE to all later articles in the thread the current buffer is part of."
926 (setq score (gnus-score-default score))
927 (when (gnus-buffer-live-p gnus-summary-buffer)
930 (goto-char (point-min))
931 (let ((id (mail-fetch-field "message-id")))
933 (set-buffer gnus-summary-buffer)
934 (gnus-summary-score-entry
936 score (current-time-string))))))))
938 (defun gnus-score-set (symbol value &optional alist)
939 ;; Set SYMBOL to VALUE in ALIST.
943 (gnus-newsgroup-score-alist)))
944 (entry (assoc symbol alist)))
945 (cond ((gnus-score-get 'read-only alist)
946 ;; This is a read-only score file, so we do nothing.
949 (setcdr entry value))
951 (error "Empty alist"))
954 (cons (cons symbol value) (cdr alist)))))))
956 (defun gnus-summary-raise-score (n)
957 "Raise the score of the current article by N."
959 (gnus-set-global-variables)
960 (gnus-summary-set-score (+ (gnus-summary-article-score)
961 (or n gnus-score-interactive-default-score ))))
963 (defun gnus-summary-set-score (n)
964 "Set the score of the current article to N."
966 (gnus-set-global-variables)
968 (gnus-summary-show-thread)
969 (let ((buffer-read-only nil))
971 (gnus-summary-update-mark
972 (if (= n (or gnus-summary-default-score 0)) ?
973 (if (< n (or gnus-summary-default-score 0))
974 gnus-score-below-mark gnus-score-over-mark))
976 (let* ((article (gnus-summary-article-number))
977 (score (assq article gnus-newsgroup-scored)))
978 (if score (setcdr score n)
979 (push (cons article n) gnus-newsgroup-scored)))
980 (gnus-summary-update-line)))
982 (defun gnus-summary-current-score ()
983 "Return the score of the current article."
985 (gnus-set-global-variables)
986 (gnus-message 1 "%s" (gnus-summary-article-score)))
988 (defun gnus-score-change-score-file (file)
989 "Change current score alist."
991 (list (read-file-name "Change to score file: " gnus-kill-files-directory)))
992 (gnus-score-load-file file)
993 (gnus-set-mode-line 'summary))
995 (defvar gnus-score-edit-exit-function)
996 (defun gnus-score-edit-current-scores (file)
997 "Edit the current score alist."
998 (interactive (list gnus-current-score-file))
999 (gnus-set-global-variables)
1000 (let ((winconf (current-window-configuration)))
1001 (when (buffer-name gnus-summary-buffer)
1003 (gnus-make-directory (file-name-directory file))
1004 (setq gnus-score-edit-buffer (find-file-noselect file))
1005 (gnus-configure-windows 'edit-score)
1006 (select-window (get-buffer-window gnus-score-edit-buffer))
1008 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1009 (make-local-variable 'gnus-prev-winconf)
1010 (setq gnus-prev-winconf winconf))
1012 4 (substitute-command-keys
1013 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
1015 (defun gnus-score-edit-file (file)
1016 "Edit a score file."
1018 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
1019 (gnus-make-directory (file-name-directory file))
1020 (when (buffer-name gnus-summary-buffer)
1022 (let ((winconf (current-window-configuration)))
1023 (setq gnus-score-edit-buffer (find-file-noselect file))
1024 (gnus-configure-windows 'edit-score)
1026 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1027 (make-local-variable 'gnus-prev-winconf)
1028 (setq gnus-prev-winconf winconf))
1030 4 (substitute-command-keys
1031 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
1033 (defun gnus-score-load-file (file)
1034 ;; Load score file FILE. Returns a list a retrieved score-alists.
1035 (let* ((file (expand-file-name
1036 (or (and (string-match
1037 (concat "^" (expand-file-name
1038 gnus-kill-files-directory))
1039 (expand-file-name file))
1041 (concat (file-name-as-directory gnus-kill-files-directory)
1043 (cached (assoc file gnus-score-cache))
1044 (global (member file gnus-internal-global-score-files))
1047 ;; The score file was already loaded.
1048 (setq alist (cdr cached))
1049 ;; We load the score file.
1050 (setq gnus-score-alist nil)
1051 (setq alist (gnus-score-load-score-alist file))
1052 ;; We add '(touched) to the alist to signify that it hasn't been
1054 (unless (assq 'touched alist)
1055 (push (list 'touched nil) alist))
1056 ;; If it is a global score file, we make it read-only.
1058 (not (assq 'read-only alist))
1059 (push (list 'read-only t) alist))
1060 (push (cons file alist) gnus-score-cache))
1064 ;; Downcase all header names.
1065 (when (stringp (caar a))
1066 (setcar (car a) (downcase (caar a)))
1069 ;; If there are actual scores in the alist, we add it to the
1070 ;; return value of this function.
1072 (setq lists (list alist))))
1073 ;; Treat the other possible atoms in the score alist.
1074 (let ((mark (car (gnus-score-get 'mark alist)))
1075 (expunge (car (gnus-score-get 'expunge alist)))
1076 (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
1077 (files (gnus-score-get 'files alist))
1078 (exclude-files (gnus-score-get 'exclude-files alist))
1079 (orphan (car (gnus-score-get 'orphan alist)))
1080 (adapt (gnus-score-get 'adapt alist))
1081 (thread-mark-and-expunge
1082 (car (gnus-score-get 'thread-mark-and-expunge alist)))
1083 (adapt-file (car (gnus-score-get 'adapt-file alist)))
1084 (local (gnus-score-get 'local alist))
1085 (decay (car (gnus-score-get 'decay alist)))
1086 (eval (car (gnus-score-get 'eval alist))))
1087 ;; Perform possible decays.
1088 (when gnus-decay-scores
1089 (when (or (not decay)
1090 (gnus-decay-scores alist decay))
1091 (gnus-score-set 'touched '(t) alist)
1092 (gnus-score-set 'decay (list (gnus-time-to-day (current-time))))))
1093 ;; We do not respect eval and files atoms from global score
1095 (and files (not global)
1096 (setq lists (apply 'append lists
1097 (mapcar (lambda (file)
1098 (gnus-score-load-file file))
1099 (if adapt-file (cons adapt-file files)
1101 (and eval (not global) (eval eval))
1102 ;; We then expand any exclude-file directives.
1103 (setq gnus-scores-exclude-files
1107 (expand-file-name sfile (file-name-directory file)))
1109 gnus-scores-exclude-files))
1113 (set-buffer gnus-summary-buffer)
1115 (and (consp (car local))
1116 (symbolp (caar local))
1118 (make-local-variable (caar local))
1119 (set (caar local) (nth 1 (car local)))))
1120 (setq local (cdr local)))))
1122 (setq gnus-orphan-score orphan))
1123 (setq gnus-adaptive-score-alist
1124 (cond ((equal adapt '(t))
1125 (setq gnus-newsgroup-adaptive t)
1126 gnus-default-adaptive-score-alist)
1127 ((equal adapt '(ignore))
1128 (setq gnus-newsgroup-adaptive nil))
1130 (setq gnus-newsgroup-adaptive t)
1133 ;;(setq gnus-newsgroup-adaptive gnus-use-adaptive-scoring)
1134 gnus-default-adaptive-score-alist)))
1135 (setq gnus-thread-expunge-below
1136 (or thread-mark-and-expunge gnus-thread-expunge-below))
1137 (setq gnus-summary-mark-below
1138 (or mark mark-and-expunge gnus-summary-mark-below))
1139 (setq gnus-summary-expunge-below
1140 (or expunge mark-and-expunge gnus-summary-expunge-below))
1141 (setq gnus-newsgroup-adaptive-score-file
1142 (or adapt-file gnus-newsgroup-adaptive-score-file)))
1143 (setq gnus-current-score-file file)
1144 (setq gnus-score-alist alist)
1147 (defun gnus-score-load (file)
1149 (let ((cache (assoc file gnus-score-cache)))
1151 (setq gnus-score-alist (cdr cache))
1152 (setq gnus-score-alist nil)
1153 (gnus-score-load-score-alist file)
1154 (unless gnus-score-alist
1155 (setq gnus-score-alist (copy-alist '((touched nil)))))
1156 (push (cons file gnus-score-alist) gnus-score-cache))))
1158 (defun gnus-score-remove-from-cache (file)
1159 (setq gnus-score-cache
1160 (delq (assoc file gnus-score-cache) gnus-score-cache)))
1162 (defun gnus-score-load-score-alist (file)
1165 (if (not (file-readable-p file))
1166 ;; Couldn't read file.
1167 (setq gnus-score-alist nil)
1170 (gnus-set-work-buffer)
1171 (insert-file-contents file)
1172 (goto-char (point-min))
1173 ;; Only do the loading if the score file isn't empty.
1174 (when (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
1177 (read (current-buffer))
1179 (gnus-error 3.2 "Problem with score file %s" file))))))
1180 (if (eq (car alist) 'setq)
1181 ;; This is an old-style score file.
1182 (setq gnus-score-alist (gnus-score-transform-old-to-new alist))
1183 (setq gnus-score-alist alist))
1184 ;; Check the syntax of the score file.
1185 (setq gnus-score-alist
1186 (gnus-score-check-syntax gnus-score-alist file)))))
1188 (defun gnus-score-check-syntax (alist file)
1189 "Check the syntax of the score ALIST."
1193 ((not (consp alist))
1194 (gnus-message 1 "Score file is not a list: %s" file)
1200 (while (and a (not err))
1204 ((not (listp (car a)))
1205 (format "Illegal score element %s in %s" (car a) file))
1208 ((not (listp (setq sr (cdar a))))
1209 (format "Illegal header match %s in %s" (nth 1 (car a)) file))
1211 (setq type (caar a))
1212 (while (and sr (not err))
1217 ((if (member (downcase type) '("lines" "chars"))
1218 (not (numberp (car s)))
1219 (not (stringp (car s))))
1220 (format "Illegal match %s in %s" (car s) file))
1221 ((and (cadr s) (not (integerp (cadr s))))
1222 (format "Non-integer score %s in %s" (cadr s) file))
1223 ((and (caddr s) (not (integerp (caddr s))))
1224 (format "Non-integer date %s in %s" (caddr s) file))
1225 ((and (cadddr s) (not (symbolp (cadddr s))))
1226 (format "Non-symbol match type %s in %s" (cadddr s) file)))))
1232 (gnus-message 3 err)
1237 (defun gnus-score-transform-old-to-new (alist)
1238 (let* ((alist (nth 2 alist))
1240 (when (eq (car alist) 'quote)
1241 (setq alist (nth 1 alist)))
1243 (setq entry (car alist))
1244 (if (stringp (car entry))
1245 (let ((scor (cdr entry)))
1249 (list (caar scor) (nth 2 (car scor))
1250 (and (nth 3 (car scor))
1251 (gnus-day-number (nth 3 (car scor))))
1252 (if (nth 1 (car scor)) 'r 's)))
1253 (setq scor (cdr scor))))
1254 (push (if (not (listp (cdr entry)))
1255 (list (car entry) (cdr entry))
1258 (setq alist (cdr alist)))
1259 (cons (list 'touched t) (nreverse out))))
1261 (defun gnus-score-save ()
1262 ;; Save all score information.
1263 (let ((cache gnus-score-cache)
1266 (setq gnus-score-alist nil)
1267 (nnheader-set-temp-buffer " *Gnus Scores*")
1270 (setq entry (pop cache)
1273 (if (or (not (equal (gnus-score-get 'touched score) '(t)))
1274 (gnus-score-get 'read-only score)
1275 (and (file-exists-p file)
1276 (not (file-writable-p file))))
1278 (setq score (setcdr entry (delq (assq 'touched score) score)))
1280 (let (emacs-lisp-mode-hook)
1282 (concat (regexp-quote gnus-adaptive-file-suffix) "$")
1284 ;; This is an adaptive score file, so we do not run
1285 ;; it through `pp'. These files can get huge, and
1286 ;; are not meant to be edited by human hands.
1288 ;; This is a normal score file, so we print it very
1290 (pp score (current-buffer))))
1291 (gnus-make-directory (file-name-directory file))
1292 ;; If the score file is empty, we delete it.
1293 (if (zerop (buffer-size))
1295 ;; There are scores, so we write the file.
1296 (when (file-writable-p file)
1297 (gnus-write-buffer file)
1298 (when gnus-score-after-write-file-function
1299 (funcall gnus-score-after-write-file-function file)))))
1300 (and gnus-score-uncacheable-files
1301 (string-match gnus-score-uncacheable-files file)
1302 (gnus-score-remove-from-cache file)))
1303 (kill-buffer (current-buffer)))))
1305 (defun gnus-score-load-files (score-files)
1306 "Load all score files in SCORE-FILES."
1307 ;; Load the score files.
1310 (if (stringp (car score-files))
1311 ;; It is a string, which means that it's a score file name,
1312 ;; so we load the score file and add the score alist to
1313 ;; the list of alists.
1314 (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
1315 ;; It is an alist, so we just add it to the list directly.
1316 (setq scores (nconc (car score-files) scores)))
1317 (setq score-files (cdr score-files)))
1318 ;; Prune the score files that are to be excluded, if any.
1319 (when gnus-scores-exclude-files
1323 (and (setq c (rassq (car s) gnus-score-cache))
1324 (member (car c) gnus-scores-exclude-files)
1325 (setq scores (delq (car s) scores)))
1329 (defun gnus-score-headers (score-files &optional trace)
1330 ;; Score `gnus-newsgroup-headers'.
1332 ;; PLM: probably this is not the best place to clear orphan-score
1333 (setq gnus-orphan-score nil
1334 gnus-scores-articles nil
1335 gnus-scores-exclude-files nil
1336 scores (gnus-score-load-files score-files))
1342 (when (and gnus-summary-default-score
1344 (let* ((entries gnus-header-index)
1345 (now (gnus-day-number (current-time-string)))
1346 (expire (and gnus-score-expiry-days
1347 (- now gnus-score-expiry-days)))
1348 (headers gnus-newsgroup-headers)
1349 (current-score-file gnus-current-score-file)
1351 (gnus-message 5 "Scoring...")
1352 ;; Create articles, an alist of the form `(HEADER . SCORE)'.
1353 (while (setq header (pop headers))
1354 ;; WARNING: The assq makes the function O(N*S) while it could
1355 ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
1356 ;; and S is (length gnus-newsgroup-scored).
1357 (unless (assq (mail-header-number header) gnus-newsgroup-scored)
1358 (setq gnus-scores-articles ;Total of 2 * N cons-cells used.
1359 (cons (cons header (or gnus-summary-default-score 0))
1360 gnus-scores-articles))))
1363 (set-buffer (get-buffer-create "*Headers*"))
1364 (buffer-disable-undo (current-buffer))
1365 (when (gnus-buffer-live-p gnus-summary-buffer)
1366 (message-clone-locals gnus-summary-buffer))
1368 ;; Set the global variant of this variable.
1369 (setq gnus-current-score-file current-score-file)
1371 (when gnus-orphan-score
1372 (setq gnus-score-index
1373 (nth 1 (assoc "references" gnus-header-index)))
1374 (gnus-score-orphans gnus-orphan-score))
1375 ;; Run each header through the score process.
1377 (setq entry (pop entries)
1378 header (nth 0 entry)
1379 gnus-score-index (nth 1 (assoc header gnus-header-index)))
1380 (when (< 0 (apply 'max (mapcar
1382 (length (gnus-score-get header score)))
1384 ;; Call the scoring function for this type of "header".
1385 (when (setq new (funcall (nth 2 entry) scores header
1388 ;; Remove the buffer.
1389 (kill-buffer (current-buffer)))
1391 ;; Add articles to `gnus-newsgroup-scored'.
1392 (while gnus-scores-articles
1393 (when (or (/= gnus-summary-default-score
1394 (cdar gnus-scores-articles))
1396 (push (cons (mail-header-number (caar gnus-scores-articles))
1397 (cdar gnus-scores-articles))
1398 gnus-newsgroup-scored))
1399 (setq gnus-scores-articles (cdr gnus-scores-articles)))
1402 (while (setq score (pop scores))
1404 (when (listp (caar score))
1405 (gnus-score-advanced (car score) trace))
1408 (gnus-message 5 "Scoring...done"))))))
1411 (defun gnus-get-new-thread-ids (articles)
1412 (let ((index (nth 1 (assoc "message-id" gnus-header-index)))
1413 (refind gnus-score-index)
1414 id-list art this tref)
1416 (setq art (car articles)
1417 this (aref (car art) index)
1418 tref (aref (car art) refind)
1419 articles (cdr articles))
1420 (when (string-equal tref "") ;no references line
1421 (push this id-list)))
1424 ;; Orphan functions written by plm@atcmp.nl (Peter Mutsaers).
1425 (defun gnus-score-orphans (score)
1426 (let ((new-thread-ids (gnus-get-new-thread-ids gnus-scores-articles))
1427 alike articles art arts this last this-id)
1429 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1430 articles gnus-scores-articles)
1432 ;;more or less the same as in gnus-score-string
1435 (setq art (car articles)
1436 this (aref (car art) gnus-score-index)
1437 articles (cdr articles))
1438 ;;completely skip if this is empty (not a child, so not an orphan)
1439 (when (not (string= this ""))
1440 (if (equal last this)
1441 ;; O(N*H) cons-cells used here, where H is the number of
1445 ;; Insert the line, with a text property on the
1446 ;; terminating newline referring to the articles with
1449 (put-text-property (1- (point)) (point) 'articles alike))
1450 (setq alike (list art)
1452 (when last ; Bwadr, duplicate code.
1454 (put-text-property (1- (point)) (point) 'articles alike))
1456 ;; PLM: now delete those lines that contain an entry from new-thread-ids
1457 (while new-thread-ids
1458 (setq this-id (car new-thread-ids)
1459 new-thread-ids (cdr new-thread-ids))
1460 (goto-char (point-min))
1461 (while (search-forward this-id nil t)
1462 ;; found a match. remove this line
1466 ;; now for each line: update its articles with score by moving to
1467 ;; every end-of-line in the buffer and read the articles property
1468 (goto-char (point-min))
1471 (setq arts (get-text-property (point) 'articles))
1473 (setq art (car arts)
1475 (setcdr art (+ score (cdr art))))
1479 (defun gnus-score-integer (scores header now expire &optional trace)
1480 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1485 (setq alist (car scores)
1487 entries (assoc header alist))
1488 (while (cdr entries) ;First entry is the header index.
1489 (let* ((rest (cdr entries))
1491 (match (nth 0 kill))
1492 (type (or (nth 3 kill) '>))
1493 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1496 (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
1497 (eq type '>=) (eq type '=))
1499 (error "Illegal match type: %s" type)))
1500 (articles gnus-scores-articles))
1501 ;; Instead of doing all the clever stuff that
1502 ;; `gnus-score-string' does to minimize searches and stuff,
1503 ;; I will assume that people generally will put so few
1504 ;; matches on numbers that any cleverness will take more
1505 ;; time than one would gain.
1507 (when (funcall match-func
1508 (or (aref (caar articles) gnus-score-index) 0)
1511 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1514 (setcdr (car articles) (+ score (cdar articles))))
1515 (setq articles (cdr articles)))
1516 ;; Update expire date
1517 (cond ((null date)) ;Permanent entry.
1518 ((and found gnus-update-score-entry-dates) ;Match, update date.
1519 (gnus-score-set 'touched '(t) alist)
1520 (setcar (nthcdr 2 kill) now))
1521 ((and expire (< date expire)) ;Old entry, remove.
1522 (gnus-score-set 'touched '(t) alist)
1523 (setcdr entries (cdr rest))
1524 (setq rest entries)))
1525 (setq entries rest)))))
1528 (defun gnus-score-date (scores header now expire &optional trace)
1529 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1530 entries alist match match-func article)
1534 (setq alist (car scores)
1536 entries (assoc header alist))
1537 (while (cdr entries) ;First entry is the header index.
1538 (let* ((rest (cdr entries))
1540 (type (or (nth 3 kill) 'before))
1541 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1544 (articles gnus-scores-articles)
1548 (setq match-func 'string<
1549 match (gnus-date-iso8601 (nth 0 kill))))
1551 (setq match-func 'gnus-string>
1552 match (gnus-date-iso8601 (nth 0 kill))))
1554 (setq match-func 'string=
1555 match (gnus-date-iso8601 (nth 0 kill))))
1557 (setq match-func 'string-match
1558 match (nth 0 kill)))
1559 (t (error "Illegal match type: %s" type)))
1560 ;; Instead of doing all the clever stuff that
1561 ;; `gnus-score-string' does to minimize searches and stuff,
1562 ;; I will assume that people generally will put so few
1563 ;; matches on numbers that any cleverness will take more
1564 ;; time than one would gain.
1565 (while (setq article (pop articles))
1567 (setq l (aref (car article) gnus-score-index))
1568 (funcall match-func match (gnus-date-iso8601 l)))
1570 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1573 (setcdr article (+ score (cdr article)))))
1574 ;; Update expire date
1575 (cond ((null date)) ;Permanent entry.
1576 ((and found gnus-update-score-entry-dates) ;Match, update date.
1577 (gnus-score-set 'touched '(t) alist)
1578 (setcar (nthcdr 2 kill) now))
1579 ((and expire (< date expire)) ;Old entry, remove.
1580 (gnus-score-set 'touched '(t) alist)
1581 (setcdr entries (cdr rest))
1582 (setq rest entries)))
1583 (setq entries rest)))))
1586 (defun gnus-score-body (scores header now expire &optional trace)
1588 (setq gnus-scores-articles
1589 (sort gnus-scores-articles
1591 (< (mail-header-number (car a1))
1592 (mail-header-number (car a2))))))
1593 (set-buffer nntp-server-buffer)
1595 (let* ((buffer-read-only nil)
1596 (articles gnus-scores-articles)
1598 (request-func (cond ((string= "head" header)
1600 ((string= "body" header)
1602 (t 'gnus-request-article)))
1603 entries alist ofunc article last)
1605 (setq last (mail-header-number (caar (last articles))))
1606 ;; Not all backends support partial fetching. In that case,
1607 ;; we just fetch the entire article.
1608 (unless (gnus-check-backend-function
1609 (and (string-match "^gnus-" (symbol-name request-func))
1610 (intern (substring (symbol-name request-func)
1612 gnus-newsgroup-name)
1613 (setq ofunc request-func)
1614 (setq request-func 'gnus-request-article))
1616 (setq article (mail-header-number (caar articles)))
1617 (gnus-message 7 "Scoring on article %s of %s..." article last)
1618 (when (funcall request-func article gnus-newsgroup-name)
1620 (goto-char (point-min))
1621 ;; If just parts of the article is to be searched, but the
1622 ;; backend didn't support partial fetching, we just narrow
1623 ;; to the relevant parts.
1625 (if (eq ofunc 'gnus-request-head)
1628 (or (search-forward "\n\n" nil t) (point-max)))
1630 (or (search-forward "\n\n" nil t) (point))
1632 (setq scores all-scores)
1635 (setq alist (pop scores)
1636 entries (assoc header alist))
1637 (while (cdr entries) ;First entry is the header index.
1638 (let* ((rest (cdr entries))
1640 (match (nth 0 kill))
1641 (type (or (nth 3 kill) 's))
1642 (score (or (nth 1 kill)
1643 gnus-score-interactive-default-score))
1647 (not (or (eq type 'R) (eq type 'S)
1648 (eq type 'Regexp) (eq type 'String))))
1650 (cond ((or (eq type 'r) (eq type 'R)
1651 (eq type 'regexp) (eq type 'Regexp))
1653 ((or (eq type 's) (eq type 'S)
1654 (eq type 'string) (eq type 'String))
1657 (error "Illegal match type: %s" type)))))
1658 (goto-char (point-min))
1659 (when (funcall search-func match nil t)
1660 ;; Found a match, update scores.
1661 (setcdr (car articles) (+ score (cdar articles)))
1665 (cons (car-safe (rassq alist gnus-score-cache)) kill)
1667 ;; Update expire date
1670 ((null date)) ;Permanent entry.
1671 ((and found gnus-update-score-entry-dates)
1672 ;; Match, update date.
1673 (gnus-score-set 'touched '(t) alist)
1674 (setcar (nthcdr 2 kill) now))
1675 ((and expire (< date expire)) ;Old entry, remove.
1676 (gnus-score-set 'touched '(t) alist)
1677 (setcdr entries (cdr rest))
1678 (setq rest entries))))
1679 (setq entries rest)))))
1680 (setq articles (cdr articles)))))))
1683 (defun gnus-score-thread (scores header now expire &optional trace)
1684 (gnus-score-followup scores header now expire trace t))
1686 (defun gnus-score-followup (scores header now expire &optional trace thread)
1687 ;; Insert the unique article headers in the buffer.
1688 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1689 (current-score-file gnus-current-score-file)
1691 ;; gnus-score-index is used as a free variable.
1692 alike last this art entries alist articles
1695 ;; Change score file to the adaptive score file. All entries that
1696 ;; this function makes will be put into this file.
1698 (set-buffer gnus-summary-buffer)
1699 (gnus-score-load-file
1700 (or gnus-newsgroup-adaptive-score-file
1701 (gnus-score-file-name
1702 gnus-newsgroup-name gnus-adaptive-file-suffix))))
1704 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1705 articles gnus-scores-articles)
1709 (setq art (car articles)
1710 this (aref (car art) gnus-score-index)
1711 articles (cdr articles))
1712 (if (equal last this)
1716 (put-text-property (1- (point)) (point) 'articles alike))
1717 (setq alike (list art)
1719 (when last ; Bwadr, duplicate code.
1721 (put-text-property (1- (point)) (point) 'articles alike))
1725 (setq alist (car scores)
1727 entries (assoc header alist))
1728 (while (cdr entries) ;First entry is the header index.
1729 (let* ((rest (cdr entries))
1731 (match (nth 0 kill))
1732 (type (or (nth 3 kill) 's))
1733 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1736 (mt (aref (symbol-name type) 0))
1738 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1741 (cond ((= dmt ?r) 're-search-forward)
1742 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1743 (t (error "Illegal match type: %s" type))))
1745 (goto-char (point-min))
1747 (while (funcall search-func match nil t)
1748 (and (= (progn (beginning-of-line) (point))
1749 (match-beginning 0))
1750 (= (progn (end-of-line) (point))
1753 (setq found (setq arts (get-text-property
1754 (point) 'articles)))
1755 ;; Found a match, update scores.
1757 (setq art (car arts)
1759 (gnus-score-add-followups
1760 (car art) score all-scores thread))))
1762 (while (funcall search-func match nil t)
1764 (setq found (setq arts (get-text-property (point) 'articles)))
1765 ;; Found a match, update scores.
1766 (while (setq art (pop arts))
1767 (when (setq new (gnus-score-add-followups
1768 (car art) score all-scores thread))
1770 ;; Update expire date
1771 (cond ((null date)) ;Permanent entry.
1772 ((and found gnus-update-score-entry-dates) ;Match, update date.
1773 (gnus-score-set 'touched '(t) alist)
1774 (setcar (nthcdr 2 kill) now))
1775 ((and expire (< date expire)) ;Old entry, remove.
1776 (gnus-score-set 'touched '(t) alist)
1777 (setcdr entries (cdr rest))
1778 (setq rest entries)))
1779 (setq entries rest))))
1780 ;; We change the score file back to the previous one.
1782 (set-buffer gnus-summary-buffer)
1783 (gnus-score-load-file current-score-file))
1784 (list (cons "references" news))))
1786 (defun gnus-score-add-followups (header score scores &optional thread)
1787 "Add a score entry to the adapt file."
1789 (set-buffer gnus-summary-buffer)
1790 (let* ((id (mail-header-id header))
1791 (scores (car scores))
1793 ;; Don't enter a score if there already is one.
1794 (while (setq entry (pop scores))
1795 (and (equal "references" (car entry))
1796 (or (null (nth 3 (cadr entry)))
1797 (eq 's (nth 3 (cadr entry))))
1801 (gnus-summary-score-entry
1802 (if thread "thread" "references")
1803 id 's score (current-time-string) nil t)))))
1805 (defun gnus-score-string (score-list header now expire &optional trace)
1806 ;; Score ARTICLES according to HEADER in SCORE-LIST.
1807 ;; Update matching entries to NOW and remove unmatched entries older
1810 ;; Insert the unique article headers in the buffer.
1811 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1812 ;; gnus-score-index is used as a free variable.
1813 alike last this art entries alist articles
1814 fuzzies arts words kill)
1816 ;; Sorting the articles costs os O(N*log N) but will allow us to
1817 ;; only match with each unique header. Thus the actual matching
1818 ;; will be O(M*U) where M is the number of strings to match with,
1819 ;; and U is the number of unique headers. It is assumed (but
1820 ;; untested) this will be a net win because of the large constant
1821 ;; factor involved with string matching.
1822 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1823 articles gnus-scores-articles)
1826 (while (setq art (pop articles))
1827 (setq this (aref (car art) gnus-score-index))
1828 (if (equal last this)
1829 ;; O(N*H) cons-cells used here, where H is the number of
1833 ;; Insert the line, with a text property on the
1834 ;; terminating newline referring to the articles with
1837 (put-text-property (1- (point)) (point) 'articles alike))
1838 (setq alike (list art)
1840 (when last ; Bwadr, duplicate code.
1842 (put-text-property (1- (point)) (point) 'articles alike))
1844 ;; Go through all the score alists and pick out the entries
1847 (setq alist (pop score-list)
1848 ;; There's only one instance of this header for
1849 ;; each score alist.
1850 entries (assoc header alist))
1851 (while (cdr entries) ;First entry is the header index.
1852 (let* ((kill (cadr entries))
1853 (match (nth 0 kill))
1854 (type (or (nth 3 kill) 's))
1855 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1858 (mt (aref (symbol-name type) 0))
1859 (case-fold-search (not (memq mt '(?R ?S ?E ?F))))
1862 (cond ((= dmt ?r) 're-search-forward)
1863 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1865 (t (error "Illegal match type: %s" type)))))
1867 ;; Fuzzy matches. We save these for later.
1869 (push (cons entries alist) fuzzies))
1870 ;; Word matches. Save these for even later.
1872 (push (cons entries alist) words))
1875 ;; Do exact matching.
1876 (goto-char (point-min))
1877 (while (and (not (eobp))
1878 (funcall search-func match nil t))
1879 ;; Is it really exact?
1881 (= (gnus-point-at-bol) (match-beginning 0))
1884 (setq found (setq arts (get-text-property
1885 (point) 'articles)))
1886 ;; Found a match, update scores.
1888 (while (setq art (pop arts))
1889 (setcdr art (+ score (cdr art)))
1892 (car-safe (rassq alist gnus-score-cache))
1895 (while (setq art (pop arts))
1896 (setcdr art (+ score (cdr art)))))))
1898 ;; Regexp and substring matching.
1900 (goto-char (point-min))
1901 (when (string= match "")
1903 (while (and (not (eobp))
1904 (funcall search-func match nil t))
1905 (goto-char (match-beginning 0))
1907 (setq found (setq arts (get-text-property (point) 'articles)))
1908 ;; Found a match, update scores.
1910 (while (setq art (pop arts))
1911 (setcdr art (+ score (cdr art)))
1912 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1914 (while (setq art (pop arts))
1915 (setcdr art (+ score (cdr art)))))
1917 ;; Update expiry date
1919 (setq entries (cdr entries))
1923 (setq entries (cdr entries)))
1924 ;; We have a match, so we update the date.
1925 ((and found gnus-update-score-entry-dates)
1926 (gnus-score-set 'touched '(t) alist)
1927 (setcar (nthcdr 2 kill) now)
1928 (setq entries (cdr entries)))
1929 ;; This entry has expired, so we remove it.
1930 ((and expire (< date expire))
1931 (gnus-score-set 'touched '(t) alist)
1932 (setcdr entries (cddr entries)))
1933 ;; No match; go to next entry.
1935 (setq entries (cdr entries))))))))
1937 ;; Find fuzzy matches.
1939 ;; Simplify the entire buffer for easy matching.
1940 (gnus-simplify-buffer-fuzzy)
1941 (while (setq kill (cadaar fuzzies))
1942 (let* ((match (nth 0 kill))
1944 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1946 (mt (aref (symbol-name type) 0))
1947 (case-fold-search (not (= mt ?F)))
1949 (goto-char (point-min))
1950 (while (and (not (eobp))
1951 (search-forward match nil t))
1952 (when (and (= (gnus-point-at-bol) (match-beginning 0))
1954 (setq found (setq arts (get-text-property (point) 'articles)))
1956 (while (setq art (pop arts))
1957 (setcdr art (+ score (cdr art)))
1959 (car-safe (rassq (cdar fuzzies) gnus-score-cache))
1962 ;; Found a match, update scores.
1963 (while (setq art (pop arts))
1964 (setcdr art (+ score (cdr art))))))
1966 ;; Update expiry date
1971 ;; Match, update date.
1972 ((and found gnus-update-score-entry-dates)
1973 (gnus-score-set 'touched '(t) (cdar fuzzies))
1974 (setcar (nthcdr 2 kill) now))
1975 ;; Old entry, remove.
1976 ((and expire (< date expire))
1977 (gnus-score-set 'touched '(t) (cdar fuzzies))
1978 (setcdr (caar fuzzies) (cddaar fuzzies))))
1979 (setq fuzzies (cdr fuzzies)))))
1982 ;; Enter all words into the hashtb.
1983 (let ((hashtb (gnus-make-hashtable
1984 (* 10 (count-lines (point-min) (point-max))))))
1985 (gnus-enter-score-words-into-hashtb hashtb)
1986 (while (setq kill (cadaar words))
1987 (let* ((score (or (nth 1 kill) gnus-score-interactive-default-score))
1990 (when (setq arts (intern-soft (nth 0 kill) hashtb))
1991 (setq arts (symbol-value arts))
1994 (while (setq art (pop arts))
1995 (setcdr art (+ score (cdr art)))
1997 (car-safe (rassq (cdar words) gnus-score-cache))
2000 ;; Found a match, update scores.
2001 (while (setq art (pop arts))
2002 (setcdr art (+ score (cdr art))))))
2003 ;; Update expiry date
2008 ;; Match, update date.
2009 ((and found gnus-update-score-entry-dates)
2010 (gnus-score-set 'touched '(t) (cdar words))
2011 (setcar (nthcdr 2 kill) now))
2012 ;; Old entry, remove.
2013 ((and expire (< date expire))
2014 (gnus-score-set 'touched '(t) (cdar words))
2015 (setcdr (caar words) (cddaar words))))
2016 (setq words (cdr words))))))
2019 (defun gnus-enter-score-words-into-hashtb (hashtb)
2020 ;; Find all the words in the buffer and enter them into
2022 (let ((syntab (syntax-table))
2024 (goto-char (point-min))
2027 (set-syntax-table gnus-adaptive-word-syntax-table)
2028 (while (re-search-forward "\\b\\w+\\b" nil t)
2031 (setq word (downcase (buffer-substring
2032 (match-beginning 0) (match-end 0))))
2036 (append (get-text-property (gnus-point-at-eol) 'articles) val)
2038 (set-syntax-table syntab))
2039 ;; Make all the ignorable words ignored.
2040 (let ((ignored (append gnus-ignored-adaptive-words
2041 gnus-default-ignored-adaptive-words)))
2043 (gnus-sethash (pop ignored) nil hashtb)))))
2045 (defun gnus-score-string< (a1 a2)
2046 ;; Compare headers in articles A2 and A2.
2047 ;; The header index used is the free variable `gnus-score-index'.
2048 (string-lessp (aref (car a1) gnus-score-index)
2049 (aref (car a2) gnus-score-index)))
2051 (defun gnus-current-score-file-nondirectory (&optional score-file)
2052 (let ((score-file (or score-file gnus-current-score-file)))
2054 (gnus-short-group-name (file-name-nondirectory score-file))
2057 (defun gnus-score-adaptive ()
2058 "Create adaptive score rules for this newsgroup."
2059 (when gnus-newsgroup-adaptive
2060 ;; We change the score file to the adaptive score file.
2062 (set-buffer gnus-summary-buffer)
2063 (gnus-score-load-file
2064 (or gnus-newsgroup-adaptive-score-file
2065 (gnus-score-file-name
2066 gnus-newsgroup-name gnus-adaptive-file-suffix))))
2067 ;; Perform ordinary line scoring.
2068 (when (or (not (listp gnus-newsgroup-adaptive))
2069 (memq 'line gnus-newsgroup-adaptive))
2071 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
2073 (date (current-time-string))
2074 (data gnus-newsgroup-data)
2076 ;; First we transform the adaptive rule alist into something
2077 ;; that's faster to process.
2079 (setq elem (car malist))
2080 (when (symbolp (car elem))
2081 (setcar elem (symbol-value (car elem))))
2082 (setq elem (cdr elem))
2085 (cons (if (eq (caar elem) 'followup)
2087 (symbol-name (caar elem)))
2092 (concat "mail-header-"
2093 (if (eq (caar elem) 'followup)
2095 (downcase (symbol-name (caar elem))))))
2097 (setq elem (cdr elem)))
2098 (setq malist (cdr malist)))
2099 ;; Then we score away.
2101 (setq elem (cdr (assq (gnus-data-mark (car data)) alist)))
2103 (gnus-data-pseudo-p (car data)))
2105 (when (setq headers (gnus-data-header (car data)))
2107 (setq match (funcall (caar elem) headers))
2108 (gnus-summary-score-entry
2109 (nth 1 (car elem)) match
2113 ((equal (nth 1 (car elem)) "date")
2116 ;; Whether we use substring or exact matches is
2118 (if (or (not gnus-score-exact-adapt-limit)
2119 (< (length match) gnus-score-exact-adapt-limit))
2121 (if (equal (nth 1 (car elem)) "subject")
2123 (nth 2 (car elem)) date nil t)
2124 (setq elem (cdr elem)))))
2125 (setq data (cdr data))))))
2127 ;; Perform adaptive word scoring.
2128 (when (and (listp gnus-newsgroup-adaptive)
2129 (memq 'word gnus-newsgroup-adaptive))
2130 (nnheader-temp-write nil
2131 (let* ((hashtb (gnus-make-hashtable 1000))
2132 (date (gnus-day-number (current-time-string)))
2133 (data gnus-newsgroup-data)
2134 (syntab (syntax-table))
2138 (set-syntax-table gnus-adaptive-word-syntax-table)
2139 ;; Go through all articles.
2140 (while (setq d (pop data))
2142 (not (gnus-data-pseudo-p d))
2146 gnus-adaptive-word-score-alist))))
2147 ;; This article has a mark that should lead to
2148 ;; adaptive word rules, so we insert the subject
2149 ;; and find all words in that string.
2150 (insert (mail-header-subject (gnus-data-header d)))
2151 (downcase-region (point-min) (point-max))
2152 (goto-char (point-min))
2153 (while (re-search-forward "\\b\\w+\\b" nil t)
2154 ;; Put the word and score into the hashtb.
2155 (setq val (gnus-gethash (setq word (match-string 0))
2157 (gnus-sethash word (+ (or val 0) score) hashtb))
2159 (set-syntax-table syntab))
2160 ;; Make all the ignorable words ignored.
2161 (let ((ignored (append gnus-ignored-adaptive-words
2162 gnus-default-ignored-adaptive-words)))
2164 (gnus-sethash (pop ignored) nil hashtb)))
2165 ;; Now we have all the words and scores, so we
2166 ;; add these rules to the ADAPT file.
2167 (set-buffer gnus-summary-buffer)
2170 (when (symbol-value word)
2171 (gnus-summary-score-entry
2172 "subject" (symbol-name word) 'w (symbol-value word)
2176 (defun gnus-score-edit-done ()
2177 (let ((bufnam (buffer-file-name (current-buffer)))
2178 (winconf gnus-prev-winconf))
2180 (set-window-configuration winconf))
2181 (gnus-score-remove-from-cache bufnam)
2182 (gnus-score-load-file bufnam)))
2184 (defun gnus-score-find-trace ()
2185 "Find all score rules that applies to the current article."
2187 (let ((old-scored gnus-newsgroup-scored))
2188 (let ((gnus-newsgroup-headers
2189 (list (gnus-summary-article-header)))
2190 (gnus-newsgroup-scored nil)
2193 (nnheader-set-temp-buffer "*Score Trace*"))
2194 (setq gnus-score-trace nil)
2195 (gnus-possibly-score-headers 'trace)
2196 (if (not (setq trace gnus-score-trace))
2198 1 "No score rules apply to the current article (default score %d)."
2199 gnus-summary-default-score)
2200 (set-buffer "*Score Trace*")
2201 (gnus-add-current-to-buffer-list)
2203 (insert (format "%S -> %s\n" (cdar trace)
2205 (file-name-nondirectory (caar trace))
2206 "(non-file rule)")))
2207 (setq trace (cdr trace)))
2208 (goto-char (point-min))
2209 (gnus-configure-windows 'score-trace)))
2210 (set-buffer gnus-summary-buffer)
2211 (setq gnus-newsgroup-scored old-scored)))
2213 (defun gnus-score-find-favourite-words ()
2214 "List words used in scoring."
2216 (let ((alists (gnus-score-load-files (gnus-all-score-files)))
2217 alist rule rules kill)
2218 ;; Go through all the score alists for this group
2219 ;; and find all `w' rules.
2220 (while (setq alist (pop alists))
2221 (while (setq rule (pop alist))
2222 (when (and (stringp (car rule))
2223 (equal "subject" (downcase (pop rule))))
2224 (while (setq kill (pop rule))
2225 (when (memq (nth 3 kill) '(w W word Word))
2226 (push (cons (or (nth 1 kill)
2227 gnus-score-interactive-default-score)
2230 (setq rules (sort rules (lambda (r1 r2)
2231 (string-lessp (cdr r1) (cdr r2)))))
2232 ;; Add up words that have appeared several times.
2235 (if (equal (cdar r) (cdadr r))
2237 (setcar (car r) (+ (caar r) (caadr r)))
2238 (setcdr r (cddr r)))
2240 ;; Insert the words.
2241 (nnheader-set-temp-buffer "*Score Words*")
2242 (if (not (setq rules (sort rules (lambda (r1 r2) (> (car r1) (car r2))))))
2243 (gnus-error 3 "No word score rules")
2245 (insert (format "%-5d: %s\n" (caar rules) (cdar rules)))
2247 (gnus-add-current-to-buffer-list)
2248 (goto-char (point-min))
2249 (gnus-configure-windows 'score-words))))
2251 (defun gnus-summary-rescore ()
2252 "Redo the entire scoring process in the current summary."
2255 (setq gnus-score-cache nil)
2256 (setq gnus-newsgroup-scored nil)
2257 (gnus-possibly-score-headers)
2258 (gnus-score-update-all-lines))
2260 (defun gnus-score-flush-cache ()
2261 "Flush the cache of score files."
2264 (setq gnus-score-cache nil
2265 gnus-score-alist nil
2266 gnus-short-name-score-file-cache nil)
2267 (gnus-message 6 "The score cache is now flushed"))
2269 (gnus-add-shutdown 'gnus-score-close 'gnus)
2271 (defvar gnus-score-file-alist-cache nil)
2273 (defun gnus-score-close ()
2274 "Clear all internal score variables."
2275 (setq gnus-score-cache nil
2276 gnus-internal-global-score-files nil
2277 gnus-score-file-list nil
2278 gnus-score-file-alist-cache nil))
2280 ;; Summary score marking commands.
2282 (defun gnus-summary-raise-same-subject-and-select (score)
2283 "Raise articles which has the same subject with SCORE and select the next."
2285 (let ((subject (gnus-summary-article-subject)))
2286 (gnus-summary-raise-score score)
2287 (while (gnus-summary-find-subject subject)
2288 (gnus-summary-raise-score score))
2289 (gnus-summary-next-article t)))
2291 (defun gnus-summary-raise-same-subject (score)
2292 "Raise articles which has the same subject with SCORE."
2294 (let ((subject (gnus-summary-article-subject)))
2295 (gnus-summary-raise-score score)
2296 (while (gnus-summary-find-subject subject)
2297 (gnus-summary-raise-score score))
2298 (gnus-summary-next-subject 1 t)))
2300 (defun gnus-score-default (level)
2301 (if level (prefix-numeric-value level)
2302 gnus-score-interactive-default-score))
2304 (defun gnus-summary-raise-thread (&optional score)
2305 "Raise the score of the articles in the current thread with SCORE."
2307 (setq score (gnus-score-default score))
2310 (let ((articles (gnus-summary-articles-in-thread)))
2312 (gnus-summary-goto-subject (car articles))
2313 (gnus-summary-raise-score score)
2314 (setq articles (cdr articles))))
2316 (let ((gnus-summary-check-current t))
2317 (unless (zerop (gnus-summary-next-subject 1 t))
2319 (gnus-summary-recenter)
2320 (gnus-summary-position-point)
2321 (gnus-set-mode-line 'summary))
2323 (defun gnus-summary-lower-same-subject-and-select (score)
2324 "Raise articles which has the same subject with SCORE and select the next."
2326 (gnus-summary-raise-same-subject-and-select (- score)))
2328 (defun gnus-summary-lower-same-subject (score)
2329 "Raise articles which has the same subject with SCORE."
2331 (gnus-summary-raise-same-subject (- score)))
2333 (defun gnus-summary-lower-thread (&optional score)
2334 "Lower score of articles in the current thread with SCORE."
2336 (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
2338 ;;; Finding score files.
2340 (defun gnus-score-score-files (group)
2341 "Return a list of all possible score files."
2342 ;; Search and set any global score files.
2343 (when gnus-global-score-files
2344 (unless gnus-internal-global-score-files
2345 (gnus-score-search-global-directories gnus-global-score-files)))
2346 ;; Fix the kill-file dir variable.
2347 (setq gnus-kill-files-directory
2348 (file-name-as-directory gnus-kill-files-directory))
2349 ;; If we can't read it, there are no score files.
2350 (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
2351 (setq gnus-score-file-list nil)
2352 (if (not (gnus-use-long-file-name 'not-score))
2353 ;; We do not use long file names, so we have to do some
2354 ;; directory traversing.
2355 (setq gnus-score-file-list
2357 (or gnus-short-name-score-file-cache
2359 (gnus-message 6 "Finding all score files...")
2360 (setq gnus-short-name-score-file-cache
2361 (gnus-score-score-files-1
2362 gnus-kill-files-directory))
2363 (gnus-message 6 "Finding all score files...done")))))
2364 ;; We want long file names.
2365 (when (or (not gnus-score-file-list)
2366 (not (car gnus-score-file-list))
2367 (gnus-file-newer-than gnus-kill-files-directory
2368 (car gnus-score-file-list)))
2369 (setq gnus-score-file-list
2370 (cons (nth 5 (file-attributes gnus-kill-files-directory))
2373 gnus-kill-files-directory t
2374 (gnus-score-file-regexp)))))))
2375 (cdr gnus-score-file-list)))
2377 (defun gnus-score-score-files-1 (dir)
2378 "Return all possible score files under DIR."
2379 (let ((files (list (expand-file-name dir)))
2380 (regexp (gnus-score-file-regexp))
2381 (case-fold-search nil)
2383 (while (setq file (pop files))
2385 ;; Ignore "." and "..".
2386 ((member (file-name-nondirectory file) '("." ".."))
2388 ;; Add subtrees of directory to also be searched.
2389 ((and (file-directory-p file)
2390 (not (member (file-truename file) seen)))
2391 (push (file-truename file) seen)
2392 (setq files (nconc (directory-files file t nil t) files)))
2393 ;; Add files to the list of score files.
2394 ((string-match regexp file)
2397 ;; Return a dummy value.
2398 (list "~/News/this.file.does.not.exist.SCORE"))))
2400 (defun gnus-score-file-regexp ()
2401 "Return a regexp that match all score files."
2402 (concat "\\(" (regexp-quote gnus-score-file-suffix )
2403 "\\|" (regexp-quote gnus-adaptive-file-suffix) "\\)\\'"))
2405 (defun gnus-score-find-bnews (group)
2406 "Return a list of score files for GROUP.
2407 The score files are those files in the ~/News/ directory which matches
2408 GROUP using BNews sys file syntax."
2409 (let* ((sfiles (append (gnus-score-score-files group)
2410 gnus-internal-global-score-files))
2411 (kill-dir (file-name-as-directory
2412 (expand-file-name gnus-kill-files-directory)))
2413 (klen (length kill-dir))
2414 (score-regexp (gnus-score-file-regexp))
2415 (trans (cdr (assq ?: nnheader-file-name-translation-alist)))
2416 ofiles not-match regexp)
2418 (set-buffer (get-buffer-create "*gnus score files*"))
2419 (buffer-disable-undo (current-buffer))
2420 ;; Go through all score file names and create regexp with them
2424 (insert (car sfiles))
2425 (goto-char (point-min))
2426 ;; First remove the suffix itself.
2427 (when (re-search-forward (concat "." score-regexp) nil t)
2428 (replace-match "" t t)
2429 (goto-char (point-min))
2430 (if (looking-at (regexp-quote kill-dir))
2431 ;; If the file name was just "SCORE", `klen' is one character
2433 (delete-char (min (1- (point-max)) klen))
2434 (goto-char (point-max))
2435 (search-backward "/")
2436 (delete-region (1+ (point)) (point-min)))
2437 ;; If short file names were used, we have to translate slashes.
2438 (goto-char (point-min))
2439 (let ((regexp (concat
2440 "[/:" (if trans (char-to-string trans) "") "]")))
2441 (while (re-search-forward regexp nil t)
2442 (replace-match "." t t)))
2443 ;; Kludge to get rid of "nntp+" problems.
2444 (goto-char (point-min))
2445 (when (looking-at "nn[a-z]+\\+")
2446 (search-forward "+")
2450 ;; Kludge to deal with "++".
2451 (while (search-forward "+" nil t)
2452 (replace-match "\\+" t t))
2453 ;; Translate "all" to ".*".
2454 (goto-char (point-min))
2455 (while (search-forward "all" nil t)
2456 (replace-match ".*" t t))
2457 (goto-char (point-min))
2458 ;; Deal with "not."s.
2459 (if (looking-at "not.")
2462 (setq regexp (concat "^" (buffer-substring 5 (point-max)) "$")))
2463 (setq regexp (concat "^" (buffer-substring 1 (point-max)) "$"))
2464 (setq not-match nil))
2465 ;; Finally - if this resulting regexp matches the group name,
2466 ;; we add this score file to the list of score files
2467 ;; applicable to this group.
2468 (when (or (and not-match
2469 (not (string-match regexp group)))
2470 (and (not not-match)
2471 (string-match regexp group)))
2472 (push (car sfiles) ofiles)))
2473 (setq sfiles (cdr sfiles)))
2474 (kill-buffer (current-buffer))
2475 ;; Slight kludge here - the last score file returned should be
2476 ;; the local score file, whether it exists or not. This is so
2477 ;; that any score commands the user enters will go to the right
2478 ;; file, and not end up in some global score file.
2479 (let ((localscore (gnus-score-file-name group)))
2480 (setq ofiles (cons localscore (delete localscore ofiles))))
2481 (gnus-sort-score-files (nreverse ofiles)))))
2483 (defun gnus-score-find-single (group)
2484 "Return list containing the score file for GROUP."
2485 (list (or gnus-newsgroup-adaptive-score-file
2486 (gnus-score-file-name group gnus-adaptive-file-suffix))
2487 (gnus-score-file-name group)))
2489 (defun gnus-score-find-hierarchical (group)
2490 "Return list of score files for GROUP.
2491 This includes the score file for the group and all its parents."
2492 (let* ((prefix (gnus-group-real-prefix group))
2494 (group (gnus-group-real-name group))
2496 (while (string-match "\\." group (1+ start))
2497 (setq start (match-beginning 0))
2498 (push (substring group 0 start) all))
2502 (mapcar (lambda (group)
2503 (gnus-score-file-name group gnus-adaptive-file-suffix))
2504 (setq all (nreverse all)))
2505 (mapcar 'gnus-score-file-name all)))
2506 (if (equal prefix "")
2510 (nnheader-translate-file-chars
2511 (concat (file-name-directory file) prefix
2512 (file-name-nondirectory file))))
2515 (defun gnus-score-file-rank (file)
2516 "Return a number that says how specific score FILE is.
2517 Destroys the current buffer."
2518 (if (member file gnus-internal-global-score-files)
2521 (concat "^" (regexp-quote
2523 (file-name-as-directory gnus-kill-files-directory))))
2525 (setq file (substring file (match-end 0))))
2527 (goto-char (point-min))
2530 (while (re-search-forward "[./]" nil t)
2531 (push (buffer-substring beg (1- (point)))
2534 (setq elems (delete "all" elems))
2537 (defun gnus-sort-score-files (files)
2538 "Sort FILES so that the most general files come first."
2539 (nnheader-temp-write nil
2543 (cons (inline (gnus-score-file-rank file)) file))
2546 (lambda (f) (cdr f))
2547 (sort alist (lambda (f1 f2) (< (car f1) (car f2))))))))
2549 (defun gnus-score-find-alist (group)
2550 "Return list of score files for GROUP.
2551 The list is determined from the variable gnus-score-file-alist."
2552 (let ((alist gnus-score-file-multiple-match-alist)
2554 ;; if this group has been seen before, return the cached entry
2555 (if (setq score-files (assoc group gnus-score-file-alist-cache))
2556 (cdr score-files) ;ensures caching groups with no matches
2557 ;; handle the multiple match alist
2559 (when (string-match (caar alist) group)
2561 (nconc score-files (copy-sequence (cdar alist)))))
2562 (setq alist (cdr alist)))
2563 (setq alist gnus-score-file-single-match-alist)
2564 ;; handle the single match alist
2566 (when (string-match (caar alist) group)
2567 ;; progn used just in case ("regexp") has no files
2568 ;; and score-files is still nil. -sj
2569 ;; this can be construed as a "stop searching here" feature :>
2570 ;; and used to simplify regexps in the single-alist
2572 (nconc score-files (copy-sequence (cdar alist))))
2574 (setq alist (cdr alist)))
2575 ;; cache the score files
2576 (push (cons group score-files) gnus-score-file-alist-cache)
2579 (defun gnus-all-score-files (&optional group)
2580 "Return a list of all score files for the current group."
2581 (let ((funcs gnus-score-find-score-files-function)
2582 (group (or group gnus-newsgroup-name))
2584 ;; Make sure funcs is a list.
2587 (setq funcs (list funcs)))
2588 ;; Get the initial score files for this group.
2590 (setq score-files (nreverse (gnus-score-find-alist group))))
2591 ;; Add any home adapt files.
2592 (let ((home (gnus-home-score-file group t)))
2594 (push home score-files)
2595 (setq gnus-newsgroup-adaptive-score-file home)))
2596 ;; Check whether there is a `adapt-file' group parameter.
2597 (let ((param-file (gnus-group-find-parameter group 'adapt-file)))
2599 (push param-file score-files)
2600 (setq gnus-newsgroup-adaptive-score-file param-file)))
2601 ;; Go through all the functions for finding score files (or actual
2602 ;; scores) and add them to a list.
2604 (when (gnus-functionp (car funcs))
2606 (nconc score-files (nreverse (funcall (car funcs) group)))))
2607 (setq funcs (cdr funcs)))
2608 ;; Add any home score files.
2609 (let ((home (gnus-home-score-file group)))
2611 (push home score-files)))
2612 ;; Check whether there is a `score-file' group parameter.
2613 (let ((param-file (gnus-group-find-parameter group 'score-file)))
2615 (push param-file score-files)))
2616 ;; Expand all files names.
2617 (let ((files score-files))
2619 (when (stringp (car files))
2620 (setcar files (expand-file-name
2621 (car files) gnus-kill-files-directory)))
2623 (setq score-files (nreverse score-files))
2624 ;; Remove any duplicate score files.
2625 (while (and score-files
2626 (member (car score-files) (cdr score-files)))
2628 (let ((files score-files))
2630 (if (member (cadr files) (cddr files))
2631 (setcdr files (cddr files))
2633 ;; Do the scoring if there are any score files for this group.
2636 (defun gnus-possibly-score-headers (&optional trace)
2637 "Do scoring if scoring is required."
2638 (let ((score-files (gnus-all-score-files)))
2640 (gnus-score-headers score-files trace))))
2642 (defun gnus-score-file-name (newsgroup &optional suffix)
2643 "Return the name of a score file for NEWSGROUP."
2644 (let ((suffix (or suffix gnus-score-file-suffix)))
2645 (nnheader-translate-file-chars
2647 ((or (null newsgroup)
2648 (string-equal newsgroup ""))
2649 ;; The global score file is placed at top of the directory.
2651 suffix gnus-kill-files-directory))
2652 ((gnus-use-long-file-name 'not-score)
2653 ;; Append ".SCORE" to newsgroup name.
2654 (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
2656 gnus-kill-files-directory))
2658 ;; Place "SCORE" under the hierarchical directory.
2659 (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
2661 gnus-kill-files-directory))))))
2663 (defun gnus-score-search-global-directories (files)
2664 "Scan all global score directories for score files."
2665 ;; Set the variable `gnus-internal-global-score-files' to all
2666 ;; available global score files.
2667 (interactive (list gnus-global-score-files))
2670 (if (string-match "/$" (car files))
2671 (setq out (nconc (directory-files
2673 (concat (gnus-score-file-regexp) "$"))))
2674 (push (car files) out))
2675 (setq files (cdr files)))
2676 (setq gnus-internal-global-score-files out)))
2678 (defun gnus-score-default-fold-toggle ()
2679 "Toggle folding for new score file entries."
2681 (setq gnus-score-default-fold (not gnus-score-default-fold))
2682 (if gnus-score-default-fold
2683 (gnus-message 1 "New score file entries will be case insensitive.")
2684 (gnus-message 1 "New score file entries will be case sensitive.")))
2686 ;;; Home score file.
2688 (defun gnus-home-score-file (group &optional adapt)
2689 "Return the home score file for GROUP.
2690 If ADAPT, return the home adaptive file instead."
2691 (let ((list (if adapt gnus-home-adapt-file gnus-home-score-file))
2693 ;; Make sure we have a list.
2694 (unless (listp list)
2695 (setq list (list list)))
2696 ;; Go through the list and look for matches.
2697 (while (and (not found)
2698 (setq elem (pop list)))
2705 ((gnus-functionp elem)
2706 (funcall elem group))
2709 (when (string-match (car elem) group)
2712 (nnheader-concat gnus-kill-files-directory found))))
2714 (defun gnus-hierarchial-home-score-file (group)
2715 "Return the score file of the top-level hierarchy of GROUP."
2716 (if (string-match "^[^.]+\\." group)
2717 (concat (match-string 0 group) gnus-score-file-suffix)
2718 ;; Group name without any dots.
2719 (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
2720 gnus-score-file-suffix)))
2722 (defun gnus-hierarchial-home-adapt-file (group)
2723 "Return the adapt file of the top-level hierarchy of GROUP."
2724 (if (string-match "^[^.]+\\." group)
2725 (concat (match-string 0 group) gnus-adaptive-file-suffix)
2726 ;; Group name without any dots.
2727 (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
2728 gnus-adaptive-file-suffix)))
2734 (defun gnus-decay-score (score)
2735 "Decay SCORE according to `gnus-score-decay-constant' and `gnus-score-decay-scale'."
2738 (* (if (< score 0) -1 1)
2740 (max gnus-score-decay-constant
2742 gnus-score-decay-scale)))))))
2744 (defun gnus-decay-scores (alist day)
2745 "Decay non-permanent scores in ALIST."
2746 (let ((times (- (gnus-time-to-day (current-time)) day))
2747 kill entry updated score n)
2748 (unless (zerop times) ;Done decays today already?
2749 (while (setq entry (pop alist))
2750 (when (stringp (car entry))
2751 (setq entry (cdr entry))
2752 (while (setq kill (pop entry))
2755 (setq score (or (nth 1 kill)
2756 gnus-score-interactive-default-score)
2758 (while (natnump (decf n))
2759 (setq score (funcall gnus-decay-score-function score)))
2760 (setcdr kill (cons score
2761 (cdr (cdr kill)))))))))
2762 ;; Return whether this score file needs to be saved. By Je-haysuss!
2765 (provide 'gnus-score)
2767 ;;; gnus-score.el ends here