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