1 ;;; gnus-score.el --- scoring code for Gnus
2 ;; Copyright (C) 1995,96 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.
33 (defvar gnus-global-score-files nil
34 "*List of global score files and directories.
35 Set this variable if you want to use people's score files. One entry
36 for each score file or each score file directory. Gnus will decide
37 by itself what score files are applicable to which group.
39 Say you want to use the single score file
40 \"/ftp.ifi.uio.no@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
41 score files in the \"/ftp.some-where:/pub/score\" directory.
43 (setq gnus-global-score-files
44 '(\"/ftp.ifi.uio.no:/pub/larsi/ding/score/soc.motss.SCORE\"
45 \"/ftp.some-where:/pub/score\"))")
47 (defvar gnus-score-file-single-match-alist nil
48 "*Alist mapping regexps to lists of score files.
49 Each element of this alist should be of the form
50 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
52 If the name of a group is matched by REGEXP, the corresponding scorefiles
53 will be used for that group.
54 The first match found is used, subsequent matching entries are ignored (to
55 use multiple matches, see gnus-score-file-multiple-match-alist).
57 These score files are loaded in addition to any files returned by
58 gnus-score-find-score-files-function (which see).")
60 (defvar gnus-score-file-multiple-match-alist nil
61 "*Alist mapping regexps to lists of score files.
62 Each element of this alist should be of the form
63 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
65 If the name of a group is matched by REGEXP, the corresponding scorefiles
66 will be used for that group.
67 If multiple REGEXPs match a group, the score files corresponding to each
68 match will be used (for only one match to be used, see
69 gnus-score-file-single-match-alist).
71 These score files are loaded in addition to any files returned by
72 gnus-score-find-score-files-function (which see).")
74 (defvar gnus-score-file-suffix "SCORE"
75 "*Suffix of the score files.")
77 (defvar gnus-adaptive-file-suffix "ADAPT"
78 "*Suffix of the adaptive score files.")
80 (defvar gnus-score-find-score-files-function 'gnus-score-find-bnews
81 "*Function used to find score files.
82 The function will be called with the group name as the argument, and
83 should return a list of score files to apply to that group. The score
84 files do not actually have to exist.
86 Predefined values are:
88 gnus-score-find-single: Only apply the group's own score file.
89 gnus-score-find-hierarchical: Also apply score files from parent groups.
90 gnus-score-find-bnews: Apply score files whose names matches.
92 See the documentation to these functions for more information.
94 This variable can also be a list of functions to be called. Each
95 function should either return a list of score files, or a list of
98 (defvar gnus-score-interactive-default-score 1000
99 "*Scoring commands will raise/lower the score with this number as the default.")
101 (defvar gnus-score-expiry-days 7
102 "*Number of days before unused score file entries are expired.
103 If this variable is nil, no score file entries will be expired.")
105 (defvar gnus-update-score-entry-dates t
106 "*In non-nil, update matching score entry dates.
107 If this variable is nil, then score entries that provide matches
108 will be expired along with non-matching score entries.")
110 (defvar gnus-orphan-score nil
111 "*All orphans get this score added. Set in the score file.")
113 (defvar gnus-home-score-file nil
114 "Variable to control where interative score entries are to go.
118 This file file will be used as the home score file.
121 The result of this function will be used as the home score file.
124 The elements in this list can be:
126 * `(regexp . file-name)'
127 If the `regexp' matches the group name, the `file-name' will
128 will be used as the home score file.
131 If the function returns non-nil, the result will be used
132 as the home score file.
135 Use the string as the home score file.
137 The list will be traversed from the beginning towards the end looking
140 (defvar gnus-home-adapt-file nil
141 "Variable to control where new adaptive score entries are to go.
142 This variable allows the same syntax as `gnus-home-score-file'.")
144 (defvar gnus-default-adaptive-score-alist
145 '((gnus-kill-file-mark)
147 (gnus-read-mark (from 3) (subject 30))
148 (gnus-catchup-mark (subject -10))
149 (gnus-killed-mark (from -1) (subject -20))
150 (gnus-del-mark (from -2) (subject -15)))
151 "*Alist of marks and scores.")
153 (defvar gnus-ignored-adaptive-words
154 '("a" "i" "the" "to" "of" "and" "in" "is" "it" "for" "that" "if" "you"
155 "this" "be" "on" "with" "not" "have" "are" "or" "as" "from" "can"
156 "but" "by" "at" "an" "will" "no" "all" "was" "do" "there" "my" "one"
157 "so" "we" "they" "what" "would" "any" "which" "about" "get" "your"
158 "use" "some" "me" "then" "name" "like" "out" "when" "up" "time"
159 "other" "more" "only" "just" "end" "also" "know" "how" "new" "should"
160 "been" "than" "them" "he" "who" "make" "may" "people" "these" "now"
161 "their" "here" "into" "first" "could" "way" "had" "see" "work" "well"
162 "were" "two" "very" "where" "while" "us" "because" "good" "same"
163 "even" "much" "most" "many" "such" "long" "his" "over" "last" "since"
164 "right" "before" "our" "without" "too" "those" "why" "must" "part"
165 "being" "current" "back" "still" "go" "point" "value" "each" "did"
166 "both" "true" "off" "say" "another" "state" "might" "under" "start"
168 "List of words to be ignored when doing adaptive word scoring.")
170 (defvar gnus-default-adaptive-word-score-alist
171 `((,gnus-read-mark . 30)
172 (,gnus-catchup-mark . -10)
173 (,gnus-killed-mark . -20)
174 (,gnus-del-mark . -15))
175 "*Alist of marks and scores.")
177 (defvar gnus-score-mimic-keymap nil
178 "*Have the score entry functions pretend that they are a keymap.")
180 (defvar gnus-score-exact-adapt-limit 10
181 "*Number that says how long a match has to be before using substring matching.
182 When doing adaptive scoring, one normally uses fuzzy or substring
183 matching. However, if the header one matches is short, the possibility
184 for false positives is great, so if the length of the match is less
185 than this variable, exact matching will be used.
187 If this variable is nil, exact matching will always be used.")
189 (defvar gnus-score-uncacheable-files "ADAPT$"
190 "*All score files that match this regexp will not be cached.")
192 (defvar gnus-score-default-header nil
193 "Default header when entering new scores.
195 Should be one of the following symbols.
208 If nil, the user will be asked for a header.")
210 (defvar gnus-score-default-type nil
211 "Default match type when entering new scores.
213 Should be one of the following symbols.
223 >: greater than number
226 If nil, the user will be asked for a match type.")
228 (defvar gnus-score-default-fold nil
229 "Use case folding for new score file entries iff not nil.")
231 (defvar gnus-score-default-duration nil
232 "Default duration of effect when entering new scores.
234 Should be one of the following symbols.
240 If nil, the user will be asked for a duration.")
242 (defvar gnus-score-after-write-file-function nil
243 "*Function called with the name of the score file just written to disk.")
247 ;; Internal variables.
249 (defvar gnus-scores-exclude-files nil)
250 (defvar gnus-internal-global-score-files nil)
251 (defvar gnus-score-file-list nil)
253 (defvar gnus-short-name-score-file-cache nil)
255 (defvar gnus-score-help-winconf nil)
256 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
257 (defvar gnus-score-trace nil)
258 (defvar gnus-score-edit-buffer nil)
260 (defvar gnus-score-alist nil
261 "Alist containing score information.
262 The keys can be symbols or strings. The following symbols are defined.
264 touched: If this alist has been modified.
265 mark: Automatically mark articles below this.
266 expunge: Automatically expunge articles below this.
267 files: List of other score files to load when loading this one.
268 eval: Sexp to be evaluated when the score file is loaded.
270 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
271 where HEADER is the header being scored, MATCH is the string we are
272 looking for, TYPE is a flag indicating whether it should use regexp or
273 substring matching, SCORE is the score to add and DATE is the date
274 of the last successful match.")
276 (defvar gnus-score-cache nil)
277 (defvar gnus-scores-articles nil)
278 (defvar gnus-score-index nil)
281 (defconst gnus-header-index
282 ;; Name to index alist.
283 '(("number" 0 gnus-score-integer)
284 ("subject" 1 gnus-score-string)
285 ("from" 2 gnus-score-string)
286 ("date" 3 gnus-score-date)
287 ("message-id" 4 gnus-score-string)
288 ("references" 5 gnus-score-string)
289 ("chars" 6 gnus-score-integer)
290 ("lines" 7 gnus-score-integer)
291 ("xref" 8 gnus-score-string)
292 ("head" -1 gnus-score-body)
293 ("body" -1 gnus-score-body)
294 ("all" -1 gnus-score-body)
295 ("followup" 2 gnus-score-followup)
296 ("thread" 5 gnus-score-thread)))
299 (autoload 'gnus-uu-ctl-map "gnus-uu" nil nil 'keymap))
301 ;;; Summary mode score maps.
303 (gnus-define-keys (gnus-summary-score-map "V" gnus-summary-mode-map)
304 "s" gnus-summary-set-score
305 "a" gnus-summary-score-entry
306 "S" gnus-summary-current-score
307 "c" gnus-score-change-score-file
308 "m" gnus-score-set-mark-below
309 "x" gnus-score-set-expunge-below
310 "R" gnus-summary-rescore
311 "e" gnus-score-edit-current-scores
312 "f" gnus-score-edit-file
313 "F" gnus-score-flush-cache
314 "t" gnus-score-find-trace
315 "w" gnus-score-find-favourite-words
316 "C" gnus-score-customize)
318 ;; Summary score file commands
320 ;; Much modification of the kill (ahem, score) code and lots of the
321 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
323 (defun gnus-summary-lower-score (&optional score)
324 "Make a score entry based on the current article.
325 The user will be prompted for header to score on, match type,
326 permanence, and the string to be used. The numerical prefix will be
329 (gnus-summary-increase-score (- (gnus-score-default score))))
331 (defvar gnus-score-default-header nil
332 "*The default header to score on when entering a score rule interactively.")
334 (defvar gnus-score-default-type nil
335 "*The default score type to use when entering a score rule interactively.")
337 (defvar gnus-score-default-duration nil
338 "*The default score duration to use on when entering a score rule interactively.")
340 (defun gnus-score-kill-help-buffer ()
341 (when (get-buffer "*Score Help*")
342 (kill-buffer "*Score Help*")
343 (and gnus-score-help-winconf
344 (set-window-configuration gnus-score-help-winconf))))
346 (defun gnus-summary-increase-score (&optional score)
347 "Make a score entry based on the current article.
348 The user will be prompted for header to score on, match type,
349 permanence, and the string to be used. The numerical prefix will be
352 (gnus-set-global-variables)
353 (let* ((nscore (gnus-score-default score))
354 (prefix (if (< nscore 0) ?L ?I))
355 (increase (> nscore 0))
357 '((?a "from" nil nil string)
358 (?s "subject" nil nil string)
359 (?b "body" "" nil body-string)
360 (?h "head" "" nil body-string)
361 (?i "message-id" nil t string)
362 (?t "references" "message-id" nil string)
363 (?x "xref" nil nil string)
364 (?l "lines" nil nil number)
365 (?d "date" nil nil date)
366 (?f "followup" nil nil string)
367 (?T "thread" nil nil string)))
369 '((?s s "substring" string)
370 (?e e "exact string" string)
371 (?f f "fuzzy string" string)
372 (?r r "regexp string" string)
373 (?z s "substring" body-string)
374 (?p s "regexp string" body-string)
375 (?b before "before date" date)
376 (?a at "at date" date)
377 (?n now "this date" date)
378 (?< < "less than number" number)
379 (?> > "greater than number" number)
380 (?= = "equal to number" number)))
382 (list (list ?t (current-time-string) "temporary")
383 '(?p perm "permanent") '(?i now "immediate")))
384 (mimic gnus-score-mimic-keymap)
385 (hchar (and gnus-score-default-header
386 (aref (symbol-name gnus-score-default-header) 0)))
387 (tchar (and gnus-score-default-type
388 (aref (symbol-name gnus-score-default-type) 0)))
389 (pchar (and gnus-score-default-duration
390 (aref (symbol-name gnus-score-default-duration) 0)))
391 entry temporary type match)
396 ;; First we read the header to score.
401 (message "%c-" prefix))
402 (message "%s header (%s?): " (if increase "Increase" "Lower")
403 (mapconcat (lambda (s) (char-to-string (car s)))
405 (setq hchar (read-char))
406 (when (or (= hchar ??) (= hchar ?\C-h))
408 (gnus-score-insert-help "Match on header" char-to-header 1)))
410 (gnus-score-kill-help-buffer)
411 (unless (setq entry (assq (downcase hchar) char-to-header))
412 (if mimic (error "%c %c" prefix hchar) (error "")))
414 (when (/= (downcase hchar) hchar)
415 ;; This was a majuscle, so we end reading and set the defaults.
416 (if mimic (message "%c %c" prefix hchar) (message ""))
417 (setq tchar (or tchar ?s)
418 pchar (or pchar ?t)))
420 ;; We continue reading - the type.
424 (sit-for 1) (message "%c %c-" prefix hchar))
425 (message "%s header '%s' with match type (%s?): "
426 (if increase "Increase" "Lower")
428 (mapconcat (lambda (s)
429 (if (eq (nth 4 entry)
431 (char-to-string (car s))
434 (setq tchar (read-char))
435 (when (or (= tchar ??) (= tchar ?\C-h))
437 (gnus-score-insert-help
441 (if (eq (nth 4 entry)
447 (gnus-score-kill-help-buffer)
448 (unless (setq type (nth 1 (assq (downcase tchar) char-to-type)))
449 (if mimic (error "%c %c" prefix hchar) (error "")))
451 (when (/= (downcase tchar) tchar)
452 ;; It was a majuscle, so we end reading and use the default.
453 (if mimic (message "%c %c %c" prefix hchar tchar)
455 (setq pchar (or pchar ?p)))
457 ;; We continue reading.
461 (sit-for 1) (message "%c %c %c-" prefix hchar tchar))
462 (message "%s permanence (%s?): " (if increase "Increase" "Lower")
463 (mapconcat (lambda (s) (char-to-string (car s)))
465 (setq pchar (read-char))
466 (when (or (= pchar ??) (= pchar ?\C-h))
468 (gnus-score-insert-help "Match permanence" char-to-perm 2)))
470 (gnus-score-kill-help-buffer)
471 (if mimic (message "%c %c %c" prefix hchar tchar pchar)
473 (unless (setq temporary (cadr (assq pchar char-to-perm)))
475 (error "%c %c %c %c" prefix hchar tchar pchar)
477 ;; Always kill the score help buffer.
478 (gnus-score-kill-help-buffer))
480 ;; We have all the data, so we enter this score.
481 (setq match (if (string= (nth 2 entry) "") ""
482 (gnus-summary-header (or (nth 2 entry) (nth 1 entry)))))
484 ;; Modify the match, perhaps.
486 ((equal (nth 1 entry) "xref")
487 (when (string-match "^Xref: *" match)
488 (setq match (substring match (match-end 0))))
489 (when (string-match "^[^:]* +" match)
490 (setq match (substring match (match-end 0))))))
492 (when (memq type '(r R regexp Regexp))
493 (setq match (regexp-quote match)))
495 (gnus-summary-score-entry
496 (nth 1 entry) ; Header
499 (if (eq 's score) nil score) ; Score
500 (if (eq 'perm temporary) ; Temp
503 (not (nth 3 entry))) ; Prompt
506 (defun gnus-score-insert-help (string alist idx)
507 (setq gnus-score-help-winconf (current-window-configuration))
509 (set-buffer (get-buffer-create "*Score Help*"))
510 (buffer-disable-undo (current-buffer))
511 (delete-windows-on (current-buffer))
513 (insert string ":\n\n")
518 ;; find the longest string to display
520 (setq n (length (nth idx (car list))))
523 (setq list (cdr list)))
524 (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
525 (setq n (/ (1- (window-width)) max)) ; items per line
526 (setq width (/ (1- (window-width)) n)) ; width of each item
527 ;; insert `n' items, each in a field of width `width'
532 (delete-char -1) ; the `\n' takes a char
534 (setq pad (- width 3))
535 (setq format (concat "%c: %-" (int-to-string pad) "s"))
536 (insert (format format (caar alist) (nth idx (car alist))))
537 (setq alist (cdr alist))
539 ;; display ourselves in a small window at the bottom
540 (gnus-appt-select-lowest-window)
542 (pop-to-buffer "*Score Help*")
543 (let ((window-min-height 1))
544 (shrink-window-if-larger-than-buffer))
545 (select-window (get-buffer-window gnus-summary-buffer))))
547 (defun gnus-summary-header (header &optional no-err)
548 ;; Return HEADER for current articles, or error.
549 (let ((article (gnus-summary-article-number))
552 (if (and (setq headers (gnus-summary-article-header article))
554 (aref headers (nth 1 (assoc header gnus-header-index)))
557 (error "Pseudo-articles can't be scored")))
559 (error "No article on current line")
562 (defun gnus-newsgroup-score-alist ()
564 (let ((param-file (gnus-group-get-parameter
565 gnus-newsgroup-name 'score-file)))
567 (gnus-score-load param-file)))
569 (gnus-score-file-name gnus-newsgroup-name)))
572 (defsubst gnus-score-get (symbol &optional alist)
573 ;; Get SYMBOL's definition in ALIST.
577 (gnus-newsgroup-score-alist)))))
579 (defun gnus-summary-score-entry
580 (header match type score date &optional prompt silent)
581 "Enter score file entry.
582 HEADER is the header being scored.
583 MATCH is the string we are looking for.
584 TYPE is the match type: substring, regexp, exact, fuzzy.
585 SCORE is the score to add.
586 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
587 If optional argument `PROMPT' is non-nil, allow user to edit match.
588 If optional argument `SILENT' is nil, show effect of score entry."
590 (list (completing-read "Header: "
592 (lambda (x) (fboundp (nth 2 x)))
594 (read-string "Match: ")
595 (if (y-or-n-p "Use regexp match? ") 'r 's)
596 (and current-prefix-arg
597 (prefix-numeric-value current-prefix-arg))
598 (cond ((not (y-or-n-p "Add to score file? "))
600 ((y-or-n-p "Expire kill? ")
601 (current-time-string))
603 ;; Regexp is the default type.
604 (if (eq type t) (setq type 'r))
605 ;; Simplify matches...
606 (cond ((or (eq type 'r) (eq type 's) (eq type nil))
607 (setq match (if match (gnus-simplify-subject-re match) "")))
609 (setq match (gnus-simplify-subject-fuzzy match))))
610 (let ((score (gnus-score-default score))
611 (header (format "%s" (downcase header)))
614 (setq match (read-string
615 (format "Match %s on %s, %s: "
616 (cond ((eq date 'now)
622 (if (< score 0) "lower" "raise"))
624 (int-to-string match)
627 ;; Get rid of string props.
628 (setq match (format "%s" match))
630 ;; If this is an integer comparison, we transform from string to int.
631 (and (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
632 (setq match (string-to-int match)))
634 (unless (eq date 'now)
635 ;; Add the score entry to the score file.
636 (when (= score gnus-score-interactive-default-score)
638 (let ((old (gnus-score-get header))
642 (type (list match score (and date (gnus-day-number date)) type))
643 (date (list match score (gnus-day-number date)))
644 (score (list match score))
646 ;; We see whether we can collapse some score entries.
647 ;; This isn't quite correct, because there may be more elements
648 ;; later on with the same key that have matching elems... Hm.
650 (setq elem (assoc match old))
651 (eq (nth 3 elem) (nth 3 new))
652 (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
653 (and (not (nth 2 elem)) (not (nth 2 new)))))
654 ;; Yup, we just add this new score to the old elem.
655 (setcar (cdr elem) (+ (or (nth 1 elem)
656 gnus-score-interactive-default-score)
658 gnus-score-interactive-default-score)))
659 ;; Nope, we have to add a new elem.
660 (gnus-score-set header (if old (cons new old) (list new))))
661 (gnus-score-set 'touched '(t))))
663 ;; Score the current buffer.
665 (if (and (>= (nth 1 (assoc header gnus-header-index)) 0)
666 (eq (nth 2 (assoc header gnus-header-index))
668 (gnus-summary-score-effect header match type score)
669 (gnus-summary-rescore)))
671 ;; Return the new scoring rule.
674 (defun gnus-summary-score-effect (header match type score)
675 "Simulate the effect of a score file entry.
676 HEADER is the header being scored.
677 MATCH is the string we are looking for.
678 TYPE is a flag indicating if it is a regexp or substring.
679 SCORE is the score to add."
680 (interactive (list (completing-read "Header: "
682 (lambda (x) (fboundp (nth 2 x)))
684 (read-string "Match: ")
685 (y-or-n-p "Use regexp match? ")
686 (prefix-numeric-value current-prefix-arg)))
688 (or (and (stringp match) (> (length match) 0))
690 (goto-char (point-min))
691 (let ((regexp (cond ((eq type 'f)
692 (gnus-simplify-subject-fuzzy match))
696 (concat "\\`" (regexp-quote match) "\\'"))
698 (regexp-quote match)))))
700 (let ((content (gnus-summary-header header 'noerr))
701 (case-fold-search t))
704 (string-equal (gnus-simplify-subject-fuzzy content)
706 (string-match regexp content))
707 (gnus-summary-raise-score score))))
708 (beginning-of-line 2)))))
710 (defun gnus-summary-score-crossposting (score date)
711 ;; Enter score file entry for current crossposting.
712 ;; SCORE is the score to add.
713 ;; DATE is the expire date.
714 (let ((xref (gnus-summary-header "xref"))
717 (or xref (error "This article is not crossposted"))
718 (while (string-match " \\([^ \t]+\\):" xref start)
719 (setq start (match-end 0))
722 (substring xref (match-beginning 1) (match-end 1)))
723 gnus-newsgroup-name))
724 (gnus-summary-score-entry
725 "xref" (concat " " group ":") nil score date t)))))
732 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
734 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
735 (defun gnus-score-set-mark-below (score)
736 "Automatically mark articles with score below SCORE as read."
738 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
739 (string-to-int (read-string "Mark below: ")))))
740 (setq score (or score gnus-summary-default-score 0))
741 (gnus-score-set 'mark (list score))
742 (gnus-score-set 'touched '(t))
743 (setq gnus-summary-mark-below score)
744 (gnus-score-update-lines))
746 (defun gnus-score-update-lines ()
747 "Update all lines in the summary buffer."
749 (goto-char (point-min))
751 (gnus-summary-update-line)
754 (defun gnus-score-update-all-lines ()
755 "Update all lines in the summary buffer, even the hidden ones."
757 (goto-char (point-min))
760 (when (gnus-summary-show-thread)
761 (push (point) hidden))
762 (gnus-summary-update-line)
764 ;; Re-hide the hidden threads.
766 (goto-char (pop hidden))
767 (gnus-summary-hide-thread)))))
769 (defun gnus-score-set-expunge-below (score)
770 "Automatically expunge articles with score below SCORE."
772 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
773 (string-to-int (read-string "Set expunge below: ")))))
774 (setq score (or score gnus-summary-default-score 0))
775 (gnus-score-set 'expunge (list score))
776 (gnus-score-set 'touched '(t)))
778 (defun gnus-score-followup-article (&optional score)
779 "Add SCORE to all followups to the article in the current buffer."
781 (setq score (gnus-score-default score))
782 (when (gnus-buffer-live-p gnus-summary-buffer)
785 (goto-char (point-min))
786 (let ((id (mail-fetch-field "message-id")))
788 (set-buffer gnus-summary-buffer)
789 (gnus-summary-score-entry
790 "references" (concat id "[ \t]*$") 'r
791 score (current-time-string) nil t)))))))
793 (defun gnus-score-followup-thread (&optional score)
794 "Add SCORE to all later articles in the thread the current buffer is part of."
796 (setq score (gnus-score-default score))
797 (when (gnus-buffer-live-p gnus-summary-buffer)
800 (goto-char (point-min))
801 (let ((id (mail-fetch-field "message-id")))
803 (set-buffer gnus-summary-buffer)
804 (gnus-summary-score-entry
806 score (current-time-string))))))))
808 (defun gnus-score-set (symbol value &optional alist)
809 ;; Set SYMBOL to VALUE in ALIST.
813 (gnus-newsgroup-score-alist)))
814 (entry (assoc symbol alist)))
815 (cond ((gnus-score-get 'read-only alist)
816 ;; This is a read-only score file, so we do nothing.
819 (setcdr entry value))
821 (error "Empty alist"))
824 (cons (cons symbol value) (cdr alist)))))))
826 (defun gnus-summary-raise-score (n)
827 "Raise the score of the current article by N."
829 (gnus-set-global-variables)
830 (gnus-summary-set-score (+ (gnus-summary-article-score)
831 (or n gnus-score-interactive-default-score ))))
833 (defun gnus-summary-set-score (n)
834 "Set the score of the current article to N."
836 (gnus-set-global-variables)
838 (gnus-summary-show-thread)
839 (let ((buffer-read-only nil))
841 (gnus-summary-update-mark
842 (if (= n (or gnus-summary-default-score 0)) ?
843 (if (< n (or gnus-summary-default-score 0))
844 gnus-score-below-mark gnus-score-over-mark)) 'score))
845 (let* ((article (gnus-summary-article-number))
846 (score (assq article gnus-newsgroup-scored)))
847 (if score (setcdr score n)
848 (setq gnus-newsgroup-scored
849 (cons (cons article n) gnus-newsgroup-scored))))
850 (gnus-summary-update-line)))
852 (defun gnus-summary-current-score ()
853 "Return the score of the current article."
855 (gnus-set-global-variables)
856 (gnus-message 1 "%s" (gnus-summary-article-score)))
858 (defun gnus-score-change-score-file (file)
859 "Change current score alist."
861 (list (read-file-name "Change to score file: " gnus-kill-files-directory)))
862 (gnus-score-load-file file)
863 (gnus-set-mode-line 'summary))
865 (defvar gnus-score-edit-exit-function)
866 (defun gnus-score-edit-current-scores (file)
867 "Edit the current score alist."
868 (interactive (list gnus-current-score-file))
869 (let ((winconf (current-window-configuration)))
870 (and (buffer-name gnus-summary-buffer) (gnus-score-save))
871 (make-directory (file-name-directory file) t)
872 (setq gnus-score-edit-buffer (find-file-noselect file))
873 (gnus-configure-windows 'edit-score)
875 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
876 (make-local-variable 'gnus-prev-winconf)
877 (setq gnus-prev-winconf winconf))
879 4 (substitute-command-keys
880 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
882 (defun gnus-score-edit-file (file)
885 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
886 (make-directory (file-name-directory file) t)
887 (and (buffer-name gnus-summary-buffer) (gnus-score-save))
888 (let ((winconf (current-window-configuration)))
889 (setq gnus-score-edit-buffer (find-file-noselect file))
890 (gnus-configure-windows 'edit-score)
892 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
893 (make-local-variable 'gnus-prev-winconf)
894 (setq gnus-prev-winconf winconf))
896 4 (substitute-command-keys
897 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
899 (defun gnus-score-load-file (file)
900 ;; Load score file FILE. Returns a list a retrieved score-alists.
901 (let* ((file (expand-file-name
902 (or (and (string-match
903 (concat "^" (expand-file-name
904 gnus-kill-files-directory))
905 (expand-file-name file))
907 (concat (file-name-as-directory gnus-kill-files-directory)
909 (cached (assoc file gnus-score-cache))
910 (global (member file gnus-internal-global-score-files))
913 ;; The score file was already loaded.
914 (setq alist (cdr cached))
915 ;; We load the score file.
916 (setq gnus-score-alist nil)
917 (setq alist (gnus-score-load-score-alist file))
918 ;; We add '(touched) to the alist to signify that it hasn't been
920 (or (assq 'touched alist) (setq alist (cons (list 'touched nil) alist)))
921 ;; If it is a global score file, we make it read-only.
923 (not (assq 'read-only alist))
924 (setq alist (cons (list 'read-only t) alist)))
925 (setq gnus-score-cache
926 (cons (cons file alist) gnus-score-cache)))
930 ;; Downcase all header names.
931 (when (stringp (caar a))
932 (setcar (car a) (downcase (caar a)))
935 ;; If there are actual scores in the alist, we add it to the
936 ;; return value of this function.
938 (setq lists (list alist))))
939 ;; Treat the other possible atoms in the score alist.
940 (let ((mark (car (gnus-score-get 'mark alist)))
941 (expunge (car (gnus-score-get 'expunge alist)))
942 (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
943 (files (gnus-score-get 'files alist))
944 (exclude-files (gnus-score-get 'exclude-files alist))
945 (orphan (car (gnus-score-get 'orphan alist)))
946 (adapt (gnus-score-get 'adapt alist))
947 (thread-mark-and-expunge
948 (car (gnus-score-get 'thread-mark-and-expunge alist)))
949 (adapt-file (car (gnus-score-get 'adapt-file alist)))
950 (local (gnus-score-get 'local alist))
951 (eval (car (gnus-score-get 'eval alist))))
952 ;; We do not respect eval and files atoms from global score
954 (and files (not global)
955 (setq lists (apply 'append lists
956 (mapcar (lambda (file)
957 (gnus-score-load-file file))
958 (if adapt-file (cons adapt-file files)
960 (and eval (not global) (eval eval))
961 ;; We then expand any exclude-file directives.
962 (setq gnus-scores-exclude-files
966 (expand-file-name sfile (file-name-directory file)))
967 exclude-files) gnus-scores-exclude-files))
971 (set-buffer gnus-summary-buffer)
973 (and (consp (car local))
974 (symbolp (caar local))
976 (make-local-variable (caar local))
977 (set (caar local) (nth 1 (car local)))))
978 (setq local (cdr local)))))
979 (if orphan (setq gnus-orphan-score orphan))
980 (setq gnus-adaptive-score-alist
981 (cond ((equal adapt '(t))
982 (setq gnus-newsgroup-adaptive t)
983 gnus-default-adaptive-score-alist)
984 ((equal adapt '(ignore))
985 (setq gnus-newsgroup-adaptive nil))
987 (setq gnus-newsgroup-adaptive t)
990 ;;(setq gnus-newsgroup-adaptive gnus-use-adaptive-scoring)
991 gnus-default-adaptive-score-alist)))
992 (setq gnus-thread-expunge-below
993 (or thread-mark-and-expunge gnus-thread-expunge-below))
994 (setq gnus-summary-mark-below
995 (or mark mark-and-expunge gnus-summary-mark-below))
996 (setq gnus-summary-expunge-below
997 (or expunge mark-and-expunge gnus-summary-expunge-below))
998 (setq gnus-newsgroup-adaptive-score-file
999 (or adapt-file gnus-newsgroup-adaptive-score-file)))
1000 (setq gnus-current-score-file file)
1001 (setq gnus-score-alist alist)
1004 (defun gnus-score-load (file)
1006 (let ((cache (assoc file gnus-score-cache)))
1008 (setq gnus-score-alist (cdr cache))
1009 (setq gnus-score-alist nil)
1010 (gnus-score-load-score-alist file)
1011 (or gnus-score-alist
1012 (setq gnus-score-alist (copy-alist '((touched nil)))))
1013 (setq gnus-score-cache
1014 (cons (cons file gnus-score-alist) gnus-score-cache)))))
1016 (defun gnus-score-remove-from-cache (file)
1017 (setq gnus-score-cache
1018 (delq (assoc file gnus-score-cache) gnus-score-cache)))
1020 (defun gnus-score-load-score-alist (file)
1022 (if (not (file-readable-p file))
1023 (setq gnus-score-alist nil)
1025 (gnus-set-work-buffer)
1026 (insert-file-contents file)
1027 (goto-char (point-min))
1028 ;; Only do the loading if the score file isn't empty.
1029 (when (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
1032 (read (current-buffer))
1035 (gnus-message 3 "Problem with score file %s" file)
1039 (if (eq (car alist) 'setq)
1040 ;; This is an old-style score file.
1041 (setq gnus-score-alist (gnus-score-transform-old-to-new alist))
1042 (setq gnus-score-alist alist))
1043 ;; Check the syntax of the score file.
1044 (setq gnus-score-alist
1045 (gnus-score-check-syntax gnus-score-alist file)))))
1047 (defun gnus-score-check-syntax (alist file)
1048 "Check the syntax of the score ALIST."
1052 ((not (consp alist))
1053 (gnus-message 1 "Score file is not a list: %s" file)
1059 (while (and a (not err))
1063 ((not (listp (car a)))
1064 (format "Illegal score element %s in %s" (car a) file))
1067 ((not (listp (setq sr (cdar a))))
1068 (format "Illegal header match %s in %s" (nth 1 (car a)) file))
1070 (setq type (caar a))
1071 (while (and sr (not err))
1076 ((if (member (downcase type) '("lines" "chars"))
1077 (not (numberp (car s)))
1078 (not (stringp (car s))))
1079 (format "Illegal match %s in %s" (car s) file))
1080 ((and (cadr s) (not (integerp (cadr s))))
1081 (format "Non-integer score %s in %s" (cadr s) file))
1082 ((and (caddr s) (not (integerp (caddr s))))
1083 (format "Non-integer date %s in %s" (caddr s) file))
1084 ((and (cadddr s) (not (symbolp (cadddr s))))
1085 (format "Non-symbol match type %s in %s" (cadddr s) file)))))
1091 (gnus-message 3 err)
1096 (defun gnus-score-transform-old-to-new (alist)
1097 (let* ((alist (nth 2 alist))
1099 (if (eq (car alist) 'quote)
1100 (setq alist (nth 1 alist)))
1102 (setq entry (car alist))
1103 (if (stringp (car entry))
1104 (let ((scor (cdr entry)))
1105 (setq out (cons entry out))
1108 (list (caar scor) (nth 2 (car scor))
1109 (and (nth 3 (car scor))
1110 (gnus-day-number (nth 3 (car scor))))
1111 (if (nth 1 (car scor)) 'r 's)))
1112 (setq scor (cdr scor))))
1113 (setq out (cons (if (not (listp (cdr entry)))
1114 (list (car entry) (cdr entry))
1117 (setq alist (cdr alist)))
1118 (cons (list 'touched t) (nreverse out))))
1120 (defun gnus-score-save ()
1121 ;; Save all score information.
1122 (let ((cache gnus-score-cache)
1125 (setq gnus-score-alist nil)
1126 (nnheader-set-temp-buffer "*Score*")
1129 (setq entry (pop cache)
1132 (if (or (not (equal (gnus-score-get 'touched score) '(t)))
1133 (gnus-score-get 'read-only score)
1134 (and (file-exists-p file)
1135 (not (file-writable-p file))))
1137 (setq score (setcdr entry (delq (assq 'touched score) score)))
1139 (let (emacs-lisp-mode-hook)
1141 (concat (regexp-quote gnus-adaptive-file-suffix)
1143 ;; This is an adaptive score file, so we do not run
1144 ;; it through `pp'. These files can get huge, and
1145 ;; are not meant to be edited by human hands.
1146 (prin1 score (current-buffer))
1147 ;; This is a normal score file, so we print it very
1149 (pp score (current-buffer))))
1150 (if (and (not (file-exists-p (file-name-directory file)))
1151 (make-directory (file-name-directory file) t))
1152 (gnus-error 1 "Can't create directory %s"
1153 (file-name-directory file))
1154 ;; If the score file is empty, we delete it.
1155 (if (zerop (buffer-size))
1157 ;; There are scores, so we write the file.
1158 (when (file-writable-p file)
1159 (write-region (point-min) (point-max) file nil 'silent)
1160 (when gnus-score-after-write-file-function
1161 (funcall gnus-score-after-write-file-function file)))))
1162 (and gnus-score-uncacheable-files
1163 (string-match gnus-score-uncacheable-files file)
1164 (gnus-score-remove-from-cache file))))
1165 (kill-buffer (current-buffer)))))
1167 (defun gnus-score-load-files (score-files)
1168 "Load all score files in SCORE-FILES."
1169 ;; Load the score files.
1172 (if (stringp (car score-files))
1173 ;; It is a string, which means that it's a score file name,
1174 ;; so we load the score file and add the score alist to
1175 ;; the list of alists.
1176 (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
1177 ;; It is an alist, so we just add it to the list directly.
1178 (setq scores (nconc (car score-files) scores)))
1179 (setq score-files (cdr score-files)))
1180 ;; Prune the score files that are to be excluded, if any.
1181 (when gnus-scores-exclude-files
1185 (and (setq c (rassq (car s) gnus-score-cache))
1186 (member (car c) gnus-scores-exclude-files)
1187 (setq scores (delq (car s) scores)))
1191 (defun gnus-score-headers (score-files &optional trace)
1192 ;; Score `gnus-newsgroup-headers'.
1194 ;; PLM: probably this is not the best place to clear orphan-score
1195 (setq gnus-orphan-score nil
1196 gnus-scores-articles nil
1197 gnus-scores-exclude-files nil
1198 scores (gnus-score-load-files score-files))
1204 (when (and gnus-summary-default-score
1206 (let* ((entries gnus-header-index)
1207 (now (gnus-day-number (current-time-string)))
1208 (expire (and gnus-score-expiry-days
1209 (- now gnus-score-expiry-days)))
1210 (headers gnus-newsgroup-headers)
1211 (current-score-file gnus-current-score-file)
1213 (gnus-message 5 "Scoring...")
1214 ;; Create articles, an alist of the form `(HEADER . SCORE)'.
1215 (while (setq header (pop headers))
1216 ;; WARNING: The assq makes the function O(N*S) while it could
1217 ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
1218 ;; and S is (length gnus-newsgroup-scored).
1219 (or (assq (mail-header-number header) gnus-newsgroup-scored)
1220 (setq gnus-scores-articles ;Total of 2 * N cons-cells used.
1221 (cons (cons header (or gnus-summary-default-score 0))
1222 gnus-scores-articles))))
1225 (set-buffer (get-buffer-create "*Headers*"))
1226 (buffer-disable-undo (current-buffer))
1228 ;; Set the global variant of this variable.
1229 (setq gnus-current-score-file current-score-file)
1231 (when gnus-orphan-score
1232 (setq gnus-score-index
1233 (nth 1 (assoc "references" gnus-header-index)))
1234 (gnus-score-orphans gnus-orphan-score))
1235 ;; Run each header through the score process.
1237 (setq entry (pop entries)
1238 header (nth 0 entry)
1239 gnus-score-index (nth 1 (assoc header gnus-header-index)))
1240 (when (< 0 (apply 'max (mapcar
1242 (length (gnus-score-get header score)))
1244 ;; Call the scoring function for this type of "header".
1245 (when (setq new (funcall (nth 2 entry) scores header
1248 ;; Remove the buffer.
1249 (kill-buffer (current-buffer)))
1251 ;; Add articles to `gnus-newsgroup-scored'.
1252 (while gnus-scores-articles
1253 (when (or (/= gnus-summary-default-score
1254 (cdar gnus-scores-articles))
1256 (push (cons (mail-header-number (caar gnus-scores-articles))
1257 (cdar gnus-scores-articles))
1258 gnus-newsgroup-scored))
1259 (setq gnus-scores-articles (cdr gnus-scores-articles)))
1262 (while (setq score (pop scores))
1264 (when (listp (caar score))
1265 (gnus-score-advanced (car score) trace))
1268 (gnus-message 5 "Scoring...done"))))))
1271 (defun gnus-get-new-thread-ids (articles)
1272 (let ((index (nth 1 (assoc "message-id" gnus-header-index)))
1273 (refind gnus-score-index)
1274 id-list art this tref)
1276 (setq art (car articles)
1277 this (aref (car art) index)
1278 tref (aref (car art) refind)
1279 articles (cdr articles))
1280 (if (string-equal tref "") ;no references line
1281 (setq id-list (cons this id-list))))
1284 ;; Orphan functions written by plm@atcmp.nl (Peter Mutsaers).
1285 (defun gnus-score-orphans (score)
1286 (let ((new-thread-ids (gnus-get-new-thread-ids gnus-scores-articles))
1287 alike articles art arts this last this-id)
1289 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1290 articles gnus-scores-articles)
1292 ;;more or less the same as in gnus-score-string
1295 (setq art (car articles)
1296 this (aref (car art) gnus-score-index)
1297 articles (cdr articles))
1298 ;;completely skip if this is empty (not a child, so not an orphan)
1299 (if (not (string= this ""))
1300 (if (equal last this)
1301 ;; O(N*H) cons-cells used here, where H is the number of
1303 (setq alike (cons art alike))
1306 ;; Insert the line, with a text property on the
1307 ;; terminating newline referring to the articles with
1310 (put-text-property (1- (point)) (point) 'articles alike)))
1311 (setq alike (list art)
1313 (and last ; Bwadr, duplicate code.
1316 (put-text-property (1- (point)) (point) 'articles alike)))
1318 ;; PLM: now delete those lines that contain an entry from new-thread-ids
1319 (while new-thread-ids
1320 (setq this-id (car new-thread-ids)
1321 new-thread-ids (cdr new-thread-ids))
1322 (goto-char (point-min))
1323 (while (search-forward this-id nil t)
1324 ;; found a match. remove this line
1328 ;; now for each line: update its articles with score by moving to
1329 ;; every end-of-line in the buffer and read the articles property
1330 (goto-char (point-min))
1333 (setq arts (get-text-property (point) 'articles))
1335 (setq art (car arts)
1337 (setcdr art (+ score (cdr art))))
1341 (defun gnus-score-integer (scores header now expire &optional trace)
1342 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1347 (setq alist (car scores)
1349 entries (assoc header alist))
1350 (while (cdr entries) ;First entry is the header index.
1351 (let* ((rest (cdr entries))
1353 (match (nth 0 kill))
1354 (type (or (nth 3 kill) '>))
1355 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1358 (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
1359 (eq type '>=) (eq type '=))
1361 (error "Illegal match type: %s" type)))
1362 (articles gnus-scores-articles))
1363 ;; Instead of doing all the clever stuff that
1364 ;; `gnus-score-string' does to minimize searches and stuff,
1365 ;; I will assume that people generally will put so few
1366 ;; matches on numbers that any cleverness will take more
1367 ;; time than one would gain.
1369 (when (funcall match-func
1370 (or (aref (caar articles) gnus-score-index) 0)
1373 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1376 (setcdr (car articles) (+ score (cdar articles))))
1377 (setq articles (cdr articles)))
1378 ;; Update expire date
1379 (cond ((null date)) ;Permanent entry.
1380 ((and found gnus-update-score-entry-dates) ;Match, update date.
1381 (gnus-score-set 'touched '(t) alist)
1382 (setcar (nthcdr 2 kill) now))
1383 ((and expire (< date expire)) ;Old entry, remove.
1384 (gnus-score-set 'touched '(t) alist)
1385 (setcdr entries (cdr rest))
1386 (setq rest entries)))
1387 (setq entries rest)))))
1390 (defun gnus-score-date (scores header now expire &optional trace)
1391 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1396 (setq alist (car scores)
1398 entries (assoc header alist))
1399 (while (cdr entries) ;First entry is the header index.
1400 (let* ((rest (cdr entries))
1402 (match (timezone-make-date-sortable (nth 0 kill)))
1403 (type (or (nth 3 kill) 'before))
1404 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1408 (cond ((eq type 'after) 'string<)
1409 ((eq type 'before) 'gnus-string>)
1410 ((eq type 'at) 'string=)
1411 (t (error "Illegal match type: %s" type))))
1412 (articles gnus-scores-articles)
1414 ;; Instead of doing all the clever stuff that
1415 ;; `gnus-score-string' does to minimize searches and stuff,
1416 ;; I will assume that people generally will put so few
1417 ;; matches on numbers that any cleverness will take more
1418 ;; time than one would gain.
1421 (setq l (aref (caar articles) gnus-score-index))
1422 (funcall match-func match (timezone-make-date-sortable l))
1424 (and trace (setq gnus-score-trace
1427 (car-safe (rassq alist gnus-score-cache))
1431 (setcdr (car articles) (+ score (cdar articles)))))
1432 (setq articles (cdr articles)))
1433 ;; Update expire date
1434 (cond ((null date)) ;Permanent entry.
1435 ((and found gnus-update-score-entry-dates) ;Match, update date.
1436 (gnus-score-set 'touched '(t) alist)
1437 (setcar (nthcdr 2 kill) now))
1438 ((and expire (< date expire)) ;Old entry, remove.
1439 (gnus-score-set 'touched '(t) alist)
1440 (setcdr entries (cdr rest))
1441 (setq rest entries)))
1442 (setq entries rest)))))
1445 (defun gnus-score-body (scores header now expire &optional trace)
1447 (set-buffer nntp-server-buffer)
1448 (setq gnus-scores-articles
1449 (sort gnus-scores-articles
1451 (< (mail-header-number (car a1))
1452 (mail-header-number (car a2))))))
1454 (let* ((buffer-read-only nil)
1455 (articles gnus-scores-articles)
1457 (request-func (cond ((string= "head" header)
1459 ((string= "body" header)
1461 (t 'gnus-request-article)))
1462 entries alist ofunc article last)
1464 (setq last (mail-header-number (caar (last articles))))
1465 ;; Not all backends support partial fetching. In that case,
1466 ;; we just fetch the entire article.
1467 (unless (gnus-check-backend-function
1468 (and (string-match "^gnus-" (symbol-name request-func))
1469 (intern (substring (symbol-name request-func)
1471 gnus-newsgroup-name)
1472 (setq ofunc request-func)
1473 (setq request-func 'gnus-request-article))
1475 (setq article (mail-header-number (caar articles)))
1476 (gnus-message 7 "Scoring on article %s of %s..." article last)
1477 (when (funcall request-func article gnus-newsgroup-name)
1479 (goto-char (point-min))
1480 ;; If just parts of the article is to be searched, but the
1481 ;; backend didn't support partial fetching, we just narrow
1482 ;; to the relevant parts.
1484 (if (eq ofunc 'gnus-request-head)
1487 (or (search-forward "\n\n" nil t) (point-max)))
1489 (or (search-forward "\n\n" nil t) (point))
1491 (setq scores all-scores)
1494 (setq alist (car scores)
1496 entries (assoc header alist))
1497 (while (cdr entries) ;First entry is the header index.
1498 (let* ((rest (cdr entries))
1500 (match (nth 0 kill))
1501 (type (or (nth 3 kill) 's))
1502 (score (or (nth 1 kill)
1503 gnus-score-interactive-default-score))
1507 (not (or (eq type 'R) (eq type 'S)
1508 (eq type 'Regexp) (eq type 'String))))
1510 (cond ((or (eq type 'r) (eq type 'R)
1511 (eq type 'regexp) (eq type 'Regexp))
1513 ((or (eq type 's) (eq type 'S)
1514 (eq type 'string) (eq type 'String))
1517 (error "Illegal match type: %s" type)))))
1518 (goto-char (point-min))
1519 (if (funcall search-func match nil t)
1520 ;; Found a match, update scores.
1522 (setcdr (car articles) (+ score (cdar articles)))
1524 (and trace (setq gnus-score-trace
1528 (rassq alist gnus-score-cache))
1530 gnus-score-trace)))))
1531 ;; Update expire date
1533 ((null date)) ;Permanent entry.
1534 ((and found gnus-update-score-entry-dates) ;Match, update date.
1535 (gnus-score-set 'touched '(t) alist)
1536 (setcar (nthcdr 2 kill) now))
1537 ((and expire (< date expire)) ;Old entry, remove.
1538 (gnus-score-set 'touched '(t) alist)
1539 (setcdr entries (cdr rest))
1540 (setq rest entries)))
1541 (setq entries rest)))))
1542 (setq articles (cdr articles)))))))
1545 (defun gnus-score-followup (scores header now expire &optional trace thread)
1546 ;; Insert the unique article headers in the buffer.
1547 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1548 (current-score-file gnus-current-score-file)
1550 ;; gnus-score-index is used as a free variable.
1551 alike last this art entries alist articles
1554 ;; Change score file to the adaptive score file. All entries that
1555 ;; this function makes will be put into this file.
1557 (set-buffer gnus-summary-buffer)
1558 (gnus-score-load-file
1559 (or gnus-newsgroup-adaptive-score-file
1560 (gnus-score-file-name
1561 gnus-newsgroup-name gnus-adaptive-file-suffix))))
1563 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1564 articles gnus-scores-articles)
1568 (setq art (car articles)
1569 this (aref (car art) gnus-score-index)
1570 articles (cdr articles))
1571 (if (equal last this)
1572 (setq alike (cons art alike))
1576 (put-text-property (1- (point)) (point) 'articles alike)))
1577 (setq alike (list art)
1579 (and last ; Bwadr, duplicate code.
1582 (put-text-property (1- (point)) (point) 'articles alike)))
1586 (setq alist (car scores)
1588 entries (assoc header alist))
1589 (while (cdr entries) ;First entry is the header index.
1590 (let* ((rest (cdr entries))
1592 (match (nth 0 kill))
1593 (type (or (nth 3 kill) 's))
1594 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1597 (mt (aref (symbol-name type) 0))
1599 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1602 (cond ((= dmt ?r) 're-search-forward)
1603 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1604 (t (error "Illegal match type: %s" type))))
1606 (goto-char (point-min))
1608 (while (funcall search-func match nil t)
1609 (and (= (progn (beginning-of-line) (point))
1610 (match-beginning 0))
1611 (= (progn (end-of-line) (point))
1614 (setq found (setq arts (get-text-property
1615 (point) 'articles)))
1616 ;; Found a match, update scores.
1618 (setq art (car arts)
1620 (gnus-score-add-followups
1621 (car art) score all-scores thread))))
1623 (while (funcall search-func match nil t)
1625 (setq found (setq arts (get-text-property (point) 'articles)))
1626 ;; Found a match, update scores.
1627 (while (setq art (pop arts))
1628 (when (setq new (gnus-score-add-followups
1629 (car art) score all-scores thread))
1631 ;; Update expire date
1632 (cond ((null date)) ;Permanent entry.
1633 ((and found gnus-update-score-entry-dates) ;Match, update date.
1634 (gnus-score-set 'touched '(t) alist)
1635 (setcar (nthcdr 2 kill) now))
1636 ((and expire (< date expire)) ;Old entry, remove.
1637 (gnus-score-set 'touched '(t) alist)
1638 (setcdr entries (cdr rest))
1639 (setq rest entries)))
1640 (setq entries rest))))
1641 ;; We change the score file back to the previous one.
1643 (set-buffer gnus-summary-buffer)
1644 (gnus-score-load-file current-score-file))
1645 (list (cons "references" news))))
1647 (defun gnus-score-add-followups (header score scores &optional thread)
1648 "Add a score entry to the adapt file."
1650 (set-buffer gnus-summary-buffer)
1651 (let* ((id (mail-header-id header))
1652 (scores (car scores))
1654 ;; Don't enter a score if there already is one.
1655 (while (setq entry (pop scores))
1656 (and (equal "references" (car entry))
1657 (or (null (nth 3 (cadr entry)))
1658 (eq 's (nth 3 (cadr entry))))
1662 (gnus-summary-score-entry
1663 (if thread "thread" "references")
1664 id 's score (current-time-string) nil t)))))
1666 (defun gnus-score-string (score-list header now expire &optional trace)
1667 ;; Score ARTICLES according to HEADER in SCORE-LIST.
1668 ;; Update matching entries to NOW and remove unmatched entries older
1671 ;; Insert the unique article headers in the buffer.
1672 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1673 ;; gnus-score-index is used as a free variable.
1674 alike last this art entries alist articles scores
1675 fuzzies arts words kill)
1677 ;; Sorting the articles costs os O(N*log N) but will allow us to
1678 ;; only match with each unique header. Thus the actual matching
1679 ;; will be O(M*U) where M is the number of strings to match with,
1680 ;; and U is the number of unique headers. It is assumed (but
1681 ;; untested) this will be a net win because of the large constant
1682 ;; factor involved with string matching.
1683 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1684 articles gnus-scores-articles)
1687 (while (setq art (pop articles))
1688 (setq this (aref (car art) gnus-score-index))
1689 (if (equal last this)
1690 ;; O(N*H) cons-cells used here, where H is the number of
1692 (setq alike (cons art alike))
1694 ;; Insert the line, with a text property on the
1695 ;; terminating newline referring to the articles with
1698 (put-text-property (1- (point)) (point) 'articles alike))
1699 (setq alike (list art)
1701 (when last ; Bwadr, duplicate code.
1703 (put-text-property (1- (point)) (point) 'articles alike))
1705 ;; Go through all the score alists and pick out the entries
1708 (setq alist (pop score-list)
1709 ;; There's only one instance of this header for
1710 ;; each score alist.
1711 entries (assoc header alist))
1712 (while (cdr entries) ;First entry is the header index.
1713 (let* ((kill (cadr entries))
1714 (match (nth 0 kill))
1715 (type (or (nth 3 kill) 's))
1716 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1719 (mt (aref (symbol-name type) 0))
1720 (case-fold-search (not (memq mt '(?R ?S ?E ?F))))
1723 (cond ((= dmt ?r) 're-search-forward)
1724 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1725 (t (error "Illegal match type: %s" type)))))
1727 ;; Fuzzy matches. We save these for later.
1729 (push entries fuzzies))
1730 ;; Word matches. Save these for even later.
1732 (push entries words))
1735 ;; Do exact matching.
1736 (goto-char (point-min))
1737 (while (and (not (eobp))
1738 (funcall search-func match nil t))
1739 ;; Is it really exact?
1741 (= (gnus-point-at-bol) (match-beginning 0))
1744 (setq found (setq arts (get-text-property
1745 (point) 'articles)))
1746 ;; Found a match, update scores.
1748 (while (setq art (pop arts))
1749 (setcdr art (+ score (cdr art)))
1750 (setq gnus-score-trace
1754 (rassq alist gnus-score-cache))
1757 (while (setq art (pop arts))
1758 (setcdr art (+ score (cdr art)))))))
1760 ;; Regexp and substring matching.
1762 (goto-char (point-min))
1763 (when (string= match "")
1765 (while (and (not (eobp))
1766 (funcall search-func match nil t))
1767 (goto-char (match-beginning 0))
1769 (setq found (setq arts (get-text-property (point) 'articles)))
1770 ;; Found a match, update scores.
1772 (while (setq art (pop arts))
1773 (setcdr art (+ score (cdr art)))
1774 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1776 (while (setq art (pop arts))
1777 (setcdr art (+ score (cdr art)))))
1779 ;; Update expiry date
1781 (setq entries (cdr entries))
1785 (setq entries (cdr entries)))
1786 ;; We have a match, so we update the date.
1787 ((and found gnus-update-score-entry-dates)
1788 (gnus-score-set 'touched '(t) alist)
1789 (setcar (nthcdr 2 kill) now)
1790 (setq entries (cdr entries)))
1791 ;; This entry has expired, so we remove it.
1792 ((and expire (< date expire))
1793 (gnus-score-set 'touched '(t) alist)
1794 (setcdr entries (cddr entries)))
1795 ;; No match; go to next entry.
1797 (setq entries (cdr entries))))))))
1799 ;; Find fuzzy matches.
1801 ;; Simplify the entire buffer for easy matching.
1802 (gnus-simplify-buffer-fuzzy)
1803 (while (setq kill (cadar fuzzies))
1804 (let* ((match (nth 0 kill))
1806 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1808 (mt (aref (symbol-name type) 0))
1809 (case-fold-search (not (= mt ?F)))
1811 (goto-char (point-min))
1812 (while (and (not (eobp))
1813 (search-forward match nil t))
1814 (when (and (= (gnus-point-at-bol) (match-beginning 0))
1816 (setq found (setq arts (get-text-property (point) 'articles)))
1818 (while (setq art (pop arts))
1819 (setcdr art (+ score (cdr art)))
1821 (car-safe (rassq alist gnus-score-cache)) kill)
1823 ;; Found a match, update scores.
1824 (while (setq art (pop arts))
1825 (setcdr art (+ score (cdr art))))))
1827 ;; Update expiry date
1832 ;; Match, update date.
1833 ((and found gnus-update-score-entry-dates)
1834 (gnus-score-set 'touched '(t) alist)
1835 (setcar (nthcdr 2 kill) now))
1836 ;; Old entry, remove.
1837 ((and expire (< date expire))
1838 (gnus-score-set 'touched '(t) alist)
1839 (setcdr (car fuzzies) (cddar fuzzies))))
1840 (setq fuzzies (cdr fuzzies)))))
1843 ;; Enter all words into the hashtb.
1844 (let ((hashtb (gnus-make-hashtable
1845 (* 10 (count-lines (point-min) (point-max))))))
1846 (gnus-enter-score-words-into-hashtb hashtb)
1847 (while (setq kill (cadar words))
1848 (let* ((score (or (nth 1 kill) gnus-score-interactive-default-score))
1851 (when (setq arts (intern-soft (nth 0 kill) hashtb))
1854 (while (setq art (pop arts))
1855 (setcdr art (+ score (cdr art)))
1857 (car-safe (rassq alist gnus-score-cache)) kill)
1859 ;; Found a match, update scores.
1860 (while (setq art (pop arts))
1861 (setcdr art (+ score (cdr art))))))
1862 ;; Update expiry date
1867 ;; Match, update date.
1868 ((and found gnus-update-score-entry-dates)
1869 (gnus-score-set 'touched '(t) alist)
1870 (setcar (nthcdr 2 kill) now))
1871 ;; Old entry, remove.
1872 ((and expire (< date expire))
1873 (gnus-score-set 'touched '(t) alist)
1874 (setcdr (car words) (cddar words))))
1875 (setq fuzzies (cdr fuzzies))))))
1878 (defun gnus-enter-score-words-into-hashtb (hashtb)
1879 ;; Find all the words in the buffer and enter them into
1882 (goto-char (point-min))
1883 (while (re-search-forward "\\b\\w+\\b" nil t)
1885 (setq word (downcase (buffer-substring
1886 (match-beginning 0) (match-end 0))))
1887 (append (get-text-property (gnus-point-at-eol) 'articles)
1888 (gnus-gethash word hashtb))
1890 ;; Make all the ignorable words ignored.
1891 (let ((ignored gnus-ignored-adaptive-words))
1893 (gnus-sethash (pop ignored) nil hashtb)))))
1895 (defun gnus-score-string< (a1 a2)
1896 ;; Compare headers in articles A2 and A2.
1897 ;; The header index used is the free variable `gnus-score-index'.
1898 (string-lessp (aref (car a1) gnus-score-index)
1899 (aref (car a2) gnus-score-index)))
1901 (defun gnus-current-score-file-nondirectory (&optional score-file)
1902 (let ((score-file (or score-file gnus-current-score-file)))
1904 (gnus-short-group-name (file-name-nondirectory score-file))
1907 (defun gnus-score-adaptive ()
1908 "Create adaptive score rules for this newsgroup."
1909 (when gnus-use-adaptive-scoring
1910 ;; We change the score file to the adaptive score file.
1912 (set-buffer gnus-summary-buffer)
1913 (gnus-score-load-file
1914 (or gnus-newsgroup-adaptive-score-file
1915 (gnus-score-file-name
1916 gnus-newsgroup-name gnus-adaptive-file-suffix))))
1918 ;; Perform ordinary line scoring.
1919 ((or (not (listp gnus-use-adaptive-scoring))
1920 (memq 'line gnus-use-adaptive-scoring))
1922 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
1924 (date (current-time-string))
1925 (data gnus-newsgroup-data)
1927 ;; First we transform the adaptive rule alist into something
1928 ;; that's faster to process.
1930 (setq elem (car malist))
1931 (if (symbolp (car elem))
1932 (setcar elem (symbol-value (car elem))))
1933 (setq elem (cdr elem))
1936 (cons (if (eq (caar elem) 'followup)
1938 (symbol-name (caar elem)))
1943 (concat "mail-header-"
1944 (if (eq (caar elem) 'followup)
1946 (downcase (symbol-name (caar elem))))))
1948 (setq elem (cdr elem)))
1949 (setq malist (cdr malist)))
1950 ;; Then we score away.
1952 (setq elem (cdr (assq (gnus-data-mark (car data)) alist)))
1954 (gnus-data-pseudo-p (car data)))
1956 (when (setq headers (gnus-data-header (car data)))
1958 (setq match (funcall (caar elem) headers))
1959 (gnus-summary-score-entry
1960 (nth 1 (car elem)) match
1964 ((equal (nth 1 (car elem)) "date")
1967 ;; Whether we use substring or exact matches is
1969 (if (or (not gnus-score-exact-adapt-limit)
1970 (< (length match) gnus-score-exact-adapt-limit))
1972 (if (equal (nth 1 (car elem)) "subject")
1974 (nth 2 (car elem)) date nil t)
1975 (setq elem (cdr elem)))))
1976 (setq data (cdr data))))))
1978 ;; Perform adaptive word scoring.
1979 ((memq 'word gnus-use-adaptive-scoring)
1980 (nnheader-temp-write nil
1981 (let* ((hashtb (gnus-make-hashtable 1000))
1982 (date (current-time-string))
1983 (data gnus-newsgroup-data)
1985 ;; Go through all articles.
1986 (while (setq d (pop data))
1987 (when (setq score (cdr (assq
1989 gnus-default-adaptive-word-score-alist)))
1990 ;; This article has a mark that should lead to
1991 ;; adaptive word rules, so we insert the subject
1992 ;; and find all words in that string.
1993 (insert (mail-header-subject (gnus-data-header d)))
1994 (downcase-region (point-min) (point-max))
1995 (goto-char (point-min))
1996 (while (re-search-forward "\\b\\w+\\b" nil t)
1997 ;; Put the word and score into the hashtb.
1998 (gnus-sethash (setq word (match-string 0))
1999 (+ (or (gnus-gethash word hashtb) 0) score)
2002 ;; Make all the ignorable words ignored.
2003 (let ((ignored gnus-ignored-adaptive-words))
2005 (gnus-sethash (pop ignored) nil hashtb)))
2006 ;; Now we have all the words and scores, so we
2007 ;; add these rules to the ADAPT file.
2010 (gnus-summary-score-entry
2011 "subject" (symbol-name word) 'w (symbol-value word)
2015 (defun gnus-score-edit-done ()
2016 (let ((bufnam (buffer-file-name (current-buffer)))
2017 (winconf gnus-prev-winconf))
2018 (and winconf (set-window-configuration winconf))
2019 (gnus-score-remove-from-cache bufnam)
2020 (gnus-score-load-file bufnam)))
2022 (defun gnus-score-find-trace ()
2023 "Find all score rules that applies to the current article."
2025 (let ((gnus-newsgroup-headers
2026 (list (gnus-summary-article-header)))
2027 (gnus-newsgroup-scored nil)
2030 (nnheader-set-temp-buffer "*Score Trace*"))
2031 (setq gnus-score-trace nil)
2032 (gnus-possibly-score-headers 'trace)
2033 (if (not (setq trace gnus-score-trace))
2034 (gnus-error 1 "No score rules apply to the current article.")
2035 (set-buffer "*Score Trace*")
2036 (gnus-add-current-to-buffer-list)
2038 (insert (format "%S -> %s\n" (cdar trace)
2039 (file-name-nondirectory (caar trace))))
2040 (setq trace (cdr trace)))
2041 (goto-char (point-min))
2042 (gnus-configure-windows 'score-trace))))
2044 (defun gnus-score-find-favourite-words ()
2045 "List words used in scoring."
2047 (let ((alists (gnus-score-load-files (gnus-all-score-files)))
2049 ;; Go through all the score alists for this group
2050 ;; and find all `w' rules.
2051 (while (setq alist (pop alists))
2052 (when (and (stringp (setq rule (pop alist)))
2053 (equal "subject" (downcase (pop rule))))
2055 (when (memq (nth 3 (car rule)) '(w W word Word))
2056 (push (cons (or (nth 1 rule) gnus-score-interactive-default-score)
2060 (setq rules (sort rules (lambda (r1 r2)
2061 (string-lessp (cdr r1) (cdr r2)))))
2062 ;; Add up words that have appeared several times.
2065 (if (equal (cdar r) (cdadr r))
2067 (setcar (car r) (+ (caar r) (caadr r)))
2068 (setcdr r (cddr r)))
2070 ;; Insert the words.
2071 (nnheader-set-temp-buffer "*Score Words*")
2072 (setq rules (sort rules (lambda (r1 r2) (> (car r1) (car r2)))))
2074 (insert (format "%-5d: %s\n" (caar rules) (cdar rules)))
2076 (gnus-add-current-to-buffer-list)
2077 (gnus-configure-windows 'score-words)))
2079 (defun gnus-summary-rescore ()
2080 "Redo the entire scoring process in the current summary."
2083 (setq gnus-score-cache nil)
2084 (setq gnus-newsgroup-scored nil)
2085 (gnus-possibly-score-headers)
2086 (gnus-score-update-all-lines))
2088 (defun gnus-score-flush-cache ()
2089 "Flush the cache of score files."
2092 (setq gnus-score-cache nil
2093 gnus-score-alist nil
2094 gnus-short-name-score-file-cache nil)
2095 (gnus-message 6 "The score cache is now flushed"))
2097 (gnus-add-shutdown 'gnus-score-close 'gnus)
2099 (defvar gnus-score-file-alist-cache nil)
2101 (defun gnus-score-close ()
2102 "Clear all internal score variables."
2103 (setq gnus-score-cache nil
2104 gnus-internal-global-score-files nil
2105 gnus-score-file-list nil
2106 gnus-score-file-alist-cache nil))
2108 ;; Summary score marking commands.
2110 (defun gnus-summary-raise-same-subject-and-select (score)
2111 "Raise articles which has the same subject with SCORE and select the next."
2113 (let ((subject (gnus-summary-article-subject)))
2114 (gnus-summary-raise-score score)
2115 (while (gnus-summary-find-subject subject)
2116 (gnus-summary-raise-score score))
2117 (gnus-summary-next-article t)))
2119 (defun gnus-summary-raise-same-subject (score)
2120 "Raise articles which has the same subject with SCORE."
2122 (let ((subject (gnus-summary-article-subject)))
2123 (gnus-summary-raise-score score)
2124 (while (gnus-summary-find-subject subject)
2125 (gnus-summary-raise-score score))
2126 (gnus-summary-next-subject 1 t)))
2128 (defun gnus-score-default (level)
2129 (if level (prefix-numeric-value level)
2130 gnus-score-interactive-default-score))
2132 (defun gnus-summary-raise-thread (&optional score)
2133 "Raise the score of the articles in the current thread with SCORE."
2135 (setq score (gnus-score-default score))
2138 (let ((articles (gnus-summary-articles-in-thread)))
2140 (gnus-summary-goto-subject (car articles))
2141 (gnus-summary-raise-score score)
2142 (setq articles (cdr articles))))
2144 (let ((gnus-summary-check-current t))
2145 (or (zerop (gnus-summary-next-subject 1 t))
2147 (gnus-summary-recenter)
2148 (gnus-summary-position-point)
2149 (gnus-set-mode-line 'summary))
2151 (defun gnus-summary-lower-same-subject-and-select (score)
2152 "Raise articles which has the same subject with SCORE and select the next."
2154 (gnus-summary-raise-same-subject-and-select (- score)))
2156 (defun gnus-summary-lower-same-subject (score)
2157 "Raise articles which has the same subject with SCORE."
2159 (gnus-summary-raise-same-subject (- score)))
2161 (defun gnus-summary-lower-thread (&optional score)
2162 "Lower score of articles in the current thread with SCORE."
2164 (gnus-summary-raise-thread (- (1- (gnus-score-default score)))))
2166 ;;; Finding score files.
2168 (defun gnus-score-score-files (group)
2169 "Return a list of all possible score files."
2170 ;; Search and set any global score files.
2171 (and gnus-global-score-files
2172 (or gnus-internal-global-score-files
2173 (gnus-score-search-global-directories gnus-global-score-files)))
2174 ;; Fix the kill-file dir variable.
2175 (setq gnus-kill-files-directory
2176 (file-name-as-directory gnus-kill-files-directory))
2177 ;; If we can't read it, there are no score files.
2178 (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
2179 (setq gnus-score-file-list nil)
2180 (if (not (gnus-use-long-file-name 'not-score))
2181 ;; We do not use long file names, so we have to do some
2182 ;; directory traversing.
2183 (setq gnus-score-file-list
2185 (or gnus-short-name-score-file-cache
2187 (gnus-message 6 "Finding all score files...")
2188 (setq gnus-short-name-score-file-cache
2189 (gnus-score-score-files-1
2190 gnus-kill-files-directory))
2191 (gnus-message 6 "Finding all score files...done")))))
2192 ;; We want long file names.
2193 (when (or (not gnus-score-file-list)
2194 (not (car gnus-score-file-list))
2195 (gnus-file-newer-than gnus-kill-files-directory
2196 (car gnus-score-file-list)))
2197 (setq gnus-score-file-list
2198 (cons (nth 5 (file-attributes gnus-kill-files-directory))
2201 gnus-kill-files-directory t
2202 (gnus-score-file-regexp)))))))
2203 (cdr gnus-score-file-list)))
2205 (defun gnus-score-score-files-1 (dir)
2206 "Return all possible score files under DIR."
2207 (let ((files (directory-files (expand-file-name dir) t nil t))
2208 (regexp (gnus-score-file-regexp))
2210 (while (setq file (pop files))
2212 ;; Ignore "." and "..".
2213 ((member (file-name-nondirectory file) '("." ".."))
2215 ;; Recurse down directories.
2216 ((file-directory-p file)
2217 (setq out (nconc (gnus-score-score-files-1 file) out)))
2218 ;; Add files to the list of score files.
2219 ((string-match regexp file)
2222 ;; Return a dummy value.
2223 (list "~/News/this.file.does.not.exist.SCORE"))))
2225 (defun gnus-score-file-regexp ()
2226 "Return a regexp that match all score files."
2227 (concat "\\(" (regexp-quote gnus-score-file-suffix )
2228 "\\|" (regexp-quote gnus-adaptive-file-suffix) "\\)\\'"))
2230 (defun gnus-score-find-bnews (group)
2231 "Return a list of score files for GROUP.
2232 The score files are those files in the ~/News/ directory which matches
2233 GROUP using BNews sys file syntax."
2234 (let* ((sfiles (append (gnus-score-score-files group)
2235 gnus-internal-global-score-files))
2236 (kill-dir (file-name-as-directory
2237 (expand-file-name gnus-kill-files-directory)))
2238 (klen (length kill-dir))
2239 (score-regexp (gnus-score-file-regexp))
2240 (trans (cdr (assq ?: nnheader-file-name-translation-alist)))
2241 ofiles not-match regexp)
2243 (set-buffer (get-buffer-create "*gnus score files*"))
2244 (buffer-disable-undo (current-buffer))
2245 ;; Go through all score file names and create regexp with them
2249 (insert (car sfiles))
2250 (goto-char (point-min))
2251 ;; First remove the suffix itself.
2252 (when (re-search-forward (concat "." score-regexp) nil t)
2253 (replace-match "" t t)
2254 (goto-char (point-min))
2255 (if (looking-at (regexp-quote kill-dir))
2256 ;; If the file name was just "SCORE", `klen' is one character
2258 (delete-char (min (1- (point-max)) klen))
2259 (goto-char (point-max))
2260 (search-backward "/")
2261 (delete-region (1+ (point)) (point-min)))
2262 ;; If short file names were used, we have to translate slashes.
2263 (goto-char (point-min))
2264 (let ((regexp (concat
2265 "[/:" (if trans (char-to-string trans) "") "]")))
2266 (while (re-search-forward regexp nil t)
2267 (replace-match "." t t)))
2268 ;; Cludge to get rid of "nntp+" problems.
2269 (goto-char (point-min))
2270 (and (looking-at "nn[a-z]+\\+")
2272 (search-forward "+")
2275 ;; Kludge to deal with "++".
2276 (goto-char (point-min))
2277 (while (search-forward "++" nil t)
2278 (replace-match "\\+\\+" t t))
2279 ;; Translate "all" to ".*".
2280 (goto-char (point-min))
2281 (while (search-forward "all" nil t)
2282 (replace-match ".*" t t))
2283 (goto-char (point-min))
2284 ;; Deal with "not."s.
2285 (if (looking-at "not.")
2288 (setq regexp (buffer-substring 5 (point-max))))
2289 (setq regexp (buffer-substring 1 (point-max)))
2290 (setq not-match nil))
2291 ;; Finally - if this resulting regexp matches the group name,
2292 ;; we add this score file to the list of score files
2293 ;; applicable to this group.
2294 (if (or (and not-match
2295 (not (string-match regexp group)))
2296 (and (not not-match)
2297 (string-match regexp group)))
2298 (setq ofiles (cons (car sfiles) ofiles))))
2299 (setq sfiles (cdr sfiles)))
2300 (kill-buffer (current-buffer))
2301 ;; Slight kludge here - the last score file returned should be
2302 ;; the local score file, whether it exists or not. This is so
2303 ;; that any score commands the user enters will go to the right
2304 ;; file, and not end up in some global score file.
2305 (let ((localscore (gnus-score-file-name group)))
2306 (setq ofiles (cons localscore (delete localscore ofiles))))
2307 (nreverse ofiles))))
2309 (defun gnus-score-find-single (group)
2310 "Return list containing the score file for GROUP."
2311 (list (or gnus-newsgroup-adaptive-score-file
2312 (gnus-score-file-name group gnus-adaptive-file-suffix))
2313 (gnus-score-file-name group)))
2315 (defun gnus-score-find-hierarchical (group)
2316 "Return list of score files for GROUP.
2317 This includes the score file for the group and all its parents."
2318 (let ((all (copy-sequence '(nil)))
2320 (while (string-match "\\." group (1+ start))
2321 (setq start (match-beginning 0))
2322 (setq all (cons (substring group 0 start) all)))
2323 (setq all (cons group all))
2325 (mapcar (lambda (newsgroup)
2326 (gnus-score-file-name newsgroup gnus-adaptive-file-suffix))
2327 (setq all (nreverse all)))
2328 (mapcar 'gnus-score-file-name all))))
2330 (defun gnus-score-find-alist (group)
2331 "Return list of score files for GROUP.
2332 The list is determined from the variable gnus-score-file-alist."
2333 (let ((alist gnus-score-file-multiple-match-alist)
2335 ;; if this group has been seen before, return the cached entry
2336 (if (setq score-files (assoc group gnus-score-file-alist-cache))
2337 (cdr score-files) ;ensures caching groups with no matches
2338 ;; handle the multiple match alist
2340 (and (string-match (caar alist) group)
2342 (nconc score-files (copy-sequence (cdar alist)))))
2343 (setq alist (cdr alist)))
2344 (setq alist gnus-score-file-single-match-alist)
2345 ;; handle the single match alist
2347 (and (string-match (caar alist) group)
2348 ;; progn used just in case ("regexp") has no files
2349 ;; and score-files is still nil. -sj
2350 ;; this can be construed as a "stop searching here" feature :>
2351 ;; and used to simplify regexps in the single-alist
2354 (nconc score-files (copy-sequence (cdar alist))))
2356 (setq alist (cdr alist)))
2357 ;; cache the score files
2358 (setq gnus-score-file-alist-cache
2359 (cons (cons group score-files) gnus-score-file-alist-cache))
2362 (defun gnus-all-score-files ()
2363 "Return a list of all score files for the current group."
2364 (let ((funcs gnus-score-find-score-files-function)
2365 (group gnus-newsgroup-name)
2367 ;; Make sure funcs is a list.
2370 (setq funcs (list funcs)))
2371 ;; Get the initial score files for this group.
2373 (setq score-files (gnus-score-find-alist group)))
2374 ;; Add any home adapt files.
2375 (let ((home (gnus-home-score-file group t)))
2377 (push home score-files)
2378 (setq gnus-newsgroup-adaptive-score-file home)))
2379 ;; Check whether there is a `adapt-file' group parameter.
2380 (let ((param-file (gnus-group-get-parameter group 'adapt-file)))
2382 (push param-file score-files)
2383 (setq gnus-newsgroup-adaptive-score-file param-file)))
2384 ;; Go through all the functions for finding score files (or actual
2385 ;; scores) and add them to a list.
2387 (when (gnus-functionp (car funcs))
2389 (nconc score-files (funcall (car funcs) group))))
2390 (setq funcs (cdr funcs)))
2391 ;; Add any home score files.
2392 (let ((home (gnus-home-score-file group)))
2394 (push home score-files)))
2395 ;; Check whether there is a `score-file' group parameter.
2396 (let ((param-file (gnus-group-get-parameter group 'score-file)))
2398 (push param-file score-files)))
2399 ;; Do the scoring if there are any score files for this group.
2402 (defun gnus-possibly-score-headers (&optional trace)
2403 "Do scoring if scoring is required."
2404 (let ((score-files (gnus-all-score-files)))
2406 (gnus-score-headers score-files trace))))
2408 (defun gnus-score-file-name (newsgroup &optional suffix)
2409 "Return the name of a score file for NEWSGROUP."
2410 (let ((suffix (or suffix gnus-score-file-suffix)))
2411 (nnheader-translate-file-chars
2413 ((or (null newsgroup)
2414 (string-equal newsgroup ""))
2415 ;; The global score file is placed at top of the directory.
2417 suffix gnus-kill-files-directory))
2418 ((gnus-use-long-file-name 'not-score)
2419 ;; Append ".SCORE" to newsgroup name.
2420 (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
2422 gnus-kill-files-directory))
2424 ;; Place "SCORE" under the hierarchical directory.
2425 (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
2427 gnus-kill-files-directory))))))
2429 (defun gnus-score-search-global-directories (files)
2430 "Scan all global score directories for score files."
2431 ;; Set the variable `gnus-internal-global-score-files' to all
2432 ;; available global score files.
2433 (interactive (list gnus-global-score-files))
2436 (if (string-match "/$" (car files))
2437 (setq out (nconc (directory-files
2439 (concat (gnus-score-file-regexp) "$"))))
2440 (setq out (cons (car files) out)))
2441 (setq files (cdr files)))
2442 (setq gnus-internal-global-score-files out)))
2444 (defun gnus-score-default-fold-toggle ()
2445 "Toggle folding for new score file entries."
2447 (setq gnus-score-default-fold (not gnus-score-default-fold))
2448 (if gnus-score-default-fold
2449 (gnus-message 1 "New score file entries will be case insensitive.")
2450 (gnus-message 1 "New score file entries will be case sensitive.")))
2452 ;;; Home score file.
2454 (defun gnus-home-score-file (group &optional adapt)
2455 "Return the home score file for GROUP.
2456 If ADAPT, return the home adaptive file instead."
2457 (let ((list (if adapt gnus-home-adapt-file gnus-home-score-file))
2459 ;; Make sure we have a list.
2460 (unless (listp list)
2461 (setq list (list list)))
2462 ;; Go through the list and look for matches.
2463 (while (and (not found)
2464 (setq elem (pop list)))
2471 ((gnus-functionp elem)
2472 (funcall elem group))
2475 (when (string-match (car elem) group)
2478 (nnheader-concat gnus-kill-files-directory found))))
2480 (defun gnus-hierarchial-home-score-file (group)
2481 "Return the score file of the top-level hierarchy of GROUP."
2482 (if (string-match "^[^.]+\\." group)
2483 (concat (match-string 0 group) gnus-score-file-suffix)
2484 ;; Group name without any dots.
2485 (concat group "." gnus-score-file-suffix)))
2487 (defun gnus-hierarchial-home-adapt-file (group)
2488 "Return the adapt file of the top-level hierarchy of GROUP."
2489 (if (string-match "^[^.]+\\." group)
2490 (concat (match-string 0 group) gnus-adaptive-file-suffix)
2491 ;; Group name without any dots.
2492 (concat group "." gnus-adaptive-file-suffix)))
2495 ;;; Adaptive word scoring
2499 (provide 'gnus-score)
2501 ;;; gnus-score.el ends here