1 ;;; gnus-score.el --- scoring code for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 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 3 of the License, or
15 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
29 (eval-when-compile (require 'cl
))
38 (defcustom gnus-global-score-files nil
39 "List of global score files and directories.
40 Set this variable if you want to use people's score files. One entry
41 for each score file or each score file directory. Gnus will decide
42 by itself what score files are applicable to which group.
44 Say you want to use the single score file
45 \"/ftp.gnus.org@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
46 score files in the \"/ftp.some-where:/pub/score\" directory.
48 (setq gnus-global-score-files
49 '(\"/ftp.gnus.org:/pub/larsi/ding/score/soc.motss.SCORE\"
50 \"/ftp.some-where:/pub/score\"))"
51 :group
'gnus-score-files
54 (defcustom gnus-score-file-single-match-alist nil
55 "Alist mapping regexps to lists of score files.
56 Each element of this alist should be of the form
57 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
59 If the name of a group is matched by REGEXP, the corresponding scorefiles
60 will be used for that group.
61 The first match found is used, subsequent matching entries are ignored (to
62 use multiple matches, see `gnus-score-file-multiple-match-alist').
64 These score files are loaded in addition to any files returned by
65 `gnus-score-find-score-files-function'."
66 :group
'gnus-score-files
67 :type
'(repeat (cons regexp
(repeat file
))))
69 (defcustom gnus-score-file-multiple-match-alist nil
70 "Alist mapping regexps to lists of score files.
71 Each element of this alist should be of the form
72 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
74 If the name of a group is matched by REGEXP, the corresponding scorefiles
75 will be used for that group.
76 If multiple REGEXPs match a group, the score files corresponding to each
77 match will be used (for only one match to be used, see
78 `gnus-score-file-single-match-alist').
80 These score files are loaded in addition to any files returned by
81 `gnus-score-find-score-files-function'."
82 :group
'gnus-score-files
83 :type
'(repeat (cons regexp
(repeat file
))))
85 (defcustom gnus-score-file-suffix
"SCORE"
86 "Suffix of the score files."
87 :group
'gnus-score-files
90 (defcustom gnus-adaptive-file-suffix
"ADAPT"
91 "Suffix of the adaptive score files."
92 :group
'gnus-score-files
93 :group
'gnus-score-adapt
96 (defcustom gnus-score-find-score-files-function
'gnus-score-find-bnews
97 "Function used to find score files.
98 The function will be called with the group name as the argument, and
99 should return a list of score files to apply to that group. The score
100 files do not actually have to exist.
102 Predefined values are:
104 `gnus-score-find-single': Only apply the group's own score file.
105 `gnus-score-find-hierarchical': Also apply score files from parent groups.
106 `gnus-score-find-bnews': Apply score files whose names matches.
108 See the documentation to these functions for more information.
110 This variable can also be a list of functions to be called. Each
111 function is given the group name as argument and should either return
112 a list of score files, or a list of score alists.
114 If functions other than these pre-defined functions are used,
115 the `a' symbolic prefix to the score commands will always use
117 :group
'gnus-score-files
118 :type
'(radio (function-item gnus-score-find-single
)
119 (function-item gnus-score-find-hierarchical
)
120 (function-item gnus-score-find-bnews
)
121 (repeat :tag
"List of functions"
122 (choice (function :tag
"Other" :value
'ignore
)
123 (function-item gnus-score-find-single
)
124 (function-item gnus-score-find-hierarchical
)
125 (function-item gnus-score-find-bnews
)))
126 (function :tag
"Other" :value
'ignore
)))
128 (defcustom gnus-score-interactive-default-score
1000
129 "*Scoring commands will raise/lower the score with this number as the default."
130 :group
'gnus-score-default
133 (defcustom gnus-score-expiry-days
7
134 "*Number of days before unused score file entries are expired.
135 If this variable is nil, no score file entries will be expired."
136 :group
'gnus-score-expire
137 :type
'(choice (const :tag
"never" nil
)
140 (defcustom gnus-update-score-entry-dates t
141 "*If non-nil, update matching score entry dates.
142 If this variable is nil, then score entries that provide matches
143 will be expired along with non-matching score entries."
144 :group
'gnus-score-expire
147 (defcustom gnus-decay-scores nil
148 "*If non-nil, decay non-permanent scores.
150 If it is a regexp, only decay score files matching regexp."
151 :group
'gnus-score-decay
152 :type
`(choice (const :tag
"never" nil
)
153 (const :tag
"always" t
)
154 (const :tag
"adaptive score files"
155 ,(concat "\\." gnus-adaptive-file-suffix
"\\'"))
158 (defcustom gnus-decay-score-function
'gnus-decay-score
159 "*Function called to decay a score.
160 It is called with one parameter -- the score to be decayed."
161 :group
'gnus-score-decay
162 :type
'(radio (function-item gnus-decay-score
)
163 (function :tag
"Other")))
165 (defcustom gnus-score-decay-constant
3
166 "*Decay all \"small\" scores with this amount."
167 :group
'gnus-score-decay
170 (defcustom gnus-score-decay-scale
.05
171 "*Decay all \"big\" scores with this factor."
172 :group
'gnus-score-decay
175 (defcustom gnus-home-score-file nil
176 "Variable to control where interactive score entries are to go.
180 This file will be used as the home score file.
183 The result of this function will be used as the home score file.
184 The function will be passed the name of the group as its
188 The elements in this list can be:
190 * `(regexp file-name ...)'
191 If the `regexp' matches the group name, the first `file-name'
192 will be used as the home score file. (Multiple filenames are
193 allowed so that one may use gnus-score-file-single-match-alist to
197 If the function returns non-nil, the result will be used
198 as the home score file. The function will be passed the
199 name of the group as its parameter.
201 * A string. Use the string as the home score file.
203 The list will be traversed from the beginning towards the end looking
205 :group
'gnus-score-files
206 :type
'(choice string
207 (repeat (choice string
208 (cons regexp
(repeat file
))
210 (function-item gnus-hierarchial-home-score-file
)
211 (function-item gnus-current-home-score-file
)
214 (defcustom gnus-home-adapt-file nil
215 "Variable to control where new adaptive score entries are to go.
216 This variable allows the same syntax as `gnus-home-score-file'."
217 :group
'gnus-score-adapt
218 :group
'gnus-score-files
219 :type
'(choice string
220 (repeat (choice string
221 (cons regexp
(repeat file
))
225 (defcustom gnus-default-adaptive-score-alist
226 `((gnus-kill-file-mark)
229 (from , (+ 2 gnus-score-decay-constant
))
230 (subject , (+ 27 gnus-score-decay-constant
)))
232 (subject , (+ -
7 (* -
1 gnus-score-decay-constant
))))
234 (from , (- -
1 gnus-score-decay-constant
))
235 (subject , (+ -
17 (* -
1 gnus-score-decay-constant
))))
237 (from , (- -
1 gnus-score-decay-constant
))
238 (subject , (+ -
12 (* -
1 gnus-score-decay-constant
)))))
239 "Alist of marks and scores.
240 If you use score decays, you might want to set values higher than
241 `gnus-score-decay-constant'."
242 :group
'gnus-score-adapt
243 :type
'(repeat (cons (symbol :tag
"Mark")
244 (repeat (list (choice :tag
"Header"
247 (symbol :tag
"other"))
248 (integer :tag
"Score"))))))
250 (defcustom gnus-adaptive-word-length-limit nil
251 "*Words of a length lesser than this limit will be ignored when doing adaptive scoring."
253 :group
'gnus-score-adapt
254 :type
'(radio (const :format
"Unlimited " nil
)
255 (integer :format
"Maximum length: %v")))
257 (defcustom gnus-ignored-adaptive-words nil
258 "List of words to be ignored when doing adaptive word scoring."
259 :group
'gnus-score-adapt
260 :type
'(repeat string
))
262 (defcustom gnus-default-ignored-adaptive-words
263 '("a" "i" "the" "to" "of" "and" "in" "is" "it" "for" "that" "if" "you"
264 "this" "be" "on" "with" "not" "have" "are" "or" "as" "from" "can"
265 "but" "by" "at" "an" "will" "no" "all" "was" "do" "there" "my" "one"
266 "so" "we" "they" "what" "would" "any" "which" "about" "get" "your"
267 "use" "some" "me" "then" "name" "like" "out" "when" "up" "time"
268 "other" "more" "only" "just" "end" "also" "know" "how" "new" "should"
269 "been" "than" "them" "he" "who" "make" "may" "people" "these" "now"
270 "their" "here" "into" "first" "could" "way" "had" "see" "work" "well"
271 "were" "two" "very" "where" "while" "us" "because" "good" "same"
272 "even" "much" "most" "many" "such" "long" "his" "over" "last" "since"
273 "right" "before" "our" "without" "too" "those" "why" "must" "part"
274 "being" "current" "back" "still" "go" "point" "value" "each" "did"
275 "both" "true" "off" "say" "another" "state" "might" "under" "start"
277 "*Default list of words to be ignored when doing adaptive word scoring."
278 :group
'gnus-score-adapt
279 :type
'(repeat string
))
281 (defcustom gnus-default-adaptive-word-score-alist
282 `((,gnus-read-mark .
30)
283 (,gnus-catchup-mark . -
10)
284 (,gnus-killed-mark . -
20)
285 (,gnus-del-mark . -
15))
286 "*Alist of marks and scores."
287 :group
'gnus-score-adapt
288 :type
'(repeat (cons (character :tag
"Mark")
289 (integer :tag
"Score"))))
291 (defcustom gnus-adaptive-word-minimum nil
292 "If a number, this is the minimum score value that can be assigned to a word."
293 :group
'gnus-score-adapt
294 :type
'(choice (const nil
) integer
))
296 (defcustom gnus-adaptive-word-no-group-words nil
297 "If t, don't adaptively score words included in the group name."
298 :group
'gnus-score-adapt
301 (defcustom gnus-score-mimic-keymap nil
302 "*Have the score entry functions pretend that they are a keymap."
303 :group
'gnus-score-default
306 (defcustom gnus-score-exact-adapt-limit
10
307 "*Number that says how long a match has to be before using substring matching.
308 When doing adaptive scoring, one normally uses fuzzy or substring
309 matching. However, if the header one matches is short, the possibility
310 for false positives is great, so if the length of the match is less
311 than this variable, exact matching will be used.
313 If this variable is nil, exact matching will always be used."
314 :group
'gnus-score-adapt
315 :type
'(choice (const nil
) integer
))
317 (defcustom gnus-score-uncacheable-files
"ADAPT$"
318 "All score files that match this regexp will not be cached."
319 :group
'gnus-score-adapt
320 :group
'gnus-score-files
323 (defcustom gnus-adaptive-pretty-print nil
324 "If non-nil, adaptive score files fill are pretty printed."
325 :group
'gnus-score-files
326 :group
'gnus-score-adapt
327 :version
"23.1" ;; No Gnus
330 (defcustom gnus-score-default-header nil
331 "Default header when entering new scores.
333 Should be one of the following symbols.
342 e: `extra' (non-standard overview)
347 If nil, the user will be asked for a header."
348 :group
'gnus-score-default
349 :type
'(choice (const :tag
"from" a
)
350 (const :tag
"subject" s
)
351 (const :tag
"body" b
)
352 (const :tag
"head" h
)
353 (const :tag
"message-id" i
)
354 (const :tag
"references" t
)
355 (const :tag
"xref" x
)
356 (const :tag
"extra" e
)
357 (const :tag
"lines" l
)
358 (const :tag
"date" d
)
359 (const :tag
"followup" f
)
360 (const :tag
"ask" nil
)))
362 (defcustom gnus-score-default-type nil
363 "Default match type when entering new scores.
365 Should be one of the following symbols.
375 >: greater than number
378 If nil, the user will be asked for a match type."
379 :group
'gnus-score-default
380 :type
'(choice (const :tag
"substring" s
)
381 (const :tag
"exact string" e
)
382 (const :tag
"fuzzy string" f
)
383 (const :tag
"regexp string" r
)
384 (const :tag
"before date" b
)
385 (const :tag
"after date" a
)
386 (const :tag
"this date" n
)
387 (const :tag
"less than number" <)
388 (const :tag
"greater than number" >)
389 (const :tag
"equal than number" =)
390 (const :tag
"ask" nil
)))
392 (defcustom gnus-score-default-fold nil
393 "Non-nil means use case folding for new score file entries."
394 :group
'gnus-score-default
397 (defcustom gnus-score-default-duration nil
398 "Default duration of effect when entering new scores.
400 Should be one of the following symbols.
406 If nil, the user will be asked for a duration."
407 :group
'gnus-score-default
408 :type
'(choice (const :tag
"temporary" t
)
409 (const :tag
"permanent" p
)
410 (const :tag
"immediate" i
)
411 (const :tag
"ask" nil
)))
413 (defcustom gnus-score-after-write-file-function nil
414 "Function called with the name of the score file just written to disk."
415 :group
'gnus-score-files
416 :type
'(choice (const nil
) function
))
418 (defcustom gnus-score-thread-simplify nil
419 "If non-nil, subjects will simplified as in threading."
420 :group
'gnus-score-various
423 (defcustom gnus-inhibit-slow-scoring nil
424 "Inhibit slow scoring, e.g. scoring on headers or body.
426 If a regexp, scoring on headers or body is inhibited if the group
427 matches the regexp. If it is t, scoring on headers or body is
428 inhibited for all groups."
429 :group
'gnus-score-various
430 :version
"23.1" ;; No Gnus
431 :type
'(choice (const :tag
"All" nil
)
432 (const :tag
"None" t
)
437 ;; Internal variables.
439 (defvar gnus-score-use-all-scores t
440 "If nil, only `gnus-score-find-score-files-function' is used.")
442 (defvar gnus-adaptive-word-syntax-table
443 (let ((table (copy-syntax-table (standard-syntax-table)))
444 (numbers '(?
0 ?
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 ?
8 ?
9)))
446 (modify-syntax-entry (pop numbers
) " " table
))
447 (modify-syntax-entry ?
' "w" table
)
449 "Syntax table used when doing adaptive word scoring.")
451 (defvar gnus-scores-exclude-files nil
)
452 (defvar gnus-internal-global-score-files nil
)
453 (defvar gnus-score-file-list nil
)
455 (defvar gnus-short-name-score-file-cache nil
)
457 (defvar gnus-score-help-winconf nil
)
458 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist
)
459 (defvar gnus-adaptive-word-score-alist gnus-default-adaptive-word-score-alist
)
460 (defvar gnus-score-trace nil
)
461 (defvar gnus-score-edit-buffer nil
)
463 (defvar gnus-score-alist nil
464 "Alist containing score information.
465 The keys can be symbols or strings. The following symbols are defined.
467 touched: If this alist has been modified.
468 mark: Automatically mark articles below this.
469 expunge: Automatically expunge articles below this.
470 files: List of other score files to load when loading this one.
471 eval: Sexp to be evaluated when the score file is loaded.
473 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
474 where HEADER is the header being scored, MATCH is the string we are
475 looking for, TYPE is a flag indicating whether it should use regexp or
476 substring matching, SCORE is the score to add and DATE is the date
477 of the last successful match.")
479 (defvar gnus-score-cache nil
)
480 (defvar gnus-scores-articles nil
)
481 (defvar gnus-score-index nil
)
484 (defconst gnus-header-index
485 ;; Name to index alist.
486 '(("number" 0 gnus-score-integer
)
487 ("subject" 1 gnus-score-string
)
488 ("from" 2 gnus-score-string
)
489 ("date" 3 gnus-score-date
)
490 ("message-id" 4 gnus-score-string
)
491 ("references" 5 gnus-score-string
)
492 ("chars" 6 gnus-score-integer
)
493 ("lines" 7 gnus-score-integer
)
494 ("xref" 8 gnus-score-string
)
495 ("extra" 9 gnus-score-string
)
496 ("head" -
1 gnus-score-body
)
497 ("body" -
1 gnus-score-body
)
498 ("all" -
1 gnus-score-body
)
499 ("followup" 2 gnus-score-followup
)
500 ("thread" 5 gnus-score-thread
)))
502 ;;; Summary mode score maps.
504 (gnus-define-keys (gnus-summary-score-map "V" gnus-summary-mode-map
)
505 "s" gnus-summary-set-score
506 "S" gnus-summary-current-score
507 "c" gnus-score-change-score-file
508 "C" gnus-score-customize
509 "m" gnus-score-set-mark-below
510 "x" gnus-score-set-expunge-below
511 "R" gnus-summary-rescore
512 "e" gnus-score-edit-current-scores
513 "f" gnus-score-edit-file
514 "F" gnus-score-flush-cache
515 "t" gnus-score-find-trace
516 "w" gnus-score-find-favourite-words
)
518 ;; Summary score file commands
520 ;; Much modification of the kill (ahem, score) code and lots of the
521 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
523 (defun gnus-summary-lower-score (&optional score symp
)
524 "Make a score entry based on the current article.
525 The user will be prompted for header to score on, match type,
526 permanence, and the string to be used. The numerical prefix will be
527 used as score. A symbolic prefix of `a' says to use the `all.SCORE'
528 file for the command instead of the current score file."
529 (interactive (gnus-interactive "P\ny"))
530 (gnus-summary-increase-score (- (gnus-score-delta-default score
)) symp
))
532 (defun gnus-score-kill-help-buffer ()
533 (when (get-buffer "*Score Help*")
534 (kill-buffer "*Score Help*")
535 (when gnus-score-help-winconf
536 (set-window-configuration gnus-score-help-winconf
))))
538 (defun gnus-summary-increase-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 (let* ((nscore (gnus-score-delta-default score
))
546 (prefix (if (< nscore
0) ?L ?I
))
547 (increase (> nscore
0))
549 '((?a
"from" nil nil string
)
550 (?s
"subject" nil nil string
)
551 (?b
"body" "" nil body-string
)
552 (?h
"head" "" nil body-string
)
553 (?i
"message-id" nil nil string
)
554 (?r
"references" "message-id" nil string
)
555 (?x
"xref" nil nil string
)
556 (?e
"extra" nil nil string
)
557 (?l
"lines" nil nil number
)
558 (?d
"date" nil nil date
)
559 (?f
"followup" nil nil string
)
560 (?t
"thread" "message-id" nil string
)))
562 '((?s s
"substring" string
)
563 (?e e
"exact string" string
)
564 (?f f
"fuzzy string" string
)
565 (?r r
"regexp string" string
)
566 (?z s
"substring" body-string
)
567 (?p r
"regexp string" body-string
)
568 (?b before
"before date" date
)
569 (?a after
"after date" date
)
570 (?n at
"this date" date
)
571 (?
< < "less than number" number
)
572 (?
> > "greater than number" number
)
573 (?
= = "equal to number" number
)))
574 (current-score-file gnus-current-score-file
)
576 (list (list ?t
(current-time-string) "temporary")
577 '(?p perm
"permanent") '(?i now
"immediate")))
578 (mimic gnus-score-mimic-keymap
)
579 (hchar (and gnus-score-default-header
580 (aref (symbol-name gnus-score-default-header
) 0)))
581 (tchar (and gnus-score-default-type
582 (aref (symbol-name gnus-score-default-type
) 0)))
583 (pchar (and gnus-score-default-duration
584 (aref (symbol-name gnus-score-default-duration
) 0)))
585 entry temporary type match extra
)
590 ;; First we read the header to score.
595 (message "%c-" prefix
))
596 (message "%s header (%s?): " (if increase
"Increase" "Lower")
597 (mapconcat (lambda (s) (char-to-string (car s
)))
599 (setq hchar
(read-char))
600 (when (or (= hchar ??
) (= hchar ?\C-h
))
602 (gnus-score-insert-help "Match on header" char-to-header
1)))
604 (gnus-score-kill-help-buffer)
605 (unless (setq entry
(assq (downcase hchar
) char-to-header
))
606 (if mimic
(error "%c %c" prefix hchar
)
607 (error "Invalid header type")))
609 (when (/= (downcase hchar
) hchar
)
610 ;; This was a majuscule, so we end reading and set the defaults.
611 (if mimic
(message "%c %c" prefix hchar
) (message ""))
612 (setq tchar
(or tchar ?s
)
613 pchar
(or pchar ?t
)))
618 (if (eq (nth 4 entry
)
622 ;; We continue reading - the type.
626 (sit-for 1) (message "%c %c-" prefix hchar
))
627 (message "%s header '%s' with match type (%s?): "
628 (if increase
"Increase" "Lower")
630 (mapconcat (lambda (s) (char-to-string (car s
)))
632 (setq tchar
(read-char))
633 (when (or (= tchar ??
) (= tchar ?\C-h
))
635 (gnus-score-insert-help "Match type" legal-types
2)))
637 (gnus-score-kill-help-buffer)
638 (unless (setq type
(nth 1 (assq (downcase tchar
) legal-types
)))
639 (if mimic
(error "%c %c" prefix hchar
)
640 (error "Invalid match type"))))
642 (when (/= (downcase tchar
) tchar
)
643 ;; It was a majuscule, so we end reading and use the default.
644 (if mimic
(message "%c %c %c" prefix hchar tchar
)
646 (setq pchar
(or pchar ?t
)))
648 ;; We continue reading.
652 (sit-for 1) (message "%c %c %c-" prefix hchar tchar
))
653 (message "%s permanence (%s?): " (if increase
"Increase" "Lower")
654 (mapconcat (lambda (s) (char-to-string (car s
)))
656 (setq pchar
(read-char))
657 (when (or (= pchar ??
) (= pchar ?\C-h
))
659 (gnus-score-insert-help "Match permanence" char-to-perm
2)))
661 (gnus-score-kill-help-buffer)
662 (if mimic
(message "%c %c %c %c" prefix hchar tchar pchar
)
664 (unless (setq temporary
(cadr (assq pchar char-to-perm
)))
665 ;; Deal with der(r)ided superannuated paradigms.
666 (when (and (eq (1+ prefix
) 77)
667 (eq (+ hchar
12) 109)
669 (eq (- pchar
4) 111))
672 (error "%c %c %c %c" prefix hchar tchar pchar
)
673 (error "Invalid match duration"))))
674 ;; Always kill the score help buffer.
675 (gnus-score-kill-help-buffer))
677 ;; If scoring an extra (non-standard overview) header,
678 ;; we must find out which header is in question.
680 (and gnus-extra-headers
681 (equal (nth 1 entry
) "extra")
682 (intern ; need symbol
683 (gnus-completing-read-with-default
684 (symbol-name (car gnus-extra-headers
)) ; default response
685 "Score extra header" ; prompt
686 (mapcar (lambda (x) ; completion list
687 (cons (symbol-name x
) x
))
689 nil
; no completion limit
690 t
)))) ; require match
691 ;; extra is now nil or a symbol.
693 ;; We have all the data, so we enter this score.
694 (setq match
(if (string= (nth 2 entry
) "") ""
695 (gnus-summary-header (or (nth 2 entry
) (nth 1 entry
))
698 ;; Modify the match, perhaps.
700 ((equal (nth 1 entry
) "xref")
701 (when (string-match "^Xref: *" match
)
702 (setq match
(substring match
(match-end 0))))
703 (when (string-match "^[^:]* +" match
)
704 (setq match
(substring match
(match-end 0))))))
706 (when (memq type
'(r R regexp Regexp
))
707 (setq match
(regexp-quote match
)))
709 ;; Change score file to the "all.SCORE" file.
712 (set-buffer gnus-summary-buffer
)
713 (gnus-score-load-file
714 ;; This is a kludge; yes...
716 ((eq gnus-score-find-score-files-function
717 'gnus-score-find-hierarchical
)
718 (gnus-score-file-name ""))
719 ((eq gnus-score-find-score-files-function
'gnus-score-find-single
)
722 (gnus-score-file-name "all"))))))
724 (gnus-summary-score-entry
725 (nth 1 entry
) ; Header
728 (if (eq score
's
) nil score
) ; Score
729 (if (eq temporary
'perm
) ; Temp
732 (not (nth 3 entry
)) ; Prompt
734 extra
) ; non-standard overview.
737 ;; We change the score file back to the previous one.
739 (set-buffer gnus-summary-buffer
)
740 (gnus-score-load-file current-score-file
)))))
742 (defun gnus-score-insert-help (string alist idx
)
743 (setq gnus-score-help-winconf
(current-window-configuration))
745 (set-buffer (gnus-get-buffer-create "*Score Help*"))
746 (buffer-disable-undo)
747 (delete-windows-on (current-buffer))
749 (insert string
":\n\n")
754 ;; find the longest string to display
756 (setq n
(length (nth idx
(car list
))))
759 (setq list
(cdr list
)))
760 (setq max
(+ max
4)) ; %c, `:', SPACE, a SPACE at end
761 (setq n
(/ (1- (window-width)) max
)) ; items per line
762 (setq width
(/ (1- (window-width)) n
)) ; width of each item
763 ;; insert `n' items, each in a field of width `width'
768 (delete-char -
1) ; the `\n' takes a char
770 (setq pad
(- width
3))
771 (setq format
(concat "%c: %-" (int-to-string pad
) "s"))
772 (insert (format format
(caar alist
) (nth idx
(car alist
))))
773 (setq alist
(cdr alist
))
775 (goto-char (point-min))
776 ;; display ourselves in a small window at the bottom
777 (gnus-select-lowest-window)
778 (if (< (/ (window-height) 2) window-min-height
)
779 (switch-to-buffer "*Score Help*")
781 (pop-to-buffer "*Score Help*"))
782 (let ((window-min-height 1))
783 (shrink-window-if-larger-than-buffer))
784 (select-window (gnus-get-buffer-window gnus-summary-buffer t
))))
786 (defun gnus-summary-header (header &optional no-err extra
)
787 ;; Return HEADER for current articles, or error.
788 (let ((article (gnus-summary-article-number))
791 (if (and (setq headers
(gnus-summary-article-header article
))
793 (if extra
; `header' must be "extra"
794 (or (cdr (assq extra
(mail-header-extra headers
))) "")
795 (aref headers
(nth 1 (assoc header gnus-header-index
))))
798 (error "Pseudo-articles can't be scored")))
800 (error "No article on current line")
803 (defun gnus-newsgroup-score-alist ()
805 (let ((param-file (gnus-group-find-parameter
806 gnus-newsgroup-name
'score-file
)))
808 (gnus-score-load param-file
)))
810 (gnus-score-file-name gnus-newsgroup-name
)))
813 (defsubst gnus-score-get
(symbol &optional alist
)
814 ;; Get SYMBOL's definition in ALIST.
818 (gnus-newsgroup-score-alist)))))
820 (defun gnus-summary-score-entry (header match type score date
821 &optional prompt silent extra
)
822 "Enter score file entry.
823 HEADER is the header being scored.
824 MATCH is the string we are looking for.
825 TYPE is the match type: substring, regexp, exact, fuzzy.
826 SCORE is the score to add.
827 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
828 If optional argument `PROMPT' is non-nil, allow user to edit match.
829 If optional argument `SILENT' is nil, show effect of score entry.
830 If optional argument `EXTRA' is non-nil, it's a non-standard overview header."
831 ;; Regexp is the default type.
834 ;; Simplify matches...
835 (cond ((or (eq type
'r
) (eq type
's
) (eq type nil
))
836 (setq match
(if match
(gnus-simplify-subject-re match
) "")))
838 (setq match
(gnus-simplify-subject-fuzzy match
))))
839 (let ((score (gnus-score-delta-default score
))
840 (header (downcase header
))
842 (set-text-properties 0 (length header
) nil header
)
844 (setq match
(read-string
845 (format "Match %s on %s, %s: "
846 (cond ((eq date
'now
)
852 (if (< score
0) "lower" "raise"))
854 (int-to-string match
)
857 ;; If this is an integer comparison, we transform from string to int.
858 (if (eq (nth 2 (assoc header gnus-header-index
)) 'gnus-score-integer
)
860 (setq match
(string-to-number match
)))
861 (set-text-properties 0 (length match
) nil match
))
863 (unless (eq date
'now
)
864 ;; Add the score entry to the score file.
865 (when (= score gnus-score-interactive-default-score
)
867 (let ((old (gnus-score-get header
))
873 (and date
(if (numberp date
) date
875 type
(symbol-name extra
)))
878 (and date
(if (numberp date
) date
881 (date (list match score
(date-to-day date
)))
882 (score (list match score
))
884 ;; We see whether we can collapse some score entries.
885 ;; This isn't quite correct, because there may be more elements
886 ;; later on with the same key that have matching elems... Hm.
888 (setq elem
(assoc match old
))
889 (eq (nth 3 elem
) (nth 3 new
))
890 (or (and (numberp (nth 2 elem
)) (numberp (nth 2 new
)))
891 (and (not (nth 2 elem
)) (not (nth 2 new
)))))
892 ;; Yup, we just add this new score to the old elem.
893 (setcar (cdr elem
) (+ (or (nth 1 elem
)
894 gnus-score-interactive-default-score
)
896 gnus-score-interactive-default-score
)))
897 ;; Nope, we have to add a new elem.
898 (gnus-score-set header
(if old
(cons new old
) (list new
)) nil t
))
899 (gnus-score-set 'touched
'(t))))
901 ;; Score the current buffer.
903 (if (and (>= (nth 1 (assoc header gnus-header-index
)) 0)
904 (eq (nth 2 (assoc header gnus-header-index
))
906 (gnus-summary-score-effect header match type score extra
)
907 (gnus-summary-rescore)))
909 ;; Return the new scoring rule.
912 (defun gnus-summary-score-effect (header match type score
&optional extra
)
913 "Simulate the effect of a score file entry.
914 HEADER is the header being scored.
915 MATCH is the string we are looking for.
916 TYPE is the score type.
917 SCORE is the score to add.
918 EXTRA is the possible non-standard header."
919 (interactive (list (completing-read "Header: "
921 (lambda (x) (fboundp (nth 2 x
)))
923 (read-string "Match: ")
924 (if (y-or-n-p "Use regexp match? ") 'r
's
)
925 (string-to-number (read-string "Score: "))))
927 (unless (and (stringp match
) (> (length match
) 0))
929 (goto-char (point-min))
930 (let ((regexp (cond ((eq type
'f
)
931 (gnus-simplify-subject-fuzzy match
))
935 (concat "\\`" (regexp-quote match
) "\\'"))
937 (regexp-quote match
)))))
939 (let ((content (gnus-summary-header header
'noerr extra
))
940 (case-fold-search t
))
942 (when (if (eq type
'f
)
943 (string-equal (gnus-simplify-subject-fuzzy content
)
945 (string-match regexp content
))
946 (gnus-summary-raise-score score
))))
947 (beginning-of-line 2))))
948 (gnus-set-mode-line 'summary
))
950 (defun gnus-summary-score-crossposting (score date
)
951 ;; Enter score file entry for current crossposting.
952 ;; SCORE is the score to add.
953 ;; DATE is the expire date.
954 (let ((xref (gnus-summary-header "xref"))
958 (error "This article is not crossposted"))
959 (while (string-match " \\([^ \t]+\\):" xref start
)
960 (setq start
(match-end 0))
963 (substring xref
(match-beginning 1) (match-end 1)))
964 gnus-newsgroup-name
))
965 (gnus-summary-score-entry
966 "xref" (concat " " group
":") nil score date t
)))))
973 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
975 (defun gnus-score-set-mark-below (score)
976 "Automatically mark articles with score below SCORE as read."
978 (list (or (and current-prefix-arg
(prefix-numeric-value current-prefix-arg
))
979 (string-to-number (read-string "Mark below: ")))))
980 (setq score
(or score gnus-summary-default-score
0))
981 (gnus-score-set 'mark
(list score
))
982 (gnus-score-set 'touched
'(t))
983 (setq gnus-summary-mark-below score
)
984 (gnus-score-update-lines))
986 (defun gnus-score-update-lines ()
987 "Update all lines in the summary buffer."
989 (goto-char (point-min))
991 (gnus-summary-update-line)
994 (defun gnus-score-update-all-lines ()
995 "Update all lines in the summary buffer, even the hidden ones."
997 (goto-char (point-min))
1000 (when (gnus-summary-show-thread)
1001 (push (point) hidden
))
1002 (gnus-summary-update-line)
1004 ;; Re-hide the hidden threads.
1006 (goto-char (pop hidden
))
1007 (gnus-summary-hide-thread)))))
1009 (defun gnus-score-set-expunge-below (score)
1010 "Automatically expunge articles with score below SCORE."
1012 (list (or (and current-prefix-arg
(prefix-numeric-value current-prefix-arg
))
1013 (string-to-number (read-string "Set expunge below: ")))))
1014 (setq score
(or score gnus-summary-default-score
0))
1015 (gnus-score-set 'expunge
(list score
))
1016 (gnus-score-set 'touched
'(t)))
1018 (defun gnus-score-followup-article (&optional score
)
1019 "Add SCORE to all followups to the article in the current buffer."
1021 (setq score
(gnus-score-delta-default score
))
1022 (when (gnus-buffer-live-p gnus-summary-buffer
)
1025 (message-narrow-to-headers)
1026 (let ((id (mail-fetch-field "message-id")))
1028 (set-buffer gnus-summary-buffer
)
1029 (gnus-summary-score-entry
1030 "references" (concat id
"[ \t]*$") 'r
1031 score
(current-time-string) nil t
)))))))
1033 (defun gnus-score-followup-thread (&optional score
)
1034 "Add SCORE to all later articles in the thread the current buffer is part of."
1036 (setq score
(gnus-score-delta-default score
))
1037 (when (gnus-buffer-live-p gnus-summary-buffer
)
1040 (goto-char (point-min))
1041 (let ((id (mail-fetch-field "message-id")))
1043 (set-buffer gnus-summary-buffer
)
1044 (gnus-summary-score-entry
1046 score
(current-time-string))))))))
1048 (defun gnus-score-set (symbol value
&optional alist warn
)
1049 ;; Set SYMBOL to VALUE in ALIST.
1053 (gnus-newsgroup-score-alist)))
1054 (entry (assoc symbol alist
)))
1055 (cond ((gnus-score-get 'read-only alist
)
1056 ;; This is a read-only score file, so we do nothing.
1058 (gnus-message 4 "Note: read-only score file; entry discarded")))
1060 (setcdr entry value
))
1062 (error "Empty alist"))
1065 (cons (cons symbol value
) (cdr alist
)))))))
1067 (defun gnus-summary-raise-score (n)
1068 "Raise the score of the current article by N."
1070 (gnus-summary-set-score (+ (gnus-summary-article-score)
1071 (or n gnus-score-interactive-default-score
))))
1073 (defun gnus-summary-set-score (n)
1074 "Set the score of the current article to N."
1077 (gnus-summary-show-thread)
1078 (let ((buffer-read-only nil
))
1080 (gnus-summary-update-mark
1081 (if (= n
(or gnus-summary-default-score
0)) ?
;Whitespace
1082 (if (< n
(or gnus-summary-default-score
0))
1083 gnus-score-below-mark gnus-score-over-mark
))
1085 (let* ((article (gnus-summary-article-number))
1086 (score (assq article gnus-newsgroup-scored
)))
1087 (if score
(setcdr score n
)
1088 (push (cons article n
) gnus-newsgroup-scored
)))
1089 (gnus-summary-update-line)))
1091 (defun gnus-summary-current-score ()
1092 "Return the score of the current article."
1094 (gnus-message 1 "%s" (gnus-summary-article-score)))
1096 (defun gnus-score-change-score-file (file)
1097 "Change current score alist."
1099 (list (read-file-name "Change to score file: " gnus-kill-files-directory
)))
1100 (gnus-score-load-file file
)
1101 (gnus-set-mode-line 'summary
))
1103 (defvar gnus-score-edit-exit-function
)
1104 (defun gnus-score-edit-current-scores (file)
1105 "Edit the current score alist."
1106 (interactive (list gnus-current-score-file
))
1107 (if (not gnus-current-score-file
)
1108 (error "No current score file")
1109 (let ((winconf (current-window-configuration)))
1110 (when (buffer-name gnus-summary-buffer
)
1112 (gnus-make-directory (file-name-directory file
))
1113 (setq gnus-score-edit-buffer
(find-file-noselect file
))
1114 (gnus-configure-windows 'edit-score
)
1116 (setq gnus-score-edit-exit-function
'gnus-score-edit-done
)
1117 (make-local-variable 'gnus-prev-winconf
)
1118 (setq gnus-prev-winconf winconf
))
1120 4 (substitute-command-keys
1121 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits"))))
1123 (defun gnus-score-edit-all-score ()
1124 "Edit the all.SCORE file."
1126 (find-file (gnus-score-file-name "all"))
1128 (setq gnus-score-edit-exit-function
'gnus-score-edit-done
)
1130 4 (substitute-command-keys
1131 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
1133 (defun gnus-score-edit-file (file)
1134 "Edit a score file."
1136 (list (read-file-name "Edit score file: " gnus-kill-files-directory
)))
1137 (gnus-make-directory (file-name-directory file
))
1138 (when (buffer-name gnus-summary-buffer
)
1140 (let ((winconf (current-window-configuration)))
1141 (setq gnus-score-edit-buffer
(find-file-noselect file
))
1142 (gnus-configure-windows 'edit-score
)
1144 (setq gnus-score-edit-exit-function
'gnus-score-edit-done
)
1145 (make-local-variable 'gnus-prev-winconf
)
1146 (setq gnus-prev-winconf winconf
))
1148 4 (substitute-command-keys
1149 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
1151 (defun gnus-score-edit-file-at-point (&optional format
)
1152 "Edit score file at point in Score Trace buffers.
1153 If FORMAT, also format the current score file."
1154 (let* ((rule (save-excursion
1156 (read (current-buffer))))
1158 ;; Must be synced with `gnus-score-find-trace':
1160 (file (save-excursion
1162 (if (and (re-search-backward reg
(point-at-bol) t
)
1163 (re-search-forward reg
(point-at-eol) t
))
1164 (buffer-substring (point) (point-at-eol))
1167 (string-match "\\<\\(non-file rule\\|A file\\)\\>" file
)
1168 ;; (see `gnus-score-find-trace' and `gnus-score-advanced')
1170 (gnus-error 3 "Can't find a score file in current line.")
1171 (gnus-score-edit-file file
)
1173 (gnus-score-pretty-print))
1174 (when (consp rule
) ;; the rule exists
1175 (setq rule
(mapconcat #'(lambda (obj)
1176 (regexp-quote (format "%S" obj
)))
1179 (goto-char (point-min))
1180 (re-search-forward rule nil t
)
1181 ;; make it easy to use `kill-sexp':
1182 (goto-char (1- (match-beginning 0)))))))
1184 (defun gnus-score-load-file (file)
1185 ;; Load score file FILE. Returns a list a retrieved score-alists.
1186 (let* ((file (expand-file-name
1187 (or (and (string-match
1188 (concat "^" (regexp-quote
1190 gnus-kill-files-directory
)))
1191 (expand-file-name file
))
1193 (expand-file-name file gnus-kill-files-directory
))))
1194 (cached (assoc file gnus-score-cache
))
1195 (global (member file gnus-internal-global-score-files
))
1198 ;; The score file was already loaded.
1199 (setq alist
(cdr cached
))
1200 ;; We load the score file.
1201 (setq gnus-score-alist nil
)
1202 (setq alist
(gnus-score-load-score-alist file
))
1203 ;; We add '(touched) to the alist to signify that it hasn't been
1205 (unless (assq 'touched alist
)
1206 (push (list 'touched nil
) alist
))
1207 ;; If it is a global score file, we make it read-only.
1209 (not (assq 'read-only alist
))
1210 (push (list 'read-only t
) alist
))
1211 (push (cons file alist
) gnus-score-cache
))
1215 ;; Downcase all header names.
1218 (setcar (car a
) (downcase (caar a
)))
1220 ;; Advanced scoring.
1224 ;; If there are actual scores in the alist, we add it to the
1225 ;; return value of this function.
1227 (setq lists
(list alist
))))
1228 ;; Treat the other possible atoms in the score alist.
1229 (let ((mark (car (gnus-score-get 'mark alist
)))
1230 (expunge (car (gnus-score-get 'expunge alist
)))
1231 (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist
)))
1232 (files (gnus-score-get 'files alist
))
1233 (exclude-files (gnus-score-get 'exclude-files alist
))
1234 (orphan (car (gnus-score-get 'orphan alist
)))
1235 (adapt (gnus-score-get 'adapt alist
))
1236 (thread-mark-and-expunge
1237 (car (gnus-score-get 'thread-mark-and-expunge alist
)))
1238 (adapt-file (car (gnus-score-get 'adapt-file alist
)))
1239 (local (gnus-score-get 'local alist
))
1240 (decay (car (gnus-score-get 'decay alist
)))
1241 (eval (car (gnus-score-get 'eval alist
))))
1242 ;; Perform possible decays.
1243 (when (and (if (stringp gnus-decay-scores
)
1244 (string-match gnus-decay-scores file
)
1246 (or cached
(file-exists-p file
))
1248 (gnus-decay-scores alist decay
)))
1249 (gnus-score-set 'touched
'(t) alist
)
1250 (gnus-score-set 'decay
(list (time-to-days (current-time))) alist
))
1251 ;; We do not respect eval and files atoms from global score
1253 (when (and files
(not global
))
1254 (setq lists
(apply 'append lists
1255 (mapcar 'gnus-score-load-file
1256 (if adapt-file
(cons adapt-file files
)
1258 (when (and eval
(not global
))
1260 ;; We then expand any exclude-file directives.
1261 (setq gnus-scores-exclude-files
1268 (expand-file-name sfile
(file-name-directory file
))
1269 (expand-file-name sfile gnus-kill-files-directory
)))
1271 gnus-scores-exclude-files
))
1274 (set-buffer gnus-summary-buffer
)
1276 (and (consp (car local
))
1277 (symbolp (caar local
))
1279 (make-local-variable (caar local
))
1280 (set (caar local
) (nth 1 (car local
)))))
1281 (setq local
(cdr local
)))))
1283 (setq gnus-orphan-score orphan
))
1284 (setq gnus-adaptive-score-alist
1285 (cond ((equal adapt
'(t))
1286 (setq gnus-newsgroup-adaptive t
)
1287 gnus-default-adaptive-score-alist
)
1288 ((equal adapt
'(ignore))
1289 (setq gnus-newsgroup-adaptive nil
))
1291 (setq gnus-newsgroup-adaptive t
)
1294 gnus-default-adaptive-score-alist
)))
1295 (setq gnus-thread-expunge-below
1296 (or thread-mark-and-expunge gnus-thread-expunge-below
))
1297 (setq gnus-summary-mark-below
1298 (or mark mark-and-expunge gnus-summary-mark-below
))
1299 (setq gnus-summary-expunge-below
1300 (or expunge mark-and-expunge gnus-summary-expunge-below
))
1301 (setq gnus-newsgroup-adaptive-score-file
1302 (or adapt-file gnus-newsgroup-adaptive-score-file
)))
1303 (setq gnus-current-score-file file
)
1304 (setq gnus-score-alist alist
)
1307 (defun gnus-score-load (file)
1309 (let ((cache (assoc file gnus-score-cache
)))
1311 (setq gnus-score-alist
(cdr cache
))
1312 (setq gnus-score-alist nil
)
1313 (gnus-score-load-score-alist file
)
1314 (unless gnus-score-alist
1315 (setq gnus-score-alist
(copy-alist '((touched nil
)))))
1316 (push (cons file gnus-score-alist
) gnus-score-cache
))))
1318 (defun gnus-score-remove-from-cache (file)
1319 (setq gnus-score-cache
1320 (delq (assoc file gnus-score-cache
) gnus-score-cache
)))
1322 (defun gnus-score-load-score-alist (file)
1325 (if (not (file-readable-p file
))
1326 ;; Couldn't read file.
1327 (setq gnus-score-alist nil
)
1330 (let ((coding-system-for-read score-mode-coding-system
))
1331 (insert-file-contents file
))
1332 (goto-char (point-min))
1333 ;; Only do the loading if the score file isn't empty.
1334 (when (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t
))
1337 (read (current-buffer))
1339 (gnus-error 3.2 "Problem with score file %s" file
))))))
1343 ;; Bogus score file.
1344 (error "Invalid syntax with score file %s" file
))
1345 ((eq (car alist
) 'setq
)
1346 ;; This is an old-style score file.
1347 (setq gnus-score-alist
(gnus-score-transform-old-to-new alist
)))
1349 (setq gnus-score-alist alist
)))
1350 ;; Check the syntax of the score file.
1351 (setq gnus-score-alist
1352 (gnus-score-check-syntax gnus-score-alist file
)))))
1354 (defun gnus-score-check-syntax (alist file
)
1355 "Check the syntax of the score ALIST."
1359 ((not (consp alist
))
1360 (gnus-message 1 "Score file is not a list: %s" file
)
1366 (while (and a
(not err
))
1370 ((not (listp (car a
)))
1371 (format "Invalid score element %s in %s" (car a
) file
))
1374 ((not (listp (setq sr
(cdar a
))))
1375 (format "Invalid header match %s in %s" (nth 1 (car a
)) file
))
1377 (setq type
(caar a
))
1378 (while (and sr
(not err
))
1383 ((if (member (downcase type
) '("lines" "chars"))
1384 (not (numberp (car s
)))
1385 (not (stringp (car s
))))
1386 (format "Invalid match %s in %s" (car s
) file
))
1387 ((and (cadr s
) (not (integerp (cadr s
))))
1388 (format "Non-integer score %s in %s" (cadr s
) file
))
1389 ((and (caddr s
) (not (integerp (caddr s
))))
1390 (format "Non-integer date %s in %s" (caddr s
) file
))
1391 ((and (cadddr s
) (not (symbolp (cadddr s
))))
1392 (format "Non-symbol match type %s in %s" (cadddr s
) file
)))))
1398 (gnus-message 3 err
)
1403 (defun gnus-score-transform-old-to-new (alist)
1404 (let* ((alist (nth 2 alist
))
1406 (when (eq (car alist
) 'quote
)
1407 (setq alist
(nth 1 alist
)))
1409 (setq entry
(car alist
))
1410 (if (stringp (car entry
))
1411 (let ((scor (cdr entry
)))
1415 (list (caar scor
) (nth 2 (car scor
))
1416 (and (nth 3 (car scor
))
1417 (date-to-day (nth 3 (car scor
))))
1418 (if (nth 1 (car scor
)) 'r
's
)))
1419 (setq scor
(cdr scor
))))
1420 (push (if (not (listp (cdr entry
)))
1421 (list (car entry
) (cdr entry
))
1424 (setq alist
(cdr alist
)))
1425 (cons (list 'touched t
) (nreverse out
))))
1427 (defun gnus-score-save ()
1428 ;; Save all score information.
1429 (let ((cache gnus-score-cache
)
1432 (setq gnus-score-alist nil
)
1433 (nnheader-set-temp-buffer " *Gnus Scores*")
1436 (setq entry
(pop cache
)
1437 file
(nnheader-translate-file-chars (car entry
) t
)
1439 (if (or (not (equal (gnus-score-get 'touched score
) '(t)))
1440 (gnus-score-get 'read-only score
)
1441 (and (file-exists-p file
)
1442 (not (file-writable-p file
))))
1444 (setq score
(setcdr entry
(gnus-delete-alist 'touched score
)))
1446 (let (emacs-lisp-mode-hook)
1447 (if (and (not gnus-adaptive-pretty-print
)
1449 (concat (regexp-quote gnus-adaptive-file-suffix
) "$")
1451 ;; This is an adaptive score file, so we do not run it through
1452 ;; `pp' unless requested. These files can get huge, and are
1453 ;; not meant to be edited by human hands.
1455 ;; This is a normal score file, so we print it very
1457 (let ((lisp-mode-syntax-table score-mode-syntax-table
))
1459 (gnus-make-directory (file-name-directory file
))
1460 ;; If the score file is empty, we delete it.
1461 (if (zerop (buffer-size))
1463 ;; There are scores, so we write the file.
1464 (when (file-writable-p file
)
1465 (let ((coding-system-for-write score-mode-coding-system
))
1466 (gnus-write-buffer file
))
1467 (when gnus-score-after-write-file-function
1468 (funcall gnus-score-after-write-file-function file
)))))
1469 (and gnus-score-uncacheable-files
1470 (string-match gnus-score-uncacheable-files file
)
1471 (gnus-score-remove-from-cache file
)))
1472 (kill-buffer (current-buffer)))))
1474 (defun gnus-score-load-files (score-files)
1475 "Load all score files in SCORE-FILES."
1476 ;; Load the score files.
1479 (if (stringp (car score-files
))
1480 ;; It is a string, which means that it's a score file name,
1481 ;; so we load the score file and add the score alist to
1482 ;; the list of alists.
1483 (setq scores
(nconc (gnus-score-load-file (car score-files
)) scores
))
1484 ;; It is an alist, so we just add it to the list directly.
1485 (setq scores
(nconc (car score-files
) scores
)))
1486 (setq score-files
(cdr score-files
)))
1487 ;; Prune the score files that are to be excluded, if any.
1488 (when gnus-scores-exclude-files
1492 (and (setq c
(rassq (car s
) gnus-score-cache
))
1493 (member (car c
) gnus-scores-exclude-files
)
1494 (setq scores
(delq (car s
) scores
)))
1498 (defun gnus-score-headers (score-files &optional trace
)
1499 ;; Score `gnus-newsgroup-headers'.
1501 ;; PLM: probably this is not the best place to clear orphan-score
1502 (setq gnus-orphan-score nil
1503 gnus-scores-articles nil
1504 gnus-scores-exclude-files nil
1505 scores
(gnus-score-load-files score-files
))
1511 (when (and gnus-summary-default-score
1513 (let* ((entries gnus-header-index
)
1514 (now (date-to-day (current-time-string)))
1515 (expire (and gnus-score-expiry-days
1516 (- now gnus-score-expiry-days
)))
1517 (headers gnus-newsgroup-headers
)
1518 (current-score-file gnus-current-score-file
)
1520 (gnus-message 7 "Scoring...")
1521 ;; Create articles, an alist of the form `(HEADER . SCORE)'.
1522 (while (setq header
(pop headers
))
1523 ;; WARNING: The assq makes the function O(N*S) while it could
1524 ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
1525 ;; and S is (length gnus-newsgroup-scored).
1526 (unless (assq (mail-header-number header
) gnus-newsgroup-scored
)
1527 (setq gnus-scores-articles
;Total of 2 * N cons-cells used.
1528 (cons (cons header
(or gnus-summary-default-score
0))
1529 gnus-scores-articles
))))
1532 (set-buffer (gnus-get-buffer-create "*Headers*"))
1533 (buffer-disable-undo)
1534 (when (gnus-buffer-live-p gnus-summary-buffer
)
1535 (message-clone-locals gnus-summary-buffer
))
1537 ;; Set the global variant of this variable.
1538 (setq gnus-current-score-file current-score-file
)
1540 (when gnus-orphan-score
1541 (setq gnus-score-index
1542 (nth 1 (assoc "references" gnus-header-index
)))
1543 (gnus-score-orphans gnus-orphan-score
))
1544 ;; Run each header through the score process.
1546 (setq entry
(pop entries
)
1547 header
(nth 0 entry
)
1548 gnus-score-index
(nth 1 (assoc header gnus-header-index
)))
1549 (when (< 0 (apply 'max
(mapcar
1551 (length (gnus-score-get header score
)))
1553 (when (if (and gnus-inhibit-slow-scoring
1554 (or (eq gnus-inhibit-slow-scoring t
)
1555 (and (stringp gnus-inhibit-slow-scoring
)
1556 ;; Always true here?
1557 ;; (stringp gnus-newsgroup-name)
1559 gnus-inhibit-slow-scoring
1560 gnus-newsgroup-name
)))
1561 (> 0 (nth 1 (assoc header gnus-header-index
))))
1564 7 "Scoring on headers or body skipped.")
1566 ;; Call the scoring function for this type of "header".
1567 (setq new
(funcall (nth 2 entry
) scores header
1570 (when (gnus-buffer-live-p gnus-summary-buffer
)
1571 (let ((scored gnus-newsgroup-scored
))
1572 (with-current-buffer gnus-summary-buffer
1573 (setq gnus-newsgroup-scored scored
))))
1574 ;; Remove the buffer.
1575 (gnus-kill-buffer (current-buffer)))
1577 ;; Add articles to `gnus-newsgroup-scored'.
1578 (while gnus-scores-articles
1579 (when (or (/= gnus-summary-default-score
1580 (cdar gnus-scores-articles
))
1582 (push (cons (mail-header-number (caar gnus-scores-articles
))
1583 (cdar gnus-scores-articles
))
1584 gnus-newsgroup-scored
))
1585 (setq gnus-scores-articles
(cdr gnus-scores-articles
)))
1588 (while (setq score
(pop scores
))
1590 (when (consp (caar score
))
1591 (gnus-score-advanced (car score
) trace
))
1594 (gnus-message 7 "Scoring...done"))))))
1596 (defun gnus-score-lower-thread (thread score-adjust
)
1597 "Lower the score on THREAD with SCORE-ADJUST.
1598 THREAD is expected to contain a list of the form `(PARENT [CHILD1
1599 CHILD2 ...])' where PARENT is a header array and each CHILD is a list
1600 of the same form as THREAD. The empty list nil is valid. For each
1601 article in the tree, the score of the corresponding entry in
1602 `gnus-newsgroup-scored' is adjusted by SCORE-ADJUST."
1604 (let ((head (car thread
)))
1606 ;; handle a child and its descendants
1607 (gnus-score-lower-thread head score-adjust
)
1608 ;; handle the parent
1609 (let* ((article (mail-header-number head
))
1610 (score (assq article gnus-newsgroup-scored
)))
1611 (if score
(setcdr score
(+ (cdr score
) score-adjust
))
1612 (push (cons article score-adjust
) gnus-newsgroup-scored
)))))
1613 (setq thread
(cdr thread
))))
1615 (defun gnus-score-orphans (score)
1617 A root is an article with no references. An orphan is an article
1618 which has references, but is not connected via its references to a
1619 root article. This function finds all the orphans, and adjusts their
1620 score in `gnus-newsgroup-scored' by SCORE."
1621 ;; gnus-make-threads produces a list, where each entry is a "thread"
1622 ;; as described in the gnus-score-lower-thread docs. This function
1623 ;; will be called again (after limiting has been done) if the display
1624 ;; is threaded. It would be nice to somehow save this info and use
1626 (dolist (thread (gnus-make-threads))
1627 (let ((id (aref (car thread
) gnus-score-index
)))
1628 ;; If the parent of the thread is not a root, lower the score of
1629 ;; it and its descendants. Note that some roots seem to satisfy
1630 ;; (eq id nil) and some (eq id ""); not sure why.
1632 (not (string= id
"")))
1633 (gnus-score-lower-thread thread score
)))))
1635 (defun gnus-score-integer (scores header now expire
&optional trace
)
1636 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index
)))
1640 (setq alist
(car scores
)
1642 entries
(assoc header alist
))
1643 (while (cdr entries
) ;First entry is the header index.
1644 (let* ((rest (cdr entries
))
1646 (match (nth 0 kill
))
1647 (type (or (nth 3 kill
) '>))
1648 (score (or (nth 1 kill
) gnus-score-interactive-default-score
))
1651 (match-func (if (or (eq type
'>) (eq type
'<) (eq type
'<=)
1652 (eq type
'>=) (eq type
'=))
1654 (error "Invalid match type: %s" type
)))
1655 (articles gnus-scores-articles
))
1656 ;; Instead of doing all the clever stuff that
1657 ;; `gnus-score-string' does to minimize searches and stuff,
1658 ;; I will assume that people generally will put so few
1659 ;; matches on numbers that any cleverness will take more
1660 ;; time than one would gain.
1662 (when (funcall match-func
1663 (or (aref (caar articles
) gnus-score-index
) 0)
1666 (push (cons (car-safe (rassq alist gnus-score-cache
)) kill
)
1669 (setcdr (car articles
) (+ score
(cdar articles
))))
1670 (setq articles
(cdr articles
)))
1671 ;; Update expire date
1672 (cond ((null date
)) ;Permanent entry.
1673 ((and found gnus-update-score-entry-dates
) ;Match, update date.
1674 (gnus-score-set 'touched
'(t) alist
)
1675 (setcar (nthcdr 2 kill
) now
))
1676 ((and expire
(< date expire
)) ;Old entry, remove.
1677 (gnus-score-set 'touched
'(t) alist
)
1678 (setcdr entries
(cdr rest
))
1679 (setq rest entries
)))
1680 (setq entries rest
)))))
1683 (defun gnus-score-date (scores header now expire
&optional trace
)
1684 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index
)))
1685 entries alist match match-func article
)
1688 (setq alist
(car scores
)
1690 entries
(assoc header alist
))
1691 (while (cdr entries
) ;First entry is the header index.
1692 (let* ((rest (cdr entries
))
1694 (type (or (nth 3 kill
) 'before
))
1695 (score (or (nth 1 kill
) gnus-score-interactive-default-score
))
1698 (articles gnus-scores-articles
)
1702 (setq match-func
'string
<
1703 match
(gnus-date-iso8601 (nth 0 kill
))))
1705 (setq match-func
'gnus-string
>
1706 match
(gnus-date-iso8601 (nth 0 kill
))))
1708 (setq match-func
'string
=
1709 match
(gnus-date-iso8601 (nth 0 kill
))))
1711 (setq match-func
'string-match
1712 match
(nth 0 kill
)))
1713 (t (error "Invalid match type: %s" type
)))
1714 ;; Instead of doing all the clever stuff that
1715 ;; `gnus-score-string' does to minimize searches and stuff,
1716 ;; I will assume that people generally will put so few
1717 ;; matches on numbers that any cleverness will take more
1718 ;; time than one would gain.
1719 (while (setq article
(pop articles
))
1721 (setq l
(aref (car article
) gnus-score-index
))
1722 (funcall match-func match
(gnus-date-iso8601 l
)))
1724 (push (cons (car-safe (rassq alist gnus-score-cache
)) kill
)
1727 (setcdr article
(+ score
(cdr article
)))))
1728 ;; Update expire date
1729 (cond ((null date
)) ;Permanent entry.
1730 ((and found gnus-update-score-entry-dates
) ;Match, update date.
1731 (gnus-score-set 'touched
'(t) alist
)
1732 (setcar (nthcdr 2 kill
) now
))
1733 ((and expire
(< date expire
)) ;Old entry, remove.
1734 (gnus-score-set 'touched
'(t) alist
)
1735 (setcdr entries
(cdr rest
))
1736 (setq rest entries
)))
1737 (setq entries rest
)))))
1740 (defun gnus-score-body (scores header now expire
&optional trace
)
1741 (if gnus-agent-fetching
1744 (setq gnus-scores-articles
1745 (sort gnus-scores-articles
1747 (< (mail-header-number (car a1
))
1748 (mail-header-number (car a2
))))))
1749 (set-buffer nntp-server-buffer
)
1751 (let* ((buffer-read-only nil
)
1752 (articles gnus-scores-articles
)
1754 (request-func (cond ((string= "head" header
)
1756 ((string= "body" header
)
1758 (t 'gnus-request-article
)))
1759 entries alist ofunc article last
)
1761 (setq last
(mail-header-number (caar (last articles
))))
1762 ;; Not all backends support partial fetching. In that case,
1763 ;; we just fetch the entire article.
1764 (unless (gnus-check-backend-function
1765 (and (string-match "^gnus-" (symbol-name request-func
))
1766 (intern (substring (symbol-name request-func
)
1768 gnus-newsgroup-name
)
1769 (setq ofunc request-func
)
1770 (setq request-func
'gnus-request-article
))
1772 (setq article
(mail-header-number (caar articles
)))
1773 (gnus-message 7 "Scoring article %s of %s..." article last
)
1775 (when (funcall request-func article gnus-newsgroup-name
)
1776 (goto-char (point-min))
1777 ;; If just parts of the article is to be searched, but the
1778 ;; backend didn't support partial fetching, we just narrow
1779 ;; to the relevant parts.
1781 (if (eq ofunc
'gnus-request-head
)
1784 (or (search-forward "\n\n" nil t
) (point-max)))
1786 (or (search-forward "\n\n" nil t
) (point))
1788 (setq scores all-scores
)
1791 (setq alist
(pop scores
)
1792 entries
(assoc header alist
))
1793 (while (cdr entries
) ;First entry is the header index.
1794 (let* ((rest (cdr entries
))
1796 (match (nth 0 kill
))
1797 (type (or (nth 3 kill
) 's
))
1798 (score (or (nth 1 kill
)
1799 gnus-score-interactive-default-score
))
1803 (not (or (eq type
'R
) (eq type
'S
)
1804 (eq type
'Regexp
) (eq type
'String
))))
1806 (cond ((or (eq type
'r
) (eq type
'R
)
1807 (eq type
'regexp
) (eq type
'Regexp
))
1809 ((or (eq type
's
) (eq type
'S
)
1810 (eq type
'string
) (eq type
'String
))
1813 (error "Invalid match type: %s" type
)))))
1814 (goto-char (point-min))
1815 (when (funcall search-func match nil t
)
1816 ;; Found a match, update scores.
1817 (setcdr (car articles
) (+ score
(cdar articles
)))
1821 (cons (car-safe (rassq alist gnus-score-cache
))
1824 ;; Update expire date
1827 ((null date
)) ;Permanent entry.
1828 ((and found gnus-update-score-entry-dates
)
1829 ;; Match, update date.
1830 (gnus-score-set 'touched
'(t) alist
)
1831 (setcar (nthcdr 2 kill
) now
))
1832 ((and expire
(< date expire
)) ;Old entry, remove.
1833 (gnus-score-set 'touched
'(t) alist
)
1834 (setcdr entries
(cdr rest
))
1835 (setq rest entries
))))
1836 (setq entries rest
)))))
1837 (setq articles
(cdr articles
)))))))
1840 (defun gnus-score-thread (scores header now expire
&optional trace
)
1841 (gnus-score-followup scores header now expire trace t
))
1843 (defun gnus-score-followup (scores header now expire
&optional trace thread
)
1844 (if gnus-agent-fetching
1845 ;; FIXME: It seems doable in fetching mode.
1847 ;; Insert the unique article headers in the buffer.
1848 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index
)))
1849 (current-score-file gnus-current-score-file
)
1851 ;; gnus-score-index is used as a free variable.
1852 alike last this art entries alist articles
1855 ;; Change score file to the adaptive score file. All entries that
1856 ;; this function makes will be put into this file.
1858 (set-buffer gnus-summary-buffer
)
1859 (gnus-score-load-file
1860 (or gnus-newsgroup-adaptive-score-file
1861 (gnus-score-file-name
1862 gnus-newsgroup-name gnus-adaptive-file-suffix
))))
1864 (setq gnus-scores-articles
(sort gnus-scores-articles
1865 'gnus-score-string
<)
1866 articles gnus-scores-articles
)
1870 (setq art
(car articles
)
1871 this
(aref (car art
) gnus-score-index
)
1872 articles
(cdr articles
))
1873 (if (equal last this
)
1877 (put-text-property (1- (point)) (point) 'articles alike
))
1878 (setq alike
(list art
)
1880 (when last
; Bwadr, duplicate code.
1882 (put-text-property (1- (point)) (point) 'articles alike
))
1886 (setq alist
(car scores
)
1888 entries
(assoc header alist
))
1889 (while (cdr entries
) ;First entry is the header index.
1890 (let* ((rest (cdr entries
))
1892 (match (nth 0 kill
))
1893 (type (or (nth 3 kill
) 's
))
1894 (score (or (nth 1 kill
) gnus-score-interactive-default-score
))
1897 (mt (aref (symbol-name type
) 0))
1899 (not (or (= mt ?R
) (= mt ?S
) (= mt ?E
) (= mt ?F
))))
1902 (cond ((= dmt ?r
) 're-search-forward
)
1903 ((or (= dmt ?e
) (= dmt ?s
) (= dmt ?f
)) 'search-forward
)
1904 (t (error "Invalid match type: %s" type
))))
1906 (goto-char (point-min))
1908 (while (funcall search-func match nil t
)
1909 (and (= (point-at-bol)
1910 (match-beginning 0))
1911 (= (progn (end-of-line) (point))
1914 (setq found
(setq arts
(get-text-property
1915 (point) 'articles
)))
1916 ;; Found a match, update scores.
1918 (setq art
(car arts
)
1920 (gnus-score-add-followups
1921 (car art
) score all-scores thread
))))
1923 (while (funcall search-func match nil t
)
1925 (setq found
(setq arts
(get-text-property (point) 'articles
)))
1926 ;; Found a match, update scores.
1927 (while (setq art
(pop arts
))
1928 (setcdr art
(+ score
(cdr art
)))
1931 (car-safe (rassq alist gnus-score-cache
))
1934 (when (setq new
(gnus-score-add-followups
1935 (car art
) score all-scores thread
))
1937 ;; Update expire date
1938 (cond ((null date
)) ;Permanent entry.
1939 ((and found gnus-update-score-entry-dates
)
1940 ;Match, update date.
1941 (gnus-score-set 'touched
'(t) alist
)
1942 (setcar (nthcdr 2 kill
) now
))
1943 ((and expire
(< date expire
)) ;Old entry, remove.
1944 (gnus-score-set 'touched
'(t) alist
)
1945 (setcdr entries
(cdr rest
))
1946 (setq rest entries
)))
1947 (setq entries rest
))))
1948 ;; We change the score file back to the previous one.
1950 (set-buffer gnus-summary-buffer
)
1951 (gnus-score-load-file current-score-file
))
1952 (list (cons "references" news
)))))
1954 (defun gnus-score-add-followups (header score scores
&optional thread
)
1955 "Add a score entry to the adapt file."
1957 (set-buffer gnus-summary-buffer
)
1958 (let* ((id (mail-header-id header
))
1959 (scores (car scores
))
1961 ;; Don't enter a score if there already is one.
1962 (while (setq entry
(pop scores
))
1963 (and (equal "references" (car entry
))
1964 (or (null (nth 3 (cadr entry
)))
1965 (eq 's
(nth 3 (cadr entry
))))
1969 (gnus-summary-score-entry
1970 (if thread
"thread" "references")
1971 id
's score
(current-time-string) nil t
)))))
1973 (defun gnus-score-string (score-list header now expire
&optional trace
)
1974 ;; Score ARTICLES according to HEADER in SCORE-LIST.
1975 ;; Update matching entries to NOW and remove unmatched entries older
1978 ;; Insert the unique article headers in the buffer.
1979 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index
)))
1980 ;; gnus-score-index is used as a free variable.
1981 (simplify (and gnus-score-thread-simplify
1982 (string= "subject" header
)))
1983 alike last this art entries alist articles
1984 fuzzies arts words kill
)
1986 ;; Sorting the articles costs os O(N*log N) but will allow us to
1987 ;; only match with each unique header. Thus the actual matching
1988 ;; will be O(M*U) where M is the number of strings to match with,
1989 ;; and U is the number of unique headers. It is assumed (but
1990 ;; untested) this will be a net win because of the large constant
1991 ;; factor involved with string matching.
1992 (setq gnus-scores-articles
1993 ;; We cannot string-sort the extra headers list. *sigh*
1994 (if (= gnus-score-index
9)
1995 gnus-scores-articles
1996 (sort gnus-scores-articles
'gnus-score-string
<))
1997 articles gnus-scores-articles
)
2000 (while (setq art
(pop articles
))
2001 (setq this
(aref (car art
) gnus-score-index
))
2003 ;; If we're working with non-standard headers, we are stuck
2004 ;; with working on them as a group. What a hassle.
2005 ;; Just wait 'til you see what horrors we commit against `match'...
2006 (if (= gnus-score-index
9)
2007 (setq this
(gnus-prin1-to-string this
))) ; ick.
2010 (setq this
(gnus-map-function gnus-simplify-subject-functions this
)))
2011 (if (equal last this
)
2012 ;; O(N*H) cons-cells used here, where H is the number of
2016 ;; Insert the line, with a text property on the
2017 ;; terminating newline referring to the articles with
2020 (put-text-property (1- (point)) (point) 'articles alike
))
2021 (setq alike
(list art
)
2023 (when last
; Bwadr, duplicate code.
2025 (put-text-property (1- (point)) (point) 'articles alike
))
2027 ;; Go through all the score alists and pick out the entries
2030 (setq alist
(pop score-list
)
2031 ;; There's only one instance of this header for
2032 ;; each score alist.
2033 entries
(assoc header alist
))
2034 (while (cdr entries
) ;First entry is the header index.
2035 (let* ((kill (cadr entries
))
2036 (type (or (nth 3 kill
) 's
))
2037 (score (or (nth 1 kill
) gnus-score-interactive-default-score
))
2039 (extra (nth 4 kill
)) ; non-standard header; string.
2041 (mt (aref (symbol-name type
) 0))
2042 (case-fold-search (not (memq mt
'(?R ?S ?E ?F
))))
2044 ;; Assume user already simplified regexp and fuzzies
2045 (match (if (and simplify
(not (memq dmt
'(?f ?r
))))
2047 gnus-simplify-subject-functions
2051 (cond ((= dmt ?r
) 're-search-forward
)
2052 ((or (= dmt ?e
) (= dmt ?s
) (= dmt ?f
)) 'search-forward
)
2054 (t (error "Invalid match type: %s" type
)))))
2056 ;; Evil hackery to make match usable in non-standard headers.
2058 (setq match
(concat "[ (](" extra
" \\. \"[^)]*"
2059 match
"[^\"]*\")[ )]")
2060 search-func
're-search-forward
)) ; XXX danger?!?
2063 ;; Fuzzy matches. We save these for later.
2065 (push (cons entries alist
) fuzzies
)
2066 (setq entries
(cdr entries
)))
2067 ;; Word matches. Save these for even later.
2069 (push (cons entries alist
) words
)
2070 (setq entries
(cdr entries
)))
2073 ;; Do exact matching.
2074 (goto-char (point-min))
2075 (while (and (not (eobp))
2076 (funcall search-func match nil t
))
2077 ;; Is it really exact?
2079 (= (point-at-bol) (match-beginning 0))
2082 (setq found
(setq arts
(get-text-property
2083 (point) 'articles
)))
2084 ;; Found a match, update scores.
2086 (while (setq art
(pop arts
))
2087 (setcdr art
(+ score
(cdr art
)))
2090 (car-safe (rassq alist gnus-score-cache
))
2093 (while (setq art
(pop arts
))
2094 (setcdr art
(+ score
(cdr art
)))))))
2096 ;; Update expiry date
2098 (setq entries
(cdr entries
))
2102 (setq entries
(cdr entries
)))
2103 ;; We have a match, so we update the date.
2104 ((and found gnus-update-score-entry-dates
)
2105 (gnus-score-set 'touched
'(t) alist
)
2106 (setcar (nthcdr 2 kill
) now
)
2107 (setq entries
(cdr entries
)))
2108 ;; This entry has expired, so we remove it.
2109 ((and expire
(< date expire
))
2110 (gnus-score-set 'touched
'(t) alist
)
2111 (setcdr entries
(cddr entries
)))
2112 ;; No match; go to next entry.
2114 (setq entries
(cdr entries
))))))
2115 ;; Regexp and substring matching.
2117 (goto-char (point-min))
2118 (when (string= match
"")
2120 (while (and (not (eobp))
2121 (funcall search-func match nil t
))
2122 (goto-char (match-beginning 0))
2124 (setq found
(setq arts
(get-text-property (point) 'articles
)))
2125 ;; Found a match, update scores.
2127 (while (setq art
(pop arts
))
2128 (setcdr art
(+ score
(cdr art
)))
2129 (push (cons (car-safe (rassq alist gnus-score-cache
)) kill
)
2131 (while (setq art
(pop arts
))
2132 (setcdr art
(+ score
(cdr art
)))))
2134 ;; Update expiry date
2136 (setq entries
(cdr entries
))
2140 (setq entries
(cdr entries
)))
2141 ;; We have a match, so we update the date.
2142 ((and found gnus-update-score-entry-dates
)
2143 (gnus-score-set 'touched
'(t) alist
)
2144 (setcar (nthcdr 2 kill
) now
)
2145 (setq entries
(cdr entries
)))
2146 ;; This entry has expired, so we remove it.
2147 ((and expire
(< date expire
))
2148 (gnus-score-set 'touched
'(t) alist
)
2149 (setcdr entries
(cddr entries
)))
2150 ;; No match; go to next entry.
2152 (setq entries
(cdr entries
))))))))))
2154 ;; Find fuzzy matches.
2156 ;; Simplify the entire buffer for easy matching.
2157 (gnus-simplify-buffer-fuzzy)
2158 (while (setq kill
(cadaar fuzzies
))
2159 (let* ((match (nth 0 kill
))
2161 (score (or (nth 1 kill
) gnus-score-interactive-default-score
))
2163 (mt (aref (symbol-name type
) 0))
2164 (case-fold-search (not (= mt ?F
)))
2166 (goto-char (point-min))
2167 (while (and (not (eobp))
2168 (search-forward match nil t
))
2169 (when (and (= (point-at-bol) (match-beginning 0))
2171 (setq found
(setq arts
(get-text-property (point) 'articles
)))
2173 (while (setq art
(pop arts
))
2174 (setcdr art
(+ score
(cdr art
)))
2176 (car-safe (rassq (cdar fuzzies
) gnus-score-cache
))
2179 ;; Found a match, update scores.
2180 (while (setq art
(pop arts
))
2181 (setcdr art
(+ score
(cdr art
))))))
2183 ;; Update expiry date
2190 ;; Match, update date.
2191 ((and found gnus-update-score-entry-dates
)
2192 (gnus-score-set 'touched
'(t) (cdar fuzzies
))
2193 (setcar (nthcdr 2 kill
) now
))
2194 ;; Old entry, remove.
2195 ((and expire
(< date expire
))
2196 (gnus-score-set 'touched
'(t) (cdar fuzzies
))
2197 (setcdr (caar fuzzies
) (cddaar fuzzies
)))))
2198 (setq fuzzies
(cdr fuzzies
)))))
2201 ;; Enter all words into the hashtb.
2202 (let ((hashtb (gnus-make-hashtable
2203 (* 10 (count-lines (point-min) (point-max))))))
2204 (gnus-enter-score-words-into-hashtb hashtb
)
2205 (while (setq kill
(cadaar words
))
2206 (let* ((score (or (nth 1 kill
) gnus-score-interactive-default-score
))
2209 (when (setq arts
(intern-soft (nth 0 kill
) hashtb
))
2210 (setq arts
(symbol-value arts
))
2213 (while (setq art
(pop arts
))
2214 (setcdr art
(+ score
(cdr art
)))
2216 (car-safe (rassq (cdar words
) gnus-score-cache
))
2219 ;; Found a match, update scores.
2220 (while (setq art
(pop arts
))
2221 (setcdr art
(+ score
(cdr art
))))))
2222 ;; Update expiry date
2229 ;; Match, update date.
2230 ((and found gnus-update-score-entry-dates
)
2231 (gnus-score-set 'touched
'(t) (cdar words
))
2232 (setcar (nthcdr 2 kill
) now
))
2233 ;; Old entry, remove.
2234 ((and expire
(< date expire
))
2235 (gnus-score-set 'touched
'(t) (cdar words
))
2236 (setcdr (caar words
) (cddaar words
)))))
2237 (setq words
(cdr words
))))))
2240 (defun gnus-enter-score-words-into-hashtb (hashtb)
2241 ;; Find all the words in the buffer and enter them into
2244 (goto-char (point-min))
2245 (with-syntax-table gnus-adaptive-word-syntax-table
2246 (while (re-search-forward "\\b\\w+\\b" nil t
)
2249 (setq word
(downcase (buffer-substring
2250 (match-beginning 0) (match-end 0))))
2254 (append (get-text-property (point-at-eol) 'articles
) val
)
2256 ;; Make all the ignorable words ignored.
2257 (let ((ignored (append gnus-ignored-adaptive-words
2258 (if gnus-adaptive-word-no-group-words
2259 (message-tokenize-header
2260 (gnus-group-real-name gnus-newsgroup-name
)
2262 gnus-default-ignored-adaptive-words
)))
2264 (gnus-sethash (pop ignored
) nil hashtb
)))))
2266 (defun gnus-score-string< (a1 a2
)
2267 ;; Compare headers in articles A2 and A2.
2268 ;; The header index used is the free variable `gnus-score-index'.
2269 (string-lessp (aref (car a1
) gnus-score-index
)
2270 (aref (car a2
) gnus-score-index
)))
2272 (defun gnus-current-score-file-nondirectory (&optional score-file
)
2273 (let ((score-file (or score-file gnus-current-score-file
)))
2275 (gnus-short-group-name (file-name-nondirectory score-file
))
2278 (defun gnus-score-adaptive ()
2279 "Create adaptive score rules for this newsgroup."
2280 (when gnus-newsgroup-adaptive
2281 ;; We change the score file to the adaptive score file.
2283 (set-buffer gnus-summary-buffer
)
2284 (gnus-score-load-file
2285 (or gnus-newsgroup-adaptive-score-file
2286 (gnus-home-score-file gnus-newsgroup-name t
)
2287 (gnus-score-file-name
2288 gnus-newsgroup-name gnus-adaptive-file-suffix
))))
2289 ;; Perform ordinary line scoring.
2290 (when (or (not (listp gnus-newsgroup-adaptive
))
2291 (memq 'line gnus-newsgroup-adaptive
))
2293 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist
))
2295 (date (current-time-string))
2296 (data gnus-newsgroup-data
)
2297 elem headers match func
)
2298 ;; First we transform the adaptive rule alist into something
2299 ;; that's faster to process.
2301 (setq elem
(car malist
))
2302 (when (symbolp (car elem
))
2303 (setcar elem
(symbol-value (car elem
))))
2304 (setq elem
(cdr elem
))
2309 (concat "mail-header-"
2310 (if (eq (caar elem
) 'followup
)
2312 (downcase (symbol-name (caar elem
))))))))
2314 (cons (if (eq (caar elem
) 'followup
)
2316 (symbol-name (caar elem
)))
2321 (setq elem
(cdr elem
)))
2322 (setq malist
(cdr malist
)))
2323 ;; Then we score away.
2325 (setq elem
(cdr (assq (gnus-data-mark (car data
)) alist
)))
2327 (gnus-data-pseudo-p (car data
)))
2329 (when (setq headers
(gnus-data-header (car data
)))
2331 (setq match
(funcall (caar elem
) headers
))
2332 (gnus-summary-score-entry
2333 (nth 1 (car elem
)) match
2337 ((equal (nth 1 (car elem
)) "date")
2340 ;; Whether we use substring or exact matches is
2342 (if (or (not gnus-score-exact-adapt-limit
)
2343 (< (length match
) gnus-score-exact-adapt-limit
))
2345 (if (equal (nth 1 (car elem
)) "subject")
2347 (nth 2 (car elem
)) date nil t
)
2348 (setq elem
(cdr elem
)))))
2349 (setq data
(cdr data
))))))
2351 ;; Perform adaptive word scoring.
2352 (when (and (listp gnus-newsgroup-adaptive
)
2353 (memq 'word gnus-newsgroup-adaptive
))
2355 (let* ((hashtb (gnus-make-hashtable 1000))
2356 (date (date-to-day (current-time-string)))
2357 (data gnus-newsgroup-data
)
2359 (with-syntax-table gnus-adaptive-word-syntax-table
2360 ;; Go through all articles.
2361 (while (setq d
(pop data
))
2363 (not (gnus-data-pseudo-p d
))
2367 gnus-adaptive-word-score-alist
))))
2368 ;; This article has a mark that should lead to
2369 ;; adaptive word rules, so we insert the subject
2370 ;; and find all words in that string.
2371 (insert (mail-header-subject (gnus-data-header d
)))
2372 (downcase-region (point-min) (point-max))
2373 (goto-char (point-min))
2374 (while (re-search-forward "\\b\\w+\\b" nil t
)
2375 ;; Put the word and score into the hashtb.
2376 (setq val
(gnus-gethash (setq word
(match-string 0))
2378 (when (or (not gnus-adaptive-word-length-limit
)
2380 gnus-adaptive-word-length-limit
))
2381 (setq val
(+ score
(or val
0)))
2382 (if (and gnus-adaptive-word-minimum
2383 (< val gnus-adaptive-word-minimum
))
2384 (setq val gnus-adaptive-word-minimum
))
2385 (gnus-sethash word val hashtb
)))
2387 ;; Make all the ignorable words ignored.
2388 (let ((ignored (append gnus-ignored-adaptive-words
2389 (if gnus-adaptive-word-no-group-words
2390 (message-tokenize-header
2391 (gnus-group-real-name
2392 gnus-newsgroup-name
)
2394 gnus-default-ignored-adaptive-words
)))
2396 (gnus-sethash (pop ignored
) nil hashtb
)))
2397 ;; Now we have all the words and scores, so we
2398 ;; add these rules to the ADAPT file.
2399 (set-buffer gnus-summary-buffer
)
2402 (when (symbol-value word
)
2403 (gnus-summary-score-entry
2404 "subject" (symbol-name word
) 'w
(symbol-value word
)
2408 (defun gnus-score-edit-done ()
2409 (let ((bufnam (buffer-file-name (current-buffer)))
2410 (winconf gnus-prev-winconf
))
2412 (set-window-configuration winconf
))
2413 (gnus-score-remove-from-cache bufnam
)
2414 (gnus-score-load-file bufnam
)
2415 (run-hooks 'gnus-score-edit-done-hook
)))
2417 (defun gnus-score-find-trace ()
2418 "Find all score rules that applies to the current article."
2420 (let ((old-scored gnus-newsgroup-scored
))
2421 (let ((gnus-newsgroup-headers
2422 (list (gnus-summary-article-header)))
2423 (gnus-newsgroup-scored nil
)
2424 ;; Must be synced with `gnus-score-edit-file-at-point':
2425 (frmt "%S [%s] -> %s\n")
2429 (nnheader-set-temp-buffer "*Score Trace*"))
2430 (setq gnus-score-trace nil
)
2431 (gnus-possibly-score-headers 'trace
)
2432 (if (not (setq trace gnus-score-trace
))
2434 1 "No score rules apply to the current article (default score %d)."
2435 gnus-summary-default-score
)
2436 (set-buffer "*Score Trace*")
2437 ;; Use a keymap instead?
2442 (gnus-summary-expand-window)))
2446 (kill-buffer (current-buffer))
2447 (gnus-summary-expand-window)))
2448 (local-set-key "e" (lambda ()
2449 "Run `gnus-score-edit-file-at-point'."
2451 (gnus-score-edit-file-at-point)))
2452 (local-set-key "f" (lambda ()
2453 "Run `gnus-score-edit-file-at-point'."
2455 (gnus-score-edit-file-at-point 'format
)))
2456 (local-set-key "t" 'toggle-truncate-lines
)
2457 (setq truncate-lines t
)
2458 (dolist (entry trace
)
2459 (setq file
(or (car entry
)
2460 ;; Must be synced with
2461 ;; `gnus-score-edit-file-at-point':
2466 ;; Don't use `file-name-sans-extension' to see .SCORE and
2468 (file-name-nondirectory file
)
2469 (abbreviate-file-name file
))))
2471 (format "\nTotal score: %d"
2475 gnus-score-interactive-default-score
))
2480 Type `e' to edit score file corresponding to the score rule on current line,
2481 `f' to format (pretty print) the score file and edit it,
2482 `t' toggle to truncate long lines in this buffer,
2483 `q' to quit, `k' to kill score trace buffer.
2485 The first sexp on each line is the score rule, followed by the file name of
2486 the score file and its full name, including the directory.")
2487 (goto-char (point-min))
2488 (gnus-configure-windows 'score-trace
)))
2489 (set-buffer gnus-summary-buffer
)
2490 (setq gnus-newsgroup-scored old-scored
)))
2492 (defun gnus-score-find-favourite-words ()
2493 "List words used in scoring."
2495 (let ((alists (gnus-score-load-files (gnus-all-score-files)))
2496 alist rule rules kill
)
2497 ;; Go through all the score alists for this group
2498 ;; and find all `w' rules.
2499 (while (setq alist
(pop alists
))
2500 (while (setq rule
(pop alist
))
2501 (when (and (stringp (car rule
))
2502 (equal "subject" (downcase (pop rule
))))
2503 (while (setq kill
(pop rule
))
2504 (when (memq (nth 3 kill
) '(w W word Word
))
2505 (push (cons (or (nth 1 kill
)
2506 gnus-score-interactive-default-score
)
2509 (setq rules
(sort rules
(lambda (r1 r2
)
2510 (string-lessp (cdr r1
) (cdr r2
)))))
2511 ;; Add up words that have appeared several times.
2514 (if (equal (cdar r
) (cdadr r
))
2516 (setcar (car r
) (+ (caar r
) (caadr r
)))
2517 (setcdr r
(cddr r
)))
2519 ;; Insert the words.
2520 (nnheader-set-temp-buffer "*Score Words*")
2521 (if (not (setq rules
(sort rules
(lambda (r1 r2
) (> (car r1
) (car r2
))))))
2522 (gnus-error 3 "No word score rules")
2524 (insert (format "%-5d: %s\n" (caar rules
) (cdar rules
)))
2526 (goto-char (point-min))
2527 (gnus-configure-windows 'score-words
))))
2529 (defun gnus-summary-rescore ()
2530 "Redo the entire scoring process in the current summary."
2533 (setq gnus-score-cache nil
)
2534 (setq gnus-newsgroup-scored nil
)
2535 (gnus-possibly-score-headers)
2536 (gnus-score-update-all-lines))
2538 (defun gnus-score-flush-cache ()
2539 "Flush the cache of score files."
2542 (setq gnus-score-cache nil
2543 gnus-score-alist nil
2544 gnus-short-name-score-file-cache nil
)
2545 (gnus-message 6 "The score cache is now flushed"))
2547 (gnus-add-shutdown 'gnus-score-close
'gnus
)
2549 (defvar gnus-score-file-alist-cache nil
)
2551 (defun gnus-score-close ()
2552 "Clear all internal score variables."
2553 (setq gnus-score-cache nil
2554 gnus-internal-global-score-files nil
2555 gnus-score-file-list nil
2556 gnus-score-file-alist-cache nil
))
2558 ;; Summary score marking commands.
2560 (defun gnus-summary-raise-same-subject-and-select (score)
2561 "Raise articles which has the same subject with SCORE and select the next."
2563 (let ((subject (gnus-summary-article-subject)))
2564 (gnus-summary-raise-score score
)
2565 (while (gnus-summary-find-subject subject
)
2566 (gnus-summary-raise-score score
))
2567 (gnus-summary-next-article t
)))
2569 (defun gnus-summary-raise-same-subject (score)
2570 "Raise articles which has the same subject with SCORE."
2572 (let ((subject (gnus-summary-article-subject)))
2573 (gnus-summary-raise-score score
)
2574 (while (gnus-summary-find-subject subject
)
2575 (gnus-summary-raise-score score
))
2576 (gnus-summary-next-subject 1 t
)))
2578 (defun gnus-score-delta-default (level)
2579 (if level
(prefix-numeric-value level
)
2580 gnus-score-interactive-default-score
))
2582 (defun gnus-summary-raise-thread (&optional score
)
2583 "Raise the score of the articles in the current thread with SCORE."
2585 (setq score
(gnus-score-delta-default score
))
2588 (let ((articles (gnus-summary-articles-in-thread)))
2590 (gnus-summary-goto-subject (car articles
))
2591 (gnus-summary-raise-score score
)
2592 (setq articles
(cdr articles
))))
2594 (let ((gnus-summary-check-current t
))
2595 (unless (zerop (gnus-summary-next-subject 1 t
))
2597 (gnus-summary-recenter)
2598 (gnus-summary-position-point)
2599 (gnus-set-mode-line 'summary
))
2601 (defun gnus-summary-lower-same-subject-and-select (score)
2602 "Raise articles which has the same subject with SCORE and select the next."
2604 (gnus-summary-raise-same-subject-and-select (- score
)))
2606 (defun gnus-summary-lower-same-subject (score)
2607 "Raise articles which has the same subject with SCORE."
2609 (gnus-summary-raise-same-subject (- score
)))
2611 (defun gnus-summary-lower-thread (&optional score
)
2612 "Lower score of articles in the current thread with SCORE."
2614 (gnus-summary-raise-thread (- (gnus-score-delta-default score
))))
2616 ;;; Finding score files.
2618 (defun gnus-score-score-files (group)
2619 "Return a list of all possible score files."
2620 ;; Search and set any global score files.
2621 (when gnus-global-score-files
2622 (unless gnus-internal-global-score-files
2623 (gnus-score-search-global-directories gnus-global-score-files
)))
2624 ;; Fix the kill-file dir variable.
2625 (setq gnus-kill-files-directory
2626 (file-name-as-directory gnus-kill-files-directory
))
2627 ;; If we can't read it, there are no score files.
2628 (if (not (file-exists-p (expand-file-name gnus-kill-files-directory
)))
2629 (setq gnus-score-file-list nil
)
2630 (if (not (gnus-use-long-file-name 'not-score
))
2631 ;; We do not use long file names, so we have to do some
2632 ;; directory traversing.
2633 (setq gnus-score-file-list
2635 (or gnus-short-name-score-file-cache
2637 (gnus-message 6 "Finding all score files...")
2638 (setq gnus-short-name-score-file-cache
2639 (gnus-score-score-files-1
2640 gnus-kill-files-directory
))
2641 (gnus-message 6 "Finding all score files...done")))))
2642 ;; We want long file names.
2643 (when (or (not gnus-score-file-list
)
2644 (not (car gnus-score-file-list
))
2645 (gnus-file-newer-than gnus-kill-files-directory
2646 (car gnus-score-file-list
)))
2647 (setq gnus-score-file-list
2648 (cons (nth 5 (file-attributes gnus-kill-files-directory
))
2651 gnus-kill-files-directory t
2652 (gnus-score-file-regexp)))))))
2653 (cdr gnus-score-file-list
)))
2655 (defun gnus-score-score-files-1 (dir)
2656 "Return all possible score files under DIR."
2657 (let ((files (list (expand-file-name dir
)))
2658 (regexp (gnus-score-file-regexp))
2659 (case-fold-search nil
)
2661 (while (setq file
(pop files
))
2663 ;; Ignore files that start with a dot.
2664 ((string-match "^\\." (file-name-nondirectory file
))
2666 ;; Add subtrees of directory to also be searched.
2667 ((and (file-directory-p file
)
2668 (not (member (file-truename file
) seen
)))
2669 (push (file-truename file
) seen
)
2670 (setq files
(nconc (directory-files file t nil t
) files
)))
2671 ;; Add files to the list of score files.
2672 ((string-match regexp file
)
2675 ;; Return a dummy value.
2676 (list (expand-file-name "this.file.does.not.exist.SCORE"
2677 gnus-kill-files-directory
)))))
2679 (defun gnus-score-file-regexp ()
2680 "Return a regexp that match all score files."
2681 (concat "\\(" (regexp-quote gnus-score-file-suffix
)
2682 "\\|" (regexp-quote gnus-adaptive-file-suffix
) "\\)\\'"))
2684 (defun gnus-score-find-bnews (group)
2685 "Return a list of score files for GROUP.
2686 The score files are those files in the ~/News/ directory which matches
2687 GROUP using BNews sys file syntax."
2688 (let* ((sfiles (append (gnus-score-score-files group
)
2689 gnus-internal-global-score-files
))
2690 (kill-dir (file-name-as-directory
2691 (expand-file-name gnus-kill-files-directory
)))
2692 (klen (length kill-dir
))
2693 (score-regexp (gnus-score-file-regexp))
2694 (trans (cdr (assq ?
: nnheader-file-name-translation-alist
)))
2695 (group-trans (nnheader-translate-file-chars group t
))
2696 ofiles not-match regexp
)
2698 (set-buffer (gnus-get-buffer-create "*gnus score files*"))
2699 (buffer-disable-undo)
2700 ;; Go through all score file names and create regexp with them
2704 (insert (car sfiles
))
2705 (goto-char (point-min))
2706 ;; First remove the suffix itself.
2707 (when (re-search-forward (concat "." score-regexp
) nil t
)
2708 (replace-match "" t t
)
2709 (goto-char (point-min))
2710 (if (looking-at (regexp-quote kill-dir
))
2711 ;; If the file name was just "SCORE", `klen' is one character
2713 (delete-char (min (1- (point-max)) klen
))
2714 (goto-char (point-max))
2715 (if (re-search-backward gnus-directory-sep-char-regexp nil t
)
2716 (delete-region (1+ (point)) (point-min))
2717 (gnus-message 1 "Can't find directory separator in %s"
2719 ;; If short file names were used, we have to translate slashes.
2720 (goto-char (point-min))
2721 (let ((regexp (concat
2722 "[/:" (if trans
(char-to-string trans
)) "]")))
2723 (while (re-search-forward regexp nil t
)
2724 (replace-match "." t t
)))
2725 ;; Kludge to get rid of "nntp+" problems.
2726 (goto-char (point-min))
2727 (when (looking-at "nn[a-z]+\\+")
2728 (search-forward "+")
2732 ;; Kludge to deal with "++".
2733 (while (search-forward "+" nil t
)
2734 (replace-match "\\+" t t
))
2735 ;; Translate "all" to ".*".
2736 (goto-char (point-min))
2737 (while (search-forward "all" nil t
)
2738 (replace-match ".*" t t
))
2739 (goto-char (point-min))
2740 ;; Deal with "not."s.
2741 (if (looking-at "not.")
2745 (concat "^" (buffer-substring 5 (point-max)) "$")))
2746 (setq regexp
(concat "^" (buffer-substring 1 (point-max)) "$"))
2747 (setq not-match nil
))
2748 ;; Finally - if this resulting regexp matches the group name,
2749 ;; we add this score file to the list of score files
2750 ;; applicable to this group.
2751 (when (or (and not-match
2753 (not (string-match regexp group-trans
))))
2754 (and (not not-match
)
2755 (ignore-errors (string-match regexp group-trans
))))
2756 (push (car sfiles
) ofiles
)))
2757 (setq sfiles
(cdr sfiles
)))
2758 (gnus-kill-buffer (current-buffer))
2759 ;; Slight kludge here - the last score file returned should be
2760 ;; the local score file, whether it exists or not. This is so
2761 ;; that any score commands the user enters will go to the right
2762 ;; file, and not end up in some global score file.
2763 (let ((localscore (gnus-score-file-name group
)))
2764 (setq ofiles
(cons localscore
(delete localscore ofiles
))))
2765 (gnus-sort-score-files (nreverse ofiles
)))))
2767 (defun gnus-score-find-single (group)
2768 "Return list containing the score file for GROUP."
2769 (list (or gnus-newsgroup-adaptive-score-file
2770 (gnus-score-file-name group gnus-adaptive-file-suffix
))
2771 (gnus-score-file-name group
)))
2773 (defun gnus-score-find-hierarchical (group)
2774 "Return list of score files for GROUP.
2775 This includes the score file for the group and all its parents."
2776 (let* ((prefix (gnus-group-real-prefix group
))
2778 (group (gnus-group-real-name group
))
2780 (while (string-match "\\." group
(1+ start
))
2781 (setq start
(match-beginning 0))
2782 (push (substring group
0 start
) all
))
2786 (mapcar (lambda (group)
2787 (gnus-score-file-name group gnus-adaptive-file-suffix
))
2788 (setq all
(nreverse all
)))
2789 (mapcar 'gnus-score-file-name all
)))
2790 (if (equal prefix
"")
2794 (nnheader-translate-file-chars
2795 (concat (file-name-directory file
) prefix
2796 (file-name-nondirectory file
))))
2799 (defun gnus-score-file-rank (file)
2800 "Return a number that says how specific score FILE is.
2801 Destroys the current buffer."
2802 (if (member file gnus-internal-global-score-files
)
2805 (concat "^" (regexp-quote
2807 (file-name-as-directory gnus-kill-files-directory
))))
2809 (setq file
(substring file
(match-end 0))))
2811 (goto-char (point-min))
2814 (while (re-search-forward "[./]" nil t
)
2815 (push (buffer-substring beg
(1- (point)))
2818 (setq elems
(delete "all" elems
))
2821 (defun gnus-sort-score-files (files)
2822 "Sort FILES so that the most general files come first."
2827 (cons (inline (gnus-score-file-rank file
)) file
))
2829 (mapcar 'cdr
(sort alist
'car-less-than-car
)))))
2831 (defun gnus-score-find-alist (group)
2832 "Return list of score files for GROUP.
2833 The list is determined from the variable `gnus-score-file-alist'."
2834 (let ((alist gnus-score-file-multiple-match-alist
)
2836 ;; if this group has been seen before, return the cached entry
2837 (if (setq score-files
(assoc group gnus-score-file-alist-cache
))
2838 (cdr score-files
) ;ensures caching groups with no matches
2839 ;; handle the multiple match alist
2841 (when (string-match (caar alist
) group
)
2843 (nconc score-files
(copy-sequence (cdar alist
)))))
2844 (setq alist
(cdr alist
)))
2845 (setq alist gnus-score-file-single-match-alist
)
2846 ;; handle the single match alist
2848 (when (string-match (caar alist
) group
)
2849 ;; progn used just in case ("regexp") has no files
2850 ;; and score-files is still nil. -sj
2851 ;; this can be construed as a "stop searching here" feature :>
2852 ;; and used to simplify regexps in the single-alist
2854 (nconc score-files
(copy-sequence (cdar alist
))))
2856 (setq alist
(cdr alist
)))
2857 ;; cache the score files
2858 (push (cons group score-files
) gnus-score-file-alist-cache
)
2861 (defun gnus-all-score-files (&optional group
)
2862 "Return a list of all score files for the current group."
2863 (let ((funcs gnus-score-find-score-files-function
)
2864 (group (or group gnus-newsgroup-name
))
2867 ;; Make sure funcs is a list.
2870 (setq funcs
(list funcs
)))
2871 (when gnus-score-use-all-scores
2872 ;; Get the initial score files for this group.
2874 (setq score-files
(nreverse (gnus-score-find-alist group
))))
2875 ;; Add any home adapt files.
2876 (let ((home (gnus-home-score-file group t
)))
2878 (push home score-files
)
2879 (setq gnus-newsgroup-adaptive-score-file home
)))
2880 ;; Check whether there is a `adapt-file' group parameter.
2881 (let ((param-file (gnus-group-find-parameter group
'adapt-file
)))
2883 (push param-file score-files
)
2884 (setq gnus-newsgroup-adaptive-score-file param-file
))))
2885 ;; Go through all the functions for finding score files (or actual
2886 ;; scores) and add them to a list.
2888 (when (functionp (car funcs
))
2891 (nreverse (funcall (car funcs
) group
)))))
2892 (setq funcs
(cdr funcs
)))
2893 (when gnus-score-use-all-scores
2894 ;; Add any home score files.
2895 (let ((home (gnus-home-score-file group
)))
2897 (push home score-files
)))
2898 ;; Check whether there is a `score-file' group parameter.
2899 (let ((param-file (gnus-group-find-parameter group
'score-file
)))
2901 (push param-file score-files
))))
2902 ;; Expand all files names.
2903 (let ((files score-files
))
2905 (when (stringp (car files
))
2906 (setcar files
(expand-file-name
2907 (car files
) gnus-kill-files-directory
)))
2909 (setq score-files
(nreverse score-files
))
2910 ;; Remove any duplicate score files.
2911 (while (and score-files
2912 (member (car score-files
) (cdr score-files
)))
2914 (let ((files score-files
))
2916 (if (member (cadr files
) (cddr files
))
2917 (setcdr files
(cddr files
))
2919 ;; Do the scoring if there are any score files for this group.
2922 (defun gnus-possibly-score-headers (&optional trace
)
2923 "Do scoring if scoring is required."
2924 (let ((score-files (gnus-all-score-files)))
2926 (gnus-score-headers score-files trace
))))
2928 (defun gnus-score-file-name (newsgroup &optional suffix
)
2929 "Return the name of a score file for NEWSGROUP."
2930 (let ((suffix (or suffix gnus-score-file-suffix
)))
2931 (nnheader-translate-file-chars
2933 ((or (null newsgroup
)
2934 (string-equal newsgroup
""))
2935 ;; The global score file is placed at top of the directory.
2936 (expand-file-name suffix gnus-kill-files-directory
))
2937 ((gnus-use-long-file-name 'not-score
)
2938 ;; Append ".SCORE" to newsgroup name.
2939 (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup
)
2941 gnus-kill-files-directory
))
2943 ;; Place "SCORE" under the hierarchical directory.
2944 (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup
)
2946 gnus-kill-files-directory
))))))
2948 (defun gnus-score-search-global-directories (files)
2949 "Scan all global score directories for score files."
2950 ;; Set the variable `gnus-internal-global-score-files' to all
2951 ;; available global score files.
2952 (interactive (list gnus-global-score-files
))
2955 ;; #### /$ Unix-specific?
2956 (if (file-directory-p (car files
))
2957 (setq out
(nconc (directory-files
2959 (concat (gnus-score-file-regexp) "$"))))
2960 (push (car files
) out
))
2961 (setq files
(cdr files
)))
2962 (setq gnus-internal-global-score-files out
)))
2964 (defun gnus-score-default-fold-toggle ()
2965 "Toggle folding for new score file entries."
2967 (setq gnus-score-default-fold
(not gnus-score-default-fold
))
2968 (if gnus-score-default-fold
2969 (gnus-message 1 "New score file entries will be case insensitive.")
2970 (gnus-message 1 "New score file entries will be case sensitive.")))
2972 ;;; Home score file.
2974 (defun gnus-home-score-file (group &optional adapt
)
2975 "Return the home score file for GROUP.
2976 If ADAPT, return the home adaptive file instead."
2977 (let ((list (if adapt gnus-home-adapt-file gnus-home-score-file
))
2979 ;; Make sure we have a list.
2980 (unless (listp list
)
2981 (setq list
(list list
)))
2982 ;; Go through the list and look for matches.
2983 (while (and (not found
)
2984 (setq elem
(pop list
)))
2992 (funcall elem group
))
2993 ;; Regexp-file cons.
2995 (when (string-match (gnus-globalify-regexp (car elem
)) group
)
2996 (replace-match (cadr elem
) t nil group
))))))
2998 (setq found
(nnheader-translate-file-chars found
))
2999 (if (file-name-absolute-p found
)
3001 (nnheader-concat gnus-kill-files-directory found
)))))
3003 (defun gnus-hierarchial-home-score-file (group)
3004 "Return the score file of the top-level hierarchy of GROUP."
3005 (if (string-match "^[^.]+\\." group
)
3006 (concat (match-string 0 group
) gnus-score-file-suffix
)
3007 ;; Group name without any dots.
3008 (concat group
(if (gnus-use-long-file-name 'not-score
) "." "/")
3009 gnus-score-file-suffix
)))
3011 (defun gnus-hierarchial-home-adapt-file (group)
3012 "Return the adapt file of the top-level hierarchy of GROUP."
3013 (if (string-match "^[^.]+\\." group
)
3014 (concat (match-string 0 group
) gnus-adaptive-file-suffix
)
3015 ;; Group name without any dots.
3016 (concat group
(if (gnus-use-long-file-name 'not-score
) "." "/")
3017 gnus-adaptive-file-suffix
)))
3019 (defun gnus-current-home-score-file (group)
3020 "Return the \"current\" regular score file."
3021 (car (nreverse (gnus-score-find-alist group
))))
3027 (defun gnus-decay-score (score)
3028 "Decay SCORE according to `gnus-score-decay-constant' and `gnus-score-decay-scale'."
3030 (* (if (< score
0) -
1 1)
3032 (max gnus-score-decay-constant
3034 gnus-score-decay-scale
)))))))
3035 (if (and (featurep 'xemacs
)
3036 ;; XEmacs' floor can handle only the floating point
3037 ;; number below the half of the maximum integer.
3038 (> (abs n
) (lsh -
1 -
2)))
3040 (car (split-string (number-to-string n
) "\\.")))
3043 (defun gnus-decay-scores (alist day
)
3044 "Decay non-permanent scores in ALIST."
3045 (let ((times (- (time-to-days (current-time)) day
))
3046 kill entry updated score n
)
3047 (unless (zerop times
) ;Done decays today already?
3048 (while (setq entry
(pop alist
))
3049 (when (stringp (car entry
))
3050 (setq entry
(cdr entry
))
3051 (while (setq kill
(pop entry
))
3054 (setq score
(or (nth 1 kill
)
3055 gnus-score-interactive-default-score
)
3057 (while (natnump (decf n
))
3058 (setq score
(funcall gnus-decay-score-function score
)))
3059 (setcdr kill
(cons score
3060 (cdr (cdr kill
)))))))))
3061 ;; Return whether this score file needs to be saved. By Je-haysuss!
3064 (defun gnus-score-regexp-bad-p (regexp)
3065 "Test whether REGEXP is safe for Gnus scoring.
3066 A regexp is unsafe if it matches newline or a buffer boundary.
3068 If the regexp is good, return nil. If the regexp is bad, return a
3069 cons cell (SYM . STRING), where the symbol SYM is `new' or `bad'.
3070 In the `new' case, the string is a safe replacement for REGEXP.
3071 In the `bad' case, the string is a unsafe subexpression of REGEXP,
3072 and we do not have a simple replacement to suggest.
3074 See Info node `(gnus)Scoring Tips' for examples of good regular expressions."
3075 (let (case-fold-search)
3077 ;; First, try a relatively fast necessary condition.
3078 ;; Notice ranges (like [^:] or [\t-\r]), \s>, \Sw, \W, \', \`:
3079 (string-match "\n\\|\\\\[SsW`']\\|\\[\\^\\|[\0-\n]-" regexp
)
3080 ;; Now break the regexp into tokens, and check each:
3081 (let ((tail regexp
) ; remaining regexp to check
3083 bad
; nil, or bad subexpression
3084 new
; nil, or replacement regexp so far
3085 end
) ; length of current token
3086 (while (and (not bad
)
3088 "\\`\\(\\\\[sS]?.\\|\\[\\^?]?[^]]*]\\|[^\\]\\)"
3090 (setq end
(match-end 0)
3091 tok
(substring tail
0 end
)
3092 tail
(substring tail end
))
3093 (if;; Is token `bad' (matching newline or buffer ends)?
3094 (or (member tok
'("\n" "\\W" "\\`" "\\'"))
3095 ;; This next handles "[...]", "\\s.", and "\\S.":
3096 (and (> end
2) (string-match tok
"\n")))
3098 ;; Try to suggest a replacement for tok ...
3099 (cond ((string-equal tok
"\\`") "^") ; or "\\(^\\)"
3100 ((string-equal tok
"\\'") "$") ; or "\\($\\)"
3101 ((string-match "\\[\\^" tok
) ; very common
3102 (concat (substring tok
0 -
1) "\n]")))))
3107 ;; good prefix so far:
3108 (substring regexp
0 (- (+ (length tail
) end
))))
3110 ;; No replacement idea, so give up:
3112 ;; tok is good, may need to extend new
3113 (and new
(setq new
(concat new tok
)))))
3114 ;; Now return a value:
3116 (bad (cons 'bad bad
))
3117 (new (cons 'new new
))
3120 (provide 'gnus-score
)
3122 ;; arch-tag: d3922589-764d-46ae-9954-9330fd192634
3123 ;;; gnus-score.el ends here