1 ;;; gnus-score.el --- scoring code for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
6 ;; Author: Per Abrahamsen <amanda@iesd.auc.dk>
7 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
31 (eval-when-compile (require 'cl))
40 (defcustom gnus-global-score-files nil
41 "List of global score files and directories.
42 Set this variable if you want to use people's score files. One entry
43 for each score file or each score file directory. Gnus will decide
44 by itself what score files are applicable to which group.
46 Say you want to use the single score file
47 \"/ftp.gnus.org@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
48 score files in the \"/ftp.some-where:/pub/score\" directory.
50 (setq gnus-global-score-files
51 '(\"/ftp.gnus.org:/pub/larsi/ding/score/soc.motss.SCORE\"
52 \"/ftp.some-where:/pub/score\"))"
53 :group 'gnus-score-files
56 (defcustom gnus-score-file-single-match-alist nil
57 "Alist mapping regexps to lists of score files.
58 Each element of this alist should be of the form
59 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
61 If the name of a group is matched by REGEXP, the corresponding scorefiles
62 will be used for that group.
63 The first match found is used, subsequent matching entries are ignored (to
64 use multiple matches, see `gnus-score-file-multiple-match-alist').
66 These score files are loaded in addition to any files returned by
67 `gnus-score-find-score-files-function'."
68 :group 'gnus-score-files
69 :type '(repeat (cons regexp (repeat file))))
71 (defcustom gnus-score-file-multiple-match-alist nil
72 "Alist mapping regexps to lists of score files.
73 Each element of this alist should be of the form
74 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
76 If the name of a group is matched by REGEXP, the corresponding scorefiles
77 will be used for that group.
78 If multiple REGEXPs match a group, the score files corresponding to each
79 match will be used (for only one match to be used, see
80 `gnus-score-file-single-match-alist').
82 These score files are loaded in addition to any files returned by
83 `gnus-score-find-score-files-function'."
84 :group 'gnus-score-files
85 :type '(repeat (cons regexp (repeat file))))
87 (defcustom gnus-score-file-suffix "SCORE"
88 "Suffix of the score files."
89 :group 'gnus-score-files
92 (defcustom gnus-adaptive-file-suffix "ADAPT"
93 "Suffix of the adaptive score files."
94 :group 'gnus-score-files
95 :group 'gnus-score-adapt
98 (defcustom gnus-score-find-score-files-function 'gnus-score-find-bnews
99 "Function used to find score files.
100 The function will be called with the group name as the argument, and
101 should return a list of score files to apply to that group. The score
102 files do not actually have to exist.
104 Predefined values are:
106 `gnus-score-find-single': Only apply the group's own score file.
107 `gnus-score-find-hierarchical': Also apply score files from parent groups.
108 `gnus-score-find-bnews': Apply score files whose names matches.
110 See the documentation to these functions for more information.
112 This variable can also be a list of functions to be called. Each
113 function is given the group name as argument and should either return
114 a list of score files, or a list of score alists.
116 If functions other than these pre-defined functions are used,
117 the `a' symbolic prefix to the score commands will always use
119 :group 'gnus-score-files
120 :type '(radio (function-item gnus-score-find-single)
121 (function-item gnus-score-find-hierarchical)
122 (function-item gnus-score-find-bnews)
123 (repeat :tag "List of functions"
124 (choice (function :tag "Other" :value 'ignore)
125 (function-item gnus-score-find-single)
126 (function-item gnus-score-find-hierarchical)
127 (function-item gnus-score-find-bnews)))
128 (function :tag "Other" :value 'ignore)))
130 (defcustom gnus-score-interactive-default-score 1000
131 "*Scoring commands will raise/lower the score with this number as the default."
132 :group 'gnus-score-default
135 (defcustom gnus-score-expiry-days 7
136 "*Number of days before unused score file entries are expired.
137 If this variable is nil, no score file entries will be expired."
138 :group 'gnus-score-expire
139 :type '(choice (const :tag "never" nil)
142 (defcustom gnus-update-score-entry-dates t
143 "*In non-nil, update matching score entry dates.
144 If this variable is nil, then score entries that provide matches
145 will be expired along with non-matching score entries."
146 :group 'gnus-score-expire
149 (defcustom gnus-decay-scores nil
150 "*If non-nil, decay non-permanent scores.
152 If it is a regexp, only decay score files matching regexp."
153 :group 'gnus-score-decay
154 :type `(choice (const :tag "never" nil)
155 (const :tag "always" t)
156 (const :tag "adaptive score files"
157 ,(concat "\\." gnus-adaptive-file-suffix "\\'"))
160 (defcustom gnus-decay-score-function 'gnus-decay-score
161 "*Function called to decay a score.
162 It is called with one parameter -- the score to be decayed."
163 :group 'gnus-score-decay
164 :type '(radio (function-item gnus-decay-score)
165 (function :tag "Other")))
167 (defcustom gnus-score-decay-constant 3
168 "*Decay all \"small\" scores with this amount."
169 :group 'gnus-score-decay
172 (defcustom gnus-score-decay-scale .05
173 "*Decay all \"big\" scores with this factor."
174 :group 'gnus-score-decay
177 (defcustom gnus-home-score-file nil
178 "Variable to control where interactive score entries are to go.
182 This file file will be used as the home score file.
185 The result of this function will be used as the home score file.
186 The function will be passed the name of the group as its
190 The elements in this list can be:
192 * `(regexp file-name ...)'
193 If the `regexp' matches the group name, the first `file-name' will
194 will be used as the home score file. (Multiple filenames are
195 allowed so that one may use gnus-score-file-single-match-alist to
199 If the function returns non-nil, the result will be used
200 as the home score file. The function will be passed the
201 name of the group as its parameter.
203 * A string. Use the string as the home score file.
205 The list will be traversed from the beginning towards the end looking
207 :group 'gnus-score-files
208 :type '(choice string
209 (repeat (choice string
210 (cons regexp (repeat file))
211 (function :value fun)))
212 (function-item gnus-hierarchial-home-score-file)
213 (function-item gnus-current-home-score-file)
214 (function :value fun)))
216 (defcustom gnus-home-adapt-file nil
217 "Variable to control where new adaptive score entries are to go.
218 This variable allows the same syntax as `gnus-home-score-file'."
219 :group 'gnus-score-adapt
220 :group 'gnus-score-files
221 :type '(choice string
222 (repeat (choice string
223 (cons regexp (repeat file))
224 (function :value fun)))
225 (function :value fun)))
227 (defcustom gnus-default-adaptive-score-alist
228 '((gnus-kill-file-mark)
230 (gnus-read-mark (from 3) (subject 30))
231 (gnus-catchup-mark (subject -10))
232 (gnus-killed-mark (from -1) (subject -20))
233 (gnus-del-mark (from -2) (subject -15)))
234 "*Alist of marks and scores."
235 :group 'gnus-score-adapt
236 :type '(repeat (cons (symbol :tag "Mark")
237 (repeat (list (choice :tag "Header"
240 (symbol :tag "other"))
241 (integer :tag "Score"))))))
243 (defcustom gnus-adaptive-word-length-limit nil
244 "*Words of a length lesser than this limit will be ignored when doing adaptive scoring."
246 :group 'gnus-score-adapt
247 :type '(radio (const :format "Unlimited " nil)
248 (integer :format "Maximum length: %v")))
250 (defcustom gnus-ignored-adaptive-words nil
251 "List of words to be ignored when doing adaptive word scoring."
252 :group 'gnus-score-adapt
253 :type '(repeat string))
255 (defcustom gnus-default-ignored-adaptive-words
256 '("a" "i" "the" "to" "of" "and" "in" "is" "it" "for" "that" "if" "you"
257 "this" "be" "on" "with" "not" "have" "are" "or" "as" "from" "can"
258 "but" "by" "at" "an" "will" "no" "all" "was" "do" "there" "my" "one"
259 "so" "we" "they" "what" "would" "any" "which" "about" "get" "your"
260 "use" "some" "me" "then" "name" "like" "out" "when" "up" "time"
261 "other" "more" "only" "just" "end" "also" "know" "how" "new" "should"
262 "been" "than" "them" "he" "who" "make" "may" "people" "these" "now"
263 "their" "here" "into" "first" "could" "way" "had" "see" "work" "well"
264 "were" "two" "very" "where" "while" "us" "because" "good" "same"
265 "even" "much" "most" "many" "such" "long" "his" "over" "last" "since"
266 "right" "before" "our" "without" "too" "those" "why" "must" "part"
267 "being" "current" "back" "still" "go" "point" "value" "each" "did"
268 "both" "true" "off" "say" "another" "state" "might" "under" "start"
270 "*Default list of words to be ignored when doing adaptive word scoring."
271 :group 'gnus-score-adapt
272 :type '(repeat string))
274 (defcustom gnus-default-adaptive-word-score-alist
275 `((,gnus-read-mark . 30)
276 (,gnus-catchup-mark . -10)
277 (,gnus-killed-mark . -20)
278 (,gnus-del-mark . -15))
279 "*Alist of marks and scores."
280 :group 'gnus-score-adapt
281 :type '(repeat (cons (character :tag "Mark")
282 (integer :tag "Score"))))
284 (defcustom gnus-adaptive-word-minimum nil
285 "If a number, this is the minimum score value that can be assigned to a word."
286 :group 'gnus-score-adapt
287 :type '(choice (const nil) integer))
289 (defcustom gnus-adaptive-word-no-group-words nil
290 "If t, don't adaptively score words included in the group name."
291 :group 'gnus-score-adapt
294 (defcustom gnus-score-mimic-keymap nil
295 "*Have the score entry functions pretend that they are a keymap."
296 :group 'gnus-score-default
299 (defcustom gnus-score-exact-adapt-limit 10
300 "*Number that says how long a match has to be before using substring matching.
301 When doing adaptive scoring, one normally uses fuzzy or substring
302 matching. However, if the header one matches is short, the possibility
303 for false positives is great, so if the length of the match is less
304 than this variable, exact matching will be used.
306 If this variable is nil, exact matching will always be used."
307 :group 'gnus-score-adapt
308 :type '(choice (const nil) integer))
310 (defcustom gnus-score-uncacheable-files "ADAPT$"
311 "All score files that match this regexp will not be cached."
312 :group 'gnus-score-adapt
313 :group 'gnus-score-files
316 (defcustom gnus-adaptive-pretty-print nil
317 "If non-nil, adaptive score files fill are pretty printed."
318 :group 'gnus-score-files
319 :group 'gnus-score-adapt
320 :version "23.0" ;; No Gnus
323 (defcustom gnus-score-default-header nil
324 "Default header when entering new scores.
326 Should be one of the following symbols.
335 e: `extra' (non-standard overview)
340 If nil, the user will be asked for a header."
341 :group 'gnus-score-default
342 :type '(choice (const :tag "from" a)
343 (const :tag "subject" s)
344 (const :tag "body" b)
345 (const :tag "head" h)
346 (const :tag "message-id" i)
347 (const :tag "references" t)
348 (const :tag "xref" x)
349 (const :tag "extra" e)
350 (const :tag "lines" l)
351 (const :tag "date" d)
352 (const :tag "followup" f)
353 (const :tag "ask" nil)))
355 (defcustom gnus-score-default-type nil
356 "Default match type when entering new scores.
358 Should be one of the following symbols.
368 >: greater than number
371 If nil, the user will be asked for a match type."
372 :group 'gnus-score-default
373 :type '(choice (const :tag "substring" s)
374 (const :tag "exact string" e)
375 (const :tag "fuzzy string" f)
376 (const :tag "regexp string" r)
377 (const :tag "before date" b)
378 (const :tag "after date" a)
379 (const :tag "this date" n)
380 (const :tag "less than number" <)
381 (const :tag "greater than number" >)
382 (const :tag "equal than number" =)
383 (const :tag "ask" nil)))
385 (defcustom gnus-score-default-fold nil
386 "Use case folding for new score file entries iff not nil."
387 :group 'gnus-score-default
390 (defcustom gnus-score-default-duration nil
391 "Default duration of effect when entering new scores.
393 Should be one of the following symbols.
399 If nil, the user will be asked for a duration."
400 :group 'gnus-score-default
401 :type '(choice (const :tag "temporary" t)
402 (const :tag "permanent" p)
403 (const :tag "immediate" i)
404 (const :tag "ask" nil)))
406 (defcustom gnus-score-after-write-file-function nil
407 "Function called with the name of the score file just written to disk."
408 :group 'gnus-score-files
409 :type '(choice (const nil) function))
411 (defcustom gnus-score-thread-simplify nil
412 "If non-nil, subjects will simplified as in threading."
413 :group 'gnus-score-various
418 ;; Internal variables.
420 (defvar gnus-score-use-all-scores t
421 "If nil, only `gnus-score-find-score-files-function' is used.")
423 (defvar gnus-adaptive-word-syntax-table
424 (let ((table (copy-syntax-table (standard-syntax-table)))
425 (numbers '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)))
427 (modify-syntax-entry (pop numbers) " " table))
428 (modify-syntax-entry ?' "w" table)
430 "Syntax table used when doing adaptive word scoring.")
432 (defvar gnus-scores-exclude-files nil)
433 (defvar gnus-internal-global-score-files nil)
434 (defvar gnus-score-file-list nil)
436 (defvar gnus-short-name-score-file-cache nil)
438 (defvar gnus-score-help-winconf nil)
439 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
440 (defvar gnus-adaptive-word-score-alist gnus-default-adaptive-word-score-alist)
441 (defvar gnus-score-trace nil)
442 (defvar gnus-score-edit-buffer nil)
444 (defvar gnus-score-alist nil
445 "Alist containing score information.
446 The keys can be symbols or strings. The following symbols are defined.
448 touched: If this alist has been modified.
449 mark: Automatically mark articles below this.
450 expunge: Automatically expunge articles below this.
451 files: List of other score files to load when loading this one.
452 eval: Sexp to be evaluated when the score file is loaded.
454 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
455 where HEADER is the header being scored, MATCH is the string we are
456 looking for, TYPE is a flag indicating whether it should use regexp or
457 substring matching, SCORE is the score to add and DATE is the date
458 of the last successful match.")
460 (defvar gnus-score-cache nil)
461 (defvar gnus-scores-articles nil)
462 (defvar gnus-score-index nil)
465 (defconst gnus-header-index
466 ;; Name to index alist.
468 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
472 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
476 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
480 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
484 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
488 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
492 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
496 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
500 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
503 ;; ("extra" 16 gnus-score-string)
504 ("extra" -1 gnus-score-body)
505 ("head" -1 gnus-score-body)
506 ("body" -1 gnus-score-body)
507 ("all" -1 gnus-score-body)
509 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
513 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
517 ;;; Summary mode score maps.
519 (gnus-define-keys (gnus-summary-score-map "V" gnus-summary-mode-map)
520 "s" gnus-summary-set-score
521 "S" gnus-summary-current-score
522 "c" gnus-score-change-score-file
523 "C" gnus-score-customize
524 "m" gnus-score-set-mark-below
525 "x" gnus-score-set-expunge-below
526 "R" gnus-summary-rescore
527 "e" gnus-score-edit-current-scores
528 "f" gnus-score-edit-file
529 "F" gnus-score-flush-cache
530 "t" gnus-score-find-trace
531 "w" gnus-score-find-favourite-words)
533 ;; Summary score file commands
535 ;; Much modification of the kill (ahem, score) code and lots of the
536 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
538 (defun gnus-summary-lower-score (&optional score symp)
539 "Make a score entry based on the current article.
540 The user will be prompted for header to score on, match type,
541 permanence, and the string to be used. The numerical prefix will be
542 used as score. A symbolic prefix of `a' says to use the `all.SCORE'
543 file for the command instead of the current score file."
544 (interactive (gnus-interactive "P\ny"))
545 (gnus-summary-increase-score (- (gnus-score-delta-default score)) symp))
547 (defun gnus-score-kill-help-buffer ()
548 (when (get-buffer "*Score Help*")
549 (kill-buffer "*Score Help*")
550 (when gnus-score-help-winconf
551 (set-window-configuration gnus-score-help-winconf))))
553 (defun gnus-summary-increase-score (&optional score symp)
554 "Make a score entry based on the current article.
555 The user will be prompted for header to score on, match type,
556 permanence, and the string to be used. The numerical prefix will be
557 used as score. A symbolic prefix of `a' says to use the `all.SCORE'
558 file for the command instead of the current score file."
559 (interactive (gnus-interactive "P\ny"))
560 (let* ((nscore (gnus-score-delta-default score))
561 (prefix (if (< nscore 0) ?L ?I))
562 (increase (> nscore 0))
564 '((?a "from" nil nil string)
565 (?s "subject" nil nil string)
566 (?b "body" "" nil body-string)
567 (?h "head" "" nil body-string)
568 (?i "message-id" nil nil string)
569 (?r "references" "message-id" nil string)
570 (?x "xref" nil nil string)
571 (?e "extra" nil nil string)
572 (?l "lines" nil nil number)
573 (?d "date" nil nil date)
574 (?f "followup" nil nil string)
575 (?t "thread" "message-id" nil string)))
577 '((?s s "substring" string)
578 (?e e "exact string" string)
579 (?f f "fuzzy string" string)
580 (?r r "regexp string" string)
581 (?z s "substring" body-string)
582 (?p r "regexp string" body-string)
583 (?b before "before date" date)
584 (?a after "after date" date)
585 (?n at "this date" date)
586 (?< < "less than number" number)
587 (?> > "greater than number" number)
588 (?= = "equal to number" number)))
589 (current-score-file gnus-current-score-file)
591 (list (list ?t (current-time-string) "temporary")
592 '(?p perm "permanent") '(?i now "immediate")))
593 (mimic gnus-score-mimic-keymap)
594 (hchar (and gnus-score-default-header
595 (aref (symbol-name gnus-score-default-header) 0)))
596 (tchar (and gnus-score-default-type
597 (aref (symbol-name gnus-score-default-type) 0)))
598 (pchar (and gnus-score-default-duration
599 (aref (symbol-name gnus-score-default-duration) 0)))
600 entry temporary type match extra)
605 ;; First we read the header to score.
610 (message "%c-" prefix))
611 (message "%s header (%s?): " (if increase "Increase" "Lower")
612 (mapconcat (lambda (s) (char-to-string (car s)))
614 (setq hchar (read-char))
615 (when (or (= hchar ??) (= hchar ?\C-h))
617 (gnus-score-insert-help "Match on header" char-to-header 1)))
619 (gnus-score-kill-help-buffer)
620 (unless (setq entry (assq (downcase hchar) char-to-header))
621 (if mimic (error "%c %c" prefix hchar)
622 (error "Invalid header type")))
624 (when (/= (downcase hchar) hchar)
625 ;; This was a majuscule, so we end reading and set the defaults.
626 (if mimic (message "%c %c" prefix hchar) (message ""))
627 (setq tchar (or tchar ?s)
628 pchar (or pchar ?t)))
633 (if (eq (nth 4 entry)
637 ;; We continue reading - the type.
641 (sit-for 1) (message "%c %c-" prefix hchar))
642 (message "%s header '%s' with match type (%s?): "
643 (if increase "Increase" "Lower")
645 (mapconcat (lambda (s) (char-to-string (car s)))
647 (setq tchar (read-char))
648 (when (or (= tchar ??) (= tchar ?\C-h))
650 (gnus-score-insert-help "Match type" legal-types 2)))
652 (gnus-score-kill-help-buffer)
653 (unless (setq type (nth 1 (assq (downcase tchar) legal-types)))
654 (if mimic (error "%c %c" prefix hchar)
655 (error "Invalid match type"))))
657 (when (/= (downcase tchar) tchar)
658 ;; It was a majuscule, so we end reading and use the default.
659 (if mimic (message "%c %c %c" prefix hchar tchar)
661 (setq pchar (or pchar ?t)))
663 ;; We continue reading.
667 (sit-for 1) (message "%c %c %c-" prefix hchar tchar))
668 (message "%s permanence (%s?): " (if increase "Increase" "Lower")
669 (mapconcat (lambda (s) (char-to-string (car s)))
671 (setq pchar (read-char))
672 (when (or (= pchar ??) (= pchar ?\C-h))
674 (gnus-score-insert-help "Match permanence" char-to-perm 2)))
676 (gnus-score-kill-help-buffer)
677 (if mimic (message "%c %c %c %c" prefix hchar tchar pchar)
679 (unless (setq temporary (cadr (assq pchar char-to-perm)))
680 ;; Deal with der(r)ided superannuated paradigms.
681 (when (and (eq (1+ prefix) 77)
682 (eq (+ hchar 12) 109)
684 (eq (- pchar 4) 111))
687 (error "%c %c %c %c" prefix hchar tchar pchar)
688 (error "Invalid match duration"))))
689 ;; Always kill the score help buffer.
690 (gnus-score-kill-help-buffer))
692 ;; If scoring an extra (non-standard overview) header,
693 ;; we must find out which header is in question.
695 (and gnus-extra-headers
696 (equal (nth 1 entry) "extra")
697 (intern ; need symbol
698 (gnus-completing-read-with-default
699 (symbol-name (car gnus-extra-headers)) ; default response
700 "Score extra header" ; prompt
701 (mapcar (lambda (x) ; completion list
702 (cons (symbol-name x) x))
704 nil ; no completion limit
705 t)))) ; require match
706 ;; extra is now nil or a symbol.
708 ;; We have all the data, so we enter this score.
709 (setq match (if (string= (nth 2 entry) "") ""
710 (gnus-summary-header (or (nth 2 entry) (nth 1 entry))
713 ;; Modify the match, perhaps.
715 ((equal (nth 1 entry) "xref")
716 (when (string-match "^Xref: *" match)
717 (setq match (substring match (match-end 0))))
718 (when (string-match "^[^:]* +" match)
719 (setq match (substring match (match-end 0))))))
721 (when (memq type '(r R regexp Regexp))
722 (setq match (regexp-quote match)))
724 ;; Change score file to the "all.SCORE" file.
727 (set-buffer gnus-summary-buffer)
728 (gnus-score-load-file
729 ;; This is a kludge; yes...
731 ((eq gnus-score-find-score-files-function
732 'gnus-score-find-hierarchical)
733 (gnus-score-file-name ""))
734 ((eq gnus-score-find-score-files-function 'gnus-score-find-single)
737 (gnus-score-file-name "all"))))))
739 (gnus-summary-score-entry
740 (nth 1 entry) ; Header
743 (if (eq score 's) nil score) ; Score
744 (if (eq temporary 'perm) ; Temp
747 (not (nth 3 entry)) ; Prompt
749 extra) ; non-standard overview.
752 ;; We change the score file back to the previous one.
754 (set-buffer gnus-summary-buffer)
755 (gnus-score-load-file current-score-file)))))
757 (defun gnus-score-insert-help (string alist idx)
758 (setq gnus-score-help-winconf (current-window-configuration))
760 (set-buffer (gnus-get-buffer-create "*Score Help*"))
761 (buffer-disable-undo)
762 (delete-windows-on (current-buffer))
764 (insert string ":\n\n")
769 ;; find the longest string to display
771 (setq n (length (nth idx (car list))))
774 (setq list (cdr list)))
775 (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
776 (setq n (/ (1- (window-width)) max)) ; items per line
777 (setq width (/ (1- (window-width)) n)) ; width of each item
778 ;; insert `n' items, each in a field of width `width'
783 (delete-char -1) ; the `\n' takes a char
785 (setq pad (- width 3))
786 (setq format (concat "%c: %-" (int-to-string pad) "s"))
787 (insert (format format (caar alist) (nth idx (car alist))))
788 (setq alist (cdr alist))
790 (goto-char (point-min))
791 ;; display ourselves in a small window at the bottom
792 (gnus-select-lowest-window)
793 (if (< (/ (window-height) 2) window-min-height)
794 (switch-to-buffer "*Score Help*")
796 (pop-to-buffer "*Score Help*"))
797 (let ((window-min-height 1))
798 (shrink-window-if-larger-than-buffer))
799 (select-window (gnus-get-buffer-window gnus-summary-buffer t))))
801 (defun gnus-summary-header (header &optional no-err extra)
802 ;; Return HEADER for current articles, or error.
803 (let ((article (gnus-summary-article-number))
806 (if (and (setq headers (gnus-summary-article-header article))
808 (if extra ; `header' must be "extra"
809 (or (cdr (assq extra (mail-header-extra headers))) "")
810 (aref headers (nth 1 (assoc header gnus-header-index))))
813 (error "Pseudo-articles can't be scored")))
815 (error "No article on current line")
818 (defun gnus-newsgroup-score-alist ()
820 (let ((param-file (gnus-group-find-parameter
821 gnus-newsgroup-name 'score-file)))
823 (gnus-score-load param-file)))
825 (gnus-score-file-name gnus-newsgroup-name)))
828 (defsubst gnus-score-get (symbol &optional alist)
829 ;; Get SYMBOL's definition in ALIST.
833 (gnus-newsgroup-score-alist)))))
835 (defun gnus-summary-score-entry (header match type score date
836 &optional prompt silent extra)
837 "Enter score file entry.
838 HEADER is the header being scored.
839 MATCH is the string we are looking for.
840 TYPE is the match type: substring, regexp, exact, fuzzy.
841 SCORE is the score to add.
842 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
843 If optional argument `PROMPT' is non-nil, allow user to edit match.
844 If optional argument `SILENT' is nil, show effect of score entry.
845 If optional argument `EXTRA' is non-nil, it's a non-standard overview header."
846 ;; Regexp is the default type.
849 ;; Simplify matches...
850 (cond ((or (eq type 'r) (eq type 's) (eq type nil))
851 (setq match (if match (gnus-simplify-subject-re match) "")))
853 (setq match (gnus-simplify-subject-fuzzy match))))
854 (let ((score (gnus-score-delta-default score))
855 (header (downcase header))
857 (set-text-properties 0 (length header) nil header)
859 (setq match (read-string
860 (format "Match %s on %s, %s: "
861 (cond ((eq date 'now)
867 (if (< score 0) "lower" "raise"))
869 (int-to-string match)
872 ;; If this is an integer comparison, we transform from string to int.
873 (if (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
875 (setq match (string-to-number match)))
876 (set-text-properties 0 (length match) nil match))
878 (unless (eq date 'now)
879 ;; Add the score entry to the score file.
880 (when (= score gnus-score-interactive-default-score)
882 (let ((old (gnus-score-get header))
888 (and date (if (numberp date) date
890 type (symbol-name extra)))
893 (and date (if (numberp date) date
896 (date (list match score (date-to-day date)))
897 (score (list match score))
899 ;; We see whether we can collapse some score entries.
900 ;; This isn't quite correct, because there may be more elements
901 ;; later on with the same key that have matching elems... Hm.
903 (setq elem (assoc match old))
904 (eq (nth 3 elem) (nth 3 new))
905 (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
906 (and (not (nth 2 elem)) (not (nth 2 new)))))
907 ;; Yup, we just add this new score to the old elem.
908 (setcar (cdr elem) (+ (or (nth 1 elem)
909 gnus-score-interactive-default-score)
911 gnus-score-interactive-default-score)))
912 ;; Nope, we have to add a new elem.
913 (gnus-score-set header (if old (cons new old) (list new)) nil t))
914 (gnus-score-set 'touched '(t))))
916 ;; Score the current buffer.
918 (if (and (>= (nth 1 (assoc header gnus-header-index)) 0)
919 (eq (nth 2 (assoc header gnus-header-index))
921 (gnus-summary-score-effect header match type score extra)
922 (gnus-summary-rescore)))
924 ;; Return the new scoring rule.
927 (defun gnus-summary-score-effect (header match type score &optional extra)
928 "Simulate the effect of a score file entry.
929 HEADER is the header being scored.
930 MATCH is the string we are looking for.
931 TYPE is the score type.
932 SCORE is the score to add.
933 EXTRA is the possible non-standard header."
934 (interactive (list (completing-read "Header: "
936 (lambda (x) (fboundp (nth 2 x)))
938 (read-string "Match: ")
939 (if (y-or-n-p "Use regexp match? ") 'r 's)
940 (string-to-number (read-string "Score: "))))
942 (unless (and (stringp match) (> (length match) 0))
944 (goto-char (point-min))
945 (let ((regexp (cond ((eq type 'f)
946 (gnus-simplify-subject-fuzzy match))
950 (concat "\\`" (regexp-quote match) "\\'"))
952 (regexp-quote match)))))
954 (let ((content (gnus-summary-header header 'noerr extra))
955 (case-fold-search t))
957 (when (if (eq type 'f)
958 (string-equal (gnus-simplify-subject-fuzzy content)
960 (string-match regexp content))
961 (gnus-summary-raise-score score))))
962 (beginning-of-line 2))))
963 (gnus-set-mode-line 'summary))
965 (defun gnus-summary-score-crossposting (score date)
966 ;; Enter score file entry for current crossposting.
967 ;; SCORE is the score to add.
968 ;; DATE is the expire date.
969 (let ((xref (gnus-summary-header "xref"))
973 (error "This article is not crossposted"))
974 (while (string-match " \\([^ \t]+\\):" xref start)
975 (setq start (match-end 0))
978 (substring xref (match-beginning 1) (match-end 1)))
979 gnus-newsgroup-name))
980 (gnus-summary-score-entry
981 "xref" (concat " " group ":") nil score date t)))))
988 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
990 (defun gnus-score-set-mark-below (score)
991 "Automatically mark articles with score below SCORE as read."
993 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
994 (string-to-number (read-string "Mark below: ")))))
995 (setq score (or score gnus-summary-default-score 0))
996 (gnus-score-set 'mark (list score))
997 (gnus-score-set 'touched '(t))
998 (setq gnus-summary-mark-below score)
999 (gnus-score-update-lines))
1001 (defun gnus-score-update-lines ()
1002 "Update all lines in the summary buffer."
1004 (goto-char (point-min))
1006 (gnus-summary-update-line)
1009 (defun gnus-score-update-all-lines ()
1010 "Update all lines in the summary buffer, even the hidden ones."
1012 (goto-char (point-min))
1015 (when (gnus-summary-show-thread)
1016 (push (point) hidden))
1017 (gnus-summary-update-line)
1019 ;; Re-hide the hidden threads.
1021 (goto-char (pop hidden))
1022 (gnus-summary-hide-thread)))))
1024 (defun gnus-score-set-expunge-below (score)
1025 "Automatically expunge articles with score below SCORE."
1027 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
1028 (string-to-number (read-string "Set expunge below: ")))))
1029 (setq score (or score gnus-summary-default-score 0))
1030 (gnus-score-set 'expunge (list score))
1031 (gnus-score-set 'touched '(t)))
1033 (defun gnus-score-followup-article (&optional score)
1034 "Add SCORE to all followups to the article in the current buffer."
1036 (setq score (gnus-score-delta-default score))
1037 (when (gnus-buffer-live-p gnus-summary-buffer)
1040 (message-narrow-to-headers)
1041 (let ((id (mail-fetch-field "message-id")))
1043 (set-buffer gnus-summary-buffer)
1044 (gnus-summary-score-entry
1045 "references" (concat id "[ \t]*$") 'r
1046 score (current-time-string) nil t)))))))
1048 (defun gnus-score-followup-thread (&optional score)
1049 "Add SCORE to all later articles in the thread the current buffer is part of."
1051 (setq score (gnus-score-delta-default score))
1052 (when (gnus-buffer-live-p gnus-summary-buffer)
1055 (goto-char (point-min))
1056 (let ((id (mail-fetch-field "message-id")))
1058 (set-buffer gnus-summary-buffer)
1059 (gnus-summary-score-entry
1061 score (current-time-string))))))))
1063 (defun gnus-score-set (symbol value &optional alist warn)
1064 ;; Set SYMBOL to VALUE in ALIST.
1068 (gnus-newsgroup-score-alist)))
1069 (entry (assoc symbol alist)))
1070 (cond ((gnus-score-get 'read-only alist)
1071 ;; This is a read-only score file, so we do nothing.
1073 (gnus-message 4 "Note: read-only score file; entry discarded")))
1075 (setcdr entry value))
1077 (error "Empty alist"))
1080 (cons (cons symbol value) (cdr alist)))))))
1082 (defun gnus-summary-raise-score (n)
1083 "Raise the score of the current article by N."
1085 (gnus-summary-set-score (+ (gnus-summary-article-score)
1086 (or n gnus-score-interactive-default-score ))))
1088 (defun gnus-summary-set-score (n)
1089 "Set the score of the current article to N."
1092 (gnus-summary-show-thread)
1093 (let ((buffer-read-only nil))
1095 (gnus-summary-update-mark
1096 (if (= n (or gnus-summary-default-score 0)) ? ;Whitespace
1097 (if (< n (or gnus-summary-default-score 0))
1098 gnus-score-below-mark gnus-score-over-mark))
1100 (let* ((article (gnus-summary-article-number))
1101 (score (assq article gnus-newsgroup-scored)))
1102 (if score (setcdr score n)
1103 (push (cons article n) gnus-newsgroup-scored)))
1104 (gnus-summary-update-line)))
1106 (defun gnus-summary-current-score ()
1107 "Return the score of the current article."
1109 (gnus-message 1 "%s" (gnus-summary-article-score)))
1111 (defun gnus-score-change-score-file (file)
1112 "Change current score alist."
1114 (list (read-file-name "Change to score file: " gnus-kill-files-directory)))
1115 (gnus-score-load-file file)
1116 (gnus-set-mode-line 'summary))
1118 (defvar gnus-score-edit-exit-function)
1119 (defun gnus-score-edit-current-scores (file)
1120 "Edit the current score alist."
1121 (interactive (list gnus-current-score-file))
1122 (if (not gnus-current-score-file)
1123 (error "No current score file")
1124 (let ((winconf (current-window-configuration)))
1125 (when (buffer-name gnus-summary-buffer)
1127 (gnus-make-directory (file-name-directory file))
1128 (setq gnus-score-edit-buffer (find-file-noselect file))
1129 (gnus-configure-windows 'edit-score)
1131 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1132 (make-local-variable 'gnus-prev-winconf)
1133 (setq gnus-prev-winconf winconf))
1135 4 (substitute-command-keys
1136 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits"))))
1138 (defun gnus-score-edit-all-score ()
1139 "Edit the all.SCORE file."
1141 (find-file (gnus-score-file-name "all"))
1143 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1145 4 (substitute-command-keys
1146 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
1148 (defun gnus-score-edit-file (file)
1149 "Edit a score file."
1151 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
1152 (gnus-make-directory (file-name-directory file))
1153 (when (buffer-name gnus-summary-buffer)
1155 (let ((winconf (current-window-configuration)))
1156 (setq gnus-score-edit-buffer (find-file-noselect file))
1157 (gnus-configure-windows 'edit-score)
1159 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1160 (make-local-variable 'gnus-prev-winconf)
1161 (setq gnus-prev-winconf winconf))
1163 4 (substitute-command-keys
1164 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
1166 (defun gnus-score-edit-file-at-point (&optional format)
1167 "Edit score file at point in Score Trace buffers.
1168 If FORMAT, also format the current score file."
1169 (let* ((rule (save-excursion
1171 (read (current-buffer))))
1173 ;; Must be synced with `gnus-score-find-trace':
1175 (file (save-excursion
1177 (if (and (re-search-backward reg (point-at-bol) t)
1178 (re-search-forward reg (point-at-eol) t))
1179 (buffer-substring (point) (point-at-eol))
1182 (string-match "\\<\\(non-file rule\\|A file\\)\\>" file)
1183 ;; (see `gnus-score-find-trace' and `gnus-score-advanced')
1185 (gnus-error 3 "Can't find a score file in current line.")
1186 (gnus-score-edit-file file)
1188 (gnus-score-pretty-print))
1189 (when (consp rule) ;; the rule exists
1190 (setq rule (mapconcat #'(lambda (obj)
1191 (regexp-quote (format "%S" obj)))
1194 (goto-char (point-min))
1195 (re-search-forward rule nil t)
1196 ;; make it easy to use `kill-sexp':
1197 (goto-char (1- (match-beginning 0)))))))
1199 (defun gnus-score-load-file (file)
1200 ;; Load score file FILE. Returns a list a retrieved score-alists.
1201 (let* ((file (expand-file-name
1202 (or (and (string-match
1203 (concat "^" (regexp-quote
1205 gnus-kill-files-directory)))
1206 (expand-file-name file))
1208 (expand-file-name file gnus-kill-files-directory))))
1209 (cached (assoc file gnus-score-cache))
1210 (global (member file gnus-internal-global-score-files))
1213 ;; The score file was already loaded.
1214 (setq alist (cdr cached))
1215 ;; We load the score file.
1216 (setq gnus-score-alist nil)
1217 (setq alist (gnus-score-load-score-alist file))
1218 ;; We add '(touched) to the alist to signify that it hasn't been
1220 (unless (assq 'touched alist)
1221 (push (list 'touched nil) alist))
1222 ;; If it is a global score file, we make it read-only.
1224 (not (assq 'read-only alist))
1225 (push (list 'read-only t) alist))
1226 (push (cons file alist) gnus-score-cache))
1230 ;; Downcase all header names.
1233 (setcar (car a) (downcase (caar a)))
1235 ;; Advanced scoring.
1239 ;; If there are actual scores in the alist, we add it to the
1240 ;; return value of this function.
1242 (setq lists (list alist))))
1243 ;; Treat the other possible atoms in the score alist.
1244 (let ((mark (car (gnus-score-get 'mark alist)))
1245 (expunge (car (gnus-score-get 'expunge alist)))
1246 (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
1247 (files (gnus-score-get 'files alist))
1248 (exclude-files (gnus-score-get 'exclude-files alist))
1249 (orphan (car (gnus-score-get 'orphan alist)))
1250 (adapt (gnus-score-get 'adapt alist))
1251 (thread-mark-and-expunge
1252 (car (gnus-score-get 'thread-mark-and-expunge alist)))
1253 (adapt-file (car (gnus-score-get 'adapt-file alist)))
1254 (local (gnus-score-get 'local alist))
1255 (decay (car (gnus-score-get 'decay alist)))
1256 (eval (car (gnus-score-get 'eval alist))))
1257 ;; Perform possible decays.
1258 (when (and (if (stringp gnus-decay-scores)
1259 (string-match gnus-decay-scores file)
1261 (or cached (file-exists-p file))
1263 (gnus-decay-scores alist decay)))
1264 (gnus-score-set 'touched '(t) alist)
1265 (gnus-score-set 'decay (list (time-to-days (current-time))) alist))
1266 ;; We do not respect eval and files atoms from global score
1268 (when (and files (not global))
1269 (setq lists (apply 'append lists
1270 (mapcar 'gnus-score-load-file
1271 (if adapt-file (cons adapt-file files)
1273 (when (and eval (not global))
1275 ;; We then expand any exclude-file directives.
1276 (setq gnus-scores-exclude-files
1283 (expand-file-name sfile (file-name-directory file))
1284 (expand-file-name sfile gnus-kill-files-directory)))
1286 gnus-scores-exclude-files))
1289 (set-buffer gnus-summary-buffer)
1291 (and (consp (car local))
1292 (symbolp (caar local))
1294 (make-local-variable (caar local))
1295 (set (caar local) (nth 1 (car local)))))
1296 (setq local (cdr local)))))
1298 (setq gnus-orphan-score orphan))
1299 (setq gnus-adaptive-score-alist
1300 (cond ((equal adapt '(t))
1301 (setq gnus-newsgroup-adaptive t)
1302 gnus-default-adaptive-score-alist)
1303 ((equal adapt '(ignore))
1304 (setq gnus-newsgroup-adaptive nil))
1306 (setq gnus-newsgroup-adaptive t)
1309 gnus-default-adaptive-score-alist)))
1310 (setq gnus-thread-expunge-below
1311 (or thread-mark-and-expunge gnus-thread-expunge-below))
1312 (setq gnus-summary-mark-below
1313 (or mark mark-and-expunge gnus-summary-mark-below))
1314 (setq gnus-summary-expunge-below
1315 (or expunge mark-and-expunge gnus-summary-expunge-below))
1316 (setq gnus-newsgroup-adaptive-score-file
1317 (or adapt-file gnus-newsgroup-adaptive-score-file)))
1318 (setq gnus-current-score-file file)
1319 (setq gnus-score-alist alist)
1322 (defun gnus-score-load (file)
1324 (let ((cache (assoc file gnus-score-cache)))
1326 (setq gnus-score-alist (cdr cache))
1327 (setq gnus-score-alist nil)
1328 (gnus-score-load-score-alist file)
1329 (unless gnus-score-alist
1330 (setq gnus-score-alist (copy-alist '((touched nil)))))
1331 (push (cons file gnus-score-alist) gnus-score-cache))))
1333 (defun gnus-score-remove-from-cache (file)
1334 (setq gnus-score-cache
1335 (delq (assoc file gnus-score-cache) gnus-score-cache)))
1337 (defun gnus-score-load-score-alist (file)
1340 (if (not (file-readable-p file))
1341 ;; Couldn't read file.
1342 (setq gnus-score-alist nil)
1345 (insert-file-contents-as-coding-system
1346 score-mode-coding-system file)
1347 (goto-char (point-min))
1348 ;; Only do the loading if the score file isn't empty.
1349 (when (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
1352 (read (current-buffer))
1354 (gnus-error 3.2 "Problem with score file %s" file))))))
1358 ;; Bogus score file.
1359 (error "Invalid syntax with score file %s" file))
1360 ((eq (car alist) 'setq)
1361 ;; This is an old-style score file.
1362 (setq gnus-score-alist (gnus-score-transform-old-to-new alist)))
1364 (setq gnus-score-alist alist)))
1365 ;; Check the syntax of the score file.
1366 (setq gnus-score-alist
1367 (gnus-score-check-syntax gnus-score-alist file)))))
1369 (defun gnus-score-check-syntax (alist file)
1370 "Check the syntax of the score ALIST."
1374 ((not (consp alist))
1375 (gnus-message 1 "Score file is not a list: %s" file)
1381 (while (and a (not err))
1385 ((not (listp (car a)))
1386 (format "Invalid score element %s in %s" (car a) file))
1389 ((not (listp (setq sr (cdar a))))
1390 (format "Invalid header match %s in %s" (nth 1 (car a)) file))
1392 (setq type (caar a))
1393 (while (and sr (not err))
1398 ((if (member (downcase type) '("lines" "chars"))
1399 (not (numberp (car s)))
1400 (not (stringp (car s))))
1401 (format "Invalid match %s in %s" (car s) file))
1402 ((and (cadr s) (not (integerp (cadr s))))
1403 (format "Non-integer score %s in %s" (cadr s) file))
1404 ((and (caddr s) (not (integerp (caddr s))))
1405 (format "Non-integer date %s in %s" (caddr s) file))
1406 ((and (cadddr s) (not (symbolp (cadddr s))))
1407 (format "Non-symbol match type %s in %s" (cadddr s) file)))))
1413 (gnus-message 3 err)
1418 (defun gnus-score-transform-old-to-new (alist)
1419 (let* ((alist (nth 2 alist))
1421 (when (eq (car alist) 'quote)
1422 (setq alist (nth 1 alist)))
1424 (setq entry (car alist))
1425 (if (stringp (car entry))
1426 (let ((scor (cdr entry)))
1430 (list (caar scor) (nth 2 (car scor))
1431 (and (nth 3 (car scor))
1432 (date-to-day (nth 3 (car scor))))
1433 (if (nth 1 (car scor)) 'r 's)))
1434 (setq scor (cdr scor))))
1435 (push (if (not (listp (cdr entry)))
1436 (list (car entry) (cdr entry))
1439 (setq alist (cdr alist)))
1440 (cons (list 'touched t) (nreverse out))))
1442 (defun gnus-score-save ()
1443 ;; Save all score information.
1444 (let ((cache gnus-score-cache)
1447 (setq gnus-score-alist nil)
1448 (nnheader-set-temp-buffer " *Gnus Scores*")
1451 (setq entry (pop cache)
1452 file (nnheader-translate-file-chars (car entry) t)
1454 (if (or (not (equal (gnus-score-get 'touched score) '(t)))
1455 (gnus-score-get 'read-only score)
1456 (and (file-exists-p file)
1457 (not (file-writable-p file))))
1459 (setq score (setcdr entry (gnus-delete-alist 'touched score)))
1461 (let (emacs-lisp-mode-hook)
1462 (if (and (not gnus-adaptive-pretty-print)
1464 (concat (regexp-quote gnus-adaptive-file-suffix) "$")
1466 ;; This is an adaptive score file, so we do not run it through
1467 ;; `pp' unless requested. These files can get huge, and are
1468 ;; not meant to be edited by human hands.
1470 ;; This is a normal score file, so we print it very
1472 (let ((lisp-mode-syntax-table score-mode-syntax-table))
1474 (gnus-make-directory (file-name-directory file))
1475 ;; If the score file is empty, we delete it.
1476 (if (zerop (buffer-size))
1478 ;; There are scores, so we write the file.
1479 (when (file-writable-p file)
1480 (gnus-write-buffer-as-coding-system
1481 score-mode-coding-system file)
1482 (when gnus-score-after-write-file-function
1483 (funcall gnus-score-after-write-file-function file)))))
1484 (and gnus-score-uncacheable-files
1485 (string-match gnus-score-uncacheable-files file)
1486 (gnus-score-remove-from-cache file)))
1487 (kill-buffer (current-buffer)))))
1489 (defun gnus-score-load-files (score-files)
1490 "Load all score files in SCORE-FILES."
1491 ;; Load the score files.
1494 (if (stringp (car score-files))
1495 ;; It is a string, which means that it's a score file name,
1496 ;; so we load the score file and add the score alist to
1497 ;; the list of alists.
1498 (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
1499 ;; It is an alist, so we just add it to the list directly.
1500 (setq scores (nconc (car score-files) scores)))
1501 (setq score-files (cdr score-files)))
1502 ;; Prune the score files that are to be excluded, if any.
1503 (when gnus-scores-exclude-files
1507 (and (setq c (rassq (car s) gnus-score-cache))
1508 (member (car c) gnus-scores-exclude-files)
1509 (setq scores (delq (car s) scores)))
1513 (defun gnus-score-headers (score-files &optional trace)
1514 ;; Score `gnus-newsgroup-headers'.
1516 ;; PLM: probably this is not the best place to clear orphan-score
1517 (setq gnus-orphan-score nil
1518 gnus-scores-articles nil
1519 gnus-scores-exclude-files nil
1520 scores (gnus-score-load-files score-files))
1526 (when (and gnus-summary-default-score
1528 (let* ((entries gnus-header-index)
1529 (now (date-to-day (current-time-string)))
1530 (expire (and gnus-score-expiry-days
1531 (- now gnus-score-expiry-days)))
1532 (headers gnus-newsgroup-headers)
1533 (current-score-file gnus-current-score-file)
1535 (gnus-message 7 "Scoring...")
1536 ;; Create articles, an alist of the form `(HEADER . SCORE)'.
1537 (while (setq header (pop headers))
1538 ;; WARNING: The assq makes the function O(N*S) while it could
1539 ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
1540 ;; and S is (length gnus-newsgroup-scored).
1541 (unless (assq (mail-header-number header) gnus-newsgroup-scored)
1542 (setq gnus-scores-articles ;Total of 2 * N cons-cells used.
1543 (cons (cons header (or gnus-summary-default-score 0))
1544 gnus-scores-articles))))
1547 (set-buffer (gnus-get-buffer-create "*Headers*"))
1548 (buffer-disable-undo)
1549 (when (gnus-buffer-live-p gnus-summary-buffer)
1550 (message-clone-locals gnus-summary-buffer))
1552 ;; Set the global variant of this variable.
1553 (setq gnus-current-score-file current-score-file)
1555 (when gnus-orphan-score
1556 (setq gnus-score-index
1557 (nth 1 (assoc "references" gnus-header-index)))
1558 (gnus-score-orphans gnus-orphan-score))
1559 ;; Run each header through the score process.
1561 (setq entry (pop entries)
1562 header (nth 0 entry)
1563 gnus-score-index (nth 1 (assoc header gnus-header-index)))
1564 (when (< 0 (apply 'max (mapcar
1566 (length (gnus-score-get header score)))
1568 ;; Call the scoring function for this type of "header".
1569 (when (setq new (funcall (nth 2 entry) scores header
1572 (when (gnus-buffer-live-p gnus-summary-buffer)
1573 (let ((scored gnus-newsgroup-scored))
1574 (with-current-buffer gnus-summary-buffer
1575 (setq gnus-newsgroup-scored scored))))
1576 ;; Remove the buffer.
1577 (gnus-kill-buffer (current-buffer)))
1579 ;; Add articles to `gnus-newsgroup-scored'.
1580 (while gnus-scores-articles
1581 (when (or (/= gnus-summary-default-score
1582 (cdar gnus-scores-articles))
1584 (push (cons (mail-header-number (caar gnus-scores-articles))
1585 (cdar gnus-scores-articles))
1586 gnus-newsgroup-scored))
1587 (setq gnus-scores-articles (cdr gnus-scores-articles)))
1590 (while (setq score (pop scores))
1592 (when (consp (caar score))
1593 (gnus-score-advanced (car score) trace))
1596 (gnus-message 7 "Scoring...done"))))))
1598 (defun gnus-score-lower-thread (thread score-adjust)
1599 "Lower the score on THREAD with SCORE-ADJUST.
1600 THREAD is expected to contain a list of the form `(PARENT [CHILD1
1601 CHILD2 ...])' where PARENT is a header array and each CHILD is a list
1602 of the same form as THREAD. The empty list nil is valid. For each
1603 article in the tree, the score of the corresponding entry in
1604 `gnus-newsgroup-scored' is adjusted by SCORE-ADJUST."
1606 (let ((head (car thread)))
1608 ;; handle a child and its descendants
1609 (gnus-score-lower-thread head score-adjust)
1610 ;; handle the parent
1611 (let* ((article (mail-header-number head))
1612 (score (assq article gnus-newsgroup-scored)))
1613 (if score (setcdr score (+ (cdr score) score-adjust))
1614 (push (cons article score-adjust) gnus-newsgroup-scored)))))
1615 (setq thread (cdr thread))))
1617 (defun gnus-score-orphans (score)
1619 A root is an article with no references. An orphan is an article
1620 which has references, but is not connected via its references to a
1621 root article. This function finds all the orphans, and adjusts their
1622 score in `gnus-newsgroup-scored' by SCORE."
1623 ;; gnus-make-threads produces a list, where each entry is a "thread"
1624 ;; as described in the gnus-score-lower-thread docs. This function
1625 ;; will be called again (after limiting has been done) if the display
1626 ;; is threaded. It would be nice to somehow save this info and use
1628 (dolist (thread (gnus-make-threads))
1629 (let ((id (aref (car thread) gnus-score-index)))
1630 ;; If the parent of the thread is not a root, lower the score of
1631 ;; it and its descendants. Note that some roots seem to satisfy
1632 ;; (eq id nil) and some (eq id ""); not sure why.
1634 (not (string= id "")))
1635 (gnus-score-lower-thread thread score)))))
1637 (defun gnus-score-integer (scores header now expire &optional trace)
1638 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1642 (setq alist (car scores)
1644 entries (assoc header alist))
1645 (while (cdr entries) ;First entry is the header index.
1646 (let* ((rest (cdr entries))
1648 (match (nth 0 kill))
1649 (type (or (nth 3 kill) '>))
1650 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1653 (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
1654 (eq type '>=) (eq type '=))
1656 (error "Invalid match type: %s" type)))
1657 (articles gnus-scores-articles))
1658 ;; Instead of doing all the clever stuff that
1659 ;; `gnus-score-string' does to minimize searches and stuff,
1660 ;; I will assume that people generally will put so few
1661 ;; matches on numbers that any cleverness will take more
1662 ;; time than one would gain.
1664 (when (funcall match-func
1665 (or (aref (caar articles) gnus-score-index) 0)
1668 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1671 (setcdr (car articles) (+ score (cdar articles))))
1672 (setq articles (cdr articles)))
1673 ;; Update expire date
1674 (cond ((null date)) ;Permanent entry.
1675 ((and found gnus-update-score-entry-dates) ;Match, update date.
1676 (gnus-score-set 'touched '(t) alist)
1677 (setcar (nthcdr 2 kill) now))
1678 ((and expire (< date expire)) ;Old entry, remove.
1679 (gnus-score-set 'touched '(t) alist)
1680 (setcdr entries (cdr rest))
1681 (setq rest entries)))
1682 (setq entries rest)))))
1685 (defun gnus-score-date (scores header now expire &optional trace)
1686 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1687 entries alist match match-func article)
1690 (setq alist (car scores)
1692 entries (assoc header alist))
1693 (while (cdr entries) ;First entry is the header index.
1694 (let* ((rest (cdr entries))
1696 (type (or (nth 3 kill) 'before))
1697 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1700 (articles gnus-scores-articles)
1704 (setq match-func 'string<
1705 match (gnus-date-iso8601 (nth 0 kill))))
1707 (setq match-func 'gnus-string>
1708 match (gnus-date-iso8601 (nth 0 kill))))
1710 (setq match-func 'string=
1711 match (gnus-date-iso8601 (nth 0 kill))))
1713 (setq match-func 'string-match
1714 match (nth 0 kill)))
1715 (t (error "Invalid match type: %s" type)))
1716 ;; Instead of doing all the clever stuff that
1717 ;; `gnus-score-string' does to minimize searches and stuff,
1718 ;; I will assume that people generally will put so few
1719 ;; matches on numbers that any cleverness will take more
1720 ;; time than one would gain.
1721 (while (setq article (pop articles))
1723 (setq l (aref (car article) gnus-score-index))
1724 (funcall match-func match (gnus-date-iso8601 l)))
1726 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1729 (setcdr article (+ score (cdr article)))))
1730 ;; Update expire date
1731 (cond ((null date)) ;Permanent entry.
1732 ((and found gnus-update-score-entry-dates) ;Match, update date.
1733 (gnus-score-set 'touched '(t) alist)
1734 (setcar (nthcdr 2 kill) now))
1735 ((and expire (< date expire)) ;Old entry, remove.
1736 (gnus-score-set 'touched '(t) alist)
1737 (setcdr entries (cdr rest))
1738 (setq rest entries)))
1739 (setq entries rest)))))
1742 (defun gnus-score-body (scores header now expire &optional trace)
1743 (if gnus-agent-fetching
1746 (setq gnus-scores-articles
1747 (sort gnus-scores-articles
1749 (< (mail-header-number (car a1))
1750 (mail-header-number (car a2))))))
1751 (set-buffer nntp-server-buffer)
1753 (let* ((buffer-read-only nil)
1754 (articles gnus-scores-articles)
1756 (request-func (cond ((string= "head" header)
1758 ((string= "body" header)
1760 (t 'gnus-request-article)))
1761 entries alist ofunc article last)
1763 (setq last (mail-header-number (caar (last articles))))
1764 ;; Not all backends support partial fetching. In that case,
1765 ;; we just fetch the entire article.
1766 (unless (gnus-check-backend-function
1767 (and (string-match "^gnus-" (symbol-name request-func))
1768 (intern (substring (symbol-name request-func)
1770 gnus-newsgroup-name)
1771 (setq ofunc request-func)
1772 (setq request-func 'gnus-request-article))
1774 (setq article (mail-header-number (caar articles)))
1775 (gnus-message 7 "Scoring article %s of %s..." article last)
1777 (when (funcall request-func article gnus-newsgroup-name)
1778 (goto-char (point-min))
1779 ;; If just parts of the article is to be searched, but the
1780 ;; backend didn't support partial fetching, we just narrow
1781 ;; to the relevant parts.
1783 (if (eq ofunc 'gnus-request-head)
1786 (or (search-forward "\n\n" nil t) (point-max)))
1788 (or (search-forward "\n\n" nil t) (point))
1790 (setq scores all-scores)
1793 (setq alist (pop scores)
1794 entries (assoc header alist))
1795 (while (cdr entries) ;First entry is the header index.
1796 (let* ((rest (cdr entries))
1798 (match (nth 0 kill))
1799 (type (or (nth 3 kill) 's))
1800 (score (or (nth 1 kill)
1801 gnus-score-interactive-default-score))
1805 (not (or (eq type 'R) (eq type 'S)
1806 (eq type 'Regexp) (eq type 'String))))
1808 (cond ((or (eq type 'r) (eq type 'R)
1809 (eq type 'regexp) (eq type 'Regexp))
1811 ((or (eq type 's) (eq type 'S)
1812 (eq type 'string) (eq type 'String))
1815 (error "Invalid match type: %s" type)))))
1816 (goto-char (point-min))
1817 (when (funcall search-func match nil t)
1818 ;; Found a match, update scores.
1819 (setcdr (car articles) (+ score (cdar articles)))
1823 (cons (car-safe (rassq alist gnus-score-cache))
1826 ;; Update expire date
1829 ((null date)) ;Permanent entry.
1830 ((and found gnus-update-score-entry-dates)
1831 ;; Match, update date.
1832 (gnus-score-set 'touched '(t) alist)
1833 (setcar (nthcdr 2 kill) now))
1834 ((and expire (< date expire)) ;Old entry, remove.
1835 (gnus-score-set 'touched '(t) alist)
1836 (setcdr entries (cdr rest))
1837 (setq rest entries))))
1838 (setq entries rest)))))
1839 (setq articles (cdr articles)))))))
1842 (defun gnus-score-thread (scores header now expire &optional trace)
1843 (gnus-score-followup scores header now expire trace t))
1845 (defun gnus-score-followup (scores header now expire &optional trace thread)
1846 (if gnus-agent-fetching
1847 ;; FIXME: It seems doable in fetching mode.
1849 ;; Insert the unique article headers in the buffer.
1850 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1851 (current-score-file gnus-current-score-file)
1853 ;; gnus-score-index is used as a free variable.
1854 alike last this art entries alist articles
1857 ;; Change score file to the adaptive score file. All entries that
1858 ;; this function makes will be put into this file.
1860 (set-buffer gnus-summary-buffer)
1861 (gnus-score-load-file
1862 (or gnus-newsgroup-adaptive-score-file
1863 (gnus-score-file-name
1864 gnus-newsgroup-name gnus-adaptive-file-suffix))))
1866 (setq gnus-scores-articles (sort gnus-scores-articles
1867 'gnus-score-string<)
1868 articles gnus-scores-articles)
1872 (setq art (car articles)
1873 this (aref (car art) gnus-score-index)
1874 articles (cdr articles))
1875 (if (equal last this)
1879 (put-text-property (1- (point)) (point) 'articles alike))
1880 (setq alike (list art)
1882 (when last ; Bwadr, duplicate code.
1884 (put-text-property (1- (point)) (point) 'articles alike))
1888 (setq alist (car scores)
1890 entries (assoc header alist))
1891 (while (cdr entries) ;First entry is the header index.
1892 (let* ((rest (cdr entries))
1894 (match (nth 0 kill))
1895 (type (or (nth 3 kill) 's))
1896 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1899 (mt (aref (symbol-name type) 0))
1901 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1904 (cond ((= dmt ?r) 're-search-forward)
1905 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1906 (t (error "Invalid match type: %s" type))))
1908 (goto-char (point-min))
1910 (while (funcall search-func match nil t)
1911 (and (= (point-at-bol)
1912 (match-beginning 0))
1913 (= (progn (end-of-line) (point))
1916 (setq found (setq arts (get-text-property
1917 (point) 'articles)))
1918 ;; Found a match, update scores.
1920 (setq art (car arts)
1922 (gnus-score-add-followups
1923 (car art) score all-scores thread))))
1925 (while (funcall search-func match nil t)
1927 (setq found (setq arts (get-text-property (point) 'articles)))
1928 ;; Found a match, update scores.
1929 (while (setq art (pop arts))
1930 (setcdr art (+ score (cdr art)))
1933 (car-safe (rassq alist gnus-score-cache))
1936 (when (setq new (gnus-score-add-followups
1937 (car art) score all-scores thread))
1939 ;; Update expire date
1940 (cond ((null date)) ;Permanent entry.
1941 ((and found gnus-update-score-entry-dates)
1942 ;Match, update date.
1943 (gnus-score-set 'touched '(t) alist)
1944 (setcar (nthcdr 2 kill) now))
1945 ((and expire (< date expire)) ;Old entry, remove.
1946 (gnus-score-set 'touched '(t) alist)
1947 (setcdr entries (cdr rest))
1948 (setq rest entries)))
1949 (setq entries rest))))
1950 ;; We change the score file back to the previous one.
1952 (set-buffer gnus-summary-buffer)
1953 (gnus-score-load-file current-score-file))
1954 (list (cons "references" news)))))
1956 (defun gnus-score-add-followups (header score scores &optional thread)
1957 "Add a score entry to the adapt file."
1959 (set-buffer gnus-summary-buffer)
1960 (let* ((id (mail-header-id header))
1961 (scores (car scores))
1963 ;; Don't enter a score if there already is one.
1964 (while (setq entry (pop scores))
1965 (and (equal "references" (car entry))
1966 (or (null (nth 3 (cadr entry)))
1967 (eq 's (nth 3 (cadr entry))))
1971 (gnus-summary-score-entry
1972 (if thread "thread" "references")
1973 id 's score (current-time-string) nil t)))))
1975 (defun gnus-score-string (score-list header now expire &optional trace)
1976 ;; Score ARTICLES according to HEADER in SCORE-LIST.
1977 ;; Update matching entries to NOW and remove unmatched entries older
1980 ;; Insert the unique article headers in the buffer.
1981 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1982 ;; gnus-score-index is used as a free variable.
1983 (simplify (and gnus-score-thread-simplify
1984 (string= "subject" header)))
1985 alike last this art entries alist articles
1986 fuzzies arts words kill)
1988 ;; Sorting the articles costs os O(N*log N) but will allow us to
1989 ;; only match with each unique header. Thus the actual matching
1990 ;; will be O(M*U) where M is the number of strings to match with,
1991 ;; and U is the number of unique headers. It is assumed (but
1992 ;; untested) this will be a net win because of the large constant
1993 ;; factor involved with string matching.
1994 (setq gnus-scores-articles
1995 ;; We cannot string-sort the extra headers list. *sigh*
1996 (if (= gnus-score-index 9)
1997 gnus-scores-articles
1998 (sort gnus-scores-articles 'gnus-score-string<))
1999 articles gnus-scores-articles)
2002 (while (setq art (pop articles))
2003 (setq this (aref (car art) gnus-score-index))
2005 ;; If we're working with non-standard headers, we are stuck
2006 ;; with working on them as a group. What a hassle.
2007 ;; Just wait 'til you see what horrors we commit against `match'...
2008 (if (= gnus-score-index 9)
2009 (setq this (gnus-prin1-to-string this))) ; ick.
2012 (setq this (gnus-map-function gnus-simplify-subject-functions this)))
2013 (if (equal last this)
2014 ;; O(N*H) cons-cells used here, where H is the number of
2018 ;; Insert the line, with a text property on the
2019 ;; terminating newline referring to the articles with
2022 (put-text-property (1- (point)) (point) 'articles alike))
2023 (setq alike (list art)
2025 (when last ; Bwadr, duplicate code.
2027 (put-text-property (1- (point)) (point) 'articles alike))
2029 ;; Go through all the score alists and pick out the entries
2032 (setq alist (pop score-list)
2033 ;; There's only one instance of this header for
2034 ;; each score alist.
2035 entries (assoc header alist))
2036 (while (cdr entries) ;First entry is the header index.
2037 (let* ((kill (cadr entries))
2038 (type (or (nth 3 kill) 's))
2039 (score (or (nth 1 kill) gnus-score-interactive-default-score))
2041 (extra (nth 4 kill)) ; non-standard header; string.
2043 (mt (aref (symbol-name type) 0))
2044 (case-fold-search (not (memq mt '(?R ?S ?E ?F))))
2046 ;; Assume user already simplified regexp and fuzzies
2047 (match (if (and simplify (not (memq dmt '(?f ?r))))
2049 gnus-simplify-subject-functions
2053 (cond ((= dmt ?r) 're-search-forward)
2054 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
2056 (t (error "Invalid match type: %s" type)))))
2058 ;; Evil hackery to make match usable in non-standard headers.
2060 (setq match (concat "[ (](" extra " \\. \"[^)]*"
2061 match "[^\"]*\")[ )]")
2062 search-func 're-search-forward)) ; XXX danger?!?
2065 ;; Fuzzy matches. We save these for later.
2067 (push (cons entries alist) fuzzies)
2068 (setq entries (cdr entries)))
2069 ;; Word matches. Save these for even later.
2071 (push (cons entries alist) words)
2072 (setq entries (cdr entries)))
2075 ;; Do exact matching.
2076 (goto-char (point-min))
2077 (while (and (not (eobp))
2078 (funcall search-func match nil t))
2079 ;; Is it really exact?
2081 (= (point-at-bol) (match-beginning 0))
2084 (setq found (setq arts (get-text-property
2085 (point) 'articles)))
2086 ;; Found a match, update scores.
2088 (while (setq art (pop arts))
2089 (setcdr art (+ score (cdr art)))
2092 (car-safe (rassq alist gnus-score-cache))
2095 (while (setq art (pop arts))
2096 (setcdr art (+ score (cdr art)))))))
2098 ;; Update expiry date
2100 (setq entries (cdr entries))
2104 (setq entries (cdr entries)))
2105 ;; We have a match, so we update the date.
2106 ((and found gnus-update-score-entry-dates)
2107 (gnus-score-set 'touched '(t) alist)
2108 (setcar (nthcdr 2 kill) now)
2109 (setq entries (cdr entries)))
2110 ;; This entry has expired, so we remove it.
2111 ((and expire (< date expire))
2112 (gnus-score-set 'touched '(t) alist)
2113 (setcdr entries (cddr entries)))
2114 ;; No match; go to next entry.
2116 (setq entries (cdr entries))))))
2117 ;; Regexp and substring matching.
2119 (goto-char (point-min))
2120 (when (string= match "")
2122 (while (and (not (eobp))
2123 (funcall search-func match nil t))
2124 (goto-char (match-beginning 0))
2126 (setq found (setq arts (get-text-property (point) 'articles)))
2127 ;; Found a match, update scores.
2129 (while (setq art (pop arts))
2130 (setcdr art (+ score (cdr art)))
2131 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
2133 (while (setq art (pop arts))
2134 (setcdr art (+ score (cdr art)))))
2136 ;; Update expiry date
2138 (setq entries (cdr entries))
2142 (setq entries (cdr entries)))
2143 ;; We have a match, so we update the date.
2144 ((and found gnus-update-score-entry-dates)
2145 (gnus-score-set 'touched '(t) alist)
2146 (setcar (nthcdr 2 kill) now)
2147 (setq entries (cdr entries)))
2148 ;; This entry has expired, so we remove it.
2149 ((and expire (< date expire))
2150 (gnus-score-set 'touched '(t) alist)
2151 (setcdr entries (cddr entries)))
2152 ;; No match; go to next entry.
2154 (setq entries (cdr entries))))))))))
2156 ;; Find fuzzy matches.
2158 ;; Simplify the entire buffer for easy matching.
2159 (gnus-simplify-buffer-fuzzy)
2160 (while (setq kill (cadaar fuzzies))
2161 (let* ((match (nth 0 kill))
2163 (score (or (nth 1 kill) gnus-score-interactive-default-score))
2165 (mt (aref (symbol-name type) 0))
2166 (case-fold-search (not (= mt ?F)))
2168 (goto-char (point-min))
2169 (while (and (not (eobp))
2170 (search-forward match nil t))
2171 (when (and (= (point-at-bol) (match-beginning 0))
2173 (setq found (setq arts (get-text-property (point) 'articles)))
2175 (while (setq art (pop arts))
2176 (setcdr art (+ score (cdr art)))
2178 (car-safe (rassq (cdar fuzzies) gnus-score-cache))
2181 ;; Found a match, update scores.
2182 (while (setq art (pop arts))
2183 (setcdr art (+ score (cdr art))))))
2185 ;; Update expiry date
2192 ;; Match, update date.
2193 ((and found gnus-update-score-entry-dates)
2194 (gnus-score-set 'touched '(t) (cdar fuzzies))
2195 (setcar (nthcdr 2 kill) now))
2196 ;; Old entry, remove.
2197 ((and expire (< date expire))
2198 (gnus-score-set 'touched '(t) (cdar fuzzies))
2199 (setcdr (caar fuzzies) (cddaar fuzzies)))))
2200 (setq fuzzies (cdr fuzzies)))))
2203 ;; Enter all words into the hashtb.
2204 (let ((hashtb (gnus-make-hashtable
2205 (* 10 (count-lines (point-min) (point-max))))))
2206 (gnus-enter-score-words-into-hashtb hashtb)
2207 (while (setq kill (cadaar words))
2208 (let* ((score (or (nth 1 kill) gnus-score-interactive-default-score))
2211 (when (setq arts (intern-soft (nth 0 kill) hashtb))
2212 (setq arts (symbol-value arts))
2215 (while (setq art (pop arts))
2216 (setcdr art (+ score (cdr art)))
2218 (car-safe (rassq (cdar words) gnus-score-cache))
2221 ;; Found a match, update scores.
2222 (while (setq art (pop arts))
2223 (setcdr art (+ score (cdr art))))))
2224 ;; Update expiry date
2231 ;; Match, update date.
2232 ((and found gnus-update-score-entry-dates)
2233 (gnus-score-set 'touched '(t) (cdar words))
2234 (setcar (nthcdr 2 kill) now))
2235 ;; Old entry, remove.
2236 ((and expire (< date expire))
2237 (gnus-score-set 'touched '(t) (cdar words))
2238 (setcdr (caar words) (cddaar words)))))
2239 (setq words (cdr words))))))
2242 (defun gnus-enter-score-words-into-hashtb (hashtb)
2243 ;; Find all the words in the buffer and enter them into
2246 (goto-char (point-min))
2247 (with-syntax-table gnus-adaptive-word-syntax-table
2248 (while (re-search-forward "\\b\\w+\\b" nil t)
2251 (setq word (downcase (buffer-substring
2252 (match-beginning 0) (match-end 0))))
2256 (append (get-text-property (point-at-eol) 'articles) val)
2258 ;; Make all the ignorable words ignored.
2259 (let ((ignored (append gnus-ignored-adaptive-words
2260 (if gnus-adaptive-word-no-group-words
2261 (message-tokenize-header
2262 (gnus-group-real-name gnus-newsgroup-name)
2264 gnus-default-ignored-adaptive-words)))
2266 (gnus-sethash (pop ignored) nil hashtb)))))
2268 (defun gnus-score-string< (a1 a2)
2269 ;; Compare headers in articles A2 and A2.
2270 ;; The header index used is the free variable `gnus-score-index'.
2271 (string-lessp (aref (car a1) gnus-score-index)
2272 (aref (car a2) gnus-score-index)))
2274 (defun gnus-current-score-file-nondirectory (&optional score-file)
2275 (let ((score-file (or score-file gnus-current-score-file)))
2277 (gnus-short-group-name (file-name-nondirectory score-file))
2280 (defun gnus-score-adaptive ()
2281 "Create adaptive score rules for this newsgroup."
2282 (when gnus-newsgroup-adaptive
2283 ;; We change the score file to the adaptive score file.
2285 (set-buffer gnus-summary-buffer)
2286 (gnus-score-load-file
2287 (or gnus-newsgroup-adaptive-score-file
2288 (gnus-home-score-file gnus-newsgroup-name t)
2289 (gnus-score-file-name
2290 gnus-newsgroup-name gnus-adaptive-file-suffix))))
2291 ;; Perform ordinary line scoring.
2292 (when (or (not (listp gnus-newsgroup-adaptive))
2293 (memq 'line gnus-newsgroup-adaptive))
2295 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
2297 (date (current-time-string))
2298 (data gnus-newsgroup-data)
2299 elem headers match func)
2300 ;; First we transform the adaptive rule alist into something
2301 ;; that's faster to process.
2303 (setq elem (car malist))
2304 (when (symbolp (car elem))
2305 (setcar elem (symbol-value (car elem))))
2306 (setq elem (cdr elem))
2311 (concat "mail-header-"
2312 (if (eq (caar elem) 'followup)
2314 (downcase (symbol-name (caar elem))))))))
2316 (cons (if (eq (caar elem) 'followup)
2318 (symbol-name (caar elem)))
2323 (setq elem (cdr elem)))
2324 (setq malist (cdr malist)))
2325 ;; Then we score away.
2327 (setq elem (cdr (assq (gnus-data-mark (car data)) alist)))
2329 (gnus-data-pseudo-p (car data)))
2331 (when (setq headers (gnus-data-header (car data)))
2333 (setq match (funcall (caar elem) headers))
2334 (gnus-summary-score-entry
2335 (nth 1 (car elem)) match
2339 ((equal (nth 1 (car elem)) "date")
2342 ;; Whether we use substring or exact matches is
2344 (if (or (not gnus-score-exact-adapt-limit)
2345 (< (length match) gnus-score-exact-adapt-limit))
2347 (if (equal (nth 1 (car elem)) "subject")
2349 (nth 2 (car elem)) date nil t)
2350 (setq elem (cdr elem)))))
2351 (setq data (cdr data))))))
2353 ;; Perform adaptive word scoring.
2354 (when (and (listp gnus-newsgroup-adaptive)
2355 (memq 'word gnus-newsgroup-adaptive))
2357 (let* ((hashtb (gnus-make-hashtable 1000))
2358 (date (date-to-day (current-time-string)))
2359 (data gnus-newsgroup-data)
2361 (with-syntax-table gnus-adaptive-word-syntax-table
2362 ;; Go through all articles.
2363 (while (setq d (pop data))
2365 (not (gnus-data-pseudo-p d))
2369 gnus-adaptive-word-score-alist))))
2370 ;; This article has a mark that should lead to
2371 ;; adaptive word rules, so we insert the subject
2372 ;; and find all words in that string.
2373 (insert (mail-header-subject (gnus-data-header d)))
2374 (downcase-region (point-min) (point-max))
2375 (goto-char (point-min))
2376 (while (re-search-forward "\\b\\w+\\b" nil t)
2377 ;; Put the word and score into the hashtb.
2378 (setq val (gnus-gethash (setq word (match-string 0))
2380 (when (or (not gnus-adaptive-word-length-limit)
2382 gnus-adaptive-word-length-limit))
2383 (setq val (+ score (or val 0)))
2384 (if (and gnus-adaptive-word-minimum
2385 (< val gnus-adaptive-word-minimum))
2386 (setq val gnus-adaptive-word-minimum))
2387 (gnus-sethash word val hashtb)))
2389 ;; Make all the ignorable words ignored.
2390 (let ((ignored (append gnus-ignored-adaptive-words
2391 (if gnus-adaptive-word-no-group-words
2392 (message-tokenize-header
2393 (gnus-group-real-name
2394 gnus-newsgroup-name)
2396 gnus-default-ignored-adaptive-words)))
2398 (gnus-sethash (pop ignored) nil hashtb)))
2399 ;; Now we have all the words and scores, so we
2400 ;; add these rules to the ADAPT file.
2401 (set-buffer gnus-summary-buffer)
2404 (when (symbol-value word)
2405 (gnus-summary-score-entry
2406 "subject" (symbol-name word) 'w (symbol-value word)
2410 (defun gnus-score-edit-done ()
2411 (let ((bufnam (buffer-file-name (current-buffer)))
2412 (winconf gnus-prev-winconf))
2414 (set-window-configuration winconf))
2415 (gnus-score-remove-from-cache bufnam)
2416 (gnus-score-load-file bufnam)
2417 (run-hooks 'gnus-score-edit-done-hook)))
2419 (defun gnus-score-find-trace ()
2420 "Find all score rules that applies to the current article."
2422 (let ((old-scored gnus-newsgroup-scored))
2423 (let ((gnus-newsgroup-headers
2424 (list (gnus-summary-article-header)))
2425 (gnus-newsgroup-scored nil)
2426 ;; Must be synced with `gnus-score-edit-file-at-point':
2427 (frmt "%S [%s] -> %s\n")
2431 (nnheader-set-temp-buffer "*Score Trace*"))
2432 (setq gnus-score-trace nil)
2433 (gnus-possibly-score-headers 'trace)
2434 (if (not (setq trace gnus-score-trace))
2436 1 "No score rules apply to the current article (default score %d)."
2437 gnus-summary-default-score)
2438 (set-buffer "*Score Trace*")
2439 ;; Use a keymap instead?
2444 (gnus-summary-expand-window)))
2448 (kill-buffer (current-buffer))
2449 (gnus-summary-expand-window)))
2450 (local-set-key "e" (lambda ()
2451 "Run `gnus-score-edit-file-at-point'."
2453 (gnus-score-edit-file-at-point)))
2454 (local-set-key "f" (lambda ()
2455 "Run `gnus-score-edit-file-at-point'."
2457 (gnus-score-edit-file-at-point 'format)))
2458 (local-set-key "t" 'toggle-truncate-lines)
2459 (setq truncate-lines t)
2460 (dolist (entry trace)
2461 (setq file (or (car entry)
2462 ;; Must be synced with
2463 ;; `gnus-score-edit-file-at-point':
2468 ;; Don't use `file-name-sans-extension' to see .SCORE and
2470 (file-name-nondirectory file)
2471 (abbreviate-file-name file))))
2475 Type `e' to edit score file corresponding to the score rule on current line,
2476 `f' to format (pretty print) the score file and edit it,
2477 `t' toggle to truncate long lines in this buffer,
2478 `q' to quit, `k' to kill score trace buffer.
2480 The first sexp on each line is the score rule, followed by the file name of
2481 the score file and its full name, including the directory.")
2482 (goto-char (point-min))
2483 (gnus-configure-windows 'score-trace)))
2484 (set-buffer gnus-summary-buffer)
2485 (setq gnus-newsgroup-scored old-scored)))
2487 (defun gnus-score-find-favourite-words ()
2488 "List words used in scoring."
2490 (let ((alists (gnus-score-load-files (gnus-all-score-files)))
2491 alist rule rules kill)
2492 ;; Go through all the score alists for this group
2493 ;; and find all `w' rules.
2494 (while (setq alist (pop alists))
2495 (while (setq rule (pop alist))
2496 (when (and (stringp (car rule))
2497 (equal "subject" (downcase (pop rule))))
2498 (while (setq kill (pop rule))
2499 (when (memq (nth 3 kill) '(w W word Word))
2500 (push (cons (or (nth 1 kill)
2501 gnus-score-interactive-default-score)
2504 (setq rules (sort rules (lambda (r1 r2)
2505 (string-lessp (cdr r1) (cdr r2)))))
2506 ;; Add up words that have appeared several times.
2509 (if (equal (cdar r) (cdadr r))
2511 (setcar (car r) (+ (caar r) (caadr r)))
2512 (setcdr r (cddr r)))
2514 ;; Insert the words.
2515 (nnheader-set-temp-buffer "*Score Words*")
2516 (if (not (setq rules (sort rules (lambda (r1 r2) (> (car r1) (car r2))))))
2517 (gnus-error 3 "No word score rules")
2519 (insert (format "%-5d: %s\n" (caar rules) (cdar rules)))
2521 (goto-char (point-min))
2522 (gnus-configure-windows 'score-words))))
2524 (defun gnus-summary-rescore ()
2525 "Redo the entire scoring process in the current summary."
2528 (setq gnus-score-cache nil)
2529 (setq gnus-newsgroup-scored nil)
2530 (gnus-possibly-score-headers)
2531 (gnus-score-update-all-lines))
2533 (defun gnus-score-flush-cache ()
2534 "Flush the cache of score files."
2537 (setq gnus-score-cache nil
2538 gnus-score-alist nil
2539 gnus-short-name-score-file-cache nil)
2540 (gnus-message 6 "The score cache is now flushed"))
2542 (gnus-add-shutdown 'gnus-score-close 'gnus)
2544 (defvar gnus-score-file-alist-cache nil)
2546 (defun gnus-score-close ()
2547 "Clear all internal score variables."
2548 (setq gnus-score-cache nil
2549 gnus-internal-global-score-files nil
2550 gnus-score-file-list nil
2551 gnus-score-file-alist-cache nil))
2553 ;; Summary score marking commands.
2555 (defun gnus-summary-raise-same-subject-and-select (score)
2556 "Raise articles which has the same subject with SCORE and select the next."
2558 (let ((subject (gnus-summary-article-subject)))
2559 (gnus-summary-raise-score score)
2560 (while (gnus-summary-find-subject subject)
2561 (gnus-summary-raise-score score))
2562 (gnus-summary-next-article t)))
2564 (defun gnus-summary-raise-same-subject (score)
2565 "Raise articles which has the same subject with SCORE."
2567 (let ((subject (gnus-summary-article-subject)))
2568 (gnus-summary-raise-score score)
2569 (while (gnus-summary-find-subject subject)
2570 (gnus-summary-raise-score score))
2571 (gnus-summary-next-subject 1 t)))
2573 (defun gnus-score-delta-default (level)
2574 (if level (prefix-numeric-value level)
2575 gnus-score-interactive-default-score))
2577 (defun gnus-summary-raise-thread (&optional score)
2578 "Raise the score of the articles in the current thread with SCORE."
2580 (setq score (gnus-score-delta-default score))
2583 (let ((articles (gnus-summary-articles-in-thread)))
2585 (gnus-summary-goto-subject (car articles))
2586 (gnus-summary-raise-score score)
2587 (setq articles (cdr articles))))
2589 (let ((gnus-summary-check-current t))
2590 (unless (zerop (gnus-summary-next-subject 1 t))
2592 (gnus-summary-recenter)
2593 (gnus-summary-position-point)
2594 (gnus-set-mode-line 'summary))
2596 (defun gnus-summary-lower-same-subject-and-select (score)
2597 "Raise articles which has the same subject with SCORE and select the next."
2599 (gnus-summary-raise-same-subject-and-select (- score)))
2601 (defun gnus-summary-lower-same-subject (score)
2602 "Raise articles which has the same subject with SCORE."
2604 (gnus-summary-raise-same-subject (- score)))
2606 (defun gnus-summary-lower-thread (&optional score)
2607 "Lower score of articles in the current thread with SCORE."
2609 (gnus-summary-raise-thread (- (gnus-score-delta-default score))))
2611 ;;; Finding score files.
2613 (defun gnus-score-score-files (group)
2614 "Return a list of all possible score files."
2615 ;; Search and set any global score files.
2616 (when gnus-global-score-files
2617 (unless gnus-internal-global-score-files
2618 (gnus-score-search-global-directories gnus-global-score-files)))
2619 ;; Fix the kill-file dir variable.
2620 (setq gnus-kill-files-directory
2621 (file-name-as-directory gnus-kill-files-directory))
2622 ;; If we can't read it, there are no score files.
2623 (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
2624 (setq gnus-score-file-list nil)
2625 (if (not (gnus-use-long-file-name 'not-score))
2626 ;; We do not use long file names, so we have to do some
2627 ;; directory traversing.
2628 (setq gnus-score-file-list
2630 (or gnus-short-name-score-file-cache
2632 (gnus-message 6 "Finding all score files...")
2633 (setq gnus-short-name-score-file-cache
2634 (gnus-score-score-files-1
2635 gnus-kill-files-directory))
2636 (gnus-message 6 "Finding all score files...done")))))
2637 ;; We want long file names.
2638 (when (or (not gnus-score-file-list)
2639 (not (car gnus-score-file-list))
2640 (gnus-file-newer-than gnus-kill-files-directory
2641 (car gnus-score-file-list)))
2642 (setq gnus-score-file-list
2643 (cons (nth 5 (file-attributes gnus-kill-files-directory))
2646 gnus-kill-files-directory t
2647 (gnus-score-file-regexp)))))))
2648 (cdr gnus-score-file-list)))
2650 (defun gnus-score-score-files-1 (dir)
2651 "Return all possible score files under DIR."
2652 (let ((files (list (expand-file-name dir)))
2653 (regexp (gnus-score-file-regexp))
2654 (case-fold-search nil)
2656 (while (setq file (pop files))
2658 ;; Ignore files that start with a dot.
2659 ((string-match "^\\." (file-name-nondirectory file))
2661 ;; Add subtrees of directory to also be searched.
2662 ((and (file-directory-p file)
2663 (not (member (file-truename file) seen)))
2664 (push (file-truename file) seen)
2665 (setq files (nconc (directory-files file t nil t) files)))
2666 ;; Add files to the list of score files.
2667 ((string-match regexp file)
2670 ;; Return a dummy value.
2671 (list (expand-file-name "this.file.does.not.exist.SCORE"
2672 gnus-kill-files-directory)))))
2674 (defun gnus-score-file-regexp ()
2675 "Return a regexp that match all score files."
2676 (concat "\\(" (regexp-quote gnus-score-file-suffix )
2677 "\\|" (regexp-quote gnus-adaptive-file-suffix) "\\)\\'"))
2679 (defun gnus-score-find-bnews (group)
2680 "Return a list of score files for GROUP.
2681 The score files are those files in the ~/News/ directory which matches
2682 GROUP using BNews sys file syntax."
2683 (let* ((sfiles (append (gnus-score-score-files group)
2684 gnus-internal-global-score-files))
2685 (kill-dir (file-name-as-directory
2686 (expand-file-name gnus-kill-files-directory)))
2687 (klen (length kill-dir))
2688 (score-regexp (gnus-score-file-regexp))
2689 (trans (cdr (assq ?: nnheader-file-name-translation-alist)))
2690 (group-trans (nnheader-translate-file-chars group t))
2691 ofiles not-match regexp)
2693 (set-buffer (gnus-get-buffer-create "*gnus score files*"))
2694 (buffer-disable-undo)
2695 ;; Go through all score file names and create regexp with them
2699 (insert (car sfiles))
2700 (goto-char (point-min))
2701 ;; First remove the suffix itself.
2702 (when (re-search-forward (concat "." score-regexp) nil t)
2703 (replace-match "" t t)
2704 (goto-char (point-min))
2705 (if (looking-at (regexp-quote kill-dir))
2706 ;; If the file name was just "SCORE", `klen' is one character
2708 (delete-char (min (1- (point-max)) klen))
2709 (goto-char (point-max))
2710 (if (re-search-backward gnus-directory-sep-char-regexp nil t)
2711 (delete-region (1+ (point)) (point-min))
2712 (gnus-message 1 "Can't find directory separator in %s"
2714 ;; If short file names were used, we have to translate slashes.
2715 (goto-char (point-min))
2716 (let ((regexp (concat
2717 "[/:" (if trans (char-to-string trans)) "]")))
2718 (while (re-search-forward regexp nil t)
2719 (replace-match "." t t)))
2720 ;; Kludge to get rid of "nntp+" problems.
2721 (goto-char (point-min))
2722 (when (looking-at "nn[a-z]+\\+")
2723 (search-forward "+")
2727 ;; Kludge to deal with "++".
2728 (while (search-forward "+" nil t)
2729 (replace-match "\\+" t t))
2730 ;; Translate "all" to ".*".
2731 (goto-char (point-min))
2732 (while (search-forward "all" nil t)
2733 (replace-match ".*" t t))
2734 (goto-char (point-min))
2735 ;; Deal with "not."s.
2736 (if (looking-at "not.")
2740 (concat "^" (buffer-substring 5 (point-max)) "$")))
2741 (setq regexp (concat "^" (buffer-substring 1 (point-max)) "$"))
2742 (setq not-match nil))
2743 ;; Finally - if this resulting regexp matches the group name,
2744 ;; we add this score file to the list of score files
2745 ;; applicable to this group.
2746 (when (or (and not-match
2748 (not (string-match regexp group-trans))))
2749 (and (not not-match)
2750 (ignore-errors (string-match regexp group-trans))))
2751 (push (car sfiles) ofiles)))
2752 (setq sfiles (cdr sfiles)))
2753 (gnus-kill-buffer (current-buffer))
2754 ;; Slight kludge here - the last score file returned should be
2755 ;; the local score file, whether it exists or not. This is so
2756 ;; that any score commands the user enters will go to the right
2757 ;; file, and not end up in some global score file.
2758 (let ((localscore (gnus-score-file-name group)))
2759 (setq ofiles (cons localscore (delete localscore ofiles))))
2760 (gnus-sort-score-files (nreverse ofiles)))))
2762 (defun gnus-score-find-single (group)
2763 "Return list containing the score file for GROUP."
2764 (list (or gnus-newsgroup-adaptive-score-file
2765 (gnus-score-file-name group gnus-adaptive-file-suffix))
2766 (gnus-score-file-name group)))
2768 (defun gnus-score-find-hierarchical (group)
2769 "Return list of score files for GROUP.
2770 This includes the score file for the group and all its parents."
2771 (let* ((prefix (gnus-group-real-prefix group))
2773 (group (gnus-group-real-name group))
2775 (while (string-match "\\." group (1+ start))
2776 (setq start (match-beginning 0))
2777 (push (substring group 0 start) all))
2781 (mapcar (lambda (group)
2782 (gnus-score-file-name group gnus-adaptive-file-suffix))
2783 (setq all (nreverse all)))
2784 (mapcar 'gnus-score-file-name all)))
2785 (if (equal prefix "")
2789 (nnheader-translate-file-chars
2790 (concat (file-name-directory file) prefix
2791 (file-name-nondirectory file))))
2794 (defun gnus-score-file-rank (file)
2795 "Return a number that says how specific score FILE is.
2796 Destroys the current buffer."
2797 (if (member file gnus-internal-global-score-files)
2800 (concat "^" (regexp-quote
2802 (file-name-as-directory gnus-kill-files-directory))))
2804 (setq file (substring file (match-end 0))))
2806 (goto-char (point-min))
2809 (while (re-search-forward "[./]" nil t)
2810 (push (buffer-substring beg (1- (point)))
2813 (setq elems (delete "all" elems))
2816 (defun gnus-sort-score-files (files)
2817 "Sort FILES so that the most general files come first."
2822 (cons (inline (gnus-score-file-rank file)) file))
2824 (mapcar 'cdr (sort alist 'car-less-than-car)))))
2826 (defun gnus-score-find-alist (group)
2827 "Return list of score files for GROUP.
2828 The list is determined from the variable `gnus-score-file-alist'."
2829 (let ((alist gnus-score-file-multiple-match-alist)
2831 ;; if this group has been seen before, return the cached entry
2832 (if (setq score-files (assoc group gnus-score-file-alist-cache))
2833 (cdr score-files) ;ensures caching groups with no matches
2834 ;; handle the multiple match alist
2836 (when (string-match (caar alist) group)
2838 (nconc score-files (copy-sequence (cdar alist)))))
2839 (setq alist (cdr alist)))
2840 (setq alist gnus-score-file-single-match-alist)
2841 ;; handle the single match alist
2843 (when (string-match (caar alist) group)
2844 ;; progn used just in case ("regexp") has no files
2845 ;; and score-files is still nil. -sj
2846 ;; this can be construed as a "stop searching here" feature :>
2847 ;; and used to simplify regexps in the single-alist
2849 (nconc score-files (copy-sequence (cdar alist))))
2851 (setq alist (cdr alist)))
2852 ;; cache the score files
2853 (push (cons group score-files) gnus-score-file-alist-cache)
2856 (defun gnus-all-score-files (&optional group)
2857 "Return a list of all score files for the current group."
2858 (let ((funcs gnus-score-find-score-files-function)
2859 (group (or group gnus-newsgroup-name))
2862 ;; Make sure funcs is a list.
2865 (setq funcs (list funcs)))
2866 (when gnus-score-use-all-scores
2867 ;; Get the initial score files for this group.
2869 (setq score-files (nreverse (gnus-score-find-alist group))))
2870 ;; Add any home adapt files.
2871 (let ((home (gnus-home-score-file group t)))
2873 (push home score-files)
2874 (setq gnus-newsgroup-adaptive-score-file home)))
2875 ;; Check whether there is a `adapt-file' group parameter.
2876 (let ((param-file (gnus-group-find-parameter group 'adapt-file)))
2878 (push param-file score-files)
2879 (setq gnus-newsgroup-adaptive-score-file param-file))))
2880 ;; Go through all the functions for finding score files (or actual
2881 ;; scores) and add them to a list.
2883 (when (functionp (car funcs))
2886 (nreverse (funcall (car funcs) group)))))
2887 (setq funcs (cdr funcs)))
2888 (when gnus-score-use-all-scores
2889 ;; Add any home score files.
2890 (let ((home (gnus-home-score-file group)))
2892 (push home score-files)))
2893 ;; Check whether there is a `score-file' group parameter.
2894 (let ((param-file (gnus-group-find-parameter group 'score-file)))
2896 (push param-file score-files))))
2897 ;; Expand all files names.
2898 (let ((files score-files))
2900 (when (stringp (car files))
2901 (setcar files (expand-file-name
2902 (car files) gnus-kill-files-directory)))
2904 (setq score-files (nreverse score-files))
2905 ;; Remove any duplicate score files.
2906 (while (and score-files
2907 (member (car score-files) (cdr score-files)))
2909 (let ((files score-files))
2911 (if (member (cadr files) (cddr files))
2912 (setcdr files (cddr files))
2914 ;; Do the scoring if there are any score files for this group.
2917 (defun gnus-possibly-score-headers (&optional trace)
2918 "Do scoring if scoring is required."
2919 (let ((score-files (gnus-all-score-files)))
2921 (gnus-score-headers score-files trace))))
2923 (defun gnus-score-file-name (newsgroup &optional suffix)
2924 "Return the name of a score file for NEWSGROUP."
2925 (let ((suffix (or suffix gnus-score-file-suffix)))
2926 (nnheader-translate-file-chars
2928 ((or (null newsgroup)
2929 (string-equal newsgroup ""))
2930 ;; The global score file is placed at top of the directory.
2931 (expand-file-name suffix gnus-kill-files-directory))
2932 ((gnus-use-long-file-name 'not-score)
2933 ;; Append ".SCORE" to newsgroup name.
2934 (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
2936 gnus-kill-files-directory))
2938 ;; Place "SCORE" under the hierarchical directory.
2939 (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
2941 gnus-kill-files-directory))))))
2943 (defun gnus-score-search-global-directories (files)
2944 "Scan all global score directories for score files."
2945 ;; Set the variable `gnus-internal-global-score-files' to all
2946 ;; available global score files.
2947 (interactive (list gnus-global-score-files))
2950 ;; #### /$ Unix-specific?
2951 (if (file-directory-p (car files))
2952 (setq out (nconc (directory-files
2954 (concat (gnus-score-file-regexp) "$"))))
2955 (push (car files) out))
2956 (setq files (cdr files)))
2957 (setq gnus-internal-global-score-files out)))
2959 (defun gnus-score-default-fold-toggle ()
2960 "Toggle folding for new score file entries."
2962 (setq gnus-score-default-fold (not gnus-score-default-fold))
2963 (if gnus-score-default-fold
2964 (gnus-message 1 "New score file entries will be case insensitive.")
2965 (gnus-message 1 "New score file entries will be case sensitive.")))
2967 ;;; Home score file.
2969 (defun gnus-home-score-file (group &optional adapt)
2970 "Return the home score file for GROUP.
2971 If ADAPT, return the home adaptive file instead."
2972 (let ((list (if adapt gnus-home-adapt-file gnus-home-score-file))
2974 ;; Make sure we have a list.
2975 (unless (listp list)
2976 (setq list (list list)))
2977 ;; Go through the list and look for matches.
2978 (while (and (not found)
2979 (setq elem (pop list)))
2987 (funcall elem group))
2988 ;; Regexp-file cons.
2990 (when (string-match (gnus-globalify-regexp (car elem)) group)
2991 (replace-match (cadr elem) t nil group))))))
2993 (setq found (nnheader-translate-file-chars found))
2994 (if (file-name-absolute-p found)
2996 (nnheader-concat gnus-kill-files-directory found)))))
2998 (defun gnus-hierarchial-home-score-file (group)
2999 "Return the score file of the top-level hierarchy of GROUP."
3000 (if (string-match "^[^.]+\\." group)
3001 (concat (match-string 0 group) gnus-score-file-suffix)
3002 ;; Group name without any dots.
3003 (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
3004 gnus-score-file-suffix)))
3006 (defun gnus-hierarchial-home-adapt-file (group)
3007 "Return the adapt file of the top-level hierarchy of GROUP."
3008 (if (string-match "^[^.]+\\." group)
3009 (concat (match-string 0 group) gnus-adaptive-file-suffix)
3010 ;; Group name without any dots.
3011 (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
3012 gnus-adaptive-file-suffix)))
3014 (defun gnus-current-home-score-file (group)
3015 "Return the \"current\" regular score file."
3016 (car (nreverse (gnus-score-find-alist group))))
3022 (defun gnus-decay-score (score)
3023 "Decay SCORE according to `gnus-score-decay-constant' and `gnus-score-decay-scale'."
3025 (* (if (< score 0) -1 1)
3027 (max gnus-score-decay-constant
3029 gnus-score-decay-scale)))))))
3030 (if (and (featurep 'xemacs)
3031 ;; XEmacs' floor can handle only the floating point
3032 ;; number below the half of the maximum integer.
3033 (> (abs n) (lsh -1 -2)))
3035 (car (split-string (number-to-string n) "\\.")))
3038 (defun gnus-decay-scores (alist day)
3039 "Decay non-permanent scores in ALIST."
3040 (let ((times (- (time-to-days (current-time)) day))
3041 kill entry updated score n)
3042 (unless (zerop times) ;Done decays today already?
3043 (while (setq entry (pop alist))
3044 (when (stringp (car entry))
3045 (setq entry (cdr entry))
3046 (while (setq kill (pop entry))
3049 (setq score (or (nth 1 kill)
3050 gnus-score-interactive-default-score)
3052 (while (natnump (decf n))
3053 (setq score (funcall gnus-decay-score-function score)))
3054 (setcdr kill (cons score
3055 (cdr (cdr kill)))))))))
3056 ;; Return whether this score file needs to be saved. By Je-haysuss!
3059 (defun gnus-score-regexp-bad-p (regexp)
3060 "Test whether REGEXP is safe for Gnus scoring.
3061 A regexp is unsafe if it matches newline or a buffer boundary.
3063 If the regexp is good, return nil. If the regexp is bad, return a
3064 cons cell (SYM . STRING), where the symbol SYM is `new' or `bad'.
3065 In the `new' case, the string is a safe replacement for REGEXP.
3066 In the `bad' case, the string is a unsafe subexpression of REGEXP,
3067 and we do not have a simple replacement to suggest.
3069 See Info node `(gnus)Scoring Tips' for examples of good regular expressions."
3070 (let (case-fold-search)
3072 ;; First, try a relatively fast necessary condition.
3073 ;; Notice ranges (like [^:] or [\t-\r]), \s>, \Sw, \W, \', \`:
3074 (string-match "\n\\|\\\\[SsW`']\\|\\[\\^\\|[\0-\n]-" regexp)
3075 ;; Now break the regexp into tokens, and check each:
3076 (let ((tail regexp) ; remaining regexp to check
3078 bad ; nil, or bad subexpression
3079 new ; nil, or replacement regexp so far
3080 end) ; length of current token
3081 (while (and (not bad)
3083 "\\`\\(\\\\[sS]?.\\|\\[\\^?]?[^]]*]\\|[^\\]\\)"
3085 (setq end (match-end 0)
3086 tok (substring tail 0 end)
3087 tail (substring tail end))
3088 (if;; Is token `bad' (matching newline or buffer ends)?
3089 (or (member tok '("\n" "\\W" "\\`" "\\'"))
3090 ;; This next handles "[...]", "\\s.", and "\\S.":
3091 (and (> end 2) (string-match tok "\n")))
3093 ;; Try to suggest a replacement for tok ...
3094 (cond ((string-equal tok "\\`") "^") ; or "\\(^\\)"
3095 ((string-equal tok "\\'") "$") ; or "\\($\\)"
3096 ((string-match "\\[\\^" tok) ; very common
3097 (concat (substring tok 0 -1) "\n]")))))
3102 ;; good prefix so far:
3103 (substring regexp 0 (- (+ (length tail) end))))
3105 ;; No replacement idea, so give up:
3107 ;; tok is good, may need to extend new
3108 (and new (setq new (concat new tok)))))
3109 ;; Now return a value:
3111 (bad (cons 'bad bad))
3112 (new (cons 'new new))
3115 (provide 'gnus-score)
3117 ;;; gnus-score.el ends here