1 ;;; gnus-score.el --- scoring code for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Author: Per Abrahamsen <amanda@iesd.auc.dk>
5 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30 (defvar gnus-score-expiry-days 7
31 "*Number of days before unused score file entries are expired.")
33 (defvar gnus-orphan-score nil
34 "*All orphans get this score added. Set in the score file.")
36 (defvar gnus-default-adaptive-score-alist
38 (gnus-ticked-mark (from 4))
39 (gnus-dormant-mark (from 5))
40 (gnus-del-mark (from -4) (subject -1))
41 (gnus-read-mark (from 4) (subject 2))
42 (gnus-expirable-mark (from -1) (subject -1))
43 (gnus-killed-mark (from -1) (subject -3))
45 (gnus-catchup-mark (from -1) (subject -1)))
46 "*Alist of marks and scores.")
48 (defvar gnus-score-mimic-keymap nil
49 "*Have the score entry functions pretend that they are a keymap.")
51 (defvar gnus-score-exact-adapt-limit nil
52 "*Number that says how long a match has to be before using substring matching.
53 When doing adaptive scoring, one normally uses substring matching.
54 However, if the header one matches is short, the possibility for false
55 positives is great, so if the length of the match is less than this
56 variable, exact matching will be used.
58 If this variable is nil, which it is by default, exact matching will
63 ;; Internal variables.
65 (defvar gnus-score-help-winconf nil)
66 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
67 (defvar gnus-score-trace nil)
68 (defvar gnus-score-edit-buffer nil)
70 (defvar gnus-score-alist nil
71 "Alist containing score information.
72 The keys can be symbols or strings. The following symbols are defined.
74 touched: If this alist has been modified.
75 mark: Automatically mark articles below this.
76 expunge: Automatically expunge articles below this.
77 files: List of other SCORE files to load when loading this one.
78 eval: Sexp to be evaluated when the score file is loaded.
80 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
81 where HEADER is the header being scored, MATCH is the string we are
82 looking for, TYPE is a flag indicating whether it should use regexp or
83 substring matching, SCORE is the score to add and DATE is the date
84 of the last successful match.")
86 (defvar gnus-score-cache nil)
87 (defvar gnus-scores-articles nil)
88 (defvar gnus-scores-exclude-files nil)
89 (defvar gnus-header-index nil)
90 (defvar gnus-score-index nil)
92 (autoload 'gnus-uu-ctl-map "gnus-uu" nil nil 'keymap)
94 ;;; Summary mode score maps.
96 (defvar gnus-summary-score-map nil)
98 (define-prefix-command 'gnus-summary-score-map)
99 (define-key gnus-summary-various-map "S" 'gnus-summary-score-map)
100 (define-key gnus-summary-score-map "s" 'gnus-summary-set-score)
101 (define-key gnus-summary-score-map "a" 'gnus-summary-score-entry)
102 (define-key gnus-summary-score-map "S" 'gnus-summary-current-score)
103 (define-key gnus-summary-score-map "c" 'gnus-score-change-score-file)
104 (define-key gnus-summary-score-map "m" 'gnus-score-set-mark-below)
105 (define-key gnus-summary-score-map "x" 'gnus-score-set-expunge-below)
106 (define-key gnus-summary-score-map "e" 'gnus-score-edit-alist)
107 (define-key gnus-summary-score-map "f" 'gnus-score-edit-file)
108 (define-key gnus-summary-score-map "t" 'gnus-score-find-trace)
109 (define-key gnus-summary-score-map "C" 'gnus-score-customize)
113 ;; Summary score file commands
115 ;; Much modification of the kill (ahem, score) code and lots of the
116 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
118 (defun gnus-summary-lower-score (score)
120 (gnus-summary-increase-score (- (gnus-score-default score))))
122 (defun gnus-summary-increase-score (score)
124 (gnus-set-global-variables)
125 (let* ((nscore (gnus-score-default score))
126 (prefix (if (< nscore 0) ?L ?I))
127 (increase (> nscore 0))
129 '((?a "from" nil nil string)
130 (?s "subject" nil nil string)
131 (?b "body" "" nil string)
132 (?h "head" "" nil string)
133 (?i "message-id" nil t string)
134 (?t "references" "message-id" t string)
135 (?x "xref" nil nil string)
136 (?l "lines" nil nil number)
137 (?d "date" nil nil date)
138 (?f "followup" nil nil string)))
140 '((?s s "substring" string)
141 (?e e "exact string" string)
142 (?f f "fuzzy string" string)
143 (?r r "regexp string" string)
144 (?b before "before date" date)
145 (?a at "at date" date)
146 (?n now "this date" date)
147 (?< < "less than number" number)
148 (?> > "greater than number" number)
149 (?= = "equal to number" number)))
151 (list (list ?t (current-time-string) "temporary")
152 '(?p perm "permanent") '(?i now "immediate")))
153 (mimic gnus-score-mimic-keymap)
154 hchar entry temporary tchar pchar end type)
155 ;; First we read the header to score.
160 (message "%c-" prefix))
161 (message "%s header (%s?): " (if increase "Increase" "Lower")
162 (mapconcat (lambda (s) (char-to-string (car s)))
164 (setq hchar (read-char))
165 (if (not (or (= hchar ??) (= hchar ?\C-h)))
168 (gnus-score-insert-help "Match on header" char-to-header 1)))
170 (and (get-buffer "*Score Help*")
172 (kill-buffer "*Score Help*")
173 (set-window-configuration gnus-score-help-winconf)))
175 (or (setq entry (assq (downcase hchar) char-to-header))
179 (if mimic (message "%c %c" prefix hchar) (message ""))))
180 (if (or end (/= (downcase hchar) hchar))
182 ;; This was a majuscle, so we end reading and set the defaults.
183 (if mimic (message "%c %c" prefix hchar) (message ""))
185 temporary (current-time-string)))
187 ;; We continue reading - the type.
192 (message "%c %c-" prefix hchar))
193 (message "%s header '%s' with match type (%s?): "
194 (if increase "Increase" "Lower")
196 (mapconcat (lambda (s)
197 (if (eq (nth 4 entry)
199 (char-to-string (car s))
202 (setq tchar (read-char))
203 (if (not (or (= tchar ??) (= tchar ?\C-h)))
206 (gnus-score-insert-help "Match type" char-to-type 2)))
208 (and (get-buffer "*Score Help*")
210 (set-window-configuration gnus-score-help-winconf)
211 (kill-buffer "*Score Help*")))
213 (or (setq type (nth 1 (assq (downcase tchar) char-to-type)))
216 (if mimic (message "%c %c" prefix hchar) (message ""))
218 (if (or end (/= (downcase tchar) tchar))
220 ;; It was a majuscle, so we end reading and the the default.
221 (if mimic (message "%c %c %c" prefix hchar tchar)
223 (setq temporary (current-time-string)))
225 ;; We continue reading.
230 (message "%c %c %c-" prefix hchar tchar))
231 (message "%s permanence (%s?): " (if increase "Increase" "Lower")
232 (mapconcat (lambda (s) (char-to-string (car s)))
234 (setq pchar (read-char))
235 (if (not (or (= pchar ??) (= pchar ?\C-h)))
238 (gnus-score-insert-help "Match permanence" char-to-perm 2)))
240 (and (get-buffer "*Score Help*")
242 (set-window-configuration gnus-score-help-winconf)
243 (kill-buffer "*Score Help*")))
245 (if mimic (message "%c %c %c" prefix hchar tchar pchar)
247 (if (setq temporary (nth 1 (assq pchar char-to-perm)))
252 (message "%c %c %c %c" prefix hchar tchar pchar)
255 ;; We have all the data, so we enter this score.
258 (gnus-summary-score-entry
259 (nth 1 entry) ; Header
260 (if (string= (nth 2 entry) "") ""
261 (gnus-summary-header (or (nth 2 entry) (nth 1 entry)))) ; Match
263 (if (eq 's score) nil score) ; Score
264 (if (eq 'perm temporary) ; Temp
267 (not (nth 3 entry))) ; Prompt
270 (defun gnus-score-insert-help (string alist idx)
271 (setq gnus-score-help-winconf (current-window-configuration))
273 (pop-to-buffer "*Score Help*")
274 (buffer-disable-undo (current-buffer))
276 (insert string ":\n\n")
278 (insert (format " %c: %s\n" (car (car alist)) (nth idx (car alist))))
279 (setq alist (cdr alist)))
280 (select-window (get-buffer-window gnus-summary-buffer))))
282 (defun gnus-summary-header (header)
283 ;; Return HEADER for current articles, or error.
284 (let ((article (gnus-summary-article-number))
287 (if (setq header (gnus-get-header-by-number article))
288 (aref header (nth 1 (assoc header gnus-header-index)))
289 (error "Pseudo-articles can't be scored"))
290 (error "No article on current line"))))
292 (defun gnus-summary-score-entry
293 (header match type score date &optional prompt silent)
294 "Enter score file entry.
295 HEADER is the header being scored.
296 MATCH is the string we are looking for.
297 TYPE is the match type: substring, regexp, exact, fuzzy.
298 SCORE is the score to add.
299 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
300 If optional argument `PROMPT' is non-nil, allow user to edit match.
301 If optional argument `SILENT' is nil, show effect of score entry."
303 (list (completing-read "Header: "
305 (lambda (x) (fboundp (nth 2 x)))
307 (read-string "Match: ")
308 (if (y-or-n-p "Use regexp match? ") 'r 's)
309 (and current-prefix-arg
310 (prefix-numeric-value current-prefix-arg))
311 (cond ((not (y-or-n-p "Add to SCORE file? "))
313 ((y-or-n-p "Expire kill? ")
314 (current-time-string))
316 ;; Regexp is the default type.
317 (if (eq type t) (setq type 'r))
318 ;; Simplify matches...
319 (if (or (eq type 'r) (eq type 's) (eq type nil))
320 (setq match (gnus-simplify-subject-re match)))
321 (let ((score (gnus-score-default score))
322 (header (downcase header)))
323 (and prompt (setq match (read-string
324 (format "Match %s on %s, %s: "
325 (cond ((eq date 'now)
331 (if (< score 0) "lower" "raise"))
333 (int-to-string match)
335 (and (>= (nth 1 (assoc header gnus-header-index)) 0)
336 (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-string)
338 (gnus-summary-score-effect header match type score))
340 ;; If this is an integer comparison, we transform from string to int.
341 (and (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
342 (setq match (string-to-int match)))
346 (and (= score gnus-score-interactive-default-score)
350 (list (gnus-simplify-subject-fuzzy match)
351 score (and date (gnus-day-number date)) type))
353 (list match score (and date (gnus-day-number date)) type))
355 (list match score (gnus-day-number date)))
360 (old (gnus-score-get header))
362 ;; We see whether we can collapse some score entries.
363 ;; This isn't quite correct, because there may be more elements
364 ;; later on with the same key that have matching elems... Hm.
366 (setq elem (assoc match old))
367 (eq (nth 3 elem) (nth 3 new))
368 (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
369 (and (not (nth 2 elem)) (not (nth 2 new)))))
370 ;; Yup, we just add this new score to the old elem.
371 (setcar (cdr elem) (+ (or (nth 1 elem)
372 gnus-score-interactive-default-score)
374 gnus-score-interactive-default-score)))
375 ;; Nope, we have to add a new elem.
376 (gnus-score-set header (if old (cons new old) (list new)))))
377 (gnus-score-set 'touched '(t)))))
379 (defun gnus-summary-score-effect (header match type score)
380 "Simulate the effect of a score file entry.
381 HEADER is the header being scored.
382 MATCH is the string we are looking for.
383 TYPE is a flag indicating if it is a regexp or substring.
384 SCORE is the score to add."
385 (interactive (list (completing-read "Header: "
387 (lambda (x) (fboundp (nth 2 x)))
389 (read-string "Match: ")
390 (y-or-n-p "Use regexp match? ")
391 (prefix-numeric-value current-prefix-arg)))
393 (or (and (stringp match) (> (length match) 0))
395 (goto-char (point-min))
396 (let ((regexp (cond ((eq type 'f)
397 (gnus-simplify-subject-fuzzy match))
399 (t (concat "\\`.*" (regexp-quote match) ".*\\'")))))
401 (let ((content (gnus-summary-header header))
402 (case-fold-search t))
405 (string-equal (gnus-simplify-subject-fuzzy content)
407 (string-match regexp content))
408 (gnus-summary-raise-score score))))