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>
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))
38 (defcustom gnus-global-score-files nil
39 "List of global score files and directories.
40 Set this variable if you want to use people's score files. One entry
41 for each score file or each score file directory. Gnus will decide
42 by itself what score files are applicable to which group.
44 Say you want to use the single score file
45 \"/ftp.gnus.org@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
46 score files in the \"/ftp.some-where:/pub/score\" directory.
48 (setq gnus-global-score-files
49 '(\"/ftp.gnus.org:/pub/larsi/ding/score/soc.motss.SCORE\"
50 \"/ftp.some-where:/pub/score\"))"
51 :group 'gnus-score-files
54 (defcustom gnus-score-file-single-match-alist nil
55 "Alist mapping regexps to lists of score files.
56 Each element of this alist should be of the form
57 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
59 If the name of a group is matched by REGEXP, the corresponding scorefiles
60 will be used for that group.
61 The first match found is used, subsequent matching entries are ignored (to
62 use multiple matches, see gnus-score-file-multiple-match-alist).
64 These score files are loaded in addition to any files returned by
65 gnus-score-find-score-files-function (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
90 (defcustom gnus-adaptive-file-suffix "ADAPT"
91 "Suffix of the adaptive score files."
92 :group 'gnus-score-files
93 :group 'gnus-score-adapt
96 (defcustom gnus-score-find-score-files-function 'gnus-score-find-bnews
97 "Function used to find score files.
98 The function will be called with the group name as the argument, and
99 should return a list of score files to apply to that group. The score
100 files do not actually have to exist.
102 Predefined values are:
104 gnus-score-find-single: Only apply the group's own score file.
105 gnus-score-find-hierarchical: Also apply score files from parent groups.
106 gnus-score-find-bnews: Apply score files whose names matches.
108 See the documentation to these functions for more information.
110 This variable can also be a list of functions to be called. Each
111 function is given the group name as argument and should either return
112 a list of score files, or a list of score alists.
114 If functions other than these pre-defined functions are used,
115 the `a' symbolic prefix to the score commands will always use
117 :group 'gnus-score-files
118 :type '(radio (function-item gnus-score-find-single)
119 (function-item gnus-score-find-hierarchical)
120 (function-item gnus-score-find-bnews)
121 (repeat :tag "List of functions"
122 (choice (function :tag "Other" :value 'ignore)
123 (function-item gnus-score-find-single)
124 (function-item gnus-score-find-hierarchical)
125 (function-item gnus-score-find-bnews)))
126 (function :tag "Other" :value 'ignore)))
128 (defcustom gnus-score-interactive-default-score 1000
129 "*Scoring commands will raise/lower the score with this number as the default."
130 :group 'gnus-score-default
133 (defcustom gnus-score-expiry-days 7
134 "*Number of days before unused score file entries are expired.
135 If this variable is nil, no score file entries will be expired."
136 :group 'gnus-score-expire
137 :type '(choice (const :tag "never" nil)
140 (defcustom gnus-update-score-entry-dates t
141 "*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
147 (defcustom gnus-decay-scores nil
148 "*If non-nil, decay non-permanent scores."
149 :group 'gnus-score-decay
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
164 (defcustom gnus-score-decay-scale .05
165 "*Decay all \"big\" scores with this factor."
166 :group 'gnus-score-decay
169 (defcustom gnus-home-score-file nil
170 "Variable to control where interactive score entries are to go.
174 This file file will be used as the home score file.
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
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
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
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)
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"
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"
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
279 (defcustom gnus-score-mimic-keymap nil
280 "*Have the score entry functions pretend that they are a keymap."
281 :group 'gnus-score-default
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
301 (defcustom gnus-score-default-header nil
302 "Default header when entering new scores.
304 Should be one of the following symbols.
313 e: `extra' (non-standard overview)
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.
346 >: greater than 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
368 (defcustom gnus-score-default-duration nil
369 "Default duration of effect when entering new scores.
371 Should be one of the following symbols.
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
389 (defcustom gnus-score-thread-simplify nil
390 "If non-nil, subjects will simplified as in threading."
391 :group 'gnus-score-various
396 ;; Internal variables.
398 (defvar gnus-score-use-all-scores t
399 "If nil, only `gnus-score-find-score-files-function' is used.")
401 (defvar gnus-adaptive-word-syntax-table
402 (let ((table (copy-syntax-table (standard-syntax-table)))
403 (numbers '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)))
405 (modify-syntax-entry (pop numbers) " " table))
406 (modify-syntax-entry ?' "w" table)
408 "Syntax table used when doing adaptive word scoring.")
410 (defvar gnus-scores-exclude-files nil)
411 (defvar gnus-internal-global-score-files nil)
412 (defvar gnus-score-file-list nil)
414 (defvar gnus-short-name-score-file-cache nil)
416 (defvar gnus-score-help-winconf nil)
417 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
418 (defvar gnus-adaptive-word-score-alist gnus-default-adaptive-word-score-alist)
419 (defvar gnus-score-trace nil)
420 (defvar gnus-score-edit-buffer nil)
422 (defvar gnus-score-alist nil
423 "Alist containing score information.
424 The keys can be symbols or strings. The following symbols are defined.
426 touched: If this alist has been modified.
427 mark: Automatically mark articles below this.
428 expunge: Automatically expunge articles below this.
429 files: List of other score files to load when loading this one.
430 eval: Sexp to be evaluated when the score file is loaded.
432 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
433 where HEADER is the header being scored, MATCH is the string we are
434 looking for, TYPE is a flag indicating whether it should use regexp or
435 substring matching, SCORE is the score to add and DATE is the date
436 of the last successful match.")
438 (defvar gnus-score-cache nil)
439 (defvar gnus-scores-articles nil)
440 (defvar gnus-score-index nil)
443 (defconst gnus-header-index
444 ;; Name to index alist.
445 '(("number" 0 gnus-score-integer)
446 ("subject" 1 gnus-score-string)
447 ("from" 2 gnus-score-string)
448 ("date" 3 gnus-score-date)
449 ("message-id" 4 gnus-score-string)
450 ("references" 5 gnus-score-string)
451 ("chars" 6 gnus-score-integer)
452 ("lines" 7 gnus-score-integer)
453 ("xref" 8 gnus-score-string)
454 ("extra" 9 gnus-score-string)
455 ("head" -1 gnus-score-body)
456 ("body" -1 gnus-score-body)
457 ("all" -1 gnus-score-body)
458 ("followup" 2 gnus-score-followup)
459 ("thread" 5 gnus-score-thread)))
461 ;;; Summary mode score maps.
463 (gnus-define-keys (gnus-summary-score-map "V" gnus-summary-mode-map)
464 "s" gnus-summary-set-score
465 "S" gnus-summary-current-score
466 "c" gnus-score-change-score-file
467 "C" gnus-score-customize
468 "m" gnus-score-set-mark-below
469 "x" gnus-score-set-expunge-below
470 "R" gnus-summary-rescore
471 "e" gnus-score-edit-current-scores
472 "f" gnus-score-edit-file
473 "F" gnus-score-flush-cache
474 "t" gnus-score-find-trace
475 "w" gnus-score-find-favourite-words)
477 ;; Summary score file commands
479 ;; Much modification of the kill (ahem, score) code and lots of the
480 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
482 (defun gnus-summary-lower-score (&optional score symp)
483 "Make a score entry based on the current article.
484 The user will be prompted for header to score on, match type,
485 permanence, and the string to be used. The numerical prefix will be
487 (interactive (gnus-interactive "P\ny"))
488 (gnus-summary-increase-score (- (gnus-score-delta-default score)) symp))
490 (defun gnus-score-kill-help-buffer ()
491 (when (get-buffer "*Score Help*")
492 (kill-buffer "*Score Help*")
493 (when gnus-score-help-winconf
494 (set-window-configuration gnus-score-help-winconf))))
496 (defun gnus-summary-increase-score (&optional score symp)
497 "Make a score entry based on the current article.
498 The user will be prompted for header to score on, match type,
499 permanence, and the string to be used. The numerical prefix will be
501 (interactive (gnus-interactive "P\ny"))
502 (let* ((nscore (gnus-score-delta-default score))
503 (prefix (if (< nscore 0) ?L ?I))
504 (increase (> nscore 0))
506 '((?a "from" nil nil string)
507 (?s "subject" nil nil string)
508 (?b "body" "" nil body-string)
509 (?h "head" "" nil body-string)
510 (?i "message-id" nil nil string)
511 (?r "references" "message-id" nil string)
512 (?x "xref" nil nil string)
513 (?e "extra" nil nil string)
514 (?l "lines" nil nil number)
515 (?d "date" nil nil date)
516 (?f "followup" nil nil string)
517 (?t "thread" "message-id" nil string)))
519 '((?s s "substring" string)
520 (?e e "exact string" string)
521 (?f f "fuzzy string" string)
522 (?r r "regexp string" string)
523 (?z s "substring" body-string)
524 (?p r "regexp string" body-string)
525 (?b before "before date" date)
526 (?a after "after date" date)
527 (?n at "this date" date)
528 (?< < "less than number" number)
529 (?> > "greater than number" number)
530 (?= = "equal to number" number)))
531 (current-score-file gnus-current-score-file)
533 (list (list ?t (current-time-string) "temporary")
534 '(?p perm "permanent") '(?i now "immediate")))
535 (mimic gnus-score-mimic-keymap)
536 (hchar (and gnus-score-default-header
537 (aref (symbol-name gnus-score-default-header) 0)))
538 (tchar (and gnus-score-default-type
539 (aref (symbol-name gnus-score-default-type) 0)))
540 (pchar (and gnus-score-default-duration
541 (aref (symbol-name gnus-score-default-duration) 0)))
542 entry temporary type match extra)
547 ;; First we read the header to score.
552 (message "%c-" prefix))
553 (message "%s header (%s?): " (if increase "Increase" "Lower")
554 (mapconcat (lambda (s) (char-to-string (car s)))
556 (setq hchar (read-char))
557 (when (or (= hchar ??) (= hchar ?\C-h))
559 (gnus-score-insert-help "Match on header" char-to-header 1)))
561 (gnus-score-kill-help-buffer)
562 (unless (setq entry (assq (downcase hchar) char-to-header))
563 (if mimic (error "%c %c" prefix hchar)
564 (error "Invalid header type")))
566 (when (/= (downcase hchar) hchar)
567 ;; This was a majuscule, so we end reading and set the defaults.
568 (if mimic (message "%c %c" prefix hchar) (message ""))
569 (setq tchar (or tchar ?s)
570 pchar (or pchar ?t)))
575 (if (eq (nth 4 entry)
579 ;; We continue reading - the type.
583 (sit-for 1) (message "%c %c-" prefix hchar))
584 (message "%s header '%s' with match type (%s?): "
585 (if increase "Increase" "Lower")
587 (mapconcat (lambda (s) (char-to-string (car s)))
589 (setq tchar (read-char))
590 (when (or (= tchar ??) (= tchar ?\C-h))
592 (gnus-score-insert-help "Match type" legal-types 2)))
594 (gnus-score-kill-help-buffer)
595 (unless (setq type (nth 1 (assq (downcase tchar) legal-types)))
596 (if mimic (error "%c %c" prefix hchar)
597 (error "Invalid match type"))))
599 (when (/= (downcase tchar) tchar)
600 ;; It was a majuscule, so we end reading and use the default.
601 (if mimic (message "%c %c %c" prefix hchar tchar)
603 (setq pchar (or pchar ?t)))
605 ;; We continue reading.
609 (sit-for 1) (message "%c %c %c-" prefix hchar tchar))
610 (message "%s permanence (%s?): " (if increase "Increase" "Lower")
611 (mapconcat (lambda (s) (char-to-string (car s)))
613 (setq pchar (read-char))
614 (when (or (= pchar ??) (= pchar ?\C-h))
616 (gnus-score-insert-help "Match permanence" char-to-perm 2)))
618 (gnus-score-kill-help-buffer)
619 (if mimic (message "%c %c %c" prefix hchar tchar pchar)
621 (unless (setq temporary (cadr (assq pchar char-to-perm)))
622 ;; Deal with der(r)ided superannuated paradigms.
623 (when (and (eq (1+ prefix) 77)
624 (eq (+ hchar 12) 109)
626 (eq (- pchar 4) 111))
629 (error "%c %c %c %c" prefix hchar tchar pchar)
630 (error "Invalid match duration"))))
631 ;; Always kill the score help buffer.
632 (gnus-score-kill-help-buffer))
634 ;; If scoring an extra (non-standard overview) header,
635 ;; we must find out which header is in question.
637 (and gnus-extra-headers
638 (equal (nth 1 entry) "extra")
639 (intern ; need symbol
640 (gnus-completing-read
641 (symbol-name (car gnus-extra-headers)) ; default response
642 "Score extra header:" ; prompt
643 (mapcar (lambda (x) ; completion list
644 (cons (symbol-name x) x))
646 nil ; no completion limit
647 t)))) ; require match
648 ;; extra is now nil or a symbol.
650 ;; We have all the data, so we enter this score.
651 (setq match (if (string= (nth 2 entry) "") ""
652 (gnus-summary-header (or (nth 2 entry) (nth 1 entry))
655 ;; Modify the match, perhaps.
657 ((equal (nth 1 entry) "xref")
658 (when (string-match "^Xref: *" match)
659 (setq match (substring match (match-end 0))))
660 (when (string-match "^[^:]* +" match)
661 (setq match (substring match (match-end 0))))))
663 (when (memq type '(r R regexp Regexp))
664 (setq match (regexp-quote match)))
666 ;; Change score file to the "all.SCORE" file.
669 (set-buffer gnus-summary-buffer)
670 (gnus-score-load-file
671 ;; This is a kludge; yes...
673 ((eq gnus-score-find-score-files-function
674 'gnus-score-find-hierarchical)
675 (gnus-score-file-name ""))
676 ((eq gnus-score-find-score-files-function 'gnus-score-find-single)
679 (gnus-score-file-name "all"))))))
681 (gnus-summary-score-entry
682 (nth 1 entry) ; Header
685 (if (eq score 's) nil score) ; Score
686 (if (eq temporary 'perm) ; Temp
689 (not (nth 3 entry)) ; Prompt
691 extra) ; non-standard overview.
694 ;; We change the score file back to the previous one.
696 (set-buffer gnus-summary-buffer)
697 (gnus-score-load-file current-score-file)))))
699 (defun gnus-score-insert-help (string alist idx)
700 (setq gnus-score-help-winconf (current-window-configuration))
702 (set-buffer (gnus-get-buffer-create "*Score Help*"))
703 (buffer-disable-undo)
704 (delete-windows-on (current-buffer))
706 (insert string ":\n\n")
711 ;; find the longest string to display
713 (setq n (length (nth idx (car list))))
716 (setq list (cdr list)))
717 (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
718 (setq n (/ (1- (window-width)) max)) ; items per line
719 (setq width (/ (1- (window-width)) n)) ; width of each item
720 ;; insert `n' items, each in a field of width `width'
725 (delete-char -1) ; the `\n' takes a char
727 (setq pad (- width 3))
728 (setq format (concat "%c: %-" (int-to-string pad) "s"))
729 (insert (format format (caar alist) (nth idx (car alist))))
730 (setq alist (cdr alist))
732 ;; display ourselves in a small window at the bottom
733 (gnus-appt-select-lowest-window)
735 (pop-to-buffer "*Score Help*")
736 (let ((window-min-height 1))
737 (shrink-window-if-larger-than-buffer))
738 (select-window (get-buffer-window gnus-summary-buffer t))))
740 (defun gnus-summary-header (header &optional no-err extra)
741 ;; Return HEADER for current articles, or error.
742 (let ((article (gnus-summary-article-number))
745 (if (and (setq headers (gnus-summary-article-header article))
747 (if extra ; `header' must be "extra"
748 (or (cdr (assq extra (mail-header-extra headers))) "")
749 (aref headers (nth 1 (assoc header gnus-header-index))))
752 (error "Pseudo-articles can't be scored")))
754 (error "No article on current line")
757 (defun gnus-newsgroup-score-alist ()
759 (let ((param-file (gnus-group-find-parameter
760 gnus-newsgroup-name 'score-file)))
762 (gnus-score-load param-file)))
764 (gnus-score-file-name gnus-newsgroup-name)))
767 (defsubst gnus-score-get (symbol &optional alist)
768 ;; Get SYMBOL's definition in ALIST.
772 (gnus-newsgroup-score-alist)))))
774 (defun gnus-summary-score-entry (header match type score date
775 &optional prompt silent extra)
776 "Enter score file entry.
777 HEADER is the header being scored.
778 MATCH is the string we are looking for.
779 TYPE is the match type: substring, regexp, exact, fuzzy.
780 SCORE is the score to add.
781 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
782 If optional argument `PROMPT' is non-nil, allow user to edit match.
783 If optional argument `SILENT' is nil, show effect of score entry.
784 If optional argument `EXTRA' is non-nil, it's a non-standard overview header."
785 ;; Regexp is the default type.
788 ;; Simplify matches...
789 (cond ((or (eq type 'r) (eq type 's) (eq type nil))
790 (setq match (if match (gnus-simplify-subject-re match) "")))
792 (setq match (gnus-simplify-subject-fuzzy match))))
793 (let ((score (gnus-score-delta-default score))
794 (header (downcase header))
796 (set-text-properties 0 (length header) nil header)
798 (setq match (read-string
799 (format "Match %s on %s, %s: "
800 (cond ((eq date 'now)
806 (if (< score 0) "lower" "raise"))
808 (int-to-string match)
811 (set-text-properties 0 (length match) nil match)
813 ;; If this is an integer comparison, we transform from string to int.
814 (when (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
815 (setq match (string-to-int match)))
817 (unless (eq date 'now)
818 ;; Add the score entry to the score file.
819 (when (= score gnus-score-interactive-default-score)
821 (let ((old (gnus-score-get header))
827 (and date (if (numberp date) date
829 type (symbol-name extra)))
832 (and date (if (numberp date) date
835 (date (list match score (date-to-day date)))
836 (score (list match score))
838 ;; We see whether we can collapse some score entries.
839 ;; This isn't quite correct, because there may be more elements
840 ;; later on with the same key that have matching elems... Hm.
842 (setq elem (assoc match old))
843 (eq (nth 3 elem) (nth 3 new))
844 (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
845 (and (not (nth 2 elem)) (not (nth 2 new)))))
846 ;; Yup, we just add this new score to the old elem.
847 (setcar (cdr elem) (+ (or (nth 1 elem)
848 gnus-score-interactive-default-score)
850 gnus-score-interactive-default-score)))
851 ;; Nope, we have to add a new elem.
852 (gnus-score-set header (if old (cons new old) (list new)) nil t))
853 (gnus-score-set 'touched '(t))))
855 ;; Score the current buffer.
857 (if (and (>= (nth 1 (assoc header gnus-header-index)) 0)
858 (eq (nth 2 (assoc header gnus-header-index))
860 (gnus-summary-score-effect header match type score extra)
861 (gnus-summary-rescore)))
863 ;; Return the new scoring rule.
866 (defun gnus-summary-score-effect (header match type score extra)
867 "Simulate the effect of a score file entry.
868 HEADER is the header being scored.
869 MATCH is the string we are looking for.
870 TYPE is the score type.
871 SCORE is the score to add.
872 EXTRA is the possible non-standard header."
873 (interactive (list (completing-read "Header: "
875 (lambda (x) (fboundp (nth 2 x)))
877 (read-string "Match: ")
878 (y-or-n-p "Use regexp match? ")
879 (prefix-numeric-value current-prefix-arg)))
881 (unless (and (stringp match) (> (length match) 0))
883 (goto-char (point-min))
884 (let ((regexp (cond ((eq type 'f)
885 (gnus-simplify-subject-fuzzy match))
889 (concat "\\`" (regexp-quote match) "\\'"))
891 (regexp-quote match)))))
893 (let ((content (gnus-summary-header header 'noerr extra))
894 (case-fold-search t))
896 (when (if (eq type 'f)
897 (string-equal (gnus-simplify-subject-fuzzy content)
899 (string-match regexp content))
900 (gnus-summary-raise-score score))))
901 (beginning-of-line 2))))
902 (gnus-set-mode-line 'summary))
904 (defun gnus-summary-score-crossposting (score date)
905 ;; Enter score file entry for current crossposting.
906 ;; SCORE is the score to add.
907 ;; DATE is the expire date.
908 (let ((xref (gnus-summary-header "xref"))
912 (error "This article is not crossposted"))
913 (while (string-match " \\([^ \t]+\\):" xref start)
914 (setq start (match-end 0))
917 (substring xref (match-beginning 1) (match-end 1)))
918 gnus-newsgroup-name))
919 (gnus-summary-score-entry
920 "xref" (concat " " group ":") nil score date t)))))
927 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
929 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
930 (defun gnus-score-set-mark-below (score)
931 "Automatically mark articles with score below SCORE as read."
933 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
934 (string-to-int (read-string "Mark below: ")))))
935 (setq score (or score gnus-summary-default-score 0))
936 (gnus-score-set 'mark (list score))
937 (gnus-score-set 'touched '(t))
938 (setq gnus-summary-mark-below score)
939 (gnus-score-update-lines))
941 (defun gnus-score-update-lines ()
942 "Update all lines in the summary buffer."
944 (goto-char (point-min))
946 (gnus-summary-update-line)
949 (defun gnus-score-update-all-lines ()
950 "Update all lines in the summary buffer, even the hidden ones."
952 (goto-char (point-min))
955 (when (gnus-summary-show-thread)
956 (push (point) hidden))
957 (gnus-summary-update-line)
959 ;; Re-hide the hidden threads.
961 (goto-char (pop hidden))
962 (gnus-summary-hide-thread)))))
964 (defun gnus-score-set-expunge-below (score)
965 "Automatically expunge articles with score below SCORE."
967 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
968 (string-to-int (read-string "Set expunge below: ")))))
969 (setq score (or score gnus-summary-default-score 0))
970 (gnus-score-set 'expunge (list score))
971 (gnus-score-set 'touched '(t)))
973 (defun gnus-score-followup-article (&optional score)
974 "Add SCORE to all followups to the article in the current buffer."
976 (setq score (gnus-score-delta-default score))
977 (when (gnus-buffer-live-p gnus-summary-buffer)
980 (message-narrow-to-headers)
981 (let ((id (mail-fetch-field "message-id")))
983 (set-buffer gnus-summary-buffer)
984 (gnus-summary-score-entry
985 "references" (concat id "[ \t]*$") 'r
986 score (current-time-string) nil t)))))))
988 (defun gnus-score-followup-thread (&optional score)
989 "Add SCORE to all later articles in the thread the current buffer is part of."
991 (setq score (gnus-score-delta-default score))
992 (when (gnus-buffer-live-p gnus-summary-buffer)
995 (goto-char (point-min))
996 (let ((id (mail-fetch-field "message-id")))
998 (set-buffer gnus-summary-buffer)
999 (gnus-summary-score-entry
1001 score (current-time-string))))))))
1003 (defun gnus-score-set (symbol value &optional alist warn)
1004 ;; Set SYMBOL to VALUE in ALIST.
1008 (gnus-newsgroup-score-alist)))
1009 (entry (assoc symbol alist)))
1010 (cond ((gnus-score-get 'read-only alist)
1011 ;; This is a read-only score file, so we do nothing.
1013 (gnus-message 4 "Note: read-only score file; entry discarded")))
1015 (setcdr entry value))
1017 (error "Empty alist"))
1020 (cons (cons symbol value) (cdr alist)))))))
1022 (defun gnus-summary-raise-score (n)
1023 "Raise the score of the current article by N."
1025 (gnus-summary-set-score (+ (gnus-summary-article-score)
1026 (or n gnus-score-interactive-default-score ))))
1028 (defun gnus-summary-set-score (n)
1029 "Set the score of the current article to N."
1032 (gnus-summary-show-thread)
1033 (let ((buffer-read-only nil))
1035 (gnus-summary-update-mark
1036 (if (= n (or gnus-summary-default-score 0)) ? ;Whitespace
1037 (if (< n (or gnus-summary-default-score 0))
1038 gnus-score-below-mark gnus-score-over-mark))
1040 (let* ((article (gnus-summary-article-number))
1041 (score (assq article gnus-newsgroup-scored)))
1042 (if score (setcdr score n)
1043 (push (cons article n) gnus-newsgroup-scored)))
1044 (gnus-summary-update-line)))
1046 (defun gnus-summary-current-score ()
1047 "Return the score of the current article."
1049 (gnus-message 1 "%s" (gnus-summary-article-score)))
1051 (defun gnus-score-change-score-file (file)
1052 "Change current score alist."
1054 (list (read-file-name "Change to score file: " gnus-kill-files-directory)))
1055 (gnus-score-load-file file)
1056 (gnus-set-mode-line 'summary))
1058 (defvar gnus-score-edit-exit-function)
1059 (defun gnus-score-edit-current-scores (file)
1060 "Edit the current score alist."
1061 (interactive (list gnus-current-score-file))
1062 (if (not gnus-current-score-file)
1063 (error "No current score file")
1064 (let ((winconf (current-window-configuration)))
1065 (when (buffer-name gnus-summary-buffer)
1067 (gnus-make-directory (file-name-directory file))
1068 (setq gnus-score-edit-buffer (find-file-noselect file))
1069 (gnus-configure-windows 'edit-score)
1071 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1072 (make-local-variable 'gnus-prev-winconf)
1073 (setq gnus-prev-winconf winconf))
1075 4 (substitute-command-keys
1076 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits"))))
1078 (defun gnus-score-edit-file (file)
1079 "Edit a score file."
1081 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
1082 (gnus-make-directory (file-name-directory file))
1083 (when (buffer-name gnus-summary-buffer)
1085 (let ((winconf (current-window-configuration)))
1086 (setq gnus-score-edit-buffer (find-file-noselect file))
1087 (gnus-configure-windows 'edit-score)
1089 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1090 (make-local-variable 'gnus-prev-winconf)
1091 (setq gnus-prev-winconf winconf))
1093 4 (substitute-command-keys
1094 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
1096 (defun gnus-score-load-file (file)
1097 ;; Load score file FILE. Returns a list a retrieved score-alists.
1098 (let* ((file (expand-file-name
1099 (or (and (string-match
1100 (concat "^" (regexp-quote
1102 gnus-kill-files-directory)))
1103 (expand-file-name file))
1105 (concat (file-name-as-directory gnus-kill-files-directory)
1107 (cached (assoc file gnus-score-cache))
1108 (global (member file gnus-internal-global-score-files))
1111 ;; The score file was already loaded.
1112 (setq alist (cdr cached))
1113 ;; We load the score file.
1114 (setq gnus-score-alist nil)
1115 (setq alist (gnus-score-load-score-alist file))
1116 ;; We add '(touched) to the alist to signify that it hasn't been
1118 (unless (assq 'touched alist)
1119 (push (list 'touched nil) alist))
1120 ;; If it is a global score file, we make it read-only.
1122 (not (assq 'read-only alist))
1123 (push (list 'read-only t) alist))
1124 (push (cons file alist) gnus-score-cache))
1128 ;; Downcase all header names.
1131 (setcar (car a) (downcase (caar a)))
1133 ;; Advanced scoring.
1137 ;; If there are actual scores in the alist, we add it to the
1138 ;; return value of this function.
1140 (setq lists (list alist))))
1141 ;; Treat the other possible atoms in the score alist.
1142 (let ((mark (car (gnus-score-get 'mark alist)))
1143 (expunge (car (gnus-score-get 'expunge alist)))
1144 (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
1145 (files (gnus-score-get 'files alist))
1146 (exclude-files (gnus-score-get 'exclude-files alist))
1147 (orphan (car (gnus-score-get 'orphan alist)))
1148 (adapt (gnus-score-get 'adapt alist))
1149 (thread-mark-and-expunge
1150 (car (gnus-score-get 'thread-mark-and-expunge alist)))
1151 (adapt-file (car (gnus-score-get 'adapt-file alist)))
1152 (local (gnus-score-get 'local alist))
1153 (decay (car (gnus-score-get 'decay alist)))
1154 (eval (car (gnus-score-get 'eval alist))))
1155 ;; Perform possible decays.
1156 (when (and gnus-decay-scores
1157 (or cached (file-exists-p file))
1159 (gnus-decay-scores alist decay)))
1160 (gnus-score-set 'touched '(t) alist)
1161 (gnus-score-set 'decay (list (time-to-days (current-time))) alist))
1162 ;; We do not respect eval and files atoms from global score
1164 (when (and files (not global))
1165 (setq lists (apply 'append lists
1166 (mapcar (lambda (file)
1167 (gnus-score-load-file file))
1168 (if adapt-file (cons adapt-file files)
1170 (when (and eval (not global))
1172 ;; We then expand any exclude-file directives.
1173 (setq gnus-scores-exclude-files
1180 (expand-file-name sfile (file-name-directory file))
1181 (expand-file-name sfile gnus-kill-files-directory)))
1183 gnus-scores-exclude-files))
1186 (set-buffer gnus-summary-buffer)
1188 (and (consp (car local))
1189 (symbolp (caar local))
1191 (make-local-variable (caar local))
1192 (set (caar local) (nth 1 (car local)))))
1193 (setq local (cdr local)))))
1195 (setq gnus-orphan-score orphan))
1196 (setq gnus-adaptive-score-alist
1197 (cond ((equal adapt '(t))
1198 (setq gnus-newsgroup-adaptive t)
1199 gnus-default-adaptive-score-alist)
1200 ((equal adapt '(ignore))
1201 (setq gnus-newsgroup-adaptive nil))
1203 (setq gnus-newsgroup-adaptive t)
1206 ;;(setq gnus-newsgroup-adaptive gnus-use-adaptive-scoring)
1207 gnus-default-adaptive-score-alist)))
1208 (setq gnus-thread-expunge-below
1209 (or thread-mark-and-expunge gnus-thread-expunge-below))
1210 (setq gnus-summary-mark-below
1211 (or mark mark-and-expunge gnus-summary-mark-below))
1212 (setq gnus-summary-expunge-below
1213 (or expunge mark-and-expunge gnus-summary-expunge-below))
1214 (setq gnus-newsgroup-adaptive-score-file
1215 (or adapt-file gnus-newsgroup-adaptive-score-file)))
1216 (setq gnus-current-score-file file)
1217 (setq gnus-score-alist alist)
1220 (defun gnus-score-load (file)
1222 (let ((cache (assoc file gnus-score-cache)))
1224 (setq gnus-score-alist (cdr cache))
1225 (setq gnus-score-alist nil)
1226 (gnus-score-load-score-alist file)
1227 (unless gnus-score-alist
1228 (setq gnus-score-alist (copy-alist '((touched nil)))))
1229 (push (cons file gnus-score-alist) gnus-score-cache))))
1231 (defun gnus-score-remove-from-cache (file)
1232 (setq gnus-score-cache
1233 (delq (assoc file gnus-score-cache) gnus-score-cache)))
1235 (defun gnus-score-load-score-alist (file)
1238 (if (not (file-readable-p file))
1239 ;; Couldn't read file.
1240 (setq gnus-score-alist nil)
1243 (let ((coding-system-for-read score-mode-coding-system))
1244 (insert-file-contents file))
1245 (goto-char (point-min))
1246 ;; Only do the loading if the score file isn't empty.
1247 (when (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
1250 (read (current-buffer))
1252 (gnus-error 3.2 "Problem with score file %s" file))))))
1256 ;; Bogus score file.
1257 (error "Invalid syntax with score file %s" file))
1258 ((eq (car alist) 'setq)
1259 ;; This is an old-style score file.
1260 (setq gnus-score-alist (gnus-score-transform-old-to-new alist)))
1262 (setq gnus-score-alist alist)))
1263 ;; Check the syntax of the score file.
1264 (setq gnus-score-alist
1265 (gnus-score-check-syntax gnus-score-alist file)))))
1267 (defun gnus-score-check-syntax (alist file)
1268 "Check the syntax of the score ALIST."
1272 ((not (consp alist))