1 ;;; gnus-score.el --- scoring code for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;; Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <amanda@iesd.auc.dk>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
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)
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.
30 (eval-when-compile (require 'cl))
39 (defcustom gnus-global-score-files nil
40 "List of global score files and directories.
41 Set this variable if you want to use people's score files. One entry
42 for each score file or each score file directory. Gnus will decide
43 by itself what score files are applicable to which group.
45 Say you want to use the single score file
46 \"/ftp.gnus.org@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
47 score files in the \"/ftp.some-where:/pub/score\" directory.
49 (setq gnus-global-score-files
50 '(\"/ftp.gnus.org:/pub/larsi/ding/score/soc.motss.SCORE\"
51 \"/ftp.some-where:/pub/score\"))"
52 :group 'gnus-score-files
55 (defcustom gnus-score-file-single-match-alist nil
56 "Alist mapping regexps to lists of score files.
57 Each element of this alist should be of the form
58 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
60 If the name of a group is matched by REGEXP, the corresponding scorefiles
61 will be used for that group.
62 The first match found is used, subsequent matching entries are ignored (to
63 use multiple matches, see `gnus-score-file-multiple-match-alist').
65 These score files are loaded in addition to any files returned by
66 `gnus-score-find-score-files-function'."
67 :group 'gnus-score-files
68 :type '(repeat (cons regexp (repeat file))))
70 (defcustom gnus-score-file-multiple-match-alist nil
71 "Alist mapping regexps to lists of score files.
72 Each element of this alist should be of the form
73 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
75 If the name of a group is matched by REGEXP, the corresponding scorefiles
76 will be used for that group.
77 If multiple REGEXPs match a group, the score files corresponding to each
78 match will be used (for only one match to be used, see
79 `gnus-score-file-single-match-alist').
81 These score files are loaded in addition to any files returned by
82 `gnus-score-find-score-files-function'."
83 :group 'gnus-score-files
84 :type '(repeat (cons regexp (repeat file))))
86 (defcustom gnus-score-file-suffix "SCORE"
87 "Suffix of the score files."
88 :group 'gnus-score-files
91 (defcustom gnus-adaptive-file-suffix "ADAPT"
92 "Suffix of the adaptive score files."
93 :group 'gnus-score-files
94 :group 'gnus-score-adapt
97 (defcustom gnus-score-find-score-files-function 'gnus-score-find-bnews
98 "Function used to find score files.
99 The function will be called with the group name as the argument, and
100 should return a list of score files to apply to that group. The score
101 files do not actually have to exist.
103 Predefined values are:
105 `gnus-score-find-single': Only apply the group's own score file.
106 `gnus-score-find-hierarchical': Also apply score files from parent groups.
107 `gnus-score-find-bnews': Apply score files whose names matches.
109 See the documentation to these functions for more information.
111 This variable can also be a list of functions to be called. Each
112 function is given the group name as argument and should either return
113 a list of score files, or a list of score alists.
115 If functions other than these pre-defined functions are used,
116 the `a' symbolic prefix to the score commands will always use
118 :group 'gnus-score-files
119 :type '(radio (function-item gnus-score-find-single)
120 (function-item gnus-score-find-hierarchical)
121 (function-item gnus-score-find-bnews)
122 (repeat :tag "List of functions"
123 (choice (function :tag "Other" :value 'ignore)
124 (function-item gnus-score-find-single)
125 (function-item gnus-score-find-hierarchical)
126 (function-item gnus-score-find-bnews)))
127 (function :tag "Other" :value 'ignore)))
129 (defcustom gnus-score-interactive-default-score 1000
130 "*Scoring commands will raise/lower the score with this number as the default."
131 :group 'gnus-score-default
134 (defcustom gnus-score-expiry-days 7
135 "*Number of days before unused score file entries are expired.
136 If this variable is nil, no score file entries will be expired."
137 :group 'gnus-score-expire
138 :type '(choice (const :tag "never" nil)
141 (defcustom gnus-update-score-entry-dates t
142 "*In non-nil, update matching score entry dates.
143 If this variable is nil, then score entries that provide matches
144 will be expired along with non-matching score entries."
145 :group 'gnus-score-expire
148 (defcustom gnus-decay-scores nil
149 "*If non-nil, decay non-permanent scores."
150 :group 'gnus-score-decay
153 (defcustom gnus-decay-score-function 'gnus-decay-score
154 "*Function called to decay a score.
155 It is called with one parameter -- the score to be decayed."
156 :group 'gnus-score-decay
157 :type '(radio (function-item gnus-decay-score)
158 (function :tag "Other")))
160 (defcustom gnus-score-decay-constant 3
161 "*Decay all \"small\" scores with this amount."
162 :group 'gnus-score-decay
165 (defcustom gnus-score-decay-scale .05
166 "*Decay all \"big\" scores with this factor."
167 :group 'gnus-score-decay
170 (defcustom gnus-home-score-file nil
171 "Variable to control where interactive score entries are to go.
175 This file file will be used as the home score file.
178 The result of this function will be used as the home score file.
179 The function will be passed the name of the group as its
183 The elements in this list can be:
185 * `(regexp file-name ...)'
186 If the `regexp' matches the group name, the first `file-name' will
187 will be used as the home score file. (Multiple filenames are
188 allowed so that one may use gnus-score-file-single-match-alist to
192 If the function returns non-nil, the result will be used
193 as the home score file. The function will be passed the
194 name of the group as its parameter.
196 * A string. Use the string as the home score file.
198 The list will be traversed from the beginning towards the end looking
200 :group 'gnus-score-files
201 :type '(choice string
202 (repeat (choice string
203 (cons regexp (repeat file))
204 (function :value fun)))
205 (function-item gnus-hierarchial-home-score-file)
206 (function-item gnus-current-home-score-file)
207 (function :value fun)))
209 (defcustom gnus-home-adapt-file nil
210 "Variable to control where new adaptive score entries are to go.
211 This variable allows the same syntax as `gnus-home-score-file'."
212 :group 'gnus-score-adapt
213 :group 'gnus-score-files
214 :type '(choice string
215 (repeat (choice string
216 (cons regexp (repeat file))
217 (function :value fun)))
218 (function :value fun)))
220 (defcustom gnus-default-adaptive-score-alist
221 '((gnus-kill-file-mark)
223 (gnus-read-mark (from 3) (subject 30))
224 (gnus-catchup-mark (subject -10))
225 (gnus-killed-mark (from -1) (subject -20))
226 (gnus-del-mark (from -2) (subject -15)))
227 "*Alist of marks and scores."
228 :group 'gnus-score-adapt
229 :type '(repeat (cons (symbol :tag "Mark")
230 (repeat (list (choice :tag "Header"
233 (symbol :tag "other"))
234 (integer :tag "Score"))))))
236 (defcustom gnus-adaptive-word-length-limit nil
237 "*Words of a length lesser than this limit will be ignored when doing adaptive scoring."
238 :group 'gnus-score-adapt
241 (defcustom gnus-ignored-adaptive-words nil
242 "List of words to be ignored when doing adaptive word scoring."
243 :group 'gnus-score-adapt
244 :type '(repeat string))
246 (defcustom gnus-default-ignored-adaptive-words
247 '("a" "i" "the" "to" "of" "and" "in" "is" "it" "for" "that" "if" "you"
248 "this" "be" "on" "with" "not" "have" "are" "or" "as" "from" "can"
249 "but" "by" "at" "an" "will" "no" "all" "was" "do" "there" "my" "one"
250 "so" "we" "they" "what" "would" "any" "which" "about" "get" "your"
251 "use" "some" "me" "then" "name" "like" "out" "when" "up" "time"
252 "other" "more" "only" "just" "end" "also" "know" "how" "new" "should"
253 "been" "than" "them" "he" "who" "make" "may" "people" "these" "now"
254 "their" "here" "into" "first" "could" "way" "had" "see" "work" "well"
255 "were" "two" "very" "where" "while" "us" "because" "good" "same"
256 "even" "much" "most" "many" "such" "long" "his" "over" "last" "since"
257 "right" "before" "our" "without" "too" "those" "why" "must" "part"
258 "being" "current" "back" "still" "go" "point" "value" "each" "did"
259 "both" "true" "off" "say" "another" "state" "might" "under" "start"
261 "*Default list of words to be ignored when doing adaptive word scoring."
262 :group 'gnus-score-adapt
263 :type '(repeat string))
265 (defcustom gnus-default-adaptive-word-score-alist
266 `((,gnus-read-mark . 30)
267 (,gnus-catchup-mark . -10)
268 (,gnus-killed-mark . -20)
269 (,gnus-del-mark . -15))
270 "*Alist of marks and scores."
271 :group 'gnus-score-adapt
272 :type '(repeat (cons (character :tag "Mark")
273 (integer :tag "Score"))))
275 (defcustom gnus-adaptive-word-minimum nil
276 "If a number, this is the minimum score value that can be assigned to a word."
277 :group 'gnus-score-adapt
278 :type '(choice (const nil) integer))
280 (defcustom gnus-adaptive-word-no-group-words nil
281 "If t, don't adaptively score words included in the group name."
282 :group 'gnus-score-adapt
285 (defcustom gnus-score-mimic-keymap nil
286 "*Have the score entry functions pretend that they are a keymap."
287 :group 'gnus-score-default
290 (defcustom gnus-score-exact-adapt-limit 10
291 "*Number that says how long a match has to be before using substring matching.
292 When doing adaptive scoring, one normally uses fuzzy or substring
293 matching. However, if the header one matches is short, the possibility
294 for false positives is great, so if the length of the match is less
295 than this variable, exact matching will be used.
297 If this variable is nil, exact matching will always be used."
298 :group 'gnus-score-adapt
299 :type '(choice (const nil) integer))
301 (defcustom gnus-score-uncacheable-files "ADAPT$"
302 "All score files that match this regexp will not be cached."
303 :group 'gnus-score-adapt
304 :group 'gnus-score-files
307 (defcustom gnus-score-default-header nil
308 "Default header when entering new scores.
310 Should be one of the following symbols.
319 e: `extra' (non-standard overview)
324 If nil, the user will be asked for a header."
325 :group 'gnus-score-default
326 :type '(choice (const :tag "from" a)
327 (const :tag "subject" s)
328 (const :tag "body" b)
329 (const :tag "head" h)
330 (const :tag "message-id" i)
331 (const :tag "references" t)
332 (const :tag "xref" x)
333 (const :tag "extra" e)
334 (const :tag "lines" l)
335 (const :tag "date" d)
336 (const :tag "followup" f)
337 (const :tag "ask" nil)))
339 (defcustom gnus-score-default-type nil
340 "Default match type when entering new scores.
342 Should be one of the following symbols.
352 >: greater than number
355 If nil, the user will be asked for a match type."
356 :group 'gnus-score-default
357 :type '(choice (const :tag "substring" s)
358 (const :tag "exact string" e)
359 (const :tag "fuzzy string" f)
360 (const :tag "regexp string" r)
361 (const :tag "before date" b)
362 (const :tag "after date" a)
363 (const :tag "this date" n)
364 (const :tag "less than number" <)
365 (const :tag "greater than number" >)
366 (const :tag "equal than number" =)
367 (const :tag "ask" nil)))
369 (defcustom gnus-score-default-fold nil
370 "Use case folding for new score file entries iff not nil."
371 :group 'gnus-score-default
374 (defcustom gnus-score-default-duration nil
375 "Default duration of effect when entering new scores.
377 Should be one of the following symbols.
383 If nil, the user will be asked for a duration."
384 :group 'gnus-score-default
385 :type '(choice (const :tag "temporary" t)
386 (const :tag "permanent" p)
387 (const :tag "immediate" i)
388 (const :tag "ask" nil)))
390 (defcustom gnus-score-after-write-file-function nil
391 "Function called with the name of the score file just written to disk."
392 :group 'gnus-score-files
393 :type '(choice (const nil) function))
395 (defcustom gnus-score-thread-simplify nil
396 "If non-nil, subjects will simplified as in threading."
397 :group 'gnus-score-various
402 ;; Internal variables.
404 (defvar gnus-score-use-all-scores t
405 "If nil, only `gnus-score-find-score-files-function' is used.")
407 (defvar gnus-adaptive-word-syntax-table
408 (let ((table (copy-syntax-table (standard-syntax-table)))
409 (numbers '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)))
411 (modify-syntax-entry (pop numbers) " " table))
412 (modify-syntax-entry ?' "w" table)
414 "Syntax table used when doing adaptive word scoring.")
416 (defvar gnus-scores-exclude-files nil)
417 (defvar gnus-internal-global-score-files nil)
418 (defvar gnus-score-file-list nil)
420 (defvar gnus-short-name-score-file-cache nil)
422 (defvar gnus-score-help-winconf nil)
423 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
424 (defvar gnus-adaptive-word-score-alist gnus-default-adaptive-word-score-alist)
425 (defvar gnus-score-trace nil)
426 (defvar gnus-score-edit-buffer nil)
428 (defvar gnus-score-alist nil
429 "Alist containing score information.
430 The keys can be symbols or strings. The following symbols are defined.
432 touched: If this alist has been modified.
433 mark: Automatically mark articles below this.
434 expunge: Automatically expunge articles below this.
435 files: List of other score files to load when loading this one.
436 eval: Sexp to be evaluated when the score file is loaded.
438 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
439 where HEADER is the header being scored, MATCH is the string we are
440 looking for, TYPE is a flag indicating whether it should use regexp or
441 substring matching, SCORE is the score to add and DATE is the date
442 of the last successful match.")
444 (defvar gnus-score-cache nil)
445 (defvar gnus-scores-articles nil)
446 (defvar gnus-score-index nil)
449 (defconst gnus-header-index
450 ;; Name to index alist.
451 '(("number" 0 gnus-score-integer)
452 ("subject" 1 gnus-score-string)
453 ("from" 2 gnus-score-string)
454 ("date" 3 gnus-score-date)
455 ("message-id" 4 gnus-score-string)
456 ("references" 5 gnus-score-string)
457 ("chars" 6 gnus-score-integer)
458 ("lines" 7 gnus-score-integer)
459 ("xref" 8 gnus-score-string)
460 ("extra" 9 gnus-score-string)
461 ("head" -1 gnus-score-body)
462 ("body" -1 gnus-score-body)
463 ("all" -1 gnus-score-body)
464 ("followup" 2 gnus-score-followup)
465 ("thread" 5 gnus-score-thread)))
467 ;;; Summary mode score maps.
469 (gnus-define-keys (gnus-summary-score-map "V" gnus-summary-mode-map)
470 "s" gnus-summary-set-score
471 "S" gnus-summary-current-score
472 "c" gnus-score-change-score-file
473 "C" gnus-score-customize
474 "m" gnus-score-set-mark-below
475 "x" gnus-score-set-expunge-below
476 "R" gnus-summary-rescore
477 "e" gnus-score-edit-current-scores
478 "f" gnus-score-edit-file
479 "F" gnus-score-flush-cache
480 "t" gnus-score-find-trace
481 "w" gnus-score-find-favourite-words)
483 ;; Summary score file commands
485 ;; Much modification of the kill (ahem, score) code and lots of the
486 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
488 (defun gnus-summary-lower-score (&optional score symp)
489 "Make a score entry based on the current article.
490 The user will be prompted for header to score on, match type,
491 permanence, and the string to be used. The numerical prefix will be
493 (interactive (gnus-interactive "P\ny"))
494 (gnus-summary-increase-score (- (gnus-score-delta-default score)) symp))
496 (defun gnus-score-kill-help-buffer ()
497 (when (get-buffer "*Score Help*")
498 (kill-buffer "*Score Help*")
499 (when gnus-score-help-winconf
500 (set-window-configuration gnus-score-help-winconf))))
502 (defun gnus-summary-increase-score (&optional score symp)
503 "Make a score entry based on the current article.
504 The user will be prompted for header to score on, match type,
505 permanence, and the string to be used. The numerical prefix will be
507 (interactive (gnus-interactive "P\ny"))
508 (let* ((nscore (gnus-score-delta-default score))
509 (prefix (if (< nscore 0) ?L ?I))
510 (increase (> nscore 0))
512 '((?a "from" nil nil string)
513 (?s "subject" nil nil string)
514 (?b "body" "" nil body-string)
515 (?h "head" "" nil body-string)
516 (?i "message-id" nil nil string)
517 (?r "references" "message-id" nil string)
518 (?x "xref" nil nil string)
519 (?e "extra" nil nil string)
520 (?l "lines" nil nil number)
521 (?d "date" nil nil date)
522 (?f "followup" nil nil string)
523 (?t "thread" "message-id" nil string)))
525 '((?s s "substring" string)
526 (?e e "exact string" string)
527 (?f f "fuzzy string" string)
528 (?r r "regexp string" string)
529 (?z s "substring" body-string)
530 (?p r "regexp string" body-string)
531 (?b before "before date" date)
532 (?a after "after date" date)
533 (?n at "this date" date)
534 (?< < "less than number" number)
535 (?> > "greater than number" number)
536 (?= = "equal to number" number)))
537 (current-score-file gnus-current-score-file)
539 (list (list ?t (current-time-string) "temporary")
540 '(?p perm "permanent") '(?i now "immediate")))
541 (mimic gnus-score-mimic-keymap)
542 (hchar (and gnus-score-default-header
543 (aref (symbol-name gnus-score-default-header) 0)))
544 (tchar (and gnus-score-default-type
545 (aref (symbol-name gnus-score-default-type) 0)))
546 (pchar (and gnus-score-default-duration
547 (aref (symbol-name gnus-score-default-duration) 0)))
548 entry temporary type match extra)
553 ;; First we read the header to score.
558 (message "%c-" prefix))
559 (message "%s header (%s?): " (if increase "Increase" "Lower")
560 (mapconcat (lambda (s) (char-to-string (car s)))
562 (setq hchar (read-char))
563 (when (or (= hchar ??) (= hchar ?\C-h))
565 (gnus-score-insert-help "Match on header" char-to-header 1)))
567 (gnus-score-kill-help-buffer)
568 (unless (setq entry (assq (downcase hchar) char-to-header))
569 (if mimic (error "%c %c" prefix hchar)
570 (error "Invalid header type")))
572 (when (/= (downcase hchar) hchar)
573 ;; This was a majuscule, so we end reading and set the defaults.
574 (if mimic (message "%c %c" prefix hchar) (message ""))
575 (setq tchar (or tchar ?s)
576 pchar (or pchar ?t)))
581 (if (eq (nth 4 entry)
585 ;; We continue reading - the type.
589 (sit-for 1) (message "%c %c-" prefix hchar))
590 (message "%s header '%s' with match type (%s?): "
591 (if increase "Increase" "Lower")
593 (mapconcat (lambda (s) (char-to-string (car s)))
595 (setq tchar (read-char))
596 (when (or (= tchar ??) (= tchar ?\C-h))
598 (gnus-score-insert-help "Match type" legal-types 2)))
600 (gnus-score-kill-help-buffer)
601 (unless (setq type (nth 1 (assq (downcase tchar) legal-types)))
602 (if mimic (error "%c %c" prefix hchar)
603 (error "Invalid match type"))))
605 (when (/= (downcase tchar) tchar)
606 ;; It was a majuscule, so we end reading and use the default.
607 (if mimic (message "%c %c %c" prefix hchar tchar)
609 (setq pchar (or pchar ?t)))
611 ;; We continue reading.
615 (sit-for 1) (message "%c %c %c-" prefix hchar tchar))
616 (message "%s permanence (%s?): " (if increase "Increase" "Lower")
617 (mapconcat (lambda (s) (char-to-string (car s)))
619 (setq pchar (read-char))
620 (when (or (= pchar ??) (= pchar ?\C-h))
622 (gnus-score-insert-help "Match permanence" char-to-perm 2)))
624 (gnus-score-kill-help-buffer)
625 (if mimic (message "%c %c %c" prefix hchar tchar pchar)
627 (unless (setq temporary (cadr (assq pchar char-to-perm)))
628 ;; Deal with der(r)ided superannuated paradigms.
629 (when (and (eq (1+ prefix) 77)
630 (eq (+ hchar 12) 109)
632 (eq (- pchar 4) 111))
635 (error "%c %c %c %c" prefix hchar tchar pchar)
636 (error "Invalid match duration"))))
637 ;; Always kill the score help buffer.
638 (gnus-score-kill-help-buffer))
640 ;; If scoring an extra (non-standard overview) header,
641 ;; we must find out which header is in question.
643 (and gnus-extra-headers
644 (equal (nth 1 entry) "extra")
645 (intern ; need symbol
646 (gnus-completing-read-with-default
647 (symbol-name (car gnus-extra-headers)) ; default response
648 "Score extra header:" ; prompt
649 (mapcar (lambda (x) ; completion list
650 (cons (symbol-name x) x))
652 nil ; no completion limit
653 t)))) ; require match
654 ;; extra is now nil or a symbol.
656 ;; We have all the data, so we enter this score.
657 (setq match (if (string= (nth 2 entry) "") ""
658 (gnus-summary-header (or (nth 2 entry) (nth 1 entry))
661 ;; Modify the match, perhaps.
663 ((equal (nth 1 entry) "xref")
664 (when (string-match "^Xref: *" match)
665 (setq match (substring match (match-end 0))))
666 (when (string-match "^[^:]* +" match)
667 (setq match (substring match (match-end 0))))))
669 (when (memq type '(r R regexp Regexp))
670 (setq match (regexp-quote match)))
672 ;; Change score file to the "all.SCORE" file.
675 (set-buffer gnus-summary-buffer)
676 (gnus-score-load-file
677 ;; This is a kludge; yes...
679 ((eq gnus-score-find-score-files-function
680 'gnus-score-find-hierarchical)
681 (gnus-score-file-name ""))
682 ((eq gnus-score-find-score-files-function 'gnus-score-find-single)
685 (gnus-score-file-name "all"))))))
687 (gnus-summary-score-entry
688 (nth 1 entry) ; Header
691 (if (eq score 's) nil score) ; Score
692 (if (eq temporary 'perm) ; Temp
695 (not (nth 3 entry)) ; Prompt
697 extra) ; non-standard overview.
700 ;; We change the score file back to the previous one.
702 (set-buffer gnus-summary-buffer)
703 (gnus-score-load-file current-score-file)))))
705 (defun gnus-score-insert-help (string alist idx)
706 (setq gnus-score-help-winconf (current-window-configuration))
708 (set-buffer (gnus-get-buffer-create "*Score Help*"))
709 (buffer-disable-undo)
710 (delete-windows-on (current-buffer))
712 (insert string ":\n\n")
717 ;; find the longest string to display
719 (setq n (length (nth idx (car list))))
722 (setq list (cdr list)))
723 (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
724 (setq n (/ (1- (window-width)) max)) ; items per line
725 (setq width (/ (1- (window-width)) n)) ; width of each item
726 ;; insert `n' items, each in a field of width `width'
731 (delete-char -1) ; the `\n' takes a char
733 (setq pad (- width 3))
734 (setq format (concat "%c: %-" (int-to-string pad) "s"))
735 (insert (format format (caar alist) (nth idx (car alist))))
736 (setq alist (cdr alist))
738 (goto-char (point-min))
739 ;; display ourselves in a small window at the bottom
740 (gnus-appt-select-lowest-window)
741 (if (< (/ (window-height) 2) window-min-height)
742 (switch-to-buffer "*Score Help*")
744 (pop-to-buffer "*Score Help*"))
745 (let ((window-min-height 1))
746 (shrink-window-if-larger-than-buffer))
747 (select-window (gnus-get-buffer-window gnus-summary-buffer t))))
749 (defun gnus-summary-header (header &optional no-err extra)
750 ;; Return HEADER for current articles, or error.
751 (let ((article (gnus-summary-article-number))
754 (if (and (setq headers (gnus-summary-article-header article))
756 (if extra ; `header' must be "extra"
757 (or (cdr (assq extra (mail-header-extra headers))) "")
758 (aref headers (nth 1 (assoc header gnus-header-index))))
761 (error "Pseudo-articles can't be scored")))
763 (error "No article on current line")
766 (defun gnus-newsgroup-score-alist ()
768 (let ((param-file (gnus-group-find-parameter
769 gnus-newsgroup-name 'score-file)))
771 (gnus-score-load param-file)))
773 (gnus-score-file-name gnus-newsgroup-name)))
776 (defsubst gnus-score-get (symbol &optional alist)
777 ;; Get SYMBOL's definition in ALIST.
781 (gnus-newsgroup-score-alist)))))
783 (defun gnus-summary-score-entry (header match type score date
784 &optional prompt silent extra)
785 "Enter score file entry.
786 HEADER is the header being scored.
787 MATCH is the string we are looking for.
788 TYPE is the match type: substring, regexp, exact, fuzzy.
789 SCORE is the score to add.
790 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
791 If optional argument `PROMPT' is non-nil, allow user to edit match.
792 If optional argument `SILENT' is nil, show effect of score entry.
793 If optional argument `EXTRA' is non-nil, it's a non-standard overview header."
794 ;; Regexp is the default type.
797 ;; Simplify matches...
798 (cond ((or (eq type 'r) (eq type 's) (eq type nil))
799 (setq match (if match (gnus-simplify-subject-re match) "")))
801 (setq match (gnus-simplify-subject-fuzzy match))))
802 (let ((score (gnus-score-delta-default score))
803 (header (downcase header))
805 (set-text-properties 0 (length header) nil header)
807 (setq match (read-string
808 (format "Match %s on %s, %s: "
809 (cond ((eq date 'now)
815 (if (< score 0) "lower" "raise"))
817 (int-to-string match)
820 ;; If this is an integer comparison, we transform from string to int.
821 (if (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
823 (setq match (string-to-int match)))
824 (set-text-properties 0 (length match) nil match))
826 (unless (eq date 'now)
827 ;; Add the score entry to the score file.
828 (when (= score gnus-score-interactive-default-score)
830 (let ((old (gnus-score-get header))
836 (and date (if (numberp date) date
838 type (symbol-name extra)))
841 (and date (if (numberp date) date
844 (date (list match score (date-to-day date)))
845 (score (list match score))
847 ;; We see whether we can collapse some score entries.
848 ;; This isn't quite correct, because there may be more elements
849 ;; later on with the same key that have matching elems... Hm.
851 (setq elem (assoc match old))
852 (eq (nth 3 elem) (nth 3 new))
853 (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
854 (and (not (nth 2 elem)) (not (nth 2 new)))))
855 ;; Yup, we just add this new score to the old elem.
856 (setcar (cdr elem) (+ (or (nth 1 elem)
857 gnus-score-interactive-default-score)
859 gnus-score-interactive-default-score)))
860 ;; Nope, we have to add a new elem.
861 (gnus-score-set header (if old (cons new old) (list new)) nil t))
862 (gnus-score-set 'touched '(t))))
864 ;; Score the current buffer.
866 (if (and (>= (nth 1 (assoc header gnus-header-index)) 0)
867 (eq (nth 2 (assoc header gnus-header-index))
869 (gnus-summary-score-effect header match type score extra)
870 (gnus-summary-rescore)))
872 ;; Return the new scoring rule.
875 (defun gnus-summary-score-effect (header match type score extra)
876 "Simulate the effect of a score file entry.
877 HEADER is the header being scored.
878 MATCH is the string we are looking for.
879 TYPE is the score type.
880 SCORE is the score to add.
881 EXTRA is the possible non-standard header."
882 (interactive (list (completing-read "Header: "
884 (lambda (x) (fboundp (nth 2 x)))
886 (read-string "Match: ")
887 (y-or-n-p "Use regexp match? ")
888 (prefix-numeric-value current-prefix-arg)))
890 (unless (and (stringp match) (> (length match) 0))
892 (goto-char (point-min))
893 (let ((regexp (cond ((eq type 'f)
894 (gnus-simplify-subject-fuzzy match))
898 (concat "\\`" (regexp-quote match) "\\'"))
900 (regexp-quote match)))))
902 (let ((content (gnus-summary-header header 'noerr extra))
903 (case-fold-search t))
905 (when (if (eq type 'f)
906 (string-equal (gnus-simplify-subject-fuzzy content)
908 (string-match regexp content))
909 (gnus-summary-raise-score score))))
910 (beginning-of-line 2))))
911 (gnus-set-mode-line 'summary))
913 (defun gnus-summary-score-crossposting (score date)
914 ;; Enter score file entry for current crossposting.
915 ;; SCORE is the score to add.
916 ;; DATE is the expire date.
917 (let ((xref (gnus-summary-header "xref"))
921 (error "This article is not crossposted"))
922 (while (string-match " \\([^ \t]+\\):" xref start)
923 (setq start (match-end 0))
926 (substring xref (match-beginning 1) (match-end 1)))
927 gnus-newsgroup-name))
928 (gnus-summary-score-entry
929 "xref" (concat " " group ":") nil score date t)))))
936 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
938 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
939 (defun gnus-score-set-mark-below (score)
940 "Automatically mark articles with score below SCORE as read."
942 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
943 (string-to-int (read-string "Mark below: ")))))
944 (setq score (or score gnus-summary-default-score 0))
945 (gnus-score-set 'mark (list score))
946 (gnus-score-set 'touched '(t))
947 (setq gnus-summary-mark-below score)
948 (gnus-score-update-lines))
950 (defun gnus-score-update-lines ()
951 "Update all lines in the summary buffer."
953 (goto-char (point-min))
955 (gnus-summary-update-line)
958 (defun gnus-score-update-all-lines ()
959 "Update all lines in the summary buffer, even the hidden ones."
961 (goto-char (point-min))
964 (when (gnus-summary-show-thread)
965 (push (point) hidden))
966 (gnus-summary-update-line)
968 ;; Re-hide the hidden threads.
970 (goto-char (pop hidden))
971 (gnus-summary-hide-thread)))))
973 (defun gnus-score-set-expunge-below (score)
974 "Automatically expunge articles with score below SCORE."
976 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
977 (string-to-int (read-string "Set expunge below: ")))))
978 (setq score (or score gnus-summary-default-score 0))
979 (gnus-score-set 'expunge (list score))
980 (gnus-score-set 'touched '(t)))
982 (defun gnus-score-followup-article (&optional score)
983 "Add SCORE to all followups to the article in the current buffer."
985 (setq score (gnus-score-delta-default score))
986 (when (gnus-buffer-live-p gnus-summary-buffer)
989 (message-narrow-to-headers)
990 (let ((id (mail-fetch-field "message-id")))
992 (set-buffer gnus-summary-buffer)
993 (gnus-summary-score-entry
994 "references" (concat id "[ \t]*$") 'r
995 score (current-time-string) nil t)))))))
997 (defun gnus-score-followup-thread (&optional score)
998 "Add SCORE to all later articles in the thread the current buffer is part of."
1000 (setq score (gnus-score-delta-default score))
1001 (when (gnus-buffer-live-p gnus-summary-buffer)
1004 (goto-char (point-min))
1005 (let ((id (mail-fetch-field "message-id")))
1007 (set-buffer gnus-summary-buffer)
1008 (gnus-summary-score-entry
1010 score (current-time-string))))))))
1012 (defun gnus-score-set (symbol value &optional alist warn)
1013 ;; Set SYMBOL to VALUE in ALIST.
1017 (gnus-newsgroup-score-alist)))
1018 (entry (assoc symbol alist)))
1019 (cond ((gnus-score-get 'read-only alist)
1020 ;; This is a read-only score file, so we do nothing.
1022 (gnus-message 4 "Note: read-only score file; entry discarded")))
1024 (setcdr entry value))
1026 (error "Empty alist"))
1029 (cons (cons symbol value) (cdr alist)))))))
1031 (defun gnus-summary-raise-score (n)
1032 "Raise the score of the current article by N."
1034 (gnus-summary-set-score (+ (gnus-summary-article-score)
1035 (or n gnus-score-interactive-default-score ))))
1037 (defun gnus-summary-set-score (n)
1038 "Set the score of the current article to N."
1041 (gnus-summary-show-thread)
1042 (let ((buffer-read-only nil))
1044 (gnus-summary-update-mark
1045 (if (= n (or gnus-summary-default-score 0)) ? ;Whitespace
1046 (if (< n (or gnus-summary-default-score 0))
1047 gnus-score-below-mark gnus-score-over-mark))
1049 (let* ((article (gnus-summary-article-number))
1050 (score (assq article gnus-newsgroup-scored)))
1051 (if score (setcdr score n)
1052 (push (cons article n) gnus-newsgroup-scored)))
1053 (gnus-summary-update-line)))
1055 (defun gnus-summary-current-score ()
1056 "Return the score of the current article."
1058 (gnus-message 1 "%s" (gnus-summary-article-score)))
1060 (defun gnus-score-change-score-file (file)
1061 "Change current score alist."
1063 (list (read-file-name "Change to score file: " gnus-kill-files-directory)))
1064 (gnus-score-load-file file)
1065 (gnus-set-mode-line 'summary))
1067 (defvar gnus-score-edit-exit-function)
1068 (defun gnus-score-edit-current-scores (file)
1069 "Edit the current score alist."
1070 (interactive (list gnus-current-score-file))
1071 (if (not gnus-current-score-file)
1072 (error "No current score file")
1073 (let ((winconf (current-window-configuration)))
1074 (when (buffer-name gnus-summary-buffer)
1076 (gnus-make-directory (file-name-directory file))
1077 (setq gnus-score-edit-buffer (find-file-noselect file))
1078 (gnus-configure-windows 'edit-score)
1080 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1081 (make-local-variable 'gnus-prev-winconf)
1082 (setq gnus-prev-winconf winconf))
1084 4 (substitute-command-keys
1085 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits"))))
1087 (defun gnus-score-edit-file (file)
1088 "Edit a score file."
1090 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
1091 (gnus-make-directory (file-name-directory file))
1092 (when (buffer-name gnus-summary-buffer)
1094 (let ((winconf (current-window-configuration)))
1095 (setq gnus-score-edit-buffer (find-file-noselect file))
1096 (gnus-configure-windows 'edit-score)
1098 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1099 (make-local-variable 'gnus-prev-winconf)
1100 (setq gnus-prev-winconf winconf))
1102 4 (substitute-command-keys
1103 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
1105 (defun gnus-score-load-file (file)
1106 ;; Load score file FILE. Returns a list a retrieved score-alists.
1107 (let* ((file (expand-file-name
1108 (or (and (string-match
1109 (concat "^" (regexp-quote
1111 gnus-kill-files-directory)))
1112 (expand-file-name file))
1114 (expand-file-name file gnus-kill-files-directory))))
1115 (cached (assoc file gnus-score-cache))
1116 (global (member file gnus-internal-global-score-files))
1119 ;; The score file was already loaded.
1120 (setq alist (cdr cached))
1121 ;; We load the score file.
1122 (setq gnus-score-alist nil)
1123 (setq alist (gnus-score-load-score-alist file))
1124 ;; We add '(touched) to the alist to signify that it hasn't been
1126 (unless (assq 'touched alist)
1127 (push (list 'touched nil) alist))
1128 ;; If it is a global score file, we make it read-only.
1130 (not (assq 'read-only alist))
1131 (push (list 'read-only t) alist))
1132 (push (cons file alist) gnus-score-cache))
1136 ;; Downcase all header names.
1139 (setcar (car a) (downcase (caar a)))
1141 ;; Advanced scoring.
1145 ;; If there are actual scores in the alist, we add it to the
1146 ;; return value of this function.
1148 (setq lists (list alist))))
1149 ;; Treat the other possible atoms in the score alist.
1150 (let ((mark (car (gnus-score-get 'mark alist)))
1151 (expunge (car (gnus-score-get 'expunge alist)))
1152 (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
1153 (files (gnus-score-get 'files alist))
1154 (exclude-files (gnus-score-get 'exclude-files alist))
1155 (orphan (car (gnus-score-get 'orphan alist)))
1156 (adapt (gnus-score-get 'adapt alist))
1157 (thread-mark-and-expunge
1158 (car (gnus-score-get 'thread-mark-and-expunge alist)))
1159 (adapt-file (car (gnus-score-get 'adapt-file alist)))
1160 (local (gnus-score-get 'local alist))
1161 (decay (car (gnus-score-get 'decay alist)))
1162 (eval (car (gnus-score-get 'eval alist))))
1163 ;; Perform possible decays.
1164 (when (and gnus-decay-scores
1165 (or cached (file-exists-p file))
1167 (gnus-decay-scores alist decay)))
1168 (gnus-score-set 'touched '(t) alist)
1169 (gnus-score-set 'decay (list (time-to-days (current-time))) alist))
1170 ;; We do not respect eval and files atoms from global score
1172 (when (and files (not global))
1173 (setq lists (apply 'append lists
1174 (mapcar (lambda (file)
1175 (gnus-score-load-file file))
1176 (if adapt-file (cons adapt-file files)
1178 (when (and eval (not global))
1180 ;; We then expand any exclude-file directives.
1181 (setq gnus-scores-exclude-files
1188 (expand-file-name sfile (file-name-directory file))
1189 (expand-file-name sfile gnus-kill-files-directory)))
1191 gnus-scores-exclude-files))
1194 (set-buffer gnus-summary-buffer)
1196 (and (consp (car local))
1197 (symbolp (caar local))
1199 (make-local-variable (caar local))
1200 (set (caar local) (nth 1 (car local)))))
1201 (setq local (cdr local)))))
1203 (setq gnus-orphan-score orphan))
1204 (setq gnus-adaptive-score-alist
1205 (cond ((equal adapt '(t))
1206 (setq gnus-newsgroup-adaptive t)
1207 gnus-default-adaptive-score-alist)
1208 ((equal adapt '(ignore))
1209 (setq gnus-newsgroup-adaptive nil))
1211 (setq gnus-newsgroup-adaptive t)
1214 gnus-default-adaptive-score-alist)))
1215 (setq gnus-thread-expunge-below
1216 (or thread-mark-and-expunge gnus-thread-expunge-below))
1217 (setq gnus-summary-mark-below
1218 (or mark mark-and-expunge gnus-summary-mark-below))
1219 (setq gnus-summary-expunge-below
1220 (or expunge mark-and-expunge gnus-summary-expunge-below))
1221 (setq gnus-newsgroup-adaptive-score-file
1222 (or adapt-file gnus-newsgroup-adaptive-score-file)))
1223 (setq gnus-current-score-file file)
1224 (setq gnus-score-alist alist)
1227 (defun gnus-score-load (file)
1229 (let ((cache (assoc file gnus-score-cache)))
1231 (setq gnus-score-alist (cdr cache))
1232 (setq gnus-score-alist nil)
1233 (gnus-score-load-score-alist file)
1234 (unless gnus-score-alist
1235 (setq gnus-score-alist (copy-alist '((touched nil)))))
1236 (push (cons file gnus-score-alist) gnus-score-cache))))
1238 (defun gnus-score-remove-from-cache (file)
1239 (setq gnus-score-cache
1240 (delq (assoc file gnus-score-cache) gnus-score-cache)))
1242 (defun gnus-score-load-score-alist (file)
1245 (if (not (file-readable-p file))
1246 ;; Couldn't read file.
1247 (setq gnus-score-alist nil)
1250 (let ((coding-system-for-read score-mode-coding-system))
1251 (insert-file-contents file))
1252 (goto-char (point-min))
1253 ;; Only do the loading if the score file isn't empty.
1254 (when (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
1257 (read (current-buffer))
1259 (gnus-error 3.2 "Problem with score file %s" file))))))
1263 ;; Bogus score file.
1264 (error "Invalid syntax with score file %s" file))
1265 ((eq (car alist) 'setq)
1266 ;; This is an old-style score file.
1267 (setq gnus-score-alist (gnus-score-transform-old-to-new alist)))
1269 (setq gnus-score-alist alist)))
1270 ;; Check the syntax of the score file.
1271 (setq gnus-score-alist
1272 (gnus-score-check-syntax gnus-score-alist file)))))
1274 (defun gnus-score-check-syntax (alist file)
1275 "Check the syntax of the score ALIST."
1279 ((not (consp alist))
1280 (gnus-message 1 "Score file is not a list: %s" file)
1286 (while (and a (not err))
1290 ((not (listp (car a)))
1291 (format "Invalid score element %s in %s" (car a) file))
1294 ((not (listp (setq sr (cdar a))))
1295 (format "Invalid header match %s in %s" (nth 1 (car a)) file))
1297 (setq type (caar a))
1298 (while (and sr (not err))
1303 ((if (member (downcase type) '("lines" "chars"))
1304 (not (numberp (car s)))
1305 (not (stringp (car s))))
1306 (format "Invalid match %s in %s" (car s) file))
1307 ((and (cadr s) (not (integerp (cadr s))))
1308 (format "Non-integer score %s in %s" (cadr s) file))
1309 ((and (caddr s) (not (integerp (caddr s))))
1310 (format "Non-integer date %s in %s" (caddr s) file))
1311 ((and (cadddr s) (not (symbolp (cadddr s))))
1312 (format "Non-symbol match type %s in %s" (cadddr s) file)))))
1318 (gnus-message 3 err)
1323 (defun gnus-score-transform-old-to-new (alist)
1324 (let* ((alist (nth 2 alist))
1326 (when (eq (car alist) 'quote)
1327 (setq alist (nth 1 alist)))
1329 (setq entry (car alist))
1330 (if (stringp (car entry))
1331 (let ((scor (cdr entry)))
1335 (list (caar scor) (nth 2 (car scor))
1336 (and (nth 3 (car scor))
1337 (date-to-day (nth 3 (car scor))))
1338 (if (nth 1 (car scor)) 'r 's)))
1339 (setq scor (cdr scor))))
1340 (push (if (not (listp (cdr entry)))
1341 (list (car entry) (cdr entry))
1344 (setq alist (cdr alist)))
1345 (cons (list 'touched t) (nreverse out))))
1347 (defun gnus-score-save ()
1348 ;; Save all score information.
1349 (let ((cache gnus-score-cache)
1352 (setq gnus-score-alist nil)
1353 (nnheader-set-temp-buffer " *Gnus Scores*")
1356 (setq entry (pop cache)
1357 file (nnheader-translate-file-chars (car entry) t)
1359 (if (or (not (equal (gnus-score-get 'touched score) '(t)))
1360 (gnus-score-get 'read-only score)
1361 (and (file-exists-p file)
1362 (not (file-writable-p file))))
1364 (setq score (setcdr entry (gnus-delete-alist 'touched score)))
1366 (let (emacs-lisp-mode-hook)
1368 (concat (regexp-quote gnus-adaptive-file-suffix) "$")
1370 ;; This is an adaptive score file, so we do not run
1371 ;; it through `pp'. These files can get huge, and
1372 ;; are not meant to be edited by human hands.
1374 ;; This is a normal score file, so we print it very
1376 (let ((lisp-mode-syntax-table score-mode-syntax-table))
1377 (pp score (current-buffer)))))
1378 (gnus-make-directory (file-name-directory file))
1379 ;; If the score file is empty, we delete it.
1380 (if (zerop (buffer-size))
1382 ;; There are scores, so we write the file.
1383 (when (file-writable-p file)
1384 (let ((coding-system-for-write score-mode-coding-system))
1385 (gnus-write-buffer file))
1386 (when gnus-score-after-write-file-function
1387 (funcall gnus-score-after-write-file-function file)))))
1388 (and gnus-score-uncacheable-files
1389 (string-match gnus-score-uncacheable-files file)
1390 (gnus-score-remove-from-cache file)))
1391 (kill-buffer (current-buffer)))))
1393 (defun gnus-score-load-files (score-files)
1394 "Load all score files in SCORE-FILES."
1395 ;; Load the score files.
1398 (if (stringp (car score-files))
1399 ;; It is a string, which means that it's a score file name,
1400 ;; so we load the score file and add the score alist to
1401 ;; the list of alists.
1402 (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
1403 ;; It is an alist, so we just add it to the list directly.
1404 (setq scores (nconc (car score-files) scores)))
1405 (setq score-files (cdr score-files)))
1406 ;; Prune the score files that are to be excluded, if any.
1407 (when gnus-scores-exclude-files
1411 (and (setq c (rassq (car s) gnus-score-cache))
1412 (member (car c) gnus-scores-exclude-files)
1413 (setq scores (delq (car s) scores)))
1417 (defun gnus-score-headers (score-files &optional trace)
1418 ;; Score `gnus-newsgroup-headers'.
1420 ;; PLM: probably this is not the best place to clear orphan-score
1421 (setq gnus-orphan-score nil
1422 gnus-scores-articles nil
1423 gnus-scores-exclude-files nil
1424 scores (gnus-score-load-files score-files))
1430 (when (and gnus-summary-default-score
1432 (let* ((entries gnus-header-index)
1433 (now (date-to-day (current-time-string)))
1434 (expire (and gnus-score-expiry-days
1435 (- now gnus-score-expiry-days)))
1436 (headers gnus-newsgroup-headers)
1437 (current-score-file gnus-current-score-file)
1439 (gnus-message 7 "Scoring...")
1440 ;; Create articles, an alist of the form `(HEADER . SCORE)'.
1441 (while (setq header (pop headers))
1442 ;; WARNING: The assq makes the function O(N*S) while it could
1443 ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
1444 ;; and S is (length gnus-newsgroup-scored).
1445 (unless (assq (mail-header-number header) gnus-newsgroup-scored)
1446 (setq gnus-scores-articles ;Total of 2 * N cons-cells used.
1447 (cons (cons header (or gnus-summary-default-score 0))
1448 gnus-scores-articles))))
1451 (set-buffer (gnus-get-buffer-create "*Headers*"))
1452 (buffer-disable-undo)
1453 (when (gnus-buffer-live-p gnus-summary-buffer)
1454 (message-clone-locals gnus-summary-buffer))
1456 ;; Set the global variant of this variable.
1457 (setq gnus-current-score-file current-score-file)
1459 (when gnus-orphan-score
1460 (setq gnus-score-index
1461 (nth 1 (assoc "references" gnus-header-index)))
1462 (gnus-score-orphans gnus-orphan-score))
1463 ;; Run each header through the score process.
1465 (setq entry (pop entries)
1466 header (nth 0 entry)
1467 gnus-score-index (nth 1 (assoc header gnus-header-index)))
1468 (when (< 0 (apply 'max (mapcar
1470 (length (gnus-score-get header score)))
1472 ;; Call the scoring function for this type of "header".
1473 (when (setq new (funcall (nth 2 entry) scores header
1476 (when (gnus-buffer-live-p gnus-summary-buffer)
1477 (let ((scored gnus-newsgroup-scored))
1478 (with-current-buffer gnus-summary-buffer
1479 (setq gnus-newsgroup-scored scored))))
1480 ;; Remove the buffer.
1481 (kill-buffer (current-buffer)))
1483 ;; Add articles to `gnus-newsgroup-scored'.
1484 (while gnus-scores-articles
1485 (when (or (/= gnus-summary-default-score
1486 (cdar gnus-scores-articles))
1488 (push (cons (mail-header-number (caar gnus-scores-articles))
1489 (cdar gnus-scores-articles))
1490 gnus-newsgroup-scored))
1491 (setq gnus-scores-articles (cdr gnus-scores-articles)))
1494 (while (setq score (pop scores))
1496 (when (consp (caar score))
1497 (gnus-score-advanced (car score) trace))
1500 (gnus-message 7 "Scoring...done"))))))
1502 (defun gnus-score-lower-thread (thread score-adjust)
1503 "Lower the score on THREAD with SCORE-ADJUST.
1504 THREAD is expected to contain a list of the form `(PARENT [CHILD1
1505 CHILD2 ...])' where PARENT is a header array and each CHILD is a list
1506 of the same form as THREAD. The empty list `nil' is valid. For each
1507 article in the tree, the score of the corresponding entry in
1508 `gnus-newsgroup-scored' is adjusted by SCORE-ADJUST."
1510 (let ((head (car thread)))
1512 ;; handle a child and its descendants
1513 (gnus-score-lower-thread head score-adjust)
1514 ;; handle the parent
1515 (let* ((article (mail-header-number head))
1516 (score (assq article gnus-newsgroup-scored)))
1517 (if score (setcdr score (+ (cdr score) score-adjust))
1518 (push (cons article score-adjust) gnus-newsgroup-scored)))))
1519 (setq thread (cdr thread))))
1521 (defun gnus-score-orphans (score)
1523 A root is an article with no references. An orphan is an article
1524 which has references, but is not connected via its references to a
1525 root article. This function finds all the orphans, and adjusts their
1526 score in `gnus-newsgroup-scored' by SCORE."
1527 ;; gnus-make-threads produces a list, where each entry is a "thread"
1528 ;; as described in the gnus-score-lower-thread docs. This function
1529 ;; will be called again (after limiting has been done) if the display
1530 ;; is threaded. It would be nice to somehow save this info and use
1532 (dolist (thread (gnus-make-threads))
1533 (let ((id (aref (car thread) gnus-score-index)))
1534 ;; If the parent of the thread is not a root, lower the score of
1535 ;; it and its descendants. Note that some roots seem to satisfy
1536 ;; (eq id nil) and some (eq id ""); not sure why.
1538 (not (string= id "")))
1539 (gnus-score-lower-thread thread score)))))
1541 (defun gnus-score-integer (scores header now expire &optional trace)
1542 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1546 (setq alist (car scores)
1548 entries (assoc header alist))
1549 (while (cdr entries) ;First entry is the header index.
1550 (let* ((rest (cdr entries))
1552 (match (nth 0 kill))
1553 (type (or (nth 3 kill) '>))
1554 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1557 (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
1558 (eq type '>=) (eq type '=))
1560 (error "Invalid match type: %s" type)))
1561 (articles gnus-scores-articles))
1562 ;; Instead of doing all the clever stuff that
1563 ;; `gnus-score-string' does to minimize searches and stuff,
1564 ;; I will assume that people generally will put so few
1565 ;; matches on numbers that any cleverness will take more
1566 ;; time than one would gain.
1568 (when (funcall match-func
1569 (or (aref (caar articles) gnus-score-index) 0)
1572 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1575 (setcdr (car articles) (+ score (cdar articles))))
1576 (setq articles (cdr articles)))
1577 ;; Update expire date
1578 (cond ((null date)) ;Permanent entry.
1579 ((and found gnus-update-score-entry-dates) ;Match, update date.
1580 (gnus-score-set 'touched '(t) alist)
1581 (setcar (nthcdr 2 kill) now))
1582 ((and expire (< date expire)) ;Old entry, remove.
1583 (gnus-score-set 'touched '(t) alist)
1584 (setcdr entries (cdr rest))
1585 (setq rest entries)))
1586 (setq entries rest)))))
1589 (defun gnus-score-date (scores header now expire &optional trace)
1590 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1591 entries alist match match-func article)
1594 (setq alist (car scores)
1596 entries (assoc header alist))
1597 (while (cdr entries) ;First entry is the header index.
1598 (let* ((rest (cdr entries))
1600 (type (or (nth 3 kill) 'before))
1601 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1604 (articles gnus-scores-articles)
1608 (setq match-func 'string<
1609 match (gnus-date-iso8601 (nth 0 kill))))
1611 (setq match-func 'gnus-string>
1612 match (gnus-date-iso8601 (nth 0 kill))))
1614 (setq match-func 'string=
1615 match (gnus-date-iso8601 (nth 0 kill))))
1617 (setq match-func 'string-match
1618 match (nth 0 kill)))
1619 (t (error "Invalid match type: %s" type)))
1620 ;; Instead of doing all the clever stuff that
1621 ;; `gnus-score-string' does to minimize searches and stuff,
1622 ;; I will assume that people generally will put so few
1623 ;; matches on numbers that any cleverness will take more
1624 ;; time than one would gain.
1625 (while (setq article (pop articles))
1627 (setq l (aref (car article) gnus-score-index))
1628 (funcall match-func match (gnus-date-iso8601 l)))
1630 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1633 (setcdr article (+ score (cdr article)))))
1634 ;; Update expire date
1635 (cond ((null date)) ;Permanent entry.
1636 ((and found gnus-update-score-entry-dates) ;Match, update date.
1637 (gnus-score-set 'touched '(t) alist)
1638 (setcar (nthcdr 2 kill) now))
1639 ((and expire (< date expire)) ;Old entry, remove.
1640 (gnus-score-set 'touched '(t) alist)
1641 (setcdr entries (cdr rest))
1642 (setq rest entries)))
1643 (setq entries rest)))))
1646 (defun gnus-score-body (scores header now expire &optional trace)
1647 (if gnus-agent-fetching
1650 (setq gnus-scores-articles
1651 (sort gnus-scores-articles
1653 (< (mail-header-number (car a1))
1654 (mail-header-number (car a2))))))
1655 (set-buffer nntp-server-buffer)
1657 (let* ((buffer-read-only nil)
1658 (articles gnus-scores-articles)
1660 (request-func (cond ((string= "head" header)
1662 ((string= "body" header)
1664 (t 'gnus-request-article)))
1665 entries alist ofunc article last)
1667 (setq last (mail-header-number (caar (last articles))))
1668 ;; Not all backends support partial fetching. In that case,
1669 ;; we just fetch the entire article.
1670 (unless (gnus-check-backend-function
1671 (and (string-match "^gnus-" (symbol-name request-func))
1672 (intern (substring (symbol-name request-func)
1674 gnus-newsgroup-name)
1675 (setq ofunc request-func)
1676 (setq request-func 'gnus-request-article))
1678 (setq article (mail-header-number (caar articles)))
1679 (gnus-message 7 "Scoring article %s of %s..." article last)
1681 (when (funcall request-func article gnus-newsgroup-name)
1682 (goto-char (point-min))
1683 ;; If just parts of the article is to be searched, but the
1684 ;; backend didn't support partial fetching, we just narrow
1685 ;; to the relevant parts.
1687 (if (eq ofunc 'gnus-request-head)
1690 (or (search-forward "\n\n" nil t) (point-max)))
1692 (or (search-forward "\n\n" nil t) (point))
1694 (setq scores all-scores)
1697 (setq alist (pop scores)
1698 entries (assoc header alist))
1699 (while (cdr entries) ;First entry is the header index.
1700 (let* ((rest (cdr entries))
1702 (match (nth 0 kill))
1703 (type (or (nth 3 kill) 's))
1704 (score (or (nth 1 kill)
1705 gnus-score-interactive-default-score))
1709 (not (or (eq type 'R) (eq type 'S)
1710 (eq type 'Regexp) (eq type 'String))))
1712 (cond ((or (eq type 'r) (eq type 'R)
1713 (eq type 'regexp) (eq type 'Regexp))
1715 ((or (eq type 's) (eq type 'S)
1716 (eq type 'string) (eq type 'String))
1719 (error "Invalid match type: %s" type)))))
1720 (goto-char (point-min))
1721 (when (funcall search-func match nil t)
1722 ;; Found a match, update scores.
1723 (setcdr (car articles) (+ score (cdar articles)))
1727 (cons (car-safe (rassq alist gnus-score-cache)) kill)
1729 ;; Update expire date
1732 ((null date)) ;Permanent entry.
1733 ((and found gnus-update-score-entry-dates)
1734 ;; Match, update date.
1735 (gnus-score-set 'touched '(t) alist)
1736 (setcar (nthcdr 2 kill) now))
1737 ((and expire (< date expire)) ;Old entry, remove.
1738 (gnus-score-set 'touched '(t) alist)
1739 (setcdr entries (cdr rest))
1740 (setq rest entries))))
1741 (setq entries rest)))))
1742 (setq articles (cdr articles)))))))
1745 (defun gnus-score-thread (scores header now expire &optional trace)
1746 (gnus-score-followup scores header now expire trace t))
1748 (defun gnus-score-followup (scores header now expire &optional trace thread)
1749 (if gnus-agent-fetching
1750 ;; FIXME: It seems doable in fetching mode.
1752 ;; Insert the unique article headers in the buffer.
1753 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1754 (current-score-file gnus-current-score-file)
1756 ;; gnus-score-index is used as a free variable.
1757 alike last this art entries alist articles
1760 ;; Change score file to the adaptive score file. All entries that
1761 ;; this function makes will be put into this file.
1763 (set-buffer gnus-summary-buffer)
1764 (gnus-score-load-file
1765 (or gnus-newsgroup-adaptive-score-file
1766 (gnus-score-file-name
1767 gnus-newsgroup-name gnus-adaptive-file-suffix))))
1769 (setq gnus-scores-articles (sort gnus-scores-articles
1770 'gnus-score-string<)
1771 articles gnus-scores-articles)
1775 (setq art (car articles)
1776 this (aref (car art) gnus-score-index)
1777 articles (cdr articles))
1778 (if (equal last this)
1782 (put-text-property (1- (point)) (point) 'articles alike))
1783 (setq alike (list art)
1785 (when last ; Bwadr, duplicate code.
1787 (put-text-property (1- (point)) (point) 'articles alike))
1791 (setq alist (car scores)
1793 entries (assoc header alist))
1794 (while (cdr entries) ;First entry is the header index.
1795 (let* ((rest (cdr entries))
1797 (match (nth 0 kill))
1798 (type (or (nth 3 kill) 's))
1799 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1802 (mt (aref (symbol-name type) 0))
1804 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1807 (cond ((= dmt ?r) 're-search-forward)
1808 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1809 (t (error "Invalid match type: %s" type))))
1811 (goto-char (point-min))
1813 (while (funcall search-func match nil t)
1814 (and (= (progn (beginning-of-line) (point))
1815 (match-beginning 0))
1816 (= (progn (end-of-line) (point))
1819 (setq found (setq arts (get-text-property
1820 (point) 'articles)))
1821 ;; Found a match, update scores.
1823 (setq art (car arts)
1825 (gnus-score-add-followups
1826 (car art) score all-scores thread))))
1828 (while (funcall search-func match nil t)
1830 (setq found (setq arts (get-text-property (point) 'articles)))
1831 ;; Found a match, update scores.
1832 (while (setq art (pop arts))
1833 (when (setq new (gnus-score-add-followups
1834 (car art) score all-scores thread))
1836 ;; Update expire date
1837 (cond ((null date)) ;Permanent entry.
1838 ((and found gnus-update-score-entry-dates)
1839 ;Match, update date.
1840 (gnus-score-set 'touched '(t) alist)
1841 (setcar (nthcdr 2 kill) now))
1842 ((and expire (< date expire)) ;Old entry, remove.
1843 (gnus-score-set 'touched '(t) alist)
1844 (setcdr entries (cdr rest))
1845 (setq rest entries)))
1846 (setq entries rest))))
1847 ;; We change the score file back to the previous one.
1849 (set-buffer gnus-summary-buffer)
1850 (gnus-score-load-file current-score-file))
1851 (list (cons "references" news)))))
1853 (defun gnus-score-add-followups (header score scores &optional thread)
1854 "Add a score entry to the adapt file."
1856 (set-buffer gnus-summary-buffer)
1857 (let* ((id (mail-header-id header))
1858 (scores (car scores))
1860 ;; Don't enter a score if there already is one.
1861 (while (setq entry (pop scores))
1862 (and (equal "references" (car entry))
1863 (or (null (nth 3 (cadr entry)))
1864 (eq 's (nth 3 (cadr entry))))
1868 (gnus-summary-score-entry
1869 (if thread "thread" "references")
1870 id 's score (current-time-string) nil t)))))
1872 (defun gnus-score-string (score-list header now expire &optional trace)
1873 ;; Score ARTICLES according to HEADER in SCORE-LIST.
1874 ;; Update matching entries to NOW and remove unmatched entries older
1877 ;; Insert the unique article headers in the buffer.
1878 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1879 ;; gnus-score-index is used as a free variable.
1880 (simplify (and gnus-score-thread-simplify
1881 (string= "subject" header)))
1882 alike last this art entries alist articles
1883 fuzzies arts words kill)
1885 ;; Sorting the articles costs os O(N*log N) but will allow us to
1886 ;; only match with each unique header. Thus the actual matching
1887 ;; will be O(M*U) where M is the number of strings to match with,
1888 ;; and U is the number of unique headers. It is assumed (but
1889 ;; untested) this will be a net win because of the large constant
1890 ;; factor involved with string matching.
1891 (setq gnus-scores-articles
1892 ;; We cannot string-sort the extra headers list. *sigh*
1893 (if (= gnus-score-index 9)
1894 gnus-scores-articles
1895 (sort gnus-scores-articles 'gnus-score-string<))
1896 articles gnus-scores-articles)
1899 (while (setq art (pop articles))
1900 (setq this (aref (car art) gnus-score-index))
1902 ;; If we're working with non-standard headers, we are stuck
1903 ;; with working on them as a group. What a hassle.
1904 ;; Just wait 'til you see what horrors we commit against `match'...
1905 (if (= gnus-score-index 9)
1906 (setq this (prin1-to-string this))) ; ick.
1909 (setq this (gnus-map-function gnus-simplify-subject-functions this)))
1910 (if (equal last this)
1911 ;; O(N*H) cons-cells used here, where H is the number of
1915 ;; Insert the line, with a text property on the
1916 ;; terminating newline referring to the articles with
1919 (put-text-property (1- (point)) (point) 'articles alike))
1920 (setq alike (list art)
1922 (when last ; Bwadr, duplicate code.
1924 (put-text-property (1- (point)) (point) 'articles alike))
1926 ;; Go through all the score alists and pick out the entries
1929 (setq alist (pop score-list)
1930 ;; There's only one instance of this header for
1931 ;; each score alist.
1932 entries (assoc header alist))
1933 (while (cdr entries) ;First entry is the header index.
1934 (let* ((kill (cadr entries))
1935 (type (or (nth 3 kill) 's))
1936 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1938 (extra (nth 4 kill)) ; non-standard header; string.
1940 (mt (aref (symbol-name type) 0))
1941 (case-fold-search (not (memq mt '(?R ?S ?E ?F))))
1943 ;; Assume user already simplified regexp and fuzzies
1944 (match (if (and simplify (not (memq dmt '(?f ?r))))
1946 gnus-simplify-subject-functions
1950 (cond ((= dmt ?r) 're-search-forward)
1951 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1953 (t (error "Invalid match type: %s" type)))))
1955 ;; Evil hackery to make match usable in non-standard headers.
1957 (setq match (concat "[ (](" extra " \\. \"[^)]*"
1958 match "[^\"]*\")[ )]")
1959 search-func 're-search-forward)) ; XXX danger?!?
1962 ;; Fuzzy matches. We save these for later.
1964 (push (cons entries alist) fuzzies)
1965 (setq entries (cdr entries)))
1966 ;; Word matches. Save these for even later.
1968 (push (cons entries alist) words)
1969 (setq entries (cdr entries)))
1972 ;; Do exact matching.
1973 (goto-char (point-min))
1974 (while (and (not (eobp))
1975 (funcall search-func match nil t))
1976 ;; Is it really exact?
1978 (= (gnus-point-at-bol) (match-beginning 0))
1981 (setq found (setq arts (get-text-property
1982 (point) 'articles)))
1983 ;; Found a match, update scores.
1985 (while (setq art (pop arts))
1986 (setcdr art (+ score (cdr art)))
1989 (car-safe (rassq alist gnus-score-cache))
1992 (while (setq art (pop arts))
1993 (setcdr art (+ score (cdr art)))))))
1995 ;; Update expiry date
1997 (setq entries (cdr entries))
2001 (setq entries (cdr entries)))
2002 ;; We have a match, so we update the date.
2003 ((and found gnus-update-score-entry-dates)
2004 (gnus-score-set 'touched '(t) alist)
2005 (setcar (nthcdr 2 kill) now)
2006 (setq entries (cdr entries)))
2007 ;; This entry has expired, so we remove it.
2008 ((and expire (< date expire))
2009 (gnus-score-set 'touched '(t) alist)
2010 (setcdr entries (cddr entries)))
2011 ;; No match; go to next entry.
2013 (setq entries (cdr entries))))))
2014 ;; Regexp and substring matching.
2016 (goto-char (point-min))
2017 (when (string= match "")
2019 (while (and (not (eobp))
2020 (funcall search-func match nil t))
2021 (goto-char (match-beginning 0))
2023 (setq found (setq arts (get-text-property (point) 'articles)))
2024 ;; Found a match, update scores.
2026 (while (setq art (pop arts))
2027 (setcdr art (+ score (cdr art)))
2028 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
2030 (while (setq art (pop arts))
2031 (setcdr art (+ score (cdr art)))))
2033 ;; Update expiry date
2035 (setq entries (cdr entries))
2039 (setq entries (cdr entries)))
2040 ;; We have a match, so we update the date.
2041 ((and found gnus-update-score-entry-dates)
2042 (gnus-score-set 'touched '(t) alist)
2043 (setcar (nthcdr 2 kill) now)
2044 (setq entries (cdr entries)))
2045 ;; This entry has expired, so we remove it.
2046 ((and expire (< date expire))
2047 (gnus-score-set 'touched '(t) alist)
2048 (setcdr entries (cddr entries)))
2049 ;; No match; go to next entry.
2051 (setq entries (cdr entries))))))))))
2053 ;; Find fuzzy matches.
2055 ;; Simplify the entire buffer for easy matching.
2056 (gnus-simplify-buffer-fuzzy)
2057 (while (setq kill (cadaar fuzzies))
2058 (let* ((match (nth 0 kill))
2060 (score (or (nth 1 kill) gnus-score-interactive-default-score))
2062 (mt (aref (symbol-name type) 0))
2063 (case-fold-search (not (= mt ?F)))
2065 (goto-char (point-min))
2066 (while (and (not (eobp))
2067 (search-forward match nil t))
2068 (when (and (= (gnus-point-at-bol) (match-beginning 0))
2070 (setq found (setq arts (get-text-property (point) 'articles)))
2072 (while (setq art (pop arts))
2073 (setcdr art (+ score (cdr art)))
2075 (car-safe (rassq (cdar fuzzies) gnus-score-cache))
2078 ;; Found a match, update scores.
2079 (while (setq art (pop arts))
2080 (setcdr art (+ score (cdr art))))))
2082 ;; Update expiry date
2089 ;; Match, update date.
2090 ((and found gnus-update-score-entry-dates)
2091 (gnus-score-set 'touched '(t) (cdar fuzzies))
2092 (setcar (nthcdr 2 kill) now))
2093 ;; Old entry, remove.
2094 ((and expire (< date expire))
2095 (gnus-score-set 'touched '(t) (cdar fuzzies))
2096 (setcdr (caar fuzzies) (cddaar fuzzies)))))
2097 (setq fuzzies (cdr fuzzies)))))
2100 ;; Enter all words into the hashtb.
2101 (let ((hashtb (gnus-make-hashtable
2102 (* 10 (count-lines (point-min) (point-max))))))
2103 (gnus-enter-score-words-into-hashtb hashtb)
2104 (while (setq kill (cadaar words))
2105 (let* ((score (or (nth 1 kill) gnus-score-interactive-default-score))
2108 (when (setq arts (intern-soft (nth 0 kill) hashtb))
2109 (setq arts (symbol-value arts))
2112 (while (setq art (pop arts))
2113 (setcdr art (+ score (cdr art)))
2115 (car-safe (rassq (cdar words) gnus-score-cache))
2118 ;; Found a match, update scores.
2119 (while (setq art (pop arts))
2120 (setcdr art (+ score (cdr art))))))
2121 ;; Update expiry date
2128 ;; Match, update date.
2129 ((and found gnus-update-score-entry-dates)
2130 (gnus-score-set 'touched '(t) (cdar words))
2131 (setcar (nthcdr 2 kill) now))
2132 ;; Old entry, remove.
2133 ((and expire (< date expire))
2134 (gnus-score-set 'touched '(t) (cdar words))
2135 (setcdr (caar words) (cddaar words)))))
2136 (setq words (cdr words))))))
2139 (defun gnus-enter-score-words-into-hashtb (hashtb)
2140 ;; Find all the words in the buffer and enter them into
2142 (let ((syntab (syntax-table))
2144 (goto-char (point-min))
2147 (set-syntax-table gnus-adaptive-word-syntax-table)
2148 (while (re-search-forward "\\b\\w+\\b" nil t)
2151 (setq word (downcase (buffer-substring
2152 (match-beginning 0) (match-end 0))))
2156 (append (get-text-property (gnus-point-at-eol) 'articles) val)
2158 (set-syntax-table syntab))
2159 ;; Make all the ignorable words ignored.
2160 (let ((ignored (append gnus-ignored-adaptive-words
2161 (if gnus-adaptive-word-no-group-words
2162 (message-tokenize-header
2163 (gnus-group-real-name gnus-newsgroup-name)
2165 gnus-default-ignored-adaptive-words)))
2167 (gnus-sethash (pop ignored) nil hashtb)))))
2169 (defun gnus-score-string< (a1 a2)
2170 ;; Compare headers in articles A2 and A2.
2171 ;; The header index used is the free variable `gnus-score-index'.
2172 (string-lessp (aref (car a1) gnus-score-index)
2173 (aref (car a2) gnus-score-index)))
2175 (defun gnus-current-score-file-nondirectory (&optional score-file)
2176 (let ((score-file (or score-file gnus-current-score-file)))
2178 (gnus-short-group-name (file-name-nondirectory score-file))
2181 (defun gnus-score-adaptive ()
2182 "Create adaptive score rules for this newsgroup."
2183 (when gnus-newsgroup-adaptive
2184 ;; We change the score file to the adaptive score file.
2186 (set-buffer gnus-summary-buffer)
2187 (gnus-score-load-file
2188 (or gnus-newsgroup-adaptive-score-file
2189 (gnus-home-score-file gnus-newsgroup-name t)
2190 (gnus-score-file-name
2191 gnus-newsgroup-name gnus-adaptive-file-suffix))))
2192 ;; Perform ordinary line scoring.
2193 (when (or (not (listp gnus-newsgroup-adaptive))
2194 (memq 'line gnus-newsgroup-adaptive))
2196 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
2198 (date (current-time-string))
2199 (data gnus-newsgroup-data)
2200 elem headers match func)
2201 ;; First we transform the adaptive rule alist into something
2202 ;; that's faster to process.
2204 (setq elem (car malist))
2205 (when (symbolp (car elem))
2206 (setcar elem (symbol-value (car elem))))
2207 (setq elem (cdr elem))
2212 (concat "mail-header-"
2213 (if (eq (caar elem) 'followup)
2215 (downcase (symbol-name (caar elem))))))))
2217 (cons (if (eq (caar elem) 'followup)
2219 (symbol-name (caar elem)))
2224 (setq elem (cdr elem)))
2225 (setq malist (cdr malist)))
2226 ;; Then we score away.
2228 (setq elem (cdr (assq (gnus-data-mark (car data)) alist)))
2230 (gnus-data-pseudo-p (car data)))
2232 (when (setq headers (gnus-data-header (car data)))
2234 (setq match (funcall (caar elem) headers))
2235 (gnus-summary-score-entry
2236 (nth 1 (car elem)) match
2240 ((equal (nth 1 (car elem)) "date")
2243 ;; Whether we use substring or exact matches is
2245 (if (or (not gnus-score-exact-adapt-limit)
2246 (< (length match) gnus-score-exact-adapt-limit))
2248 (if (equal (nth 1 (car elem)) "subject")
2250 (nth 2 (car elem)) date nil t)
2251 (setq elem (cdr elem)))))
2252 (setq data (cdr data))))))
2254 ;; Perform adaptive word scoring.
2255 (when (and (listp gnus-newsgroup-adaptive)
2256 (memq 'word gnus-newsgroup-adaptive))
2258 (let* ((hashtb (gnus-make-hashtable 1000))
2259 (date (date-to-day (current-time-string)))
2260 (data gnus-newsgroup-data)
2261 (syntab (syntax-table))
2265 (set-syntax-table gnus-adaptive-word-syntax-table)
2266 ;; Go through all articles.
2267 (while (setq d (pop data))
2269 (not (gnus-data-pseudo-p d))
2273 gnus-adaptive-word-score-alist))))
2274 ;; This article has a mark that should lead to
2275 ;; adaptive word rules, so we insert the subject
2276 ;; and find all words in that string.
2277 (insert (mail-header-subject (gnus-data-header d)))
2278 (downcase-region (point-min) (point-max))
2279 (goto-char (point-min))
2280 (while (re-search-forward "\\b\\w+\\b" nil t)
2281 ;; Put the word and score into the hashtb.
2282 (setq val (gnus-gethash (setq word (match-string 0))
2284 (when (or (not gnus-adaptive-word-length-limit)
2286 gnus-adaptive-word-length-limit))
2287 (setq val (+ score (or val 0)))
2288 (if (and gnus-adaptive-word-minimum
2289 (< val gnus-adaptive-word-minimum))
2290 (setq val gnus-adaptive-word-minimum))
2291 (gnus-sethash word val hashtb)))
2293 (set-syntax-table syntab))
2294 ;; Make all the ignorable words ignored.
2295 (let ((ignored (append gnus-ignored-adaptive-words
2296 (if gnus-adaptive-word-no-group-words
2297 (message-tokenize-header
2298 (gnus-group-real-name
2299 gnus-newsgroup-name)
2301 gnus-default-ignored-adaptive-words)))
2303 (gnus-sethash (pop ignored) nil hashtb)))
2304 ;; Now we have all the words and scores, so we
2305 ;; add these rules to the ADAPT file.
2306 (set-buffer gnus-summary-buffer)
2309 (when (symbol-value word)
2310 (gnus-summary-score-entry
2311 "subject" (symbol-name word) 'w (symbol-value word)
2315 (defun gnus-score-edit-done ()
2316 (let ((bufnam (buffer-file-name (current-buffer)))
2317 (winconf gnus-prev-winconf))
2319 (set-window-configuration winconf))
2320 (gnus-score-remove-from-cache bufnam)
2321 (gnus-score-load-file bufnam)))
2323 (defun gnus-score-find-trace ()
2324 "Find all score rules that applies to the current article."
2326 (let ((old-scored gnus-newsgroup-scored))
2327 (let ((gnus-newsgroup-headers
2328 (list (gnus-summary-article-header)))
2329 (gnus-newsgroup-scored nil)
2332 (nnheader-set-temp-buffer "*Score Trace*"))
2333 (setq gnus-score-trace nil)
2334 (gnus-possibly-score-headers 'trace)
2335 (if (not (setq trace gnus-score-trace))
2337 1 "No score rules apply to the current article (default score %d)."
2338 gnus-summary-default-score)
2339 (set-buffer "*Score Trace*")
2340 (setq truncate-lines t)
2342 (insert (format "%S -> %s\n" (cdar trace)
2343 (or (caar trace) "(non-file rule)")))
2344 (setq trace (cdr trace)))
2345 (goto-char (point-min))
2346 (gnus-configure-windows 'score-trace)))
2347 (set-buffer gnus-summary-buffer)
2348 (setq gnus-newsgroup-scored old-scored)))
2350 (defun gnus-score-find-favourite-words ()
2351 "List words used in scoring."
2353 (let ((alists (gnus-score-load-files (gnus-all-score-files)))
2354 alist rule rules kill)
2355 ;; Go through all the score alists for this group
2356 ;; and find all `w' rules.
2357 (while (setq alist (pop alists))
2358 (while (setq rule (pop alist))
2359 (when (and (stringp (car rule))
2360 (equal "subject" (downcase (pop rule))))
2361 (while (setq kill (pop rule))
2362 (when (memq (nth 3 kill) '(w W word Word))
2363 (push (cons (or (nth 1 kill)
2364 gnus-score-interactive-default-score)
2367 (setq rules (sort rules (lambda (r1 r2)
2368 (string-lessp (cdr r1) (cdr r2)))))
2369 ;; Add up words that have appeared several times.
2372 (if (equal (cdar r) (cdadr r))
2374 (setcar (car r) (+ (caar r) (caadr r)))
2375 (setcdr r (cddr r)))
2377 ;; Insert the words.
2378 (nnheader-set-temp-buffer "*Score Words*")
2379 (if (not (setq rules (sort rules (lambda (r1 r2) (> (car r1) (car r2))))))
2380 (gnus-error 3 "No word score rules")
2382 (insert (format "%-5d: %s\n" (caar rules) (cdar rules)))
2384 (goto-char (point-min))
2385 (gnus-configure-windows 'score-words))))
2387 (defun gnus-summary-rescore ()
2388 "Redo the entire scoring process in the current summary."
2391 (setq gnus-score-cache nil)
2392 (setq gnus-newsgroup-scored nil)
2393 (gnus-possibly-score-headers)
2394 (gnus-score-update-all-lines))
2396 (defun gnus-score-flush-cache ()
2397 "Flush the cache of score files."
2400 (setq gnus-score-cache nil
2401 gnus-score-alist nil
2402 gnus-short-name-score-file-cache nil)
2403 (gnus-message 6 "The score cache is now flushed"))
2405 (gnus-add-shutdown 'gnus-score-close 'gnus)
2407 (defvar gnus-score-file-alist-cache nil)
2409 (defun gnus-score-close ()
2410 "Clear all internal score variables."
2411 (setq gnus-score-cache nil
2412 gnus-internal-global-score-files nil
2413 gnus-score-file-list nil
2414 gnus-score-file-alist-cache nil))
2416 ;; Summary score marking commands.
2418 (defun gnus-summary-raise-same-subject-and-select (score)
2419 "Raise articles which has the same subject with SCORE and select the next."
2421 (let ((subject (gnus-summary-article-subject)))
2422 (gnus-summary-raise-score score)
2423 (while (gnus-summary-find-subject subject)
2424 (gnus-summary-raise-score score))
2425 (gnus-summary-next-article t)))
2427 (defun gnus-summary-raise-same-subject (score)
2428 "Raise articles which has the same subject with SCORE."
2430 (let ((subject (gnus-summary-article-subject)))
2431 (gnus-summary-raise-score score)
2432 (while (gnus-summary-find-subject subject)
2433 (gnus-summary-raise-score score))
2434 (gnus-summary-next-subject 1 t)))
2436 (defun gnus-score-delta-default (level)
2437 (if level (prefix-numeric-value level)
2438 gnus-score-interactive-default-score))
2440 (defun gnus-summary-raise-thread (&optional score)
2441 "Raise the score of the articles in the current thread with SCORE."
2443 (setq score (gnus-score-delta-default score))
2446 (let ((articles (gnus-summary-articles-in-thread)))
2448 (gnus-summary-goto-subject (car articles))
2449 (gnus-summary-raise-score score)
2450 (setq articles (cdr articles))))
2452 (let ((gnus-summary-check-current t))
2453 (unless (zerop (gnus-summary-next-subject 1 t))
2455 (gnus-summary-recenter)
2456 (gnus-summary-position-point)
2457 (gnus-set-mode-line 'summary))
2459 (defun gnus-summary-lower-same-subject-and-select (score)
2460 "Raise articles which has the same subject with SCORE and select the next."
2462 (gnus-summary-raise-same-subject-and-select (- score)))
2464 (defun gnus-summary-lower-same-subject (score)
2465 "Raise articles which has the same subject with SCORE."
2467 (gnus-summary-raise-same-subject (- score)))
2469 (defun gnus-summary-lower-thread (&optional score)
2470 "Lower score of articles in the current thread with SCORE."
2472 (gnus-summary-raise-thread (- (gnus-score-delta-default score))))
2474 ;;; Finding score files.
2476 (defun gnus-score-score-files (group)
2477 "Return a list of all possible score files."
2478 ;; Search and set any global score files.
2479 (when gnus-global-score-files
2480 (unless gnus-internal-global-score-files
2481 (gnus-score-search-global-directories gnus-global-score-files)))
2482 ;; Fix the kill-file dir variable.
2483 (setq gnus-kill-files-directory
2484 (file-name-as-directory gnus-kill-files-directory))
2485 ;; If we can't read it, there are no score files.
2486 (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
2487 (setq gnus-score-file-list nil)
2488 (if (not (gnus-use-long-file-name 'not-score))
2489 ;; We do not use long file names, so we have to do some
2490 ;; directory traversing.
2491 (setq gnus-score-file-list
2493 (or gnus-short-name-score-file-cache
2495 (gnus-message 6 "Finding all score files...")
2496 (setq gnus-short-name-score-file-cache
2497 (gnus-score-score-files-1
2498 gnus-kill-files-directory))
2499 (gnus-message 6 "Finding all score files...done")))))
2500 ;; We want long file names.
2501 (when (or (not gnus-score-file-list)
2502 (not (car gnus-score-file-list))
2503 (gnus-file-newer-than gnus-kill-files-directory
2504 (car gnus-score-file-list)))
2505 (setq gnus-score-file-list
2506 (cons (nth 5 (file-attributes gnus-kill-files-directory))
2509 gnus-kill-files-directory t
2510 (gnus-score-file-regexp)))))))
2511 (cdr gnus-score-file-list)))
2513 (defun gnus-score-score-files-1 (dir)
2514 "Return all possible score files under DIR."
2515 (let ((files (list (expand-file-name dir)))
2516 (regexp (gnus-score-file-regexp))
2517 (case-fold-search nil)
2519 (while (setq file (pop files))
2521 ;; Ignore files that start with a dot.
2522 ((string-match "^\\." (file-name-nondirectory file))
2524 ;; Add subtrees of directory to also be searched.
2525 ((and (file-directory-p file)
2526 (not (member (file-truename file) seen)))
2527 (push (file-truename file) seen)
2528 (setq files (nconc (directory-files file t nil t) files)))
2529 ;; Add files to the list of score files.
2530 ((string-match regexp file)
2533 ;; Return a dummy value.
2534 (list (expand-file-name "this.file.does.not.exist.SCORE"
2535 gnus-kill-files-directory)))))
2537 (defun gnus-score-file-regexp ()
2538 "Return a regexp that match all score files."
2539 (concat "\\(" (regexp-quote gnus-score-file-suffix )
2540 "\\|" (regexp-quote gnus-adaptive-file-suffix) "\\)\\'"))
2542 (defun gnus-score-find-bnews (group)
2543 "Return a list of score files for GROUP.
2544 The score files are those files in the ~/News/ directory which matches
2545 GROUP using BNews sys file syntax."
2546 (let* ((sfiles (append (gnus-score-score-files group)
2547 gnus-internal-global-score-files))
2548 (kill-dir (file-name-as-directory
2549 (expand-file-name gnus-kill-files-directory)))
2550 (klen (length kill-dir))
2551 (score-regexp (gnus-score-file-regexp))
2552 (trans (cdr (assq ?: nnheader-file-name-translation-alist)))
2553 (group-trans (nnheader-translate-file-chars group t))
2554 ofiles not-match regexp)
2556 (set-buffer (gnus-get-buffer-create "*gnus score files*"))
2557 (buffer-disable-undo)
2558 ;; Go through all score file names and create regexp with them
2562 (insert (car sfiles))
2563 (goto-char (point-min))
2564 ;; First remove the suffix itself.
2565 (when (re-search-forward (concat "." score-regexp) nil t)
2566 (replace-match "" t t)
2567 (goto-char (point-min))
2568 (if (looking-at (regexp-quote kill-dir))
2569 ;; If the file name was just "SCORE", `klen' is one character
2571 (delete-char (min (1- (point-max)) klen))
2572 (goto-char (point-max))
2573 (if (re-search-backward gnus-directory-sep-char-regexp nil t)
2574 (delete-region (1+ (point)) (point-min))
2575 (gnus-message 1 "Can't find directory separator in %s"
2577 ;; If short file names were used, we have to translate slashes.
2578 (goto-char (point-min))
2579 (let ((regexp (concat
2580 "[/:" (if trans (char-to-string trans)) "]")))
2581 (while (re-search-forward regexp nil t)
2582 (replace-match "." t t)))
2583 ;; Kludge to get rid of "nntp+" problems.
2584 (goto-char (point-min))
2585 (when (looking-at "nn[a-z]+\\+")
2586 (search-forward "+")
2590 ;; Kludge to deal with "++".
2591 (while (search-forward "+" nil t)
2592 (replace-match "\\+" t t))
2593 ;; Translate "all" to ".*".
2594 (goto-char (point-min))
2595 (while (search-forward "all" nil t)
2596 (replace-match ".*" t t))
2597 (goto-char (point-min))
2598 ;; Deal with "not."s.
2599 (if (looking-at "not.")
2603 (concat "^" (buffer-substring 5 (point-max)) "$")))
2604 (setq regexp (concat "^" (buffer-substring 1 (point-max)) "$"))
2605 (setq not-match nil))
2606 ;; Finally - if this resulting regexp matches the group name,
2607 ;; we add this score file to the list of score files
2608 ;; applicable to this group.
2609 (when (or (and not-match
2611 (not (string-match regexp group-trans))))
2612 (and (not not-match)
2613 (ignore-errors (string-match regexp group-trans))))
2614 (push (car sfiles) ofiles)))
2615 (setq sfiles (cdr sfiles)))
2616 (kill-buffer (current-buffer))
2617 ;; Slight kludge here - the last score file returned should be
2618 ;; the local score file, whether it exists or not. This is so
2619 ;; that any score commands the user enters will go to the right
2620 ;; file, and not end up in some global score file.
2621 (let ((localscore (gnus-score-file-name group)))
2622 (setq ofiles (cons localscore (delete localscore ofiles))))
2623 (gnus-sort-score-files (nreverse ofiles)))))
2625 (defun gnus-score-find-single (group)
2626 "Return list containing the score file for GROUP."
2627 (list (or gnus-newsgroup-adaptive-score-file
2628 (gnus-score-file-name group gnus-adaptive-file-suffix))
2629 (gnus-score-file-name group)))
2631 (defun gnus-score-find-hierarchical (group)
2632 "Return list of score files for GROUP.
2633 This includes the score file for the group and all its parents."
2634 (let* ((prefix (gnus-group-real-prefix group))
2636 (group (gnus-group-real-name group))
2638 (while (string-match "\\." group (1+ start))
2639 (setq start (match-beginning 0))
2640 (push (substring group 0 start) all))
2644 (mapcar (lambda (group)
2645 (gnus-score-file-name group gnus-adaptive-file-suffix))
2646 (setq all (nreverse all)))
2647 (mapcar 'gnus-score-file-name all)))
2648 (if (equal prefix "")
2652 (nnheader-translate-file-chars
2653 (concat (file-name-directory file) prefix
2654 (file-name-nondirectory file))))
2657 (defun gnus-score-file-rank (file)
2658 "Return a number that says how specific score FILE is.
2659 Destroys the current buffer."
2660 (if (member file gnus-internal-global-score-files)
2663 (concat "^" (regexp-quote
2665 (file-name-as-directory gnus-kill-files-directory))))
2667 (setq file (substring file (match-end 0))))
2669 (goto-char (point-min))
2672 (while (re-search-forward "[./]" nil t)
2673 (push (buffer-substring beg (1- (point)))
2676 (setq elems (delete "all" elems))
2679 (defun gnus-sort-score-files (files)
2680 "Sort FILES so that the most general files come first."
2685 (cons (inline (gnus-score-file-rank file)) file))
2688 (lambda (f) (cdr f))
2689 (sort alist 'car-less-than-car)))))
2691 (defun gnus-score-find-alist (group)
2692 "Return list of score files for GROUP.
2693 The list is determined from the variable `gnus-score-file-alist'."
2694 (let ((alist gnus-score-file-multiple-match-alist)
2696 ;; if this group has been seen before, return the cached entry
2697 (if (setq score-files (assoc group gnus-score-file-alist-cache))
2698 (cdr score-files) ;ensures caching groups with no matches
2699 ;; handle the multiple match alist
2701 (when (string-match (caar alist) group)
2703 (nconc score-files (copy-sequence (cdar alist)))))
2704 (setq alist (cdr alist)))
2705 (setq alist gnus-score-file-single-match-alist)
2706 ;; handle the single match alist
2708 (when (string-match (caar alist) group)
2709 ;; progn used just in case ("regexp") has no files
2710 ;; and score-files is still nil. -sj
2711 ;; this can be construed as a "stop searching here" feature :>
2712 ;; and used to simplify regexps in the single-alist
2714 (nconc score-files (copy-sequence (cdar alist))))
2716 (setq alist (cdr alist)))
2717 ;; cache the score files
2718 (push (cons group score-files) gnus-score-file-alist-cache)
2721 (defun gnus-all-score-files (&optional group)
2722 "Return a list of all score files for the current group."
2723 (let ((funcs gnus-score-find-score-files-function)
2724 (group (or group gnus-newsgroup-name))
2727 ;; Make sure funcs is a list.
2730 (setq funcs (list funcs)))
2731 (when gnus-score-use-all-scores
2732 ;; Get the initial score files for this group.
2734 (setq score-files (nreverse (gnus-score-find-alist group))))
2735 ;; Add any home adapt files.
2736 (let ((home (gnus-home-score-file group t)))
2738 (push home score-files)
2739 (setq gnus-newsgroup-adaptive-score-file home)))
2740 ;; Check whether there is a `adapt-file' group parameter.
2741 (let ((param-file (gnus-group-find-parameter group 'adapt-file)))
2743 (push param-file score-files)
2744 (setq gnus-newsgroup-adaptive-score-file param-file))))
2745 ;; Go through all the functions for finding score files (or actual
2746 ;; scores) and add them to a list.
2748 (when (gnus-functionp (car funcs))
2751 (nreverse (funcall (car funcs) group)))))
2752 (setq funcs (cdr funcs)))
2753 (when gnus-score-use-all-scores
2754 ;; Add any home score files.
2755 (let ((home (gnus-home-score-file group)))
2757 (push home score-files)))
2758 ;; Check whether there is a `score-file' group parameter.
2759 (let ((param-file (gnus-group-find-parameter group 'score-file)))
2761 (push param-file score-files))))
2762 ;; Expand all files names.
2763 (let ((files score-files))
2765 (when (stringp (car files))
2766 (setcar files (expand-file-name
2767 (car files) gnus-kill-files-directory)))
2769 (setq score-files (nreverse score-files))
2770 ;; Remove any duplicate score files.
2771 (while (and score-files
2772 (member (car score-files) (cdr score-files)))
2774 (let ((files score-files))
2776 (if (member (cadr files) (cddr files))
2777 (setcdr files (cddr files))
2779 ;; Do the scoring if there are any score files for this group.
2782 (defun gnus-possibly-score-headers (&optional trace)
2783 "Do scoring if scoring is required."
2784 (let ((score-files (gnus-all-score-files)))
2786 (gnus-score-headers score-files trace))))
2788 (defun gnus-score-file-name (newsgroup &optional suffix)
2789 "Return the name of a score file for NEWSGROUP."
2790 (let ((suffix (or suffix gnus-score-file-suffix)))
2791 (nnheader-translate-file-chars
2793 ((or (null newsgroup)
2794 (string-equal newsgroup ""))
2795 ;; The global score file is placed at top of the directory.
2796 (expand-file-name suffix gnus-kill-files-directory))
2797 ((gnus-use-long-file-name 'not-score)
2798 ;; Append ".SCORE" to newsgroup name.
2799 (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
2801 gnus-kill-files-directory))
2803 ;; Place "SCORE" under the hierarchical directory.
2804 (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
2806 gnus-kill-files-directory))))))
2808 (defun gnus-score-search-global-directories (files)
2809 "Scan all global score directories for score files."
2810 ;; Set the variable `gnus-internal-global-score-files' to all
2811 ;; available global score files.
2812 (interactive (list gnus-global-score-files))
2815 ;; #### /$ Unix-specific?
2816 (if (file-directory-p (car files))
2817 (setq out (nconc (directory-files
2819 (concat (gnus-score-file-regexp) "$"))))
2820 (push (car files) out))
2821 (setq files (cdr files)))
2822 (setq gnus-internal-global-score-files out)))
2824 (defun gnus-score-default-fold-toggle ()
2825 "Toggle folding for new score file entries."
2827 (setq gnus-score-default-fold (not gnus-score-default-fold))
2828 (if gnus-score-default-fold
2829 (gnus-message 1 "New score file entries will be case insensitive.")
2830 (gnus-message 1 "New score file entries will be case sensitive.")))
2832 ;;; Home score file.
2834 (defun gnus-home-score-file (group &optional adapt)
2835 "Return the home score file for GROUP.
2836 If ADAPT, return the home adaptive file instead."
2837 (let ((list (if adapt gnus-home-adapt-file gnus-home-score-file))
2839 ;; Make sure we have a list.
2840 (unless (listp list)
2841 (setq list (list list)))
2842 ;; Go through the list and look for matches.
2843 (while (and (not found)
2844 (setq elem (pop list)))
2851 ((gnus-functionp elem)
2852 (funcall elem group))
2853 ;; Regexp-file cons.
2855 (when (string-match (gnus-globalify-regexp (car elem)) group)
2856 (replace-match (cadr elem) t nil group))))))
2858 (setq found (nnheader-translate-file-chars found))
2859 (if (file-name-absolute-p found)
2861 (nnheader-concat gnus-kill-files-directory found)))))
2863 (defun gnus-hierarchial-home-score-file (group)
2864 "Return the score file of the top-level hierarchy of GROUP."
2865 (if (string-match "^[^.]+\\." group)
2866 (concat (match-string 0 group) gnus-score-file-suffix)
2867 ;; Group name without any dots.
2868 (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
2869 gnus-score-file-suffix)))
2871 (defun gnus-hierarchial-home-adapt-file (group)
2872 "Return the adapt file of the top-level hierarchy of GROUP."
2873 (if (string-match "^[^.]+\\." group)
2874 (concat (match-string 0 group) gnus-adaptive-file-suffix)
2875 ;; Group name without any dots.
2876 (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
2877 gnus-adaptive-file-suffix)))
2879 (defun gnus-current-home-score-file (group)
2880 "Return the \"current\" regular score file."
2881 (car (nreverse (gnus-score-find-alist group))))
2887 (defun gnus-decay-score (score)
2888 "Decay SCORE according to `gnus-score-decay-constant' and `gnus-score-decay-scale'."
2891 (* (if (< score 0) -1 1)
2893 (max gnus-score-decay-constant
2895 gnus-score-decay-scale)))))))
2897 (defun gnus-decay-scores (alist day)
2898 "Decay non-permanent scores in ALIST."
2899 (let ((times (- (time-to-days (current-time)) day))
2900 kill entry updated score n)
2901 (unless (zerop times) ;Done decays today already?
2902 (while (setq entry (pop alist))
2903 (when (stringp (car entry))
2904 (setq entry (cdr entry))
2905 (while (setq kill (pop entry))
2908 (setq score (or (nth 1 kill)
2909 gnus-score-interactive-default-score)
2911 (while (natnump (decf n))
2912 (setq score (funcall gnus-decay-score-function score)))
2913 (setcdr kill (cons score
2914 (cdr (cdr kill)))))))))
2915 ;; Return whether this score file needs to be saved. By Je-haysuss!
2918 (defun gnus-score-regexp-bad-p (regexp)
2919 "Test whether REGEXP is safe for Gnus scoring.
2920 A regexp is unsafe if it matches newline or a buffer boundary.
2922 If the regexp is good, return nil. If the regexp is bad, return a
2923 cons cell (SYM . STRING), where the symbol SYM is `new' or `bad'.
2924 In the `new' case, the string is a safe replacement for REGEXP.
2925 In the `bad' case, the string is a unsafe subexpression of REGEXP,
2926 and we do not have a simple replacement to suggest.
2928 See Info node `(gnus)Scoring Tips' for examples of good regular expressions."
2929 (let (case-fold-search)
2931 ;; First, try a relatively fast necessary condition.
2932 ;; Notice ranges (like [^:] or [\t-\r]), \s>, \Sw, \W, \', \`:
2933 (string-match "\n\\|\\\\[SsW`']\\|\\[\\^\\|[\0-\n]-" regexp)
2934 ;; Now break the regexp into tokens, and check each:
2935 (let ((tail regexp) ; remaining regexp to check
2937 bad ; nil, or bad subexpression
2938 new ; nil, or replacement regexp so far
2939 end) ; length of current token
2940 (while (and (not bad)
2942 "\\`\\(\\\\[sS]?.\\|\\[\\^?]?[^]]*]\\|[^\\]\\)"
2944 (setq end (match-end 0)
2945 tok (substring tail 0 end)
2946 tail (substring tail end))
2947 (if;; Is token `bad' (matching newline or buffer ends)?
2948 (or (member tok '("\n" "\\W" "\\`" "\\'"))
2949 ;; This next handles "[...]", "\\s.", and "\\S.":
2950 (and (> end 2) (string-match tok "\n")))
2952 ;; Try to suggest a replacement for tok ...
2953 (cond ((string-equal tok "\\`") "^") ; or "\\(^\\)"
2954 ((string-equal tok "\\'") "$") ; or "\\($\\)"
2955 ((string-match "\\[\\^" tok) ; very common
2956 (concat (substring tok 0 -1) "\n]")))))
2961 ;; good prefix so far:
2962 (substring regexp 0 (- (+ (length tail) end))))
2964 ;; No replacement idea, so give up:
2966 ;; tok is good, may need to extend new
2967 (and new (setq new (concat new tok)))))
2968 ;; Now return a value:
2970 (bad (cons 'bad bad))
2971 (new (cons 'new new))
2974 (provide 'gnus-score)
2976 ;;; gnus-score.el ends here