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
37 '((gnus-kill-file-mark)
39 (gnus-read-mark (from 3) (subject 30))
40 (gnus-catchup-mark (subject -10))
41 (gnus-killed-mark (from -1) (subject -20))
42 (gnus-del-mark (from -2) (subject -15)))
43 "*Alist of marks and scores.")
45 (defvar gnus-score-mimic-keymap nil
46 "*Have the score entry functions pretend that they are a keymap.")
48 (defvar gnus-score-exact-adapt-limit 10
49 "*Number that says how long a match has to be before using substring matching.
50 When doing adaptive scoring, one normally uses fuzzy or substring
51 matching. However, if the header one matches is short, the possibility
52 for false positives is great, so if the length of the match is less
53 than this variable, exact matching will be used.
55 If this variable is nil, exact matching will always be used.")
57 (defvar gnus-score-uncacheable-files "ADAPT$"
58 "*All score files that match this regexp will not be cached.")
62 ;; Internal variables.
64 (defvar gnus-score-help-winconf nil)
65 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
66 (defvar gnus-score-trace nil)
67 (defvar gnus-score-edit-buffer nil)
69 (defvar gnus-score-alist nil
70 "Alist containing score information.
71 The keys can be symbols or strings. The following symbols are defined.
73 touched: If this alist has been modified.
74 mark: Automatically mark articles below this.
75 expunge: Automatically expunge articles below this.
76 files: List of other score files to load when loading this one.
77 eval: Sexp to be evaluated when the score file is loaded.
79 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
80 where HEADER is the header being scored, MATCH is the string we are
81 looking for, TYPE is a flag indicating whether it should use regexp or
82 substring matching, SCORE is the score to add and DATE is the date
83 of the last successful match.")
85 (defvar gnus-score-cache nil)
86 (defvar gnus-scores-articles nil)
87 (defvar gnus-scores-exclude-files nil)
88 (defvar gnus-header-index nil)
89 (defvar gnus-score-index nil)
92 (autoload 'gnus-uu-ctl-map "gnus-uu" nil nil 'keymap)
93 (autoload 'appt-select-lowest-window "appt.el"))
95 ;;; Summary mode score maps.
97 (defvar gnus-summary-score-map nil)
99 (define-prefix-command 'gnus-summary-score-map)
100 (define-key gnus-summary-mode-map "V" 'gnus-summary-score-map)
101 (define-key gnus-summary-score-map "s" 'gnus-summary-set-score)
102 (define-key gnus-summary-score-map "a" 'gnus-summary-score-entry)
103 (define-key gnus-summary-score-map "S" 'gnus-summary-current-score)
104 (define-key gnus-summary-score-map "c" 'gnus-score-change-score-file)
105 (define-key gnus-summary-score-map "m" 'gnus-score-set-mark-below)
106 (define-key gnus-summary-score-map "x" 'gnus-score-set-expunge-below)
107 (define-key gnus-summary-score-map "e" 'gnus-score-edit-alist)
108 (define-key gnus-summary-score-map "f" 'gnus-score-edit-file)
109 (define-key gnus-summary-score-map "t" 'gnus-score-find-trace)
110 (define-key gnus-summary-score-map "C" 'gnus-score-customize)
114 ;; Summary score file commands
116 ;; Much modification of the kill (ahem, score) code and lots of the
117 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
119 (defun gnus-summary-lower-score (&optional score)
120 "Make a score entry based on the current article.
121 The user will be prompted for header to score on, match type,
122 permanence, and the string to be used. The numerical prefix will be
125 (gnus-summary-increase-score (- (gnus-score-default score))))
127 (defun gnus-summary-increase-score (&optional score)
128 "Make a score entry based on the current article.
129 The user will be prompted for header to score on, match type,
130 permanence, and the string to be used. The numerical prefix will be
133 (gnus-set-global-variables)
134 (let* ((nscore (gnus-score-default score))
135 (prefix (if (< nscore 0) ?L ?I))
136 (increase (> nscore 0))
138 '((?a "from" nil nil string)
139 (?s "subject" nil nil string)
140 (?b "body" "" nil body-string)
141 (?h "head" "" nil body-string)
142 (?i "message-id" nil t string)
143 (?t "references" "message-id" t string)
144 (?x "xref" nil nil string)
145 (?l "lines" nil nil number)
146 (?d "date" nil nil date)
147 (?f "followup" nil nil string)))
149 '((?s s "substring" string)
150 (?e e "exact string" string)
151 (?f f "fuzzy string" string)
152 (?r r "regexp string" string)
153 (?s s "substring" body-string)
154 (?r s "regexp string" body-string)
155 (?b before "before date" date)
156 (?a at "at date" date)
157 (?n now "this date" date)
158 (?< < "less than number" number)
159 (?> > "greater than number" number)
160 (?= = "equal to number" number)))
162 (list (list ?t (current-time-string) "temporary")
163 '(?p perm "permanent") '(?i now "immediate")))
164 (mimic gnus-score-mimic-keymap)
165 hchar entry temporary tchar pchar end type)
166 ;; First we read the header to score.
171 (message "%c-" prefix))
172 (message "%s header (%s?): " (if increase "Increase" "Lower")
173 (mapconcat (lambda (s) (char-to-string (car s)))
175 (setq hchar (read-char))
176 (if (not (or (= hchar ??) (= hchar ?\C-h)))
179 (gnus-score-insert-help "Match on header" char-to-header 1)))
181 (and (get-buffer "*Score Help*")
183 (kill-buffer "*Score Help*")
184 (and gnus-score-help-winconf
185 (set-window-configuration gnus-score-help-winconf))))
187 (or (setq entry (assq (downcase hchar) char-to-header))
191 (if mimic (message "%c %c" prefix hchar) (message ""))))
192 (if (or end (/= (downcase hchar) hchar))
194 ;; This was a majuscle, so we end reading and set the defaults.
195 (if mimic (message "%c %c" prefix hchar) (message ""))
197 temporary (current-time-string)))
199 ;; We continue reading - the type.
204 (message "%c %c-" prefix hchar))
205 (message "%s header '%s' with match type (%s?): "
206 (if increase "Increase" "Lower")
208 (mapconcat (lambda (s)
209 (if (eq (nth 4 entry)
211 (char-to-string (car s))
214 (setq tchar (read-char))
215 (if (not (or (= tchar ??) (= tchar ?\C-h)))
218 (gnus-score-insert-help "Match type" char-to-type 2)))
220 (and (get-buffer "*Score Help*")
222 (and gnus-score-help-winconf
223 (set-window-configuration gnus-score-help-winconf))
224 (kill-buffer "*Score Help*")))
226 (or (setq type (nth 1 (assq (downcase tchar) char-to-type)))
229 (if mimic (message "%c %c" prefix hchar) (message ""))
231 (if (or end (/= (downcase tchar) tchar))
233 ;; It was a majuscle, so we end reading and the the default.
234 (if mimic (message "%c %c %c" prefix hchar tchar)
236 (setq temporary (current-time-string)))
238 ;; We continue reading.
243 (message "%c %c %c-" prefix hchar tchar))
244 (message "%s permanence (%s?): " (if increase "Increase" "Lower")
245 (mapconcat (lambda (s) (char-to-string (car s)))
247 (setq pchar (read-char))
248 (if (not (or (= pchar ??) (= pchar ?\C-h)))
251 (gnus-score-insert-help "Match permanence" char-to-perm 2)))
253 (and (get-buffer "*Score Help*")
255 (and gnus-score-help-winconf
256 (set-window-configuration gnus-score-help-winconf))
257 (kill-buffer "*Score Help*")))
259 (if mimic (message "%c %c %c" prefix hchar tchar pchar)
261 (if (setq temporary (nth 1 (assq pchar char-to-perm)))
266 (message "%c %c %c %c" prefix hchar tchar pchar)
269 ;; We have all the data, so we enter this score.
272 (gnus-summary-score-entry
273 (nth 1 entry) ; Header
274 (if (string= (nth 2 entry) "") ""
275 (gnus-summary-header (or (nth 2 entry) (nth 1 entry)))) ; Match
277 (if (eq 's score) nil score) ; Score
278 (if (eq 'perm temporary) ; Temp
281 (not (nth 3 entry))) ; Prompt
284 (defun gnus-score-insert-help (string alist idx)
285 (setq gnus-score-help-winconf (current-window-configuration))
287 (set-buffer (get-buffer-create "*Score Help*"))
288 (buffer-disable-undo (current-buffer))
289 (delete-windows-on (current-buffer))
291 (insert string ":\n\n")
296 ;; find the longest string to display
298 (setq n (length (nth idx (car list))))
301 (setq list (cdr list)))
302 (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
303 (setq n (/ (window-width) max)) ; items per line
304 (setq width (/ (window-width) n)) ; width of each item
305 ;; insert `n' items, each in a field of width `width'
310 (delete-char -1) ; the `\n' takes a char
312 (setq pad (- width 3))
313 (setq format (concat "%c: %-" (int-to-string pad) "s"))
314 (insert (format format (car (car alist)) (nth idx (car alist))))
315 (setq alist (cdr alist))
317 ;; display ourselves in a small window at the bottom
318 (appt-select-lowest-window)
320 (pop-to-buffer "*Score Help*")
321 (shrink-window-if-larger-than-buffer)
322 (select-window (get-buffer-window gnus-summary-buffer))))
324 (defun gnus-summary-header (header &optional no-err)
325 ;; Return HEADER for current articles, or error.
326 (let ((article (gnus-summary-article-number))
329 (if (and (setq headers (gnus-summary-article-header article))
331 (aref headers (nth 1 (assoc header gnus-header-index)))
334 (error "Pseudo-articles can't be scored")))
336 (error "No article on current line")
339 (defun gnus-summary-score-entry
340 (header match type score date &optional prompt silent)
341 "Enter score file entry.
342 HEADER is the header being scored.
343 MATCH is the string we are looking for.
344 TYPE is the match type: substring, regexp, exact, fuzzy.
345 SCORE is the score to add.
346 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
347 If optional argument `PROMPT' is non-nil, allow user to edit match.
348 If optional argument `SILENT' is nil, show effect of score entry."
350 (list (completing-read "Header: "
352 (lambda (x) (fboundp (nth 2 x)))
354 (read-string "Match: ")
355 (if (y-or-n-p "Use regexp match? ") 'r 's)
356 (and current-prefix-arg
357 (prefix-numeric-value current-prefix-arg))
358 (cond ((not (y-or-n-p "Add to score file? "))
360 ((y-or-n-p "Expire kill? ")
361 (current-time-string))
363 ;; Regexp is the default type.
364 (if (eq type t) (setq type 'r))
365 ;; Simplify matches...
366 (cond ((or (eq type 'r) (eq type 's) (eq type nil))
367 (setq match (if match (gnus-simplify-subject-re match) "")))
369 (setq match (gnus-simplify-subject-fuzzy match))))
370 (let ((score (gnus-score-default score))
371 (header (downcase header)))
372 (and prompt (setq match (read-string
373 (format "Match %s on %s, %s: "
374 (cond ((eq date 'now)
380 (if (< score 0) "lower" "raise"))
382 (int-to-string match)
384 (and (>= (nth 1 (assoc header gnus-header-index)) 0)
385 (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-string)
387 (gnus-summary-score-effect header match type score))
389 ;; If this is an integer comparison, we transform from string to int.
390 (and (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
391 (setq match (string-to-int match)))
395 (and (= score gnus-score-interactive-default-score)
399 (list match score (and date (gnus-day-number date)) type))
401 (list match score (gnus-day-number date)))
406 (old (gnus-score-get header))
408 ;; We see whether we can collapse some score entries.
409 ;; This isn't quite correct, because there may be more elements
410 ;; later on with the same key that have matching elems... Hm.
412 (setq elem (assoc match old))
413 (eq (nth 3 elem) (nth 3 new))
414 (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
415 (and (not (nth 2 elem)) (not (nth 2 new)))))
416 ;; Yup, we just add this new score to the old elem.
417 (setcar (cdr elem) (+ (or (nth 1 elem)
418 gnus-score-interactive-default-score)
420 gnus-score-interactive-default-score)))
421 ;; Nope, we have to add a new elem.
422 (gnus-score-set header (if old (cons new old) (list new)))))
423 (gnus-score-set 'touched '(t)))))
425 (defun gnus-summary-score-effect (header match type score)
426 "Simulate the effect of a score file entry.
427 HEADER is the header being scored.
428 MATCH is the string we are looking for.
429 TYPE is a flag indicating if it is a regexp or substring.
430 SCORE is the score to add."
431 (interactive (list (completing-read "Header: "
433 (lambda (x) (fboundp (nth 2 x)))
435 (read-string "Match: ")
436 (y-or-n-p "Use regexp match? ")
437 (prefix-numeric-value current-prefix-arg)))
439 (or (and (stringp match) (> (length match) 0))
441 (goto-char (point-min))
442 (let ((regexp (cond ((eq type 'f)
443 (gnus-simplify-subject-fuzzy match))
445 (t (concat "\\`.*" (regexp-quote match) ".*\\'")))))
447 (let ((content (gnus-summary-header header 'noerr))
448 (case-fold-search t))
451 (string-equal (gnus-simplify-subject-fuzzy content)
453 (string-match regexp content))
454 (gnus-summary-raise-score score))))
455 (beginning-of-line 2)))))
457 (defun gnus-summary-score-crossposting (score date)
458 ;; Enter score file entry for current crossposting.
459 ;; SCORE is the score to add.
460 ;; DATE is the expire date.
461 (let ((xref (gnus-summary-header "xref"))
464 (or xref (error "This article is not crossposted"))
465 (while (string-match " \\([^ \t]+\\):" xref start)
466 (setq start (match-end 0))
469 (substring xref (match-beginning 1) (match-end 1)))
470 gnus-newsgroup-name))
471 (gnus-summary-score-entry
472 "xref" (concat " " group ":") nil score date t)))))
479 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
481 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
482 (defun gnus-score-set-mark-below (score)
483 "Automatically mark articles with score below SCORE as read."
485 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
486 (string-to-int (read-string "Mark below: ")))))
487 (setq score (or score gnus-summary-default-score 0))
488 (gnus-score-set 'mark (list score))
489 (gnus-score-set 'touched '(t))
490 (setq gnus-summary-mark-below score)
491 (gnus-summary-update-lines))
493 (defun gnus-score-set-expunge-below (score)
494 "Automatically expunge articles with score below SCORE."
496 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
497 (string-to-int (read-string "Expunge below: ")))))
498 (setq score (or score gnus-summary-default-score 0))
499 (gnus-score-set 'expunge (list score))
500 (gnus-score-set 'touched '(t)))
502 (defun gnus-score-set (symbol value &optional alist)
503 ;; Set SYMBOL to VALUE in ALIST.
508 (gnus-score-load (gnus-score-file-name gnus-newsgroup-name))
510 (entry (assoc symbol alist)))
511 (cond ((gnus-score-get 'read-only alist)
512 ;; This is a read-only score file, so we do nothing.
515 (setcdr entry value))
517 (error "Empty alist"))
520 (cons (cons symbol value) (cdr alist)))))))
522 (defun gnus-score-get (symbol &optional alist)
523 ;; Get SYMBOL's definition in ALIST.
529 (gnus-score-file-name gnus-newsgroup-name))
530 gnus-score-alist)))))
532 (defun gnus-score-change-score-file (file)
533 "Change current score alist."
535 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
536 (gnus-score-load-file file)
537 (gnus-set-mode-line 'summary))
539 (defun gnus-score-edit-alist (file)
540 "Edit the current score alist."
541 (interactive (list gnus-current-score-file))
542 (let ((winconf (current-window-configuration)))
543 (and (buffer-name gnus-summary-buffer) (gnus-score-save))
544 (setq gnus-score-edit-buffer (find-file-noselect file))
545 (gnus-configure-windows 'edit-score)
547 (make-local-variable 'gnus-prev-winconf)
548 (setq gnus-prev-winconf winconf))
550 4 (substitute-command-keys
551 "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits")))
553 (defun gnus-score-edit-file (file)
556 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
557 (and (buffer-name gnus-summary-buffer) (gnus-score-save))
558 (let ((winconf (current-window-configuration)))
559 (setq gnus-score-edit-buffer (find-file-noselect file))
560 (gnus-configure-windows 'edit-score)
562 (make-local-variable 'gnus-prev-winconf)
563 (setq gnus-prev-winconf winconf))
565 4 (substitute-command-keys
566 "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits")))
568 (defun gnus-score-load-file (file)
569 ;; Load score file FILE. Returns a list a retrieved score-alists.
570 (setq gnus-kill-files-directory (or gnus-kill-files-directory "~/News/"))
571 (let* ((file (expand-file-name
572 (or (and (string-match
573 (concat "^" (expand-file-name
574 gnus-kill-files-directory))
575 (expand-file-name file))
577 (concat gnus-kill-files-directory file))))
578 (cached (assoc file gnus-score-cache))
579 (global (member file gnus-internal-global-score-files))
582 ;; The score file was already loaded.
583 (setq alist (cdr cached))
584 ;; We load the score file.
585 (setq gnus-score-alist nil)
586 (setq alist (gnus-score-load-score-alist file))
587 ;; We add '(touched) to the alist to signify that it hasn't been
589 (or (assq 'touched alist) (setq alist (cons (list 'touched nil) alist)))
590 ;; If it is a global score file, we make it read-only.
592 (not (assq 'read-only alist))
593 (setq alist (cons (list 'read-only t) alist)))
594 (setq gnus-score-cache
595 (cons (cons file alist) gnus-score-cache)))
596 ;; If there are actual scores in the alist, we add it to the
597 ;; return value of this function.
598 (if (memq t (mapcar (lambda (e) (stringp (car e))) alist))
599 (setq lists (list alist)))
600 ;; Treat the other possible atoms in the score alist.
601 (let ((mark (car (gnus-score-get 'mark alist)))
602 (expunge (car (gnus-score-get 'expunge alist)))
603 (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
604 (files (gnus-score-get 'files alist))
605 (exclude-files (gnus-score-get 'exclude-files alist))
606 (orphan (car (gnus-score-get 'orphan alist)))
607 (adapt (gnus-score-get 'adapt alist))
608 (local (gnus-score-get 'local alist))
609 (eval (car (gnus-score-get 'eval alist))))
610 ;; We do not respect eval and files atoms from global score
612 (and files (not global)
613 (setq lists (apply 'append lists
614 (mapcar (lambda (file)
615 (gnus-score-load-file file))
617 (and eval (not global) (eval eval))
618 (setq gnus-scores-exclude-files
619 (append exclude-files gnus-scores-exclude-files))
623 (set-buffer gnus-summary-buffer)
625 (and (consp (car local))
626 (symbolp (car (car local)))
628 (make-local-variable (car (car local)))
629 (set (car (car local)) (nth 1 (car local)))))
630 (setq local (cdr local)))))
631 (if orphan (setq gnus-orphan-score orphan))
632 (setq gnus-adaptive-score-alist
633 (cond ((equal adapt '(t))
634 (setq gnus-newsgroup-adaptive t)
635 gnus-default-adaptive-score-alist)
636 ((equal adapt '(ignore))
637 (setq gnus-newsgroup-adaptive nil))
639 (setq gnus-newsgroup-adaptive t)
642 ;;(setq gnus-newsgroup-adaptive gnus-use-adaptive-scoring)
643 gnus-default-adaptive-score-alist)))
644 (setq gnus-summary-mark-below
645 (or mark mark-and-expunge gnus-summary-mark-below))
646 (setq gnus-summary-expunge-below
647 (or expunge mark-and-expunge gnus-summary-expunge-below)))
648 (setq gnus-current-score-file file)
649 (setq gnus-score-alist alist)
652 (defun gnus-score-load (file)
654 (let ((cache (assoc file gnus-score-cache)))
656 (setq gnus-score-alist (cdr cache))
657 (setq gnus-score-alist nil)
658 (gnus-score-load-score-alist file)
660 (setq gnus-score-alist (copy-alist '((touched nil)))))
661 (setq gnus-score-cache
662 (cons (cons file gnus-score-alist) gnus-score-cache)))))
664 (defun gnus-score-remove-from-cache (file)
665 (setq gnus-score-cache
666 (delq (assoc file gnus-score-cache) gnus-score-cache)))
668 (defun gnus-score-load-score-alist (file)
670 (if (file-readable-p file)
673 (gnus-set-work-buffer)
674 (insert-file-contents file)
675 (goto-char (point-min))
676 ;; Only do the loading if the score file isn't empty.
677 (if (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
680 (read (current-buffer))
683 (gnus-message 3 "Problem with score file %s" file)
687 (if (eq (car alist) 'setq)
688 (setq gnus-score-alist (gnus-score-transform-old-to-new alist))
689 (setq gnus-score-alist alist))
690 (setq gnus-score-alist
691 (gnus-score-check-syntax gnus-score-alist file)))
692 (setq gnus-score-alist nil))))
694 (defun gnus-score-check-syntax (alist file)
699 (gnus-message 1 "Score file is not a list: %s" file)
705 (while (and a (not err))
706 (cond ((not (listp (car a)))
707 (gnus-message 3 "Illegal score element %s in %s" (car a) file)
709 ((and (stringp (car (car a)))
710 (not (listp (nth 1 (car a)))))
711 (gnus-message 3 "Illegal header match %s in %s" (nth 1 (car a)) file)
721 (defun gnus-score-transform-old-to-new (alist)
722 (let* ((alist (nth 2 alist))
724 (if (eq (car alist) 'quote)
725 (setq alist (nth 1 alist)))
727 (setq entry (car alist))
728 (if (stringp (car entry))
729 (let ((scor (cdr entry)))
730 (setq out (cons entry out))
733 (list (car (car scor)) (nth 2 (car scor))
734 (and (nth 3 (car scor))
735 (gnus-day-number (nth 3 (car scor))))
736 (if (nth 1 (car scor)) 'r 's)))
737 (setq scor (cdr scor))))
738 (setq out (cons (if (not (listp (cdr entry)))
739 (list (car entry) (cdr entry))
742 (setq alist (cdr alist)))
743 (cons (list 'touched t) (nreverse out))))
745 (defun gnus-score-save ()
746 ;; Save all score information.
747 (let ((cache gnus-score-cache))
749 (setq gnus-score-alist nil)
750 (set-buffer (get-buffer-create "*Score*"))
751 (buffer-disable-undo (current-buffer))
752 (let (entry score file)
754 (setq entry (car cache)
758 (if (or (not (equal (gnus-score-get 'touched score) '(t)))
759 (gnus-score-get 'read-only score)
760 (and (file-exists-p file)
761 (not (file-writable-p file))))
763 (setq score (setcdr entry (delq (assq 'touched score) score)))
765 (let (emacs-lisp-mode-hook)
766 (if (string-match (concat gnus-adaptive-file-suffix "$") file)
767 ;; This is an adaptive score file, so we do not run
768 ;; it through `pp'. These files can get huge, and
769 ;; are not meant to be edited by human hands.
770 (insert (format "%S" score))
771 ;; This is a normal score file, so we print it very
773 (pp score (current-buffer))))
774 (if (not (gnus-make-directory (file-name-directory file)))
776 ;; If the score file is empty, we delete it.
777 (if (zerop (buffer-size))
779 ;; There are scores, so we write the file.
780 (and (file-writable-p file)
781 (write-region (point-min) (point-max)
783 (and gnus-score-uncacheable-files
784 (string-match gnus-score-uncacheable-files file)
785 (gnus-score-remove-from-cache file)))))
786 (kill-buffer (current-buffer)))))
788 (defun gnus-score-headers (score-files &optional trace)
789 ;; Score `gnus-newsgroup-headers'.
791 ;; PLM: probably this is not the best place to clear orphan-score
792 (setq gnus-orphan-score nil)
793 (setq gnus-scores-articles nil)
794 (setq gnus-scores-exclude-files nil)
795 ;; Load the score files.
797 (if (stringp (car score-files))
798 ;; It is a string, which means that it's a score file name,
799 ;; so we load the score file and add the score alist to
800 ;; the list of alists.
801 (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
802 ;; It is an alist, so we just add it to the list directly.
803 (setq scores (nconc (car score-files) scores)))
804 (setq score-files (cdr score-files)))
805 ;; Prune the score files that are to be excluded, if any.
806 (if (not gnus-scores-exclude-files)
811 (and (setq c (rassq (car s) gnus-score-cache))
812 (member (car c) gnus-scores-exclude-files)
813 (setq scores (delq (car s) scores)))
815 (if (not (and gnus-summary-default-score
817 (> (length gnus-newsgroup-headers)
818 (length gnus-newsgroup-scored))))
820 (let* ((entries gnus-header-index)
821 (now (gnus-day-number (current-time-string)))
822 (expire (- now gnus-score-expiry-days))
823 (headers gnus-newsgroup-headers)
824 (current-score-file gnus-current-score-file)
826 (gnus-message 5 "Scoring...")
827 ;; Create articles, an alist of the form `(HEADER . SCORE)'.
829 (setq header (car headers)
830 headers (cdr headers))
831 ;; WARNING: The assq makes the function O(N*S) while it could
832 ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
833 ;; and S is (length gnus-newsgroup-scored).
834 (or (assq (mail-header-number header) gnus-newsgroup-scored)
835 (setq gnus-scores-articles ;Total of 2 * N cons-cells used.
836 (cons (cons header (or gnus-summary-default-score 0))
837 gnus-scores-articles))))
840 (set-buffer (get-buffer-create "*Headers*"))
841 (buffer-disable-undo (current-buffer))
843 ;; Set the global variant of this variable.
844 (setq gnus-current-score-file current-score-file)
846 (if gnus-orphan-score
848 (setq gnus-score-index
849 (nth 1 (assoc "references" gnus-header-index)))
850 (gnus-score-orphans gnus-orphan-score)))
851 ;; Run each header through the score process.
853 (setq entry (car entries)
855 entries (cdr entries))
856 (setq gnus-score-index (nth 1 (assoc header gnus-header-index)))
857 (if (< 0 (apply 'max (mapcar
859 (length (gnus-score-get header score)))
861 (funcall (nth 2 entry) scores header now expire trace)))
862 ;; Remove the buffer.
863 (kill-buffer (current-buffer)))
865 ;; Add articles to `gnus-newsgroup-scored'.
866 (while gnus-scores-articles
867 (or (= gnus-summary-default-score (cdr (car gnus-scores-articles)))
868 (setq gnus-newsgroup-scored
869 (cons (cons (mail-header-number
870 (car (car gnus-scores-articles)))
871 (cdr (car gnus-scores-articles)))
872 gnus-newsgroup-scored)))
873 (setq gnus-scores-articles (cdr gnus-scores-articles)))
875 (gnus-message 5 "Scoring...done")))))
878 (defun gnus-get-new-thread-ids (articles)
879 (let ((index (nth 1 (assoc "message-id" gnus-header-index)))
880 (refind gnus-score-index)
881 id-list art this tref)
883 (setq art (car articles)
884 this (aref (car art) index)
885 tref (aref (car art) refind)
886 articles (cdr articles))
887 (if (string-equal tref "") ;no references line
888 (setq id-list (cons this id-list))))
891 ;; Orphan functions written by plm@atcmp.nl (Peter Mutsaers).
892 (defun gnus-score-orphans (score)
893 (let ((new-thread-ids (gnus-get-new-thread-ids gnus-scores-articles))
894 alike articles art arts this last this-id)
896 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
897 articles gnus-scores-articles)
899 ;;more or less the same as in gnus-score-string
902 (setq art (car articles)
903 this (aref (car art) gnus-score-index)
904 articles (cdr articles))
905 ;;completely skip if this is empty (not a child, so not an orphan)
906 (if (not (string= this ""))
907 (if (equal last this)
908 ;; O(N*H) cons-cells used here, where H is the number of
910 (setq alike (cons art alike))
913 ;; Insert the line, with a text property on the
914 ;; terminating newline refering to the articles with
917 (put-text-property (1- (point)) (point) 'articles alike)))
918 (setq alike (list art)
920 (and last ; Bwadr, duplicate code.
923 (put-text-property (1- (point)) (point) 'articles alike)))
925 ;; PLM: now delete those lines that contain an entry from new-thread-ids
926 (while new-thread-ids
927 (setq this-id (car new-thread-ids)
928 new-thread-ids (cdr new-thread-ids))
929 (goto-char (point-min))
930 (while (search-forward this-id nil t)
931 ;; found a match. remove this line
935 ;; now for each line: update its articles with score by moving to
936 ;; every end-of-line in the buffer and read the articles property
937 (goto-char (point-min))
940 (setq arts (get-text-property (point) 'articles))
944 (setcdr art (+ score (cdr art))))
948 (defun gnus-score-integer (scores header now expire &optional trace)
949 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
954 (setq alist (car scores)
956 entries (assoc header alist))
957 (while (cdr entries) ;First entry is the header index.
958 (let* ((rest (cdr entries))
961 (type (or (nth 3 kill) '>))
962 (score (or (nth 1 kill) gnus-score-interactive-default-score))
965 (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
966 (eq type '>=) (eq type '=))
968 (error "Illegal match type: %s" type)))
969 (articles gnus-scores-articles))
970 ;; Instead of doing all the clever stuff that
971 ;; `gnus-score-string' does to minimize searches and stuff,
972 ;; I will assume that people generally will put so few
973 ;; matches on numbers that any cleverness will take more
974 ;; time than one would gain.
976 (and (funcall match-func
977 (or (aref (car (car articles)) gnus-score-index) 0)
980 (and trace (setq gnus-score-trace
983 (car-safe (rassq alist gnus-score-cache))
987 (setcdr (car articles) (+ score (cdr (car articles))))))
988 (setq articles (cdr articles)))
989 ;; Update expire date
990 (cond ((null date)) ;Permanent entry.
991 (found ;Match, update date.
992 (gnus-score-set 'touched '(t) alist)
993 (setcar (nthcdr 2 kill) now))
994 ((< date expire) ;Old entry, remove.
995 (gnus-score-set 'touched '(t) alist)
996 (setcdr entries (cdr rest))
997 (setq rest entries)))
998 (setq entries rest))))))
1000 (defun gnus-score-date (scores header now expire &optional trace)
1001 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1006 (setq alist (car scores)
1008 entries (assoc header alist))
1009 (while (cdr entries) ;First entry is the header index.
1010 (let* ((rest (cdr entries))
1012 (match (timezone-make-date-sortable (nth 0 kill)))
1013 (type (or (nth 3 kill) 'before))
1014 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1018 (cond ((eq type 'after) 'string<)
1019 ((eq type 'before) 'gnus-string>)
1020 ((eq type 'at) 'string=)
1021 (t (error "Illegal match type: %s" type))))
1022 (articles gnus-scores-articles)
1024 ;; Instead of doing all the clever stuff that
1025 ;; `gnus-score-string' does to minimize searches and stuff,
1026 ;; I will assume that people generally will put so few
1027 ;; matches on numbers that any cleverness will take more
1028 ;; time than one would gain.
1031 (setq l (aref (car (car articles)) gnus-score-index))
1032 (funcall match-func match (timezone-make-date-sortable l))
1034 (and trace (setq gnus-score-trace
1037 (car-safe (rassq alist gnus-score-cache))
1041 (setcdr (car articles) (+ score (cdr (car articles))))))
1042 (setq articles (cdr articles)))
1043 ;; Update expire date
1044 (cond ((null date)) ;Permanent entry.
1045 (found ;Match, update date.
1046 (gnus-score-set 'touched '(t) alist)
1047 (setcar (nthcdr 2 kill) now))
1048 ((< date expire) ;Old entry, remove.
1049 (gnus-score-set 'touched '(t) alist)
1050 (setcdr entries (cdr rest))
1051 (setq rest entries)))
1052 (setq entries rest))))))
1054 (defun gnus-score-body (scores header now expire &optional trace)
1056 (set-buffer nntp-server-buffer)
1058 (let* ((buffer-read-only nil)
1059 (articles gnus-scores-articles)
1060 (last (mail-header-number (car (car gnus-scores-articles))))
1062 (request-func (cond ((string= "head" (downcase header))
1064 ((string= "body" (downcase header))
1066 (t 'gnus-request-article)))
1067 entries alist ofunc article)
1068 ;; Not all backends support partial fetching. In that case,
1069 ;; we just fetch the entire article.
1070 (or (gnus-check-backend-function
1071 (and (string-match "^gnus-" (symbol-name request-func))
1072 (intern (substring (symbol-name request-func)
1074 gnus-newsgroup-name)
1076 (setq ofunc request-func)
1077 (setq request-func 'gnus-request-article)))
1079 (setq article (mail-header-number (car (car articles))))
1080 (gnus-message 7 "Scoring on article %s of %s..." article last)
1081 (if (not (funcall request-func article gnus-newsgroup-name))
1084 (goto-char (point-min))
1085 ;; If just parts of the article is to be searched, but the
1086 ;; backend didn't support partial fetching, we just narrow
1087 ;; to the relevant parts.
1089 (if (eq ofunc 'gnus-request-head)
1092 (or (search-forward "\n\n" nil t) (point-max)))
1094 (or (search-forward "\n\n" nil t) (point))
1096 (setq scores all-scores)
1099 (setq alist (car scores)
1101 entries (assoc header alist))
1102 (while (cdr entries) ;First entry is the header index.
1103 (let* ((rest (cdr entries))
1105 (match (nth 0 kill))
1106 (type (or (nth 3 kill) 's))
1107 (score (or (nth 1 kill)
1108 gnus-score-interactive-default-score))
1112 (not (or (eq type 'R) (eq type 'S)
1113 (eq type 'Regexp) (eq type 'String))))
1115 (cond ((or (eq type 'r) (eq type 'R)
1116 (eq type 'regexp) (eq type 'Regexp))
1118 ((or (eq type 's) (eq type 'S)
1119 (eq type 'string) (eq type 'String))
1122 (error "Illegal match type: %s" type)))))
1123 (goto-char (point-min))
1124 (if (funcall search-func match nil t)
1125 ;; Found a match, update scores.
1127 (setcdr (car articles) (+ score (cdr (car articles))))
1129 (and trace (setq gnus-score-trace
1133 (rassq alist gnus-score-cache))
1135 gnus-score-trace)))))
1136 ;; Update expire date
1137 (cond ((null date)) ;Permanent entry.
1138 (found ;Match, update date.
1139 (gnus-score-set 'touched '(t) alist)
1140 (setcar (nthcdr 2 kill) now))
1141 ((< date expire) ;Old entry, remove.
1142 (gnus-score-set 'touched '(t) alist)
1143 (setcdr entries (cdr rest))
1144 (setq rest entries)))
1145 (setq entries rest)))))
1146 (setq articles (cdr articles)))))))
1150 (defun gnus-score-followup (scores header now expire &optional trace)
1151 ;; Insert the unique article headers in the buffer.
1152 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1153 (current-score-file gnus-current-score-file)
1155 ;; gnus-score-index is used as a free variable.
1156 alike last this art entries alist articles)
1158 ;; Change score file to the adaptive score file. All entries that
1159 ;; this function makes will be put into this file.
1160 (gnus-score-load-file (gnus-score-file-name
1161 gnus-newsgroup-name gnus-adaptive-file-suffix))
1163 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1164 articles gnus-scores-articles)
1168 (setq art (car articles)
1169 this (aref (car art) gnus-score-index)
1170 articles (cdr articles))
1171 (if (equal last this)
1172 (setq alike (cons art alike))
1176 (put-text-property (1- (point)) (point) 'articles alike)))
1177 (setq alike (list art)
1179 (and last ; Bwadr, duplicate code.
1182 (put-text-property (1- (point)) (point) 'articles alike)))
1186 (setq alist (car scores)
1188 entries (assoc header alist))
1189 (while (cdr entries) ;First entry is the header index.
1190 (let* ((rest (cdr entries))
1192 (match (nth 0 kill))
1193 (type (or (nth 3 kill) 's))
1194 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1197 (mt (aref (symbol-name type) 0))
1199 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1202 (cond ((= dmt ?r) 're-search-forward)
1203 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1204 (t (error "Illegal match type: %s" type))))
1206 (goto-char (point-min))
1208 (while (funcall search-func match nil t)
1209 (and (= (progn (beginning-of-line) (point))
1210 (match-beginning 0))
1211 (= (progn (end-of-line) (point))
1214 (setq found (setq arts (get-text-property
1215 (point) 'articles)))
1216 ;; Found a match, update scores.
1218 (setq art (car arts)
1220 (gnus-score-add-followups
1221 (car art) score all-scores)))))
1222 (while (funcall search-func match nil t)
1224 (setq found (setq arts (get-text-property (point) 'articles)))
1225 ;; Found a match, update scores.
1227 (setq art (car arts)
1229 (gnus-score-add-followups (car art) score all-scores))))
1230 ;; Update expire date
1231 (cond ((null date)) ;Permanent entry.
1232 (found ;Match, update date.
1233 (gnus-score-set 'touched '(t) alist)
1234 (setcar (nthcdr 2 kill) now))
1235 ((< date expire) ;Old entry, remove.
1236 (gnus-score-set 'touched '(t) alist)
1237 (setcdr entries (cdr rest))
1238 (setq rest entries)))
1239 (setq entries rest))))
1240 ;; We change the score file back to the previous one.
1241 (gnus-score-load-file current-score-file)))
1243 (defun gnus-score-add-followups (header score scores)
1245 (set-buffer gnus-summary-buffer)
1246 (let* ((id (mail-header-id header))
1247 (scores (car scores))
1249 ;; Don't enter a score if there already is one.
1251 (setq entry (car scores))
1252 (and (equal "references" (car entry))
1253 (or (null (nth 3 (car (cdr entry))))
1254 (eq 's (nth 3 (car (cdr entry)))))
1256 (if (assoc id entry)
1258 (setq scores (cdr scores)))
1260 (gnus-summary-score-entry
1261 "references" id 's score (current-time-string) nil t)))))
1264 (defun gnus-score-string (score-list header now expire &optional trace)
1265 ;; Score ARTICLES according to HEADER in SCORE-LIST.
1266 ;; Update matches entries to NOW and remove unmatched entried older
1269 ;; Insert the unique article headers in the buffer.
1270 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1271 ;; gnus-score-index is used as a free variable.
1272 alike last this art entries alist articles scores fuzzy)
1274 ;; Sorting the articles costs os O(N*log N) but will allow us to
1275 ;; only match with each unique header. Thus the actual matching
1276 ;; will be O(M*U) where M is the number of strings to match with,
1277 ;; and U is the number of unique headers. It is assumed (but
1278 ;; untested) this will be a net win because of the large constant
1279 ;; factor involved with string matching.
1280 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1281 articles gnus-scores-articles)
1285 (setq art (car articles)
1286 this (aref (car art) gnus-score-index)
1287 articles (cdr articles))
1288 (if (equal last this)
1289 ;; O(N*H) cons-cells used here, where H is the number of
1291 (setq alike (cons art alike))
1294 ;; Insert the line, with a text property on the
1295 ;; terminating newline refering to the articles with
1298 (put-text-property (1- (point)) (point) 'articles alike)))
1299 (setq alike (list art)
1301 (and last ; Bwadr, duplicate code.
1304 (put-text-property (1- (point)) (point) 'articles alike)))
1306 ;; Find ordinary matches.
1307 (setq scores score-list)
1309 (setq alist (car scores)
1311 entries (assoc header alist))
1312 (while (cdr entries) ;First entry is the header index.
1313 (let* ((rest (cdr entries))
1315 (match (nth 0 kill))
1316 (type (or (nth 3 kill) 's))
1317 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1320 (mt (aref (symbol-name type) 0))
1322 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1325 (cond ((= dmt ?r) 're-search-forward)
1326 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1327 (t (error "Illegal match type: %s" type))))
1331 ;; Do non-fuzzy matching.
1332 (goto-char (point-min))
1334 ;; Do exact matching.
1335 (while (and (not (eobp))
1336 (funcall search-func match nil t))
1337 (and (= (progn (beginning-of-line) (point))
1338 (match-beginning 0))
1339 (= (progn (end-of-line) (point))
1342 (setq found (setq arts (get-text-property
1343 (point) 'articles)))
1344 ;; Found a match, update scores.
1347 (setq art (car arts)
1349 (setcdr art (+ score (cdr art)))
1350 (setq gnus-score-trace
1354 (rassq alist gnus-score-cache))
1358 (setq art (car arts)
1360 (setcdr art (+ score (cdr art)))))))
1362 ;; Do regexp and substring matching.
1363 (and (string= match "") (setq match "\n"))
1364 (while (and (not (eobp))
1365 (funcall search-func match nil t))
1366 (goto-char (match-beginning 0))
1368 (setq found (setq arts (get-text-property (point) 'articles)))
1369 ;; Found a match, update scores.
1372 (setq art (car arts)
1374 (setcdr art (+ score (cdr art)))
1375 (setq gnus-score-trace
1379 (rassq alist gnus-score-cache))
1383 (setq art (car arts)
1385 (setcdr art (+ score (cdr art)))))
1387 ;; Update expire date
1388 (cond ((null date)) ;Permanent entry.
1389 (found ;Match, update date.
1390 (gnus-score-set 'touched '(t) alist)
1391 (setcar (nthcdr 2 kill) now))
1392 ((< date expire) ;Old entry, remove.
1393 (gnus-score-set 'touched '(t) alist)
1394 (setcdr entries (cdr rest))
1395 (setq rest entries))))
1396 (setq entries rest))))
1398 ;; Find fuzzy matches.
1399 (setq scores (and fuzzy score-list))
1400 (if fuzzy (gnus-simplify-buffer-fuzzy))
1402 (setq alist (car scores)
1404 entries (assoc header alist))
1405 (while (cdr entries) ;First entry is the header index.
1406 (let* ((rest (cdr entries))
1408 (match (nth 0 kill))
1409 (type (or (nth 3 kill) 's))
1410 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1413 (mt (aref (symbol-name type) 0))
1415 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1418 (cond ((= dmt ?r) 're-search-forward)
1419 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1420 (t (error "Illegal match type: %s" type))))
1424 (goto-char (point-min))
1425 (while (and (not (eobp))
1426 (funcall search-func match nil t))
1427 (and (= (progn (beginning-of-line) (point))
1428 (match-beginning 0))
1429 (= (progn (end-of-line) (point))
1432 (setq found (setq arts (get-text-property
1433 (point) 'articles)))
1434 ;; Found a match, update scores.
1437 (setq art (car arts)
1439 (setcdr art (+ score (cdr art)))
1440 (setq gnus-score-trace
1444 (rassq alist gnus-score-cache))
1448 (setq art (car arts)
1450 (setcdr art (+ score (cdr art)))))))
1452 ;; Update expire date
1453 (cond ((null date)) ;Permanent entry.
1454 (found ;Match, update date.
1455 (gnus-score-set 'touched '(t) alist)
1456 (setcar (nthcdr 2 kill) now))
1457 ((< date expire) ;Old entry, remove.
1458 (gnus-score-set 'touched '(t) alist)
1459 (setcdr entries (cdr rest))
1460 (setq rest entries))))
1461 (setq entries rest))))))
1463 (defun gnus-score-string< (a1 a2)
1464 ;; Compare headers in articles A2 and A2.
1465 ;; The header index used is the free variable `gnus-score-index'.
1466 (string-lessp (aref (car a1) gnus-score-index)
1467 (aref (car a2) gnus-score-index)))
1469 (defun gnus-score-build-cons (article)
1470 ;; Build a `gnus-newsgroup-scored' type cons from ARTICLE.
1471 (cons (mail-header-number (car article)) (cdr article)))
1473 (defconst gnus-header-index
1474 ;; Name to index alist.
1475 '(("number" 0 gnus-score-integer)
1476 ("subject" 1 gnus-score-string)
1477 ("from" 2 gnus-score-string)
1478 ("date" 3 gnus-score-date)
1479 ("message-id" 4 gnus-score-string)
1480 ("references" 5 gnus-score-string)
1481 ("chars" 6 gnus-score-integer)
1482 ("lines" 7 gnus-score-integer)
1483 ("xref" 8 gnus-score-string)
1484 ("head" -1 gnus-score-body)
1485 ("body" -1 gnus-score-body)
1486 ("all" -1 gnus-score-body)
1487 ("followup" 2 gnus-score-followup)))
1489 (defun gnus-current-score-file-nondirectory (&optional score-file)
1490 (let ((score-file (or score-file gnus-current-score-file)))
1492 (gnus-short-group-name (file-name-nondirectory score-file))
1495 (defun gnus-score-adaptive ()
1497 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
1499 (date (current-time-string))
1500 (data gnus-newsgroup-data)
1502 ;; First we transform the adaptive rule alist into something
1503 ;; that's faster to process.
1505 (setq elem (car malist))
1506 (if (symbolp (car elem))
1507 (setcar elem (symbol-value (car elem))))
1508 (setq elem (cdr elem))
1511 (cons (if (eq (car (car elem)) 'followup)
1513 (symbol-name (car (car elem))))
1517 (concat "gnus-header-"
1518 (if (eq (car (car elem)) 'followup)
1520 (downcase (symbol-name (car (car elem))))))))
1521 (setq elem (cdr elem)))
1522 (setq malist (cdr malist)))
1523 ;; We change the score file to the adaptive score file.
1524 (gnus-score-load-file (gnus-score-file-name
1525 gnus-newsgroup-name gnus-adaptive-file-suffix))
1526 ;; The we score away.
1528 (setq elem (cdr (assq (gnus-data-mark (car data)) alist)))
1530 (gnus-data-pseudo-p (car data)))
1532 (when (setq headers (gnus-data-header (car data)))
1534 (setq match (funcall (car (car elem)) headers))
1535 (gnus-summary-score-entry
1536 (nth 1 (car elem)) match
1540 ((equal (nth 1 (car elem)) "date")
1543 ;; Whether we use substring or exact matches are controlled
1545 (if (or (not gnus-score-exact-adapt-limit)
1546 (< (length match) gnus-score-exact-adapt-limit))
1548 (if (equal (nth 1 (car elem)) "subject")
1550 (nth 2 (car elem)) date nil t)
1551 (setq elem (cdr elem)))))
1552 (setq data (cdr data))))))
1558 (defvar gnus-score-mode-map nil)
1559 (defvar gnus-score-mode-hook nil)
1561 (if gnus-score-mode-map
1563 (setq gnus-score-mode-map (copy-keymap emacs-lisp-mode-map))
1564 (define-key gnus-score-mode-map "\C-c\C-c" 'gnus-score-edit-done)
1565 (define-key gnus-score-mode-map "\C-c\C-d" 'gnus-score-edit-insert-date))
1567 (defun gnus-score-mode ()
1568 "Mode for editing score files.
1569 This mode is an extended emacs-lisp mode.
1571 \\{gnus-score-mode-map}"
1573 (kill-all-local-variables)
1574 (use-local-map gnus-score-mode-map)
1575 (set-syntax-table emacs-lisp-mode-syntax-table)
1576 (setq major-mode 'gnus-score-mode)
1577 (setq mode-name "Score")
1578 (lisp-mode-variables nil)
1579 (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
1581 (defun gnus-score-edit-insert-date ()
1582 "Insert date in numerical format."
1584 (insert (int-to-string (gnus-day-number (current-time-string)))))
1586 (defun gnus-score-edit-done ()
1587 "Save the score file and return to the summary buffer."
1589 (let ((bufnam (buffer-file-name (current-buffer)))
1590 (winconf gnus-prev-winconf))
1591 (gnus-make-directory (file-name-directory (buffer-file-name)))
1593 (kill-buffer (current-buffer))
1594 (gnus-score-remove-from-cache bufnam)
1595 (gnus-score-load-file bufnam)
1596 (and winconf (set-window-configuration winconf))))
1598 (defun gnus-score-find-trace ()
1599 "Find all score rules applied to this article."
1601 (let ((gnus-newsgroup-headers
1602 (list (gnus-summary-article-header)))
1603 (gnus-newsgroup-scored nil)
1604 (buf (current-buffer))
1606 (setq gnus-score-trace nil)
1607 (gnus-possibly-score-headers 'trace)
1608 (or (setq trace gnus-score-trace)
1609 (error "No score rules apply to the current article."))
1610 (pop-to-buffer "*Gnus Scores*")
1611 (gnus-add-current-to-buffer-list)
1614 (insert (format "%S -> %s\n" (cdr (car trace))
1615 (file-name-nondirectory (car (car trace)))))
1616 (setq trace (cdr trace)))
1617 (goto-char (point-min))
1618 (pop-to-buffer buf)))
1620 (defun gnus-score-flush-cache ()
1621 "Flush the cache of score files."
1623 (setq gnus-score-cache nil)
1624 (gnus-message 6 "The score cache is now flushed"))
1626 (defun gnus-score-close ()
1627 "Clear all internal score variables."
1628 (setq gnus-score-cache nil
1629 gnus-internal-global-score-files nil))
1631 (provide 'gnus-score)
1633 ;;; gnus-score.el ends here