1 ;;; gnus-score.el --- scoring code for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
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-adaptive-word-length-limit nil
236 "*Words of a length lesser than this limit will be ignored when doing adaptive scoring."
237 :group 'gnus-score-adapt
240 (defcustom gnus-ignored-adaptive-words nil
241 "List of words to be ignored when doing adaptive word scoring."
242 :group 'gnus-score-adapt
243 :type '(repeat string))
245 (defcustom gnus-default-ignored-adaptive-words
246 '("a" "i" "the" "to" "of" "and" "in" "is" "it" "for" "that" "if" "you"
247 "this" "be" "on" "with" "not" "have" "are" "or" "as" "from" "can"
248 "but" "by" "at" "an" "will" "no" "all" "was" "do" "there" "my" "one"
249 "so" "we" "they" "what" "would" "any" "which" "about" "get" "your"
250 "use" "some" "me" "then" "name" "like" "out" "when" "up" "time"
251 "other" "more" "only" "just" "end" "also" "know" "how" "new" "should"
252 "been" "than" "them" "he" "who" "make" "may" "people" "these" "now"
253 "their" "here" "into" "first" "could" "way" "had" "see" "work" "well"
254 "were" "two" "very" "where" "while" "us" "because" "good" "same"
255 "even" "much" "most" "many" "such" "long" "his" "over" "last" "since"
256 "right" "before" "our" "without" "too" "those" "why" "must" "part"
257 "being" "current" "back" "still" "go" "point" "value" "each" "did"
258 "both" "true" "off" "say" "another" "state" "might" "under" "start"
260 "*Default list of words to be ignored when doing adaptive word scoring."
261 :group 'gnus-score-adapt
262 :type '(repeat string))
264 (defcustom gnus-default-adaptive-word-score-alist
265 `((,gnus-read-mark . 30)
266 (,gnus-catchup-mark . -10)
267 (,gnus-killed-mark . -20)
268 (,gnus-del-mark . -15))
269 "*Alist of marks and scores."
270 :group 'gnus-score-adapt
271 :type '(repeat (cons (character :tag "Mark")
272 (integer :tag "Score"))))
274 (defcustom gnus-adaptive-word-minimum nil
275 "If a number, this is the minimum score value that can be assigned to a word."
276 :group 'gnus-score-adapt
277 :type '(choice (const nil) integer))
279 (defcustom gnus-adaptive-word-no-group-words nil
280 "If t, don't adaptively score words included in the group name."
281 :group 'gnus-score-adapt
284 (defcustom gnus-score-mimic-keymap nil
285 "*Have the score entry functions pretend that they are a keymap."
286 :group 'gnus-score-default
289 (defcustom gnus-score-exact-adapt-limit 10
290 "*Number that says how long a match has to be before using substring matching.
291 When doing adaptive scoring, one normally uses fuzzy or substring
292 matching. However, if the header one matches is short, the possibility
293 for false positives is great, so if the length of the match is less
294 than this variable, exact matching will be used.
296 If this variable is nil, exact matching will always be used."
297 :group 'gnus-score-adapt
298 :type '(choice (const nil) integer))
300 (defcustom gnus-score-uncacheable-files "ADAPT$"
301 "All score files that match this regexp will not be cached."
302 :group 'gnus-score-adapt
303 :group 'gnus-score-files
306 (defcustom gnus-score-default-header nil
307 "Default header when entering new scores.
309 Should be one of the following symbols.
318 e: `extra' (non-standard overview)
323 If nil, the user will be asked for a header."
324 :group 'gnus-score-default
325 :type '(choice (const :tag "from" a)
326 (const :tag "subject" s)
327 (const :tag "body" b)
328 (const :tag "head" h)
329 (const :tag "message-id" i)
330 (const :tag "references" t)
331 (const :tag "xref" x)
332 (const :tag "extra" e)
333 (const :tag "lines" l)
334 (const :tag "date" d)
335 (const :tag "followup" f)
336 (const :tag "ask" nil)))
338 (defcustom gnus-score-default-type nil
339 "Default match type when entering new scores.
341 Should be one of the following symbols.
351 >: greater than number
354 If nil, the user will be asked for a match type."
355 :group 'gnus-score-default
356 :type '(choice (const :tag "substring" s)
357 (const :tag "exact string" e)
358 (const :tag "fuzzy string" f)
359 (const :tag "regexp string" r)
360 (const :tag "before date" b)
361 (const :tag "after date" a)
362 (const :tag "this date" n)
363 (const :tag "less than number" <)
364 (const :tag "greater than number" >)
365 (const :tag "equal than number" =)
366 (const :tag "ask" nil)))
368 (defcustom gnus-score-default-fold nil
369 "Use case folding for new score file entries iff not nil."
370 :group 'gnus-score-default
373 (defcustom gnus-score-default-duration nil
374 "Default duration of effect when entering new scores.
376 Should be one of the following symbols.
382 If nil, the user will be asked for a duration."
383 :group 'gnus-score-default
384 :type '(choice (const :tag "temporary" t)
385 (const :tag "permanent" p)
386 (const :tag "immediate" i)
387 (const :tag "ask" nil)))
389 (defcustom gnus-score-after-write-file-function nil
390 "Function called with the name of the score file just written to disk."
391 :group 'gnus-score-files
392 :type '(choice (const nil) function))
394 (defcustom gnus-score-thread-simplify nil
395 "If non-nil, subjects will simplified as in threading."
396 :group 'gnus-score-various
401 ;; Internal variables.
403 (defvar gnus-score-use-all-scores t
404 "If nil, only `gnus-score-find-score-files-function' is used.")
406 (defvar gnus-adaptive-word-syntax-table
407 (let ((table (copy-syntax-table (standard-syntax-table)))
408 (numbers '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)))
410 (modify-syntax-entry (pop numbers) " " table))
411 (modify-syntax-entry ?' "w" table)
413 "Syntax table used when doing adaptive word scoring.")
415 (defvar gnus-scores-exclude-files nil)
416 (defvar gnus-internal-global-score-files nil)
417 (defvar gnus-score-file-list nil)
419 (defvar gnus-short-name-score-file-cache nil)
421 (defvar gnus-score-help-winconf nil)
422 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
423 (defvar gnus-adaptive-word-score-alist gnus-default-adaptive-word-score-alist)
424 (defvar gnus-score-trace nil)
425 (defvar gnus-score-edit-buffer nil)
427 (defvar gnus-score-alist nil
428 "Alist containing score information.
429 The keys can be symbols or strings. The following symbols are defined.
431 touched: If this alist has been modified.
432 mark: Automatically mark articles below this.
433 expunge: Automatically expunge articles below this.
434 files: List of other score files to load when loading this one.
435 eval: Sexp to be evaluated when the score file is loaded.
437 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
438 where HEADER is the header being scored, MATCH is the string we are
439 looking for, TYPE is a flag indicating whether it should use regexp or
440 substring matching, SCORE is the score to add and DATE is the date
441 of the last successful match.")
443 (defvar gnus-score-cache nil)
444 (defvar gnus-scores-articles nil)
445 (defvar gnus-score-index nil)
448 (defconst gnus-header-index
449 ;; Name to index alist.
450 '(("number" 0 gnus-score-integer)
451 ("subject" 1 gnus-score-string)
452 ("from" 2 gnus-score-string)
453 ("date" 3 gnus-score-date)
454 ("message-id" 4 gnus-score-string)
455 ("references" 5 gnus-score-string)
456 ("chars" 6 gnus-score-integer)
457 ("lines" 7 gnus-score-integer)
458 ("xref" 8 gnus-score-string)
459 ("extra" 9 gnus-score-string)
460 ("head" -1 gnus-score-body)
461 ("body" -1 gnus-score-body)
462 ("all" -1 gnus-score-body)
463 ("followup" 2 gnus-score-followup)
464 ("thread" 5 gnus-score-thread)))
466 ;;; Summary mode score maps.
468 (gnus-define-keys (gnus-summary-score-map "V" gnus-summary-mode-map)
469 "s" gnus-summary-set-score
470 "S" gnus-summary-current-score
471 "c" gnus-score-change-score-file
472 "C" gnus-score-customize
473 "m" gnus-score-set-mark-below
474 "x" gnus-score-set-expunge-below
475 "R" gnus-summary-rescore
476 "e" gnus-score-edit-current-scores
477 "f" gnus-score-edit-file
478 "F" gnus-score-flush-cache
479 "t" gnus-score-find-trace
480 "w" gnus-score-find-favourite-words)
482 ;; Summary score file commands
484 ;; Much modification of the kill (ahem, score) code and lots of the
485 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
487 (defun gnus-summary-lower-score (&optional score symp)
488 "Make a score entry based on the current article.
489 The user will be prompted for header to score on, match type,
490 permanence, and the string to be used. The numerical prefix will be
492 (interactive (gnus-interactive "P\ny"))
493 (gnus-summary-increase-score (- (gnus-score-delta-default score)) symp))
495 (defun gnus-score-kill-help-buffer ()
496 (when (get-buffer "*Score Help*")
497 (kill-buffer "*Score Help*")
498 (when gnus-score-help-winconf
499 (set-window-configuration gnus-score-help-winconf))))
501 (defun gnus-summary-increase-score (&optional score symp)
502 "Make a score entry based on the current article.
503 The user will be prompted for header to score on, match type,
504 permanence, and the string to be used. The numerical prefix will be
506 (interactive (gnus-interactive "P\ny"))
507 (let* ((nscore (gnus-score-delta-default score))
508 (prefix (if (< nscore 0) ?L ?I))
509 (increase (> nscore 0))
511 '((?a "from" nil nil string)
512 (?s "subject" nil nil string)
513 (?b "body" "" nil body-string)
514 (?h "head" "" nil body-string)
515 (?i "message-id" nil nil string)
516 (?r "references" "message-id" nil string)
517 (?x "xref" nil nil string)
518 (?e "extra" nil nil string)
519 (?l "lines" nil nil number)
520 (?d "date" nil nil date)
521 (?f "followup" nil nil string)
522 (?t "thread" "message-id" nil string)))
524 '((?s s "substring" string)
525 (?e e "exact string" string)
526 (?f f "fuzzy string" string)
527 (?r r "regexp string" string)
528 (?z s "substring" body-string)
529 (?p r "regexp string" body-string)
530 (?b before "before date" date)
531 (?a after "after date" date)
532 (?n at "this date" date)
533 (?< < "less than number" number)
534 (?> > "greater than number" number)
535 (?= = "equal to number" number)))
536 (current-score-file gnus-current-score-file)
538 (list (list ?t (current-time-string) "temporary")
539 '(?p perm "permanent") '(?i now "immediate")))
540 (mimic gnus-score-mimic-keymap)
541 (hchar (and gnus-score-default-header
542 (aref (symbol-name gnus-score-default-header) 0)))
543 (tchar (and gnus-score-default-type
544 (aref (symbol-name gnus-score-default-type) 0)))
545 (pchar (and gnus-score-default-duration
546 (aref (symbol-name gnus-score-default-duration) 0)))
547 entry temporary type match extra)
552 ;; First we read the header to score.
557 (message "%c-" prefix))
558 (message "%s header (%s?): " (if increase "Increase" "Lower")
559 (mapconcat (lambda (s) (char-to-string (car s)))
561 (setq hchar (read-char))
562 (when (or (= hchar ??) (= hchar ?\C-h))
564 (gnus-score-insert-help "Match on header" char-to-header 1)))
566 (gnus-score-kill-help-buffer)
567 (unless (setq entry (assq (downcase hchar) char-to-header))
568 (if mimic (error "%c %c" prefix hchar)
569 (error "Invalid header type")))
571 (when (/= (downcase hchar) hchar)
572 ;; This was a majuscule, so we end reading and set the defaults.
573 (if mimic (message "%c %c" prefix hchar) (message ""))
574 (setq tchar (or tchar ?s)
575 pchar (or pchar ?t)))
580 (if (eq (nth 4 entry)
584 ;; We continue reading - the type.
588 (sit-for 1) (message "%c %c-" prefix hchar))
589 (message "%s header '%s' with match type (%s?): "
590 (if increase "Increase" "Lower")
592 (mapconcat (lambda (s) (char-to-string (car s)))
594 (setq tchar (read-char))
595 (when (or (= tchar ??) (= tchar ?\C-h))
597 (gnus-score-insert-help "Match type" legal-types 2)))
599 (gnus-score-kill-help-buffer)
600 (unless (setq type (nth 1 (assq (downcase tchar) legal-types)))
601 (if mimic (error "%c %c" prefix hchar)
602 (error "Invalid match type"))))
604 (when (/= (downcase tchar) tchar)
605 ;; It was a majuscule, so we end reading and use the default.
606 (if mimic (message "%c %c %c" prefix hchar tchar)
608 (setq pchar (or pchar ?t)))
610 ;; We continue reading.
614 (sit-for 1) (message "%c %c %c-" prefix hchar tchar))
615 (message "%s permanence (%s?): " (if increase "Increase" "Lower")
616 (mapconcat (lambda (s) (char-to-string (car s)))
618 (setq pchar (read-char))
619 (when (or (= pchar ??) (= pchar ?\C-h))
621 (gnus-score-insert-help "Match permanence" char-to-perm 2)))
623 (gnus-score-kill-help-buffer)
624 (if mimic (message "%c %c %c" prefix hchar tchar pchar)
626 (unless (setq temporary (cadr (assq pchar char-to-perm)))
627 ;; Deal with der(r)ided superannuated paradigms.
628 (when (and (eq (1+ prefix) 77)
629 (eq (+ hchar 12) 109)
631 (eq (- pchar 4) 111))
634 (error "%c %c %c %c" prefix hchar tchar pchar)
635 (error "Invalid match duration"))))
636 ;; Always kill the score help buffer.
637 (gnus-score-kill-help-buffer))
639 ;; If scoring an extra (non-standard overview) header,
640 ;; we must find out which header is in question.
642 (and gnus-extra-headers
643 (equal (nth 1 entry) "extra")
644 (intern ; need symbol
645 (gnus-completing-read
646 (symbol-name (car gnus-extra-headers)) ; default response
647 "Score extra header:" ; prompt
648 (mapcar (lambda (x) ; completion list
649 (cons (symbol-name x) x))
651 nil ; no completion limit
652 t)))) ; require match
653 ;; extra is now nil or a symbol.
655 ;; We have all the data, so we enter this score.
656 (setq match (if (string= (nth 2 entry) "") ""
657 (gnus-summary-header (or (nth 2 entry) (nth 1 entry))
660 ;; Modify the match, perhaps.
662 ((equal (nth 1 entry) "xref")
663 (when (string-match "^Xref: *" match)
664 (setq match (substring match (match-end 0))))
665 (when (string-match "^[^:]* +" match)
666 (setq match (substring match (match-end 0))))))
668 (when (memq type '(r R regexp Regexp))
669 (setq match (regexp-quote match)))
671 ;; Change score file to the "all.SCORE" file.
674 (set-buffer gnus-summary-buffer)
675 (gnus-score-load-file
676 ;; This is a kludge; yes...
678 ((eq gnus-score-find-score-files-function
679 'gnus-score-find-hierarchical)
680 (gnus-score-file-name ""))
681 ((eq gnus-score-find-score-files-function 'gnus-score-find-single)
684 (gnus-score-file-name "all"))))))
686 (gnus-summary-score-entry
687 (nth 1 entry) ; Header
690 (if (eq score 's) nil score) ; Score
691 (if (eq temporary 'perm) ; Temp
694 (not (nth 3 entry)) ; Prompt
696 extra) ; non-standard overview.
699 ;; We change the score file back to the previous one.
701 (set-buffer gnus-summary-buffer)
702 (gnus-score-load-file current-score-file)))))
704 (defun gnus-score-insert-help (string alist idx)
705 (setq gnus-score-help-winconf (current-window-configuration))
707 (set-buffer (gnus-get-buffer-create "*Score Help*"))
708 (buffer-disable-undo)
709 (delete-windows-on (current-buffer))
711 (insert string ":\n\n")
716 ;; find the longest string to display
718 (setq n (length (nth idx (car list))))
721 (setq list (cdr list)))
722 (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
723 (setq n (/ (1- (window-width)) max)) ; items per line
724 (setq width (/ (1- (window-width)) n)) ; width of each item
725 ;; insert `n' items, each in a field of width `width'
730 (delete-char -1) ; the `\n' takes a char
732 (setq pad (- width 3))
733 (setq format (concat "%c: %-" (int-to-string pad) "s"))
734 (insert (format format (caar alist) (nth idx (car alist))))
735 (setq alist (cdr alist))
737 ;; display ourselves in a small window at the bottom
738 (gnus-appt-select-lowest-window)
740 (pop-to-buffer "*Score Help*")
741 (let ((window-min-height 1))
742 (shrink-window-if-larger-than-buffer))
743 (select-window (get-buffer-window gnus-summary-buffer t))))
745 (defun gnus-summary-header (header &optional no-err extra)
746 ;; Return HEADER for current articles, or error.
747 (let ((article (gnus-summary-article-number))
750 (if (and (setq headers (gnus-summary-article-header article))
752 (if extra ; `header' must be "extra"
753 (or (cdr (assq extra (mail-header-extra headers))) "")
754 (aref headers (nth 1 (assoc header gnus-header-index))))
757 (error "Pseudo-articles can't be scored")))
759 (error "No article on current line")
762 (defun gnus-newsgroup-score-alist ()
764 (let ((param-file (gnus-group-find-parameter
765 gnus-newsgroup-name 'score-file)))
767 (gnus-score-load param-file)))
769 (gnus-score-file-name gnus-newsgroup-name)))
772 (defsubst gnus-score-get (symbol &optional alist)
773 ;; Get SYMBOL's definition in ALIST.
777 (gnus-newsgroup-score-alist)))))
779 (defun gnus-summary-score-entry (header match type score date
780 &optional prompt silent extra)
781 "Enter score file entry.
782 HEADER is the header being scored.
783 MATCH is the string we are looking for.
784 TYPE is the match type: substring, regexp, exact, fuzzy.
785 SCORE is the score to add.
786 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
787 If optional argument `PROMPT' is non-nil, allow user to edit match.
788 If optional argument `SILENT' is nil, show effect of score entry.
789 If optional argument `EXTRA' is non-nil, it's a non-standard overview header."
790 ;; Regexp is the default type.
793 ;; Simplify matches...
794 (cond ((or (eq type 'r) (eq type 's) (eq type nil))
795 (setq match (if match (gnus-simplify-subject-re match) "")))
797 (setq match (gnus-simplify-subject-fuzzy match))))
798 (let ((score (gnus-score-delta-default score))
799 (header (downcase header))
801 (set-text-properties 0 (length header) nil header)
803 (setq match (read-string
804 (format "Match %s on %s, %s: "
805 (cond ((eq date 'now)
811 (if (< score 0) "lower" "raise"))
813 (int-to-string match)
816 ;; If this is an integer comparison, we transform from string to int.
817 (if (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
819 (setq match (string-to-int match)))
820 (set-text-properties 0 (length match) nil match))
822 (unless (eq date 'now)
823 ;; Add the score entry to the score file.
824 (when (= score gnus-score-interactive-default-score)
826 (let ((old (gnus-score-get header))
832 (and date (if (numberp date) date
834 type (symbol-name extra)))
837 (and date (if (numberp date) date
840 (date (list match score (date-to-day date)))
841 (score (list match score))
843 ;; We see whether we can collapse some score entries.
844 ;; This isn't quite correct, because there may be more elements
845 ;; later on with the same key that have matching elems... Hm.
847 (setq elem (assoc match old))
848 (eq (nth 3 elem) (nth 3 new))
849 (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
850 (and (not (nth 2 elem)) (not (nth 2 new)))))
851 ;; Yup, we just add this new score to the old elem.
852 (setcar (cdr elem) (+ (or (nth 1 elem)
853 gnus-score-interactive-default-score)
855 gnus-score-interactive-default-score)))
856 ;; Nope, we have to add a new elem.
857 (gnus-score-set header (if old (cons new old) (list new)) nil t))
858 (gnus-score-set 'touched '(t))))
860 ;; Score the current buffer.
862 (if (and (>= (nth 1 (assoc header gnus-header-index)) 0)
863 (eq (nth 2 (assoc header gnus-header-index))
865 (gnus-summary-score-effect header match type score extra)
866 (gnus-summary-rescore)))
868 ;; Return the new scoring rule.
871 (defun gnus-summary-score-effect (header match type score extra)
872 "Simulate the effect of a score file entry.
873 HEADER is the header being scored.
874 MATCH is the string we are looking for.
875 TYPE is the score type.
876 SCORE is the score to add.
877 EXTRA is the possible non-standard header."
878 (interactive (list (completing-read "Header: "
880 (lambda (x) (fboundp (nth 2 x)))
882 (read-string "Match: ")
883 (y-or-n-p "Use regexp match? ")
884 (prefix-numeric-value current-prefix-arg)))
886 (unless (and (stringp match) (> (length match) 0))
888 (goto-char (point-min))
889 (let ((regexp (cond ((eq type 'f)
890 (gnus-simplify-subject-fuzzy match))
894 (concat "\\`" (regexp-quote match) "\\'"))
896 (regexp-quote match)))))
898 (let ((content (gnus-summary-header header 'noerr extra))
899 (case-fold-search t))
901 (when (if (eq type 'f)
902 (string-equal (gnus-simplify-subject-fuzzy content)
904 (string-match regexp content))
905 (gnus-summary-raise-score score))))
906 (beginning-of-line 2))))
907 (gnus-set-mode-line 'summary))
909 (defun gnus-summary-score-crossposting (score date)
910 ;; Enter score file entry for current crossposting.
911 ;; SCORE is the score to add.
912 ;; DATE is the expire date.
913 (let ((xref (gnus-summary-header "xref"))
917 (error "This article is not crossposted"))
918 (while (string-match " \\([^ \t]+\\):" xref start)
919 (setq start (match-end 0))
922 (substring xref (match-beginning 1) (match-end 1)))
923 gnus-newsgroup-name))
924 (gnus-summary-score-entry
925 "xref" (concat " " group ":") nil score date t)))))
932 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
934 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
935 (defun gnus-score-set-mark-below (score)
936 "Automatically mark articles with score below SCORE as read."
938 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
939 (string-to-int (read-string "Mark below: ")))))
940 (setq score (or score gnus-summary-default-score 0))
941 (gnus-score-set 'mark (list score))
942 (gnus-score-set 'touched '(t))
943 (setq gnus-summary-mark-below score)
944 (gnus-score-update-lines))
946 (defun gnus-score-update-lines ()
947 "Update all lines in the summary buffer."
949 (goto-char (point-min))
951 (gnus-summary-update-line)
954 (defun gnus-score-update-all-lines ()
955 "Update all lines in the summary buffer, even the hidden ones."
957 (goto-char (point-min))
960 (when (gnus-summary-show-thread)
961 (push (point) hidden))
962 (gnus-summary-update-line)
964 ;; Re-hide the hidden threads.
966 (goto-char (pop hidden))
967 (gnus-summary-hide-thread)))))
969 (defun gnus-score-set-expunge-below (score)
970 "Automatically expunge articles with score below SCORE."
972 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
973 (string-to-int (read-string "Set expunge below: ")))))
974 (setq score (or score gnus-summary-default-score 0))
975 (gnus-score-set 'expunge (list score))
976 (gnus-score-set 'touched '(t)))
978 (defun gnus-score-followup-article (&optional score)
979 "Add SCORE to all followups to the article in the current buffer."
981 (setq score (gnus-score-delta-default score))
982 (when (gnus-buffer-live-p gnus-summary-buffer)
985 (message-narrow-to-headers)
986 (let ((id (mail-fetch-field "message-id")))
988 (set-buffer gnus-summary-buffer)
989 (gnus-summary-score-entry
990 "references" (concat id "[ \t]*$") 'r
991 score (current-time-string) nil t)))))))
993 (defun gnus-score-followup-thread (&optional score)
994 "Add SCORE to all later articles in the thread the current buffer is part of."
996 (setq score (gnus-score-delta-default score))
997 (when (gnus-buffer-live-p gnus-summary-buffer)
1000 (goto-char (point-min))
1001 (let ((id (mail-fetch-field "message-id")))
1003 (set-buffer gnus-summary-buffer)
1004 (gnus-summary-score-entry
1006 score (current-time-string))))))))
1008 (defun gnus-score-set (symbol value &optional alist warn)
1009 ;; Set SYMBOL to VALUE in ALIST.
1013 (gnus-newsgroup-score-alist)))
1014 (entry (assoc symbol alist)))
1015 (cond ((gnus-score-get 'read-only alist)
1016 ;; This is a read-only score file, so we do nothing.
1018 (gnus-message 4 "Note: read-only score file; entry discarded")))
1020 (setcdr entry value))
1022 (error "Empty alist"))
1025 (cons (cons symbol value) (cdr alist)))))))
1027 (defun gnus-summary-raise-score (n)
1028 "Raise the score of the current article by N."
1030 (gnus-summary-set-score (+ (gnus-summary-article-score)
1031 (or n gnus-score-interactive-default-score ))))
1033 (defun gnus-summary-set-score (n)
1034 "Set the score of the current article to N."
1037 (gnus-summary-show-thread)
1038 (let ((buffer-read-only nil))
1040 (gnus-summary-update-mark
1041 (if (= n (or gnus-summary-default-score 0)) ? ;Whitespace
1042 (if (< n (or gnus-summary-default-score 0))
1043 gnus-score-below-mark gnus-score-over-mark))
1045 (let* ((article (gnus-summary-article-number))
1046 (score (assq article gnus-newsgroup-scored)))
1047 (if score (setcdr score n)
1048 (push (cons article n) gnus-newsgroup-scored)))
1049 (gnus-summary-update-line)))
1051 (defun gnus-summary-current-score ()
1052 "Return the score of the current article."
1054 (gnus-message 1 "%s" (gnus-summary-article-score)))
1056 (defun gnus-score-change-score-file (file)
1057 "Change current score alist."
1059 (list (read-file-name "Change to score file: " gnus-kill-files-directory)))
1060 (gnus-score-load-file file)
1061 (gnus-set-mode-line 'summary))
1063 (defvar gnus-score-edit-exit-function)
1064 (defun gnus-score-edit-current-scores (file)
1065 "Edit the current score alist."
1066 (interactive (list gnus-current-score-file))
1067 (if (not gnus-current-score-file)
1068 (error "No current score file")
1069 (let ((winconf (current-window-configuration)))
1070 (when (buffer-name gnus-summary-buffer)
1072 (gnus-make-directory (file-name-directory file))
1073 (setq gnus-score-edit-buffer (find-file-noselect file))
1074 (gnus-configure-windows 'edit-score)
1076 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1077 (make-local-variable 'gnus-prev-winconf)
1078 (setq gnus-prev-winconf winconf))
1080 4 (substitute-command-keys
1081 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits"))))
1083 (defun gnus-score-edit-file (file)
1084 "Edit a score file."
1086 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
1087 (gnus-make-directory (file-name-directory file))
1088 (when (buffer-name gnus-summary-buffer)
1090 (let ((winconf (current-window-configuration)))
1091 (setq gnus-score-edit-buffer (find-file-noselect file))
1092 (gnus-configure-windows 'edit-score)
1094 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1095 (make-local-variable 'gnus-prev-winconf)
1096 (setq gnus-prev-winconf winconf))
1098 4 (substitute-command-keys
1099 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
1101 (defun gnus-score-load-file (file)
1102 ;; Load score file FILE. Returns a list a retrieved score-alists.
1103 (let* ((file (expand-file-name
1104 (or (and (string-match
1105 (concat "^" (regexp-quote
1107 gnus-kill-files-directory)))
1108 (expand-file-name file))
1110 (expand-file-name file gnus-kill-files-directory))))
1111 (cached (assoc file gnus-score-cache))
1112 (global (member file gnus-internal-global-score-files))
1115 ;; The score file was already loaded.
1116 (setq alist (cdr cached))
1117 ;; We load the score file.
1118 (setq gnus-score-alist nil)
1119 (setq alist (gnus-score-load-score-alist file))
1120 ;; We add '(touched) to the alist to signify that it hasn't been
1122 (unless (assq 'touched alist)
1123 (push (list 'touched nil) alist))
1124 ;; If it is a global score file, we make it read-only.
1126 (not (assq 'read-only alist))
1127 (push (list 'read-only t) alist))
1128 (push (cons file alist) gnus-score-cache))
1132 ;; Downcase all header names.
1135 (setcar (car a) (downcase (caar a)))
1137 ;; Advanced scoring.
1141 ;; If there are actual scores in the alist, we add it to the
1142 ;; return value of this function.
1144 (setq lists (list alist))))
1145 ;; Treat the other possible atoms in the score alist.
1146 (let ((mark (car (gnus-score-get 'mark alist)))
1147 (expunge (car (gnus-score-get 'expunge alist)))
1148 (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
1149 (files (gnus-score-get 'files alist))
1150 (exclude-files (gnus-score-get 'exclude-files alist))
1151 (orphan (car (gnus-score-get 'orphan alist)))
1152 (adapt (gnus-score-get 'adapt alist))
1153 (thread-mark-and-expunge
1154 (car (gnus-score-get 'thread-mark-and-expunge alist)))
1155 (adapt-file (car (gnus-score-get 'adapt-file alist)))
1156 (local (gnus-score-get 'local alist))
1157 (decay (car (gnus-score-get 'decay alist)))
1158 (eval (car (gnus-score-get 'eval alist))))
1159 ;; Perform possible decays.
1160 (when (and gnus-decay-scores
1161 (or cached (file-exists-p file))
1163 (gnus-decay-scores alist decay)))
1164 (gnus-score-set 'touched '(t) alist)
1165 (gnus-score-set 'decay (list (time-to-days (current-time))) alist))
1166 ;; We do not respect eval and files atoms from global score
1168 (when (and files (not global))
1169 (setq lists (apply 'append lists
1170 (mapcar (lambda (file)
1171 (gnus-score-load-file file))
1172 (if adapt-file (cons adapt-file files)
1174 (when (and eval (not global))
1176 ;; We then expand any exclude-file directives.
1177 (setq gnus-scores-exclude-files
1184 (expand-file-name sfile (file-name-directory file))
1185 (expand-file-name sfile gnus-kill-files-directory)))
1187 gnus-scores-exclude-files))
1190 (set-buffer gnus-summary-buffer)
1192 (and (consp (car local))
1193 (symbolp (caar local))
1195 (make-local-variable (caar local))
1196 (set (caar local) (nth 1 (car local)))))
1197 (setq local (cdr local)))))
1199 (setq gnus-orphan-score orphan))
1200 (setq gnus-adaptive-score-alist
1201 (cond ((equal adapt '(t))
1202 (setq gnus-newsgroup-adaptive t)
1203 gnus-default-adaptive-score-alist)
1204 ((equal adapt '(ignore))
1205 (setq gnus-newsgroup-adaptive nil))
1207 (setq gnus-newsgroup-adaptive t)
1210 ;;(setq gnus-newsgroup-adaptive gnus-use-adaptive-scoring)
1211 gnus-default-adaptive-score-alist)))
1212 (setq gnus-thread-expunge-below
1213 (or thread-mark-and-expunge gnus-thread-expunge-below))
1214 (setq gnus-summary-mark-below
1215 (or mark mark-and-expunge gnus-summary-mark-below))
1216 (setq gnus-summary-expunge-below
1217 (or expunge mark-and-expunge gnus-summary-expunge-below))
1218 (setq gnus-newsgroup-adaptive-score-file
1219 (or adapt-file gnus-newsgroup-adaptive-score-file)))
1220 (setq gnus-current-score-file file)
1221 (setq gnus-score-alist alist)
1224 (defun gnus-score-load (file)
1226 (let ((cache (assoc file gnus-score-cache)))
1228 (setq gnus-score-alist (cdr cache))
1229 (setq gnus-score-alist nil)
1230 (gnus-score-load-score-alist file)
1231 (unless gnus-score-alist
1232 (setq gnus-score-alist (copy-alist '((touched nil)))))
1233 (push (cons file gnus-score-alist) gnus-score-cache))))
1235 (defun gnus-score-remove-from-cache (file)
1236 (setq gnus-score-cache
1237 (delq (assoc file gnus-score-cache) gnus-score-cache)))
1239 (defun gnus-score-load-score-alist (file)
1242 (if (not (file-readable-p file))
1243 ;; Couldn't read file.
1244 (setq gnus-score-alist nil)
1247 (let ((coding-system-for-read score-mode-coding-system))
1248 (insert-file-contents file))
1249 (goto-char (point-min))
1250 ;; Only do the loading if the score file isn't empty.
1251 (when (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
1254 (read (current-buffer))
1256 (gnus-error 3.2 "Problem with score file %s" file))))))
1260 ;; Bogus score file.
1261 (error "Invalid syntax with score file %s" file))
1262 ((eq (car alist) 'setq)
1263 ;; This is an old-style score file.
1264 (setq gnus-score-alist (gnus-score-transform-old-to-new alist)))
1266 (setq gnus-score-alist alist)))
1267 ;; Check the syntax of the score file.
1268 (setq gnus-score-alist
1269 (gnus-score-check-syntax gnus-score-alist file)))))
1271 (defun gnus-score-check-syntax (alist file)
1272 "Check the syntax of the score ALIST."
1276 ((not (consp alist))
1277 (gnus-message 1 "Score file is not a list: %s" file)
1283 (while (and a (not err))
1287 ((not (listp (car a)))
1288 (format "Invalid score element %s in %s" (car a) file))
1291 ((not (listp (setq sr (cdar a))))
1292 (format "Invalid header match %s in %s" (nth 1 (car a)) file))
1294 (setq type (caar a))
1295 (while (and sr (not err))
1300 ((if (member (downcase type) '("lines" "chars"))
1301 (not (numberp (car s)))
1302 (not (stringp (car s))))
1303 (format "Invalid match %s in %s" (car s) file))
1304 ((and (cadr s) (not (integerp (cadr s))))
1305 (format "Non-integer score %s in %s" (cadr s) file))
1306 ((and (caddr s) (not (integerp (caddr s))))
1307 (format "Non-integer date %s in %s" (caddr s) file))
1308 ((and (cadddr s) (not (symbolp (cadddr s))))
1309 (format "Non-symbol match type %s in %s" (cadddr s) file)))))
1315 (gnus-message 3 err)
1320 (defun gnus-score-transform-old-to-new (alist)
1321 (let* ((alist (nth 2 alist))
1323 (when (eq (car alist) 'quote)
1324 (setq alist (nth 1 alist)))
1326 (setq entry (car alist))
1327 (if (stringp (car entry))
1328 (let ((scor (cdr entry)))
1332 (list (caar scor) (nth 2 (car scor))
1333 (and (nth 3 (car scor))
1334 (date-to-day (nth 3 (car scor))))
1335 (if (nth 1 (car scor)) 'r 's)))
1336 (setq scor (cdr scor))))
1337 (push (if (not (listp (cdr entry)))
1338 (list (car entry) (cdr entry))
1341 (setq alist (cdr alist)))
1342 (cons (list 'touched t) (nreverse out))))
1344 (defun gnus-score-save ()
1345 ;; Save all score information.
1346 (let ((cache gnus-score-cache)
1349 (setq gnus-score-alist nil)
1350 (nnheader-set-temp-buffer " *Gnus Scores*")
1353 (setq entry (pop cache)
1354 file (nnheader-translate-file-chars (car entry) t)
1356 (if (or (not (equal (gnus-score-get 'touched score) '(t)))
1357 (gnus-score-get 'read-only score)
1358 (and (file-exists-p file)
1359 (not (file-writable-p file))))
1361 (setq score (setcdr entry (gnus-delete-alist 'touched score)))
1363 (let (emacs-lisp-mode-hook)
1365 (concat (regexp-quote gnus-adaptive-file-suffix) "$")
1367 ;; This is an adaptive score file, so we do not run
1368 ;; it through `pp'. These files can get huge, and
1369 ;; are not meant to be edited by human hands.
1371 ;; This is a normal score file, so we print it very
1373 (let ((lisp-mode-syntax-table score-mode-syntax-table))
1374 (pp score (current-buffer)))))
1375 (gnus-make-directory (file-name-directory file))
1376 ;; If the score file is empty, we delete it.
1377 (if (zerop (buffer-size))
1379 ;; There are scores, so we write the file.
1380 (when (file-writable-p file)
1381 (let ((coding-system-for-write score-mode-coding-system))
1382 (gnus-write-buffer file))
1383 (when gnus-score-after-write-file-function
1384 (funcall gnus-score-after-write-file-function file)))))
1385 (and gnus-score-uncacheable-files
1386 (string-match gnus-score-uncacheable-files file)
1387 (gnus-score-remove-from-cache file)))
1388 (kill-buffer (current-buffer)))))
1390 (defun gnus-score-load-files (score-files)
1391 "Load all score files in SCORE-FILES."
1392 ;; Load the score files.
1395 (if (stringp (car score-files))
1396 ;; It is a string, which means that it's a score file name,
1397 ;; so we load the score file and add the score alist to
1398 ;; the list of alists.
1399 (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
1400 ;; It is an alist, so we just add it to the list directly.
1401 (setq scores (nconc (car score-files) scores)))
1402 (setq score-files (cdr score-files)))