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.")
59 ;; Internal variables.
61 (defvar gnus-score-help-winconf nil)
62 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
63 (defvar gnus-score-trace nil)
64 (defvar gnus-score-edit-buffer nil)
66 (defvar gnus-score-alist nil
67 "Alist containing score information.
68 The keys can be symbols or strings. The following symbols are defined.
70 touched: If this alist has been modified.
71 mark: Automatically mark articles below this.
72 expunge: Automatically expunge articles below this.
73 files: List of other score files to load when loading this one.
74 eval: Sexp to be evaluated when the score file is loaded.
76 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
77 where HEADER is the header being scored, MATCH is the string we are
78 looking for, TYPE is a flag indicating whether it should use regexp or
79 substring matching, SCORE is the score to add and DATE is the date
80 of the last successful match.")
82 (defvar gnus-score-cache nil)
83 (defvar gnus-scores-articles nil)
84 (defvar gnus-scores-exclude-files nil)
85 (defvar gnus-header-index nil)
86 (defvar gnus-score-index nil)
89 (autoload 'gnus-uu-ctl-map "gnus-uu" nil nil 'keymap)
90 (autoload 'appt-select-lowest-window "appt.el"))
92 ;;; Summary mode score maps.
94 (defvar gnus-summary-score-map nil)
96 (define-prefix-command 'gnus-summary-score-map)
97 (define-key gnus-summary-mode-map "V" 'gnus-summary-score-map)
98 (define-key gnus-summary-score-map "s" 'gnus-summary-set-score)
99 (define-key gnus-summary-score-map "a" 'gnus-summary-score-entry)
100 (define-key gnus-summary-score-map "S" 'gnus-summary-current-score)
101 (define-key gnus-summary-score-map "c" 'gnus-score-change-score-file)
102 (define-key gnus-summary-score-map "m" 'gnus-score-set-mark-below)
103 (define-key gnus-summary-score-map "x" 'gnus-score-set-expunge-below)
104 (define-key gnus-summary-score-map "e" 'gnus-score-edit-alist)
105 (define-key gnus-summary-score-map "f" 'gnus-score-edit-file)
106 (define-key gnus-summary-score-map "t" 'gnus-score-find-trace)
107 (define-key gnus-summary-score-map "C" 'gnus-score-customize)
111 ;; Summary score file commands
113 ;; Much modification of the kill (ahem, score) code and lots of the
114 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
116 (defun gnus-summary-lower-score (&optional score)
117 "Make a score entry based on the current article.
118 The user will be prompted for header to score on, match type,
119 permanence, and the string to be used. The numerical prefix will be
122 (gnus-summary-increase-score (- (gnus-score-default score))))
124 (defun gnus-summary-increase-score (&optional score)
125 "Make a score entry based on the current article.
126 The user will be prompted for header to score on, match type,
127 permanence, and the string to be used. The numerical prefix will be
130 (gnus-set-global-variables)
131 (let* ((nscore (gnus-score-default score))
132 (prefix (if (< nscore 0) ?L ?I))
133 (increase (> nscore 0))
135 '((?a "from" nil nil string)
136 (?s "subject" nil nil string)
137 (?b "body" "" nil body-string)
138 (?h "head" "" nil body-string)
139 (?i "message-id" nil t string)
140 (?t "references" "message-id" t string)
141 (?x "xref" nil nil string)
142 (?l "lines" nil nil number)
143 (?d "date" nil nil date)
144 (?f "followup" nil nil string)))
146 '((?s s "substring" string)
147 (?e e "exact string" string)
148 (?f f "fuzzy string" string)
149 (?r r "regexp string" string)
150 (?s s "substring" body-string)
151 (?r s "regexp string" body-string)
152 (?b before "before date" date)
153 (?a at "at date" date)
154 (?n now "this date" date)
155 (?< < "less than number" number)
156 (?> > "greater than number" number)
157 (?= = "equal to number" number)))
159 (list (list ?t (current-time-string) "temporary")
160 '(?p perm "permanent") '(?i now "immediate")))
161 (mimic gnus-score-mimic-keymap)
162 hchar entry temporary tchar pchar end type)
163 ;; First we read the header to score.
168 (message "%c-" prefix))
169 (message "%s header (%s?): " (if increase "Increase" "Lower")
170 (mapconcat (lambda (s) (char-to-string (car s)))
172 (setq hchar (read-char))
173 (if (not (or (= hchar ??) (= hchar ?\C-h)))
176 (gnus-score-insert-help "Match on header" char-to-header 1)))
178 (and (get-buffer "*Score Help*")
180 (kill-buffer "*Score Help*")
181 (and gnus-score-help-winconf
182 (set-window-configuration gnus-score-help-winconf))))
184 (or (setq entry (assq (downcase hchar) char-to-header))
188 (if mimic (message "%c %c" prefix hchar) (message ""))))
189 (if (or end (/= (downcase hchar) hchar))
191 ;; This was a majuscle, so we end reading and set the defaults.
192 (if mimic (message "%c %c" prefix hchar) (message ""))
194 temporary (current-time-string)))
196 ;; We continue reading - the type.
201 (message "%c %c-" prefix hchar))
202 (message "%s header '%s' with match type (%s?): "
203 (if increase "Increase" "Lower")
205 (mapconcat (lambda (s)
206 (if (eq (nth 4 entry)
208 (char-to-string (car s))
211 (setq tchar (read-char))
212 (if (not (or (= tchar ??) (= tchar ?\C-h)))
215 (gnus-score-insert-help "Match type" char-to-type 2)))
217 (and (get-buffer "*Score Help*")
219 (and gnus-score-help-winconf
220 (set-window-configuration gnus-score-help-winconf))
221 (kill-buffer "*Score Help*")))
223 (or (setq type (nth 1 (assq (downcase tchar) char-to-type)))
226 (if mimic (message "%c %c" prefix hchar) (message ""))
228 (if (or end (/= (downcase tchar) tchar))
230 ;; It was a majuscle, so we end reading and the the default.
231 (if mimic (message "%c %c %c" prefix hchar tchar)
233 (setq temporary (current-time-string)))
235 ;; We continue reading.
240 (message "%c %c %c-" prefix hchar tchar))
241 (message "%s permanence (%s?): " (if increase "Increase" "Lower")
242 (mapconcat (lambda (s) (char-to-string (car s)))
244 (setq pchar (read-char))
245 (if (not (or (= pchar ??) (= pchar ?\C-h)))
248 (gnus-score-insert-help "Match permanence" char-to-perm 2)))
250 (and (get-buffer "*Score Help*")
252 (and gnus-score-help-winconf
253 (set-window-configuration gnus-score-help-winconf))
254 (kill-buffer "*Score Help*")))
256 (if mimic (message "%c %c %c" prefix hchar tchar pchar)
258 (if (setq temporary (nth 1 (assq pchar char-to-perm)))
263 (message "%c %c %c %c" prefix hchar tchar pchar)
266 ;; We have all the data, so we enter this score.
269 (gnus-summary-score-entry
270 (nth 1 entry) ; Header
271 (if (string= (nth 2 entry) "") ""
272 (gnus-summary-header (or (nth 2 entry) (nth 1 entry)))) ; Match
274 (if (eq 's score) nil score) ; Score
275 (if (eq 'perm temporary) ; Temp
278 (not (nth 3 entry))) ; Prompt
281 (defun gnus-score-insert-help (string alist idx)
282 (setq gnus-score-help-winconf (current-window-configuration))
284 (set-buffer (get-buffer-create "*Score Help*"))
285 (buffer-disable-undo (current-buffer))
287 (insert string ":\n\n")
292 ;; find the longest string to display
294 (setq n (length (nth idx (car list))))
297 (setq list (cdr list)))
298 (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
299 (setq n (/ (window-width) max)) ; items per line
300 (setq width (/ (window-width) n)) ; width of each item
301 ;; insert `n' items, each in a field of width `width'
306 (delete-char -1) ; the `\n' takes a char
308 (setq pad (- width 3))
309 (setq format (concat "%c: %-" (int-to-string pad) "s"))
310 (insert (format format (car (car alist)) (nth idx (car alist))))
311 (setq alist (cdr alist))
313 ;; display ourselves in a small window at the bottom
314 (appt-select-lowest-window)
316 (pop-to-buffer "*Score Help*")
317 (shrink-window-if-larger-than-buffer)
318 (select-window (get-buffer-window gnus-summary-buffer))))
320 (defun gnus-summary-header (header &optional no-err)
321 ;; Return HEADER for current articles, or error.
322 (let ((article (gnus-summary-article-number))
325 (if (setq headers (gnus-get-header-by-number article))
326 (aref headers (nth 1 (assoc header gnus-header-index)))
329 (error "Pseudo-articles can't be scored")))
331 (error "No article on current line")
334 (defun gnus-summary-score-entry
335 (header match type score date &optional prompt silent)
336 "Enter score file entry.
337 HEADER is the header being scored.
338 MATCH is the string we are looking for.
339 TYPE is the match type: substring, regexp, exact, fuzzy.
340 SCORE is the score to add.
341 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
342 If optional argument `PROMPT' is non-nil, allow user to edit match.
343 If optional argument `SILENT' is nil, show effect of score entry."
345 (list (completing-read "Header: "
347 (lambda (x) (fboundp (nth 2 x)))
349 (read-string "Match: ")
350 (if (y-or-n-p "Use regexp match? ") 'r 's)
351 (and current-prefix-arg
352 (prefix-numeric-value current-prefix-arg))
353 (cond ((not (y-or-n-p "Add to score file? "))
355 ((y-or-n-p "Expire kill? ")
356 (current-time-string))
358 ;; Regexp is the default type.
359 (if (eq type t) (setq type 'r))
360 ;; Simplify matches...
361 (if (or (eq type 'r) (eq type 's) (eq type nil))
362 (setq match (gnus-simplify-subject-re match)))
363 (let ((score (gnus-score-default score))
364 (header (downcase header)))
365 (and prompt (setq match (read-string
366 (format "Match %s on %s, %s: "
367 (cond ((eq date 'now)
373 (if (< score 0) "lower" "raise"))
375 (int-to-string match)
377 (and (>= (nth 1 (assoc header gnus-header-index)) 0)
378 (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-string)
380 (gnus-summary-score-effect header match type score))
382 ;; If this is an integer comparison, we transform from string to int.
383 (and (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
384 (setq match (string-to-int match)))
388 (and (= score gnus-score-interactive-default-score)
392 (list (gnus-simplify-subject-fuzzy match)
393 score (and date (gnus-day-number date)) type))
395 (list match score (and date (gnus-day-number date)) type))
397 (list match score (gnus-day-number date)))
402 (old (gnus-score-get header))
404 ;; We see whether we can collapse some score entries.
405 ;; This isn't quite correct, because there may be more elements
406 ;; later on with the same key that have matching elems... Hm.
408 (setq elem (assoc match old))
409 (eq (nth 3 elem) (nth 3 new))
410 (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
411 (and (not (nth 2 elem)) (not (nth 2 new)))))
412 ;; Yup, we just add this new score to the old elem.
413 (setcar (cdr elem) (+ (or (nth 1 elem)
414 gnus-score-interactive-default-score)
416 gnus-score-interactive-default-score)))
417 ;; Nope, we have to add a new elem.
418 (gnus-score-set header (if old (cons new old) (list new)))))
419 (gnus-score-set 'touched '(t)))))
421 (defun gnus-summary-score-effect (header match type score)
422 "Simulate the effect of a score file entry.
423 HEADER is the header being scored.
424 MATCH is the string we are looking for.
425 TYPE is a flag indicating if it is a regexp or substring.
426 SCORE is the score to add."
427 (interactive (list (completing-read "Header: "
429 (lambda (x) (fboundp (nth 2 x)))
431 (read-string "Match: ")
432 (y-or-n-p "Use regexp match? ")
433 (prefix-numeric-value current-prefix-arg)))
435 (or (and (stringp match) (> (length match) 0))
437 (goto-char (point-min))
438 (let ((regexp (cond ((eq type 'f)
439 (gnus-simplify-subject-fuzzy match))
441 (t (concat "\\`.*" (regexp-quote match) ".*\\'")))))
443 (let ((content (gnus-summary-header header 'noerr))
444 (case-fold-search t))
447 (string-equal (gnus-simplify-subject-fuzzy content)
449 (string-match regexp content))
450 (gnus-summary-raise-score score))))
451 (beginning-of-line 2)))))
453 (defun gnus-summary-score-crossposting (score date)
454 ;; Enter score file entry for current crossposting.
455 ;; SCORE is the score to add.
456 ;; DATE is the expire date.
457 (let ((xref (gnus-summary-header "xref"))
460 (or xref (error "This article is not crossposted"))
461 (while (string-match " \\([^ \t]+\\):" xref start)
462 (setq start (match-end 0))
465 (substring xref (match-beginning 1) (match-end 1)))
466 gnus-newsgroup-name))
467 (gnus-summary-score-entry
468 "xref" (concat " " group ":") nil score date t)))))
475 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
477 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
478 (defun gnus-score-set-mark-below (score)
479 "Automatically mark articles with score below SCORE as read."
481 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
482 (string-to-int (read-string "Mark below: ")))))
483 (setq score (or score gnus-summary-default-score 0))
484 (gnus-score-set 'mark (list score))
485 (gnus-score-set 'touched '(t))
486 (setq gnus-summary-mark-below score)
487 (gnus-summary-update-lines))
489 (defun gnus-score-set-expunge-below (score)
490 "Automatically expunge articles with score below SCORE."
492 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
493 (string-to-int (read-string "Expunge below: ")))))
494 (setq score (or score gnus-summary-default-score 0))
495 (gnus-score-set 'expunge (list score))
496 (gnus-score-set 'touched '(t)))
498 (defun gnus-score-set (symbol value &optional alist)
499 ;; Set SYMBOL to VALUE in ALIST.
504 (gnus-score-load (gnus-score-file-name gnus-newsgroup-name))
506 (entry (assoc symbol alist)))
507 (cond ((gnus-score-get 'read-only alist)
508 ;; This is a read-only score file, so we do nothing.
511 (setcdr entry value))
513 (error "Empty alist"))
516 (cons (cons symbol value) (cdr alist)))))))
518 (defun gnus-score-get (symbol &optional alist)
519 ;; Get SYMBOL's definition in ALIST.
525 (gnus-score-file-name gnus-newsgroup-name))
526 gnus-score-alist)))))
528 (defun gnus-score-change-score-file (file)
529 "Change current score alist."
531 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
532 (gnus-score-load-file file)
533 (gnus-set-mode-line 'summary))
535 (defun gnus-score-edit-alist (file)
536 "Edit the current score alist."
537 (interactive (list gnus-current-score-file))
538 (let ((winconf (current-window-configuration)))
539 (and (buffer-name gnus-summary-buffer) (gnus-score-save))
540 (setq gnus-score-edit-buffer (find-file-noselect file))
541 (gnus-configure-windows 'edit-score)
543 (make-local-variable 'gnus-prev-winconf)
544 (setq gnus-prev-winconf winconf))
546 4 (substitute-command-keys
547 "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits")))
549 (defun gnus-score-edit-file (file)
552 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
553 (and (buffer-name gnus-summary-buffer) (gnus-score-save))
554 (let ((winconf (current-window-configuration)))
555 (setq gnus-score-edit-buffer (find-file-noselect file))
556 (gnus-configure-windows 'edit-score)
558 (make-local-variable 'gnus-prev-winconf)
559 (setq gnus-prev-winconf winconf))
561 4 (substitute-command-keys
562 "\\<gnus-score-mode-map>\\[gnus-score-edit-done] to save edits")))
564 (defun gnus-score-load-file (file)
565 ;; Load score file FILE. Returns a list a retrieved score-alists.
566 (setq gnus-kill-files-directory (or gnus-kill-files-directory "~/News/"))
567 (let* ((file (expand-file-name
568 (or (and (string-match
569 (concat "^" (expand-file-name
570 gnus-kill-files-directory))
571 (expand-file-name file))
573 (concat gnus-kill-files-directory file))))
574 (cached (assoc file gnus-score-cache))
575 (global (member file gnus-internal-global-score-files))
578 ;; The score file was already loaded.
579 (setq alist (cdr cached))
580 ;; We load the score file.
581 (setq gnus-score-alist nil)
582 (setq alist (gnus-score-load-score-alist file))
583 ;; We add '(touched) to the alist to signify that it hasn't been
585 (or (assq 'touched alist) (setq alist (cons (list 'touched nil) alist)))
586 ;; If it is a global score file, we make it read-only.
588 (not (assq 'read-only alist))
589 (setq alist (cons (list 'read-only t) alist)))
591 (setq gnus-score-cache
592 (cons (cons file alist) gnus-score-cache)))
593 ;; If there are actual scores in the alist, we add it to the
594 ;; return value of this function.
595 (if (memq t (mapcar (lambda (e) (stringp (car e))) alist))
596 (setq lists (list alist)))
597 ;; Treat the other possible atoms in the score alist.
598 (let ((mark (car (gnus-score-get 'mark alist)))
599 (expunge (car (gnus-score-get 'expunge alist)))
600 (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
601 (files (gnus-score-get 'files alist))
602 (exclude-files (gnus-score-get 'exclude-files alist))
603 (orphan (car (gnus-score-get 'orphan alist)))
604 (adapt (gnus-score-get 'adapt alist))
605 (local (gnus-score-get 'local alist))
606 (eval (car (gnus-score-get 'eval alist))))
607 ;; We do not respect eval and files atoms from global score
609 (and files (not global)
610 (setq lists (apply 'append lists
611 (mapcar (lambda (file)
612 (gnus-score-load-file file))
614 (and eval (not global) (eval eval))
615 (setq gnus-scores-exclude-files exclude-files)
619 (set-buffer gnus-summary-buffer)
621 (and (consp (car local))
622 (symbolp (car (car local)))
624 (make-local-variable (car (car local)))
625 (set (car (car local)) (nth 1 (car local)))))
626 (setq local (cdr local)))))
627 (if orphan (setq gnus-orphan-score orphan))
628 (setq gnus-adaptive-score-alist
629 (cond ((equal adapt '(t))
630 (setq gnus-newsgroup-adaptive t)
631 gnus-default-adaptive-score-alist)
632 ((equal adapt '(ignore))
633 (setq gnus-newsgroup-adaptive nil))
635 (setq gnus-newsgroup-adaptive t)
638 ;;(setq gnus-newsgroup-adaptive gnus-use-adaptive-scoring)
639 gnus-default-adaptive-score-alist)))
640 (setq gnus-summary-mark-below
641 (or mark mark-and-expunge gnus-summary-mark-below))
642 (setq gnus-summary-expunge-below
643 (or expunge mark-and-expunge gnus-summary-expunge-below)))
644 (setq gnus-current-score-file file)
645 (setq gnus-score-alist alist)
648 (defun gnus-score-load (file)
650 (let ((cache (assoc file gnus-score-cache)))
652 (setq gnus-score-alist (cdr cache))
653 (setq gnus-score-alist nil)
654 (gnus-score-load-score-alist file)
656 (setq gnus-score-alist (copy-alist '((touched nil)))))
657 (setq gnus-score-cache
658 (cons (cons file gnus-score-alist) gnus-score-cache)))))
660 (defun gnus-score-remove-from-cache (file)
661 (setq gnus-score-cache
662 (delq (assoc file gnus-score-cache) gnus-score-cache)))
664 (defun gnus-score-load-score-alist (file)
666 (if (file-readable-p file)
669 (gnus-set-work-buffer)
670 (insert-file-contents file)
671 (goto-char (point-min))
672 ;; Only do the loading if the score file isn't empty.
673 (if (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
676 (read (current-buffer))
679 (gnus-message 3 "Problem with score file %s" file)
683 (if (eq (car alist) 'setq)
684 (setq gnus-score-alist (gnus-score-transform-old-to-new alist))
685 (setq gnus-score-alist alist))
686 (setq gnus-score-alist
687 (gnus-score-check-syntax gnus-score-alist file)))
688 (setq gnus-score-alist nil))))
690 (defun gnus-score-check-syntax (alist file)
695 (gnus-message 1 "Score file is not a list: %s" file)
701 (while (and a (not err))
702 (cond ((not (listp (car a)))
703 (gnus-message 3 "Illegal score element %s in %s" (car a) file)
705 ((and (stringp (car (car a)))
706 (not (listp (nth 1 (car a)))))
707 (gnus-message 3 "Illegal header match %s in %s" (nth 1 (car a)) file)
717 (defun gnus-score-transform-old-to-new (alist)
718 (let* ((alist (nth 2 alist))
720 (if (eq (car alist) 'quote)
721 (setq alist (nth 1 alist)))
723 (setq entry (car alist))
724 (if (stringp (car entry))
725 (let ((scor (cdr entry)))
726 (setq out (cons entry out))
729 (list (car (car scor)) (nth 2 (car scor))
730 (and (nth 3 (car scor))
731 (gnus-day-number (nth 3 (car scor))))
732 (if (nth 1 (car scor)) 'r 's)))
733 (setq scor (cdr scor))))
734 (setq out (cons (if (not (listp (cdr entry)))
735 (list (car entry) (cdr entry))
738 (setq alist (cdr alist)))
739 (cons (list 'touched t) (nreverse out))))
741 (defun gnus-score-save ()
742 ;; Save all score information.
743 (let ((cache gnus-score-cache))
745 (setq gnus-score-alist nil)
746 (set-buffer (get-buffer-create "*Score*"))
747 (buffer-disable-undo (current-buffer))
748 (let (entry score file)
750 (setq entry (car cache)
754 (if (or (not (equal (gnus-score-get 'touched score) '(t)))
755 (gnus-score-get 'read-only score)
756 (and (file-exists-p file)
757 (not (file-writable-p file))))
759 (setq score (setcdr entry (delq (assq 'touched score) score)))
761 (let (emacs-lisp-mode-hook)
762 (if (string-match (concat gnus-adaptive-file-suffix "$") file)
763 ;; This is an adaptive score file, so we do not run
764 ;; it through `pp'. These files can get huge, and
765 ;; are not meant to be edited by human hands.
766 (insert (format "%S" score))
767 ;; This is a normal score file, so we print it very
769 (pp score (current-buffer))))
770 (if (not (gnus-make-directory (file-name-directory file)))
772 ;; If the score file is empty, we delete it.
773 (if (zerop (buffer-size))
775 ;; There are scores, so we write the file.
776 (write-region (point-min) (point-max) file nil 'silent))))))
777 (kill-buffer (current-buffer)))))
779 (defun gnus-score-headers (score-files &optional trace)
780 ;; Score `gnus-newsgroup-headers'.
782 ;; PLM: probably this is not the best place to clear orphan-score
783 (setq gnus-orphan-score nil)
784 ;; Load the score files.
786 (if (stringp (car score-files))
787 ;; It is a string, which means that it's a score file name,
788 ;; so we load the score file and add the score alist to
789 ;; the list of alists.
790 (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
791 ;; It is an alist, so we just add it to the list directly.
792 (setq scores (nconc (car score-files) scores)))
793 (setq score-files (cdr score-files)))
794 ;; Prune the score files that are to be excluded, if any.
795 (if (not gnus-scores-exclude-files)
800 (and (setq c (rassq (car s) gnus-score-cache))
801 (member (car c) gnus-scores-exclude-files)
802 (setq scores (delq (car s) scores)))
804 (if (not (and gnus-summary-default-score
806 (> (length gnus-newsgroup-headers)
807 (length gnus-newsgroup-scored))))
809 (let* ((entries gnus-header-index)
810 (now (gnus-day-number (current-time-string)))
811 (expire (- now gnus-score-expiry-days))
812 (headers gnus-newsgroup-headers)
813 (current-score-file gnus-current-score-file)
815 (gnus-message 5 "Scoring...")
816 ;; Create articles, an alist of the form `(HEADER . SCORE)'.
818 (setq header (car headers)
819 headers (cdr headers))
820 ;; WARNING: The assq makes the function O(N*S) while it could
821 ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
822 ;; and S is (length gnus-newsgroup-scored).
823 (or (assq (header-number header) gnus-newsgroup-scored)
824 (setq gnus-scores-articles ;Total of 2 * N cons-cells used.
825 (cons (cons header (or gnus-summary-default-score 0))
826 gnus-scores-articles))))
829 (set-buffer (get-buffer-create "*Headers*"))
830 (buffer-disable-undo (current-buffer))
832 ;; Set the global variant of this variable.
833 (setq gnus-current-score-file current-score-file)
835 (if gnus-orphan-score
837 (setq gnus-score-index
838 (nth 1 (assoc "references" gnus-header-index)))
839 (gnus-score-orphans gnus-orphan-score)))
840 ;; Run each header through the score process.
842 (setq entry (car entries)
844 entries (cdr entries))
845 (setq gnus-score-index (nth 1 (assoc header gnus-header-index)))
846 (if (< 0 (apply 'max (mapcar
848 (length (gnus-score-get header score)))
850 (funcall (nth 2 entry) scores header now expire trace)))
851 ;; Remove the buffer.
852 (kill-buffer (current-buffer)))
854 ;; Add articles to `gnus-newsgroup-scored'.
855 (while gnus-scores-articles
856 (or (= gnus-summary-default-score (cdr (car gnus-scores-articles)))
857 (setq gnus-newsgroup-scored
858 (cons (cons (header-number
859 (car (car gnus-scores-articles)))
860 (cdr (car gnus-scores-articles)))
861 gnus-newsgroup-scored)))
862 (setq gnus-scores-articles (cdr gnus-scores-articles)))
864 (gnus-message 5 "Scoring...done")))))
867 (defun gnus-get-new-thread-ids (articles)
868 (let ((index (nth 1 (assoc "message-id" gnus-header-index)))
869 (refind gnus-score-index)
870 id-list art this tref)
872 (setq art (car articles)
873 this (aref (car art) index)
874 tref (aref (car art) refind)
875 articles (cdr articles))
876 (if (string-equal tref "") ;no references line
877 (setq id-list (cons this id-list))))
880 ;; Orphan functions written by plm@atcmp.nl (Peter Mutsaers).
881 (defun gnus-score-orphans (score)
882 (let ((new-thread-ids (gnus-get-new-thread-ids gnus-scores-articles))
883 alike articles art arts this last this-id)
885 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
886 articles gnus-scores-articles)
888 ;;more or less the same as in gnus-score-string
891 (setq art (car articles)
892 this (aref (car art) gnus-score-index)
893 articles (cdr articles))
894 ;;completely skip if this is empty (not a child, so not an orphan)
895 (if (not (string= this ""))
896 (if (equal last this)
897 ;; O(N*H) cons-cells used here, where H is the number of
899 (setq alike (cons art alike))
902 ;; Insert the line, with a text property on the
903 ;; terminating newline refering to the articles with
906 (put-text-property (1- (point)) (point) 'articles alike)))
907 (setq alike (list art)
909 (and last ; Bwadr, duplicate code.
912 (put-text-property (1- (point)) (point) 'articles alike)))
914 ;; PLM: now delete those lines that contain an entry from new-thread-ids
915 (while new-thread-ids
916 (setq this-id (car new-thread-ids)
917 new-thread-ids (cdr new-thread-ids))
918 (goto-char (point-min))
919 (while (search-forward this-id nil t)
920 ;; found a match. remove this line
924 ;; now for each line: update its articles with score by moving to
925 ;; every end-of-line in the buffer and read the articles property
926 (goto-char (point-min))
929 (setq arts (get-text-property (point) 'articles))
933 (setcdr art (+ score (cdr art))))
937 (defun gnus-score-integer (scores header now expire &optional trace)
938 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
943 (setq alist (car scores)
945 entries (assoc header alist))
946 (while (cdr entries) ;First entry is the header index.
947 (let* ((rest (cdr entries))
950 (type (or (nth 3 kill) '>))
951 (score (or (nth 1 kill) gnus-score-interactive-default-score))
954 (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
955 (eq type '>=) (eq type '=))
957 (error "Illegal match type: %s" type)))
958 (articles gnus-scores-articles))
959 ;; Instead of doing all the clever stuff that
960 ;; `gnus-score-string' does to minimize searches and stuff,
961 ;; I will assume that people generally will put so few
962 ;; matches on numbers that any cleverness will take more
963 ;; time than one would gain.
965 (and (funcall match-func
966 (or (aref (car (car articles)) gnus-score-index) 0)
969 (and trace (setq gnus-score-trace
970 (cons (cons (car (car articles)) kill)
973 (setcdr (car articles) (+ score (cdr (car articles))))))
974 (setq articles (cdr articles)))
975 ;; Update expire date
976 (cond ((null date)) ;Permanent entry.
977 (found ;Match, update date.
978 (gnus-score-set 'touched '(t) alist)
979 (setcar (nthcdr 2 kill) now))
980 ((< date expire) ;Old entry, remove.
981 (gnus-score-set 'touched '(t) alist)
982 (setcdr entries (cdr rest))
983 (setq rest entries)))
984 (setq entries rest))))))
986 (defun gnus-score-date (scores header now expire &optional trace)
987 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
992 (setq alist (car scores)
994 entries (assoc header alist))
995 (while (cdr entries) ;First entry is the header index.
996 (let* ((rest (cdr entries))
998 (match (timezone-make-date-sortable (nth 0 kill)))
999 (type (or (nth 3 kill) 'before))
1000 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1004 (cond ((eq type 'after) 'string<)
1005 ((eq type 'before) 'gnus-string>)
1006 ((eq type 'at) 'string=)
1007 (t (error "Illegal match type: %s" type))))
1008 (articles gnus-scores-articles)
1010 ;; Instead of doing all the clever stuff that
1011 ;; `gnus-score-string' does to minimize searches and stuff,
1012 ;; I will assume that people generally will put so few
1013 ;; matches on numbers that any cleverness will take more
1014 ;; time than one would gain.
1017 (setq l (aref (car (car articles)) gnus-score-index))
1018 (funcall match-func match (timezone-make-date-sortable l))
1020 (and trace (setq gnus-score-trace
1021 (cons (cons (car (car articles)) kill)
1024 (setcdr (car articles) (+ score (cdr (car articles))))))
1025 (setq articles (cdr articles)))
1026 ;; Update expire date
1027 (cond ((null date)) ;Permanent entry.
1028 (found ;Match, update date.
1029 (gnus-score-set 'touched '(t) alist)
1030 (setcar (nthcdr 2 kill) now))
1031 ((< date expire) ;Old entry, remove.
1032 (gnus-score-set 'touched '(t) alist)
1033 (setcdr entries (cdr rest))
1034 (setq rest entries)))
1035 (setq entries rest))))))
1037 (defun gnus-score-body (scores header now expire &optional trace)
1039 (set-buffer nntp-server-buffer)
1041 (let* ((buffer-read-only nil)
1042 (articles gnus-scores-articles)
1043 (last (header-number (car (car gnus-scores-articles))))
1045 (request-func (cond ((string= "head" (downcase header))
1047 ((string= "body" (downcase header))
1049 (t 'gnus-request-article)))
1050 entries alist ofunc article)
1051 ;; Not all backends support partial fetching. In that case,
1052 ;; we just fetch the entire article.
1053 (or (gnus-check-backend-function
1054 (and (string-match "^gnus-" (symbol-name request-func))
1055 (intern (substring (symbol-name request-func)
1057 gnus-newsgroup-name)
1059 (setq ofunc request-func)
1060 (setq request-func 'gnus-request-article)))
1062 (setq article (header-number (car (car articles))))
1063 (gnus-message 7 "Scoring on article %s of %s..." article last)
1064 (if (not (funcall request-func article gnus-newsgroup-name))
1067 (goto-char (point-min))
1068 ;; If just parts of the article is to be searched, but the
1069 ;; backend didn't support partial fetching, we just narrow
1070 ;; to the relevant parts.
1072 (if (eq ofunc 'gnus-request-head)
1075 (or (search-forward "\n\n" nil t) (point-max)))
1077 (or (search-forward "\n\n" nil t) (point))
1079 (setq scores all-scores)
1082 (setq alist (car scores)
1084 entries (assoc header alist))
1085 (while (cdr entries) ;First entry is the header index.
1086 (let* ((rest (cdr entries))
1088 (match (nth 0 kill))
1089 (type (or (nth 3 kill) 's))
1090 (score (or (nth 1 kill)
1091 gnus-score-interactive-default-score))
1095 (not (or (eq type 'R) (eq type 'S)
1096 (eq type 'Regexp) (eq type 'String))))
1098 (cond ((or (eq type 'r) (eq type 'R)
1099 (eq type 'regexp) (eq type 'Regexp))
1101 ((or (eq type 's) (eq type 'S)
1102 (eq type 'string) (eq type 'String))
1105 (error "Illegal match type: %s" type)))))
1106 (goto-char (point-min))
1107 (if (funcall search-func match nil t)
1108 ;; Found a match, update scores.
1110 (setcdr (car articles) (+ score (cdr (car articles))))
1112 (and trace (setq gnus-score-trace
1113 (cons (cons (car (car articles)) kill)
1114 gnus-score-trace)))))
1115 ;; Update expire date
1116 (cond ((null date)) ;Permanent entry.
1117 (found ;Match, update date.
1118 (gnus-score-set 'touched '(t) alist)
1119 (setcar (nthcdr 2 kill) now))
1120 ((< date expire) ;Old entry, remove.
1121 (gnus-score-set 'touched '(t) alist)
1122 (setcdr entries (cdr rest))
1123 (setq rest entries)))
1124 (setq entries rest)))))
1125 (setq articles (cdr articles)))))))
1129 (defun gnus-score-followup (scores header now expire &optional trace)
1130 ;; Insert the unique article headers in the buffer.
1131 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1132 (current-score-file gnus-current-score-file)
1134 ;; gnus-score-index is used as a free variable.
1135 alike last this art entries alist articles)
1137 ;; Change score file to the adaptive score file. All entries that
1138 ;; this function makes will be put into this file.
1139 (gnus-score-load-file (gnus-score-file-name
1140 gnus-newsgroup-name gnus-adaptive-file-suffix))
1142 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1143 articles gnus-scores-articles)
1147 (setq art (car articles)
1148 this (aref (car art) gnus-score-index)
1149 articles (cdr articles))
1150 (if (equal last this)
1151 (setq alike (cons art alike))
1155 (put-text-property (1- (point)) (point) 'articles alike)))
1156 (setq alike (list art)
1158 (and last ; Bwadr, duplicate code.
1161 (put-text-property (1- (point)) (point) 'articles alike)))
1165 (setq alist (car scores)
1167 entries (assoc header alist))
1168 (while (cdr entries) ;First entry is the header index.
1169 (let* ((rest (cdr entries))
1171 (match (nth 0 kill))
1172 (type (or (nth 3 kill) 's))
1173 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1176 (mt (aref (symbol-name type) 0))
1178 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1181 (cond ((= dmt ?r) 're-search-forward)
1182 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1183 (t (error "Illegal match type: %s" type))))
1185 (goto-char (point-min))
1187 (while (funcall search-func match nil t)
1188 (and (= (progn (beginning-of-line) (point))
1189 (match-beginning 0))
1190 (= (progn (end-of-line) (point))
1193 (setq found (setq arts (get-text-property
1194 (point) 'articles)))
1195 ;; Found a match, update scores.
1197 (setq art (car arts)
1199 (gnus-score-add-followups
1200 (car art) score all-scores)))))
1201 (while (funcall search-func match nil t)
1203 (setq found (setq arts (get-text-property (point) 'articles)))
1204 ;; Found a match, update scores.
1206 (setq art (car arts)
1208 (gnus-score-add-followups (car art) score all-scores))))
1209 ;; Update expire date
1210 (cond ((null date)) ;Permanent entry.
1211 (found ;Match, update date.
1212 (gnus-score-set 'touched '(t) alist)
1213 (setcar (nthcdr 2 kill) now))
1214 ((< date expire) ;Old entry, remove.
1215 (gnus-score-set 'touched '(t) alist)
1216 (setcdr entries (cdr rest))
1217 (setq rest entries)))
1218 (setq entries rest))))
1219 ;; We change the score file back to the previous one.
1220 (gnus-score-load-file current-score-file)))
1222 (defun gnus-score-add-followups (header score scores)
1224 (set-buffer gnus-summary-buffer)
1225 (let* ((id (header-id header))
1226 (scores (car scores))
1228 ;; Don't enter a score if there already is one.
1230 (setq entry (car scores))
1231 (and (equal "references" (car entry))
1232 (or (null (nth 3 (car (cdr entry))))
1233 (eq 's (nth 3 (car (cdr entry)))))
1235 (if (assoc id entry)
1237 (setq scores (cdr scores)))
1239 (gnus-summary-score-entry
1240 "references" id 's score (current-time-string) nil t)))))
1243 (defun gnus-score-string (score-list header now expire &optional trace)
1244 ;; Score ARTICLES according to HEADER in SCORE-LIST.
1245 ;; Update matches entries to NOW and remove unmatched entried older
1248 ;; Insert the unique article headers in the buffer.
1249 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1250 ;; gnus-score-index is used as a free variable.
1251 alike last this art entries alist articles scores fuzzy)
1253 ;; Sorting the articles costs os O(N*log N) but will allow us to
1254 ;; only match with each unique header. Thus the actual matching
1255 ;; will be O(M*U) where M is the number of strings to match with,
1256 ;; and U is the number of unique headers. It is assumed (but
1257 ;; untested) this will be a net win because of the large constant
1258 ;; factor involved with string matching.
1259 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1260 articles gnus-scores-articles)
1264 (setq art (car articles)
1265 this (aref (car art) gnus-score-index)
1266 articles (cdr articles))
1267 (if (equal last this)
1268 ;; O(N*H) cons-cells used here, where H is the number of
1270 (setq alike (cons art alike))
1273 ;; Insert the line, with a text property on the
1274 ;; terminating newline refering to the articles with
1277 (put-text-property (1- (point)) (point) 'articles alike)))
1278 (setq alike (list art)
1280 (and last ; Bwadr, duplicate code.
1283 (put-text-property (1- (point)) (point) 'articles alike)))
1285 ;; Find ordinary matches.
1286 (setq scores score-list)
1288 (setq alist (car scores)
1290 entries (assoc header alist))
1291 (while (cdr entries) ;First entry is the header index.
1292 (let* ((rest (cdr entries))
1294 (match (nth 0 kill))
1295 (type (or (nth 3 kill) 's))
1296 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1299 (mt (aref (symbol-name type) 0))
1301 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1304 (cond ((= dmt ?r) 're-search-forward)
1305 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1306 (t (error "Illegal match type: %s" type))))
1310 (goto-char (point-min))
1312 (while (and (not (eobp))
1313 (funcall search-func match nil t))
1314 (and (= (progn (beginning-of-line) (point))
1315 (match-beginning 0))
1316 (= (progn (end-of-line) (point))
1319 (setq found (setq arts (get-text-property
1320 (point) 'articles)))
1321 ;; Found a match, update scores.
1324 (setq art (car arts)
1326 (setcdr art (+ score (cdr art)))
1327 (setq gnus-score-trace
1328 (cons (cons (header-number
1332 (setq art (car arts)
1334 (setcdr art (+ score (cdr art)))))))
1336 (and (string= match "") (setq match "\n"))
1337 (while (and (not (eobp))
1338 (funcall search-func match nil t))
1339 (goto-char (match-beginning 0))
1341 (setq found (setq arts (get-text-property (point) 'articles)))
1342 ;; Found a match, update scores.
1345 (setq art (car arts)
1347 (setcdr art (+ score (cdr art)))
1348 (setq gnus-score-trace
1349 (cons (cons (header-number (car art)) kill)
1352 (setq art (car arts)
1354 (setcdr art (+ score (cdr art)))))
1356 ;; Update expire date
1357 (cond ((null date)) ;Permanent entry.
1358 (found ;Match, update date.
1359 (gnus-score-set 'touched '(t) alist)
1360 (setcar (nthcdr 2 kill) now))
1361 ((< date expire) ;Old entry, remove.
1362 (gnus-score-set 'touched '(t) alist)
1363 (setcdr entries (cdr rest))
1364 (setq rest entries))))
1365 (setq entries rest))))
1367 ;; Find fuzzy matches.
1368 (setq scores (and fuzzy score-list))
1369 (if fuzzy (gnus-simplify-buffer-fuzzy))
1371 (setq alist (car scores)
1373 entries (assoc header alist))
1374 (while (cdr entries) ;First entry is the header index.
1375 (let* ((rest (cdr entries))
1377 (match (nth 0 kill))
1378 (type (or (nth 3 kill) 's))
1379 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1382 (mt (aref (symbol-name type) 0))
1384 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1387 (cond ((= dmt ?r) 're-search-forward)
1388 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1389 (t (error "Illegal match type: %s" type))))
1393 (goto-char (point-min))
1394 (while (and (not (eobp))
1395 (funcall search-func match nil t))
1396 (and (= (progn (beginning-of-line) (point))
1397 (match-beginning 0))
1398 (= (progn (end-of-line) (point))
1401 (setq found (setq arts (get-text-property
1402 (point) 'articles)))
1403 ;; Found a match, update scores.
1405 (setq art (car arts)
1407 (setcdr art (+ score (cdr art))))))
1409 ;; Update expire date
1410 (cond ((null date)) ;Permanent entry.
1411 (found ;Match, update date.
1412 (gnus-score-set 'touched '(t) alist)
1413 (setcar (nthcdr 2 kill) now))
1414 ((< date expire) ;Old entry, remove.
1415 (gnus-score-set 'touched '(t) alist)
1416 (setcdr entries (cdr rest))
1417 (setq rest entries))))
1418 (setq entries rest))))))
1420 (defun gnus-score-string< (a1 a2)
1421 ;; Compare headers in articles A2 and A2.
1422 ;; The header index used is the free variable `gnus-score-index'.
1423 (string-lessp (aref (car a1) gnus-score-index)
1424 (aref (car a2) gnus-score-index)))
1426 (defun gnus-score-build-cons (article)
1427 ;; Build a `gnus-newsgroup-scored' type cons from ARTICLE.
1428 (cons (header-number (car article)) (cdr article)))
1430 (defconst gnus-header-index
1431 ;; Name to index alist.
1432 '(("number" 0 gnus-score-integer)
1433 ("subject" 1 gnus-score-string)
1434 ("from" 2 gnus-score-string)
1435 ("date" 3 gnus-score-date)
1436 ("message-id" 4 gnus-score-string)
1437 ("references" 5 gnus-score-string)
1438 ("chars" 6 gnus-score-integer)
1439 ("lines" 7 gnus-score-integer)
1440 ("xref" 8 gnus-score-string)
1441 ("head" -1 gnus-score-body)
1442 ("body" -1 gnus-score-body)
1443 ("all" -1 gnus-score-body)
1444 ("followup" 2 gnus-score-followup)))
1446 (defun gnus-current-score-file-nondirectory (&optional score-file)
1447 (let ((score-file (or score-file gnus-current-score-file)))
1449 (gnus-short-group-name (file-name-nondirectory score-file))
1452 (defun gnus-score-adaptive ()
1454 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
1456 (date (current-time-string))
1458 ;; First we transform the adaptive rule alist into something
1459 ;; that's faster to process.
1461 (setq elem (car malist))
1462 (if (symbolp (car elem))
1463 (setcar elem (symbol-value (car elem))))
1464 (setq elem (cdr elem))
1467 (cons (symbol-name (car (car elem))) (cdr (car elem))))
1470 (concat "gnus-header-"
1471 (downcase (symbol-name (car (car elem)))))))
1472 (setq elem (cdr elem)))
1473 (setq malist (cdr malist)))
1474 ;; We change the score file to the adaptive score file.
1475 (gnus-score-load-file (gnus-score-file-name
1476 gnus-newsgroup-name gnus-adaptive-file-suffix))
1477 ;; The we score away.
1478 (goto-char (point-min))
1480 (setq elem (cdr (assq (gnus-summary-article-mark) alist)))
1482 (get-text-property (point) 'gnus-pseudo))
1484 (setq headers (gnus-get-header-by-number
1485 (gnus-summary-article-number)))
1486 (while (and elem headers)
1487 (setq match (funcall (car (car elem)) headers))
1488 (gnus-summary-score-entry
1489 (nth 1 (car elem)) match
1490 ;; Whether we use substring or exact matches are controlled
1492 (if (or (not gnus-score-exact-adapt-limit)
1493 (< (length match) gnus-score-exact-adapt-limit))
1495 (if (equal (nth 1 (car elem)) "subject")
1497 (nth 2 (car elem)) date nil t)
1498 (setq elem (cdr elem))))
1499 (forward-line 1)))))
1501 (defun gnus-score-remove-lines-adaptive (marks)
1503 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
1505 (date (current-time-string))
1506 (cur-score gnus-current-score-file)
1508 ;; First we transform the adaptive rule alist into something
1509 ;; that's faster to process.
1511 (setq elem (car malist))
1512 (if (symbolp (car elem))
1513 (setcar elem (symbol-value (car elem))))
1514 (setq elem (cdr elem))
1517 (cons (symbol-name (car (car elem))) (cdr (car elem))))
1520 (concat "gnus-header-"
1521 (downcase (symbol-name (car (car elem)))))))
1522 (setq elem (cdr elem)))
1523 (setq malist (cdr malist)))
1524 ;; The we score away.
1525 (goto-char (point-min))
1526 ;; We change the score file to the adaptive score file.
1527 (gnus-score-load-file (gnus-score-file-name
1528 gnus-newsgroup-name gnus-adaptive-file-suffix))
1529 (while (re-search-forward marks nil t)
1531 (setq elem (cdr (assq (gnus-summary-article-mark) alist)))
1533 (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
1535 (setq headers (gnus-get-header-by-number
1536 (gnus-summary-article-number)))
1538 (setq match (funcall (car (car elem)) headers))
1539 (gnus-summary-score-entry
1540 (nth 1 (car elem)) match
1541 (if (or (not gnus-score-exact-adapt-limit)
1542 (< (length match) gnus-score-exact-adapt-limit))
1544 (nth 2 (car elem)) date nil t)
1545 (setq elem (cdr elem))))
1546 (delete-region (point) (progn (forward-line 1) (point))))
1547 ;; Switch back to the old score file.
1548 (gnus-score-load-file cur-score))))
1554 (defvar gnus-score-mode-map nil)
1555 (defvar gnus-score-mode-hook nil)
1557 (if gnus-score-mode-map
1559 (setq gnus-score-mode-map (copy-keymap emacs-lisp-mode-map))
1560 (define-key gnus-score-mode-map "\C-c\C-c" 'gnus-score-edit-done)
1561 (define-key gnus-score-mode-map "\C-c\C-d" 'gnus-score-edit-insert-date))
1563 (defun gnus-score-mode ()
1564 "Mode for editing score files.
1565 This mode is an extended emacs-lisp mode.
1567 \\{gnus-score-mode-map}"
1569 (kill-all-local-variables)
1570 (use-local-map gnus-score-mode-map)
1571 (set-syntax-table emacs-lisp-mode-syntax-table)
1572 (setq major-mode 'gnus-score-mode)
1573 (setq mode-name "Score")
1574 (lisp-mode-variables nil)
1575 (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
1577 (defun gnus-score-edit-insert-date ()
1578 "Insert date in numerical format."
1580 (insert (int-to-string (gnus-day-number (current-time-string)))))
1582 (defun gnus-score-edit-done ()
1583 "Save the score file and return to the summary buffer."
1585 (let ((bufnam (buffer-file-name (current-buffer)))
1586 (winconf gnus-prev-winconf))
1587 (gnus-make-directory (file-name-directory (buffer-file-name)))
1589 (kill-buffer (current-buffer))
1590 (gnus-score-remove-from-cache bufnam)
1591 (gnus-score-load-file bufnam)
1592 (and winconf (set-window-configuration winconf))))
1594 (defun gnus-score-find-trace ()
1595 "Find all score rules applied to this article."
1597 (let ((gnus-newsgroup-headers
1598 (list (gnus-get-header-by-number (gnus-summary-article-number))))
1599 (gnus-newsgroup-scored nil)
1600 (buf (current-buffer))
1602 (setq gnus-score-trace nil)
1603 (gnus-possibly-score-headers 'trace)
1604 (or (setq trace gnus-score-trace)
1605 (error "No score rules apply to the current article."))
1606 (pop-to-buffer "*Gnus Scores*")
1609 (insert (format "%S\n" (cdr (car trace))))
1610 (setq trace (cdr trace)))
1611 (goto-char (point-min))
1612 (pop-to-buffer buf)))
1615 (provide 'gnus-score)
1617 ;;; gnus-score.el ends here