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 exclude-files)
622 (set-buffer gnus-summary-buffer)
624 (and (consp (car local))
625 (symbolp (car (car local)))
627 (make-local-variable (car (car local)))
628 (set (car (car local)) (nth 1 (car local)))))
629 (setq local (cdr local)))))
630 (if orphan (setq gnus-orphan-score orphan))
631 (setq gnus-adaptive-score-alist
632 (cond ((equal adapt '(t))
633 (setq gnus-newsgroup-adaptive t)
634 gnus-default-adaptive-score-alist)
635 ((equal adapt '(ignore))
636 (setq gnus-newsgroup-adaptive nil))
638 (setq gnus-newsgroup-adaptive t)
641 ;;(setq gnus-newsgroup-adaptive gnus-use-adaptive-scoring)
642 gnus-default-adaptive-score-alist)))
643 (setq gnus-summary-mark-below
644 (or mark mark-and-expunge gnus-summary-mark-below))
645 (setq gnus-summary-expunge-below
646 (or expunge mark-and-expunge gnus-summary-expunge-below)))
647 (setq gnus-current-score-file file)
648 (setq gnus-score-alist alist)
651 (defun gnus-score-load (file)
653 (let ((cache (assoc file gnus-score-cache)))
655 (setq gnus-score-alist (cdr cache))
656 (setq gnus-score-alist nil)
657 (gnus-score-load-score-alist file)
659 (setq gnus-score-alist (copy-alist '((touched nil)))))
660 (setq gnus-score-cache
661 (cons (cons file gnus-score-alist) gnus-score-cache)))))
663 (defun gnus-score-remove-from-cache (file)
664 (setq gnus-score-cache
665 (delq (assoc file gnus-score-cache) gnus-score-cache)))
667 (defun gnus-score-load-score-alist (file)
669 (if (file-readable-p file)
672 (gnus-set-work-buffer)
673 (insert-file-contents file)
674 (goto-char (point-min))
675 ;; Only do the loading if the score file isn't empty.
676 (if (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
679 (read (current-buffer))
682 (gnus-message 3 "Problem with score file %s" file)
686 (if (eq (car alist) 'setq)
687 (setq gnus-score-alist (gnus-score-transform-old-to-new alist))
688 (setq gnus-score-alist alist))
689 (setq gnus-score-alist
690 (gnus-score-check-syntax gnus-score-alist file)))
691 (setq gnus-score-alist nil))))
693 (defun gnus-score-check-syntax (alist file)
698 (gnus-message 1 "Score file is not a list: %s" file)
704 (while (and a (not err))
705 (cond ((not (listp (car a)))
706 (gnus-message 3 "Illegal score element %s in %s" (car a) file)
708 ((and (stringp (car (car a)))
709 (not (listp (nth 1 (car a)))))
710 (gnus-message 3 "Illegal header match %s in %s" (nth 1 (car a)) file)
720 (defun gnus-score-transform-old-to-new (alist)
721 (let* ((alist (nth 2 alist))
723 (if (eq (car alist) 'quote)
724 (setq alist (nth 1 alist)))
726 (setq entry (car alist))
727 (if (stringp (car entry))
728 (let ((scor (cdr entry)))
729 (setq out (cons entry out))
732 (list (car (car scor)) (nth 2 (car scor))
733 (and (nth 3 (car scor))
734 (gnus-day-number (nth 3 (car scor))))
735 (if (nth 1 (car scor)) 'r 's)))
736 (setq scor (cdr scor))))
737 (setq out (cons (if (not (listp (cdr entry)))
738 (list (car entry) (cdr entry))
741 (setq alist (cdr alist)))
742 (cons (list 'touched t) (nreverse out))))
744 (defun gnus-score-save ()
745 ;; Save all score information.
746 (let ((cache gnus-score-cache))
748 (setq gnus-score-alist nil)
749 (set-buffer (get-buffer-create "*Score*"))
750 (buffer-disable-undo (current-buffer))
751 (let (entry score file)
753 (setq entry (car cache)
757 (if (or (not (equal (gnus-score-get 'touched score) '(t)))
758 (gnus-score-get 'read-only score)
759 (and (file-exists-p file)
760 (not (file-writable-p file))))
762 (setq score (setcdr entry (delq (assq 'touched score) score)))
764 (let (emacs-lisp-mode-hook)
765 (if (string-match (concat gnus-adaptive-file-suffix "$") file)
766 ;; This is an adaptive score file, so we do not run
767 ;; it through `pp'. These files can get huge, and
768 ;; are not meant to be edited by human hands.
769 (insert (format "%S" score))
770 ;; This is a normal score file, so we print it very
772 (pp score (current-buffer))))
773 (if (not (gnus-make-directory (file-name-directory file)))
775 ;; If the score file is empty, we delete it.
776 (if (zerop (buffer-size))
778 ;; There are scores, so we write the file.
779 (and (file-writable-p file)
780 (write-region (point-min) (point-max)
782 (and gnus-score-uncacheable-files
783 (string-match gnus-score-uncacheable-files file)
784 (gnus-score-remove-from-cache file)))))
785 (kill-buffer (current-buffer)))))
787 (defun gnus-score-headers (score-files &optional trace)
788 ;; Score `gnus-newsgroup-headers'.
790 ;; PLM: probably this is not the best place to clear orphan-score
791 (setq gnus-orphan-score nil)
792 ;; Load the score files.
794 (if (stringp (car score-files))
795 ;; It is a string, which means that it's a score file name,
796 ;; so we load the score file and add the score alist to
797 ;; the list of alists.
798 (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
799 ;; It is an alist, so we just add it to the list directly.
800 (setq scores (nconc (car score-files) scores)))
801 (setq score-files (cdr score-files)))
802 ;; Prune the score files that are to be excluded, if any.
803 (if (not gnus-scores-exclude-files)
808 (and (setq c (rassq (car s) gnus-score-cache))
809 (member (car c) gnus-scores-exclude-files)
810 (setq scores (delq (car s) scores)))
812 (if (not (and gnus-summary-default-score
814 (> (length gnus-newsgroup-headers)
815 (length gnus-newsgroup-scored))))
817 (let* ((entries gnus-header-index)
818 (now (gnus-day-number (current-time-string)))
819 (expire (- now gnus-score-expiry-days))
820 (headers gnus-newsgroup-headers)
821 (current-score-file gnus-current-score-file)
823 (gnus-message 5 "Scoring...")
824 ;; Create articles, an alist of the form `(HEADER . SCORE)'.
826 (setq header (car headers)
827 headers (cdr headers))
828 ;; WARNING: The assq makes the function O(N*S) while it could
829 ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
830 ;; and S is (length gnus-newsgroup-scored).
831 (or (assq (mail-header-number header) gnus-newsgroup-scored)
832 (setq gnus-scores-articles ;Total of 2 * N cons-cells used.
833 (cons (cons header (or gnus-summary-default-score 0))
834 gnus-scores-articles))))
837 (set-buffer (get-buffer-create "*Headers*"))
838 (buffer-disable-undo (current-buffer))
840 ;; Set the global variant of this variable.
841 (setq gnus-current-score-file current-score-file)
843 (if gnus-orphan-score
845 (setq gnus-score-index
846 (nth 1 (assoc "references" gnus-header-index)))
847 (gnus-score-orphans gnus-orphan-score)))
848 ;; Run each header through the score process.
850 (setq entry (car entries)
852 entries (cdr entries))
853 (setq gnus-score-index (nth 1 (assoc header gnus-header-index)))
854 (if (< 0 (apply 'max (mapcar
856 (length (gnus-score-get header score)))
858 (funcall (nth 2 entry) scores header now expire trace)))
859 ;; Remove the buffer.
860 (kill-buffer (current-buffer)))
862 ;; Add articles to `gnus-newsgroup-scored'.
863 (while gnus-scores-articles
864 (or (= gnus-summary-default-score (cdr (car gnus-scores-articles)))
865 (setq gnus-newsgroup-scored
866 (cons (cons (mail-header-number
867 (car (car gnus-scores-articles)))
868 (cdr (car gnus-scores-articles)))
869 gnus-newsgroup-scored)))
870 (setq gnus-scores-articles (cdr gnus-scores-articles)))
872 (gnus-message 5 "Scoring...done")))))
875 (defun gnus-get-new-thread-ids (articles)
876 (let ((index (nth 1 (assoc "message-id" gnus-header-index)))
877 (refind gnus-score-index)
878 id-list art this tref)
880 (setq art (car articles)
881 this (aref (car art) index)
882 tref (aref (car art) refind)
883 articles (cdr articles))
884 (if (string-equal tref "") ;no references line
885 (setq id-list (cons this id-list))))
888 ;; Orphan functions written by plm@atcmp.nl (Peter Mutsaers).
889 (defun gnus-score-orphans (score)
890 (let ((new-thread-ids (gnus-get-new-thread-ids gnus-scores-articles))
891 alike articles art arts this last this-id)
893 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
894 articles gnus-scores-articles)
896 ;;more or less the same as in gnus-score-string
899 (setq art (car articles)
900 this (aref (car art) gnus-score-index)
901 articles (cdr articles))
902 ;;completely skip if this is empty (not a child, so not an orphan)
903 (if (not (string= this ""))
904 (if (equal last this)
905 ;; O(N*H) cons-cells used here, where H is the number of
907 (setq alike (cons art alike))
910 ;; Insert the line, with a text property on the
911 ;; terminating newline refering to the articles with
914 (put-text-property (1- (point)) (point) 'articles alike)))
915 (setq alike (list art)
917 (and last ; Bwadr, duplicate code.
920 (put-text-property (1- (point)) (point) 'articles alike)))
922 ;; PLM: now delete those lines that contain an entry from new-thread-ids
923 (while new-thread-ids
924 (setq this-id (car new-thread-ids)
925 new-thread-ids (cdr new-thread-ids))
926 (goto-char (point-min))
927 (while (search-forward this-id nil t)
928 ;; found a match. remove this line
932 ;; now for each line: update its articles with score by moving to
933 ;; every end-of-line in the buffer and read the articles property
934 (goto-char (point-min))
937 (setq arts (get-text-property (point) 'articles))
941 (setcdr art (+ score (cdr art))))
945 (defun gnus-score-integer (scores header now expire &optional trace)
946 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
951 (setq alist (car scores)
953 entries (assoc header alist))
954 (while (cdr entries) ;First entry is the header index.
955 (let* ((rest (cdr entries))
958 (type (or (nth 3 kill) '>))
959 (score (or (nth 1 kill) gnus-score-interactive-default-score))
962 (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
963 (eq type '>=) (eq type '=))
965 (error "Illegal match type: %s" type)))
966 (articles gnus-scores-articles))
967 ;; Instead of doing all the clever stuff that
968 ;; `gnus-score-string' does to minimize searches and stuff,
969 ;; I will assume that people generally will put so few
970 ;; matches on numbers that any cleverness will take more
971 ;; time than one would gain.
973 (and (funcall match-func
974 (or (aref (car (car articles)) gnus-score-index) 0)
977 (and trace (setq gnus-score-trace
980 (car-safe (rassq alist gnus-score-cache))
984 (setcdr (car articles) (+ score (cdr (car articles))))))
985 (setq articles (cdr articles)))
986 ;; Update expire date
987 (cond ((null date)) ;Permanent entry.
988 (found ;Match, update date.
989 (gnus-score-set 'touched '(t) alist)
990 (setcar (nthcdr 2 kill) now))
991 ((< date expire) ;Old entry, remove.
992 (gnus-score-set 'touched '(t) alist)
993 (setcdr entries (cdr rest))
994 (setq rest entries)))
995 (setq entries rest))))))
997 (defun gnus-score-date (scores header now expire &optional trace)
998 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1003 (setq alist (car scores)
1005 entries (assoc header alist))
1006 (while (cdr entries) ;First entry is the header index.
1007 (let* ((rest (cdr entries))
1009 (match (timezone-make-date-sortable (nth 0 kill)))
1010 (type (or (nth 3 kill) 'before))
1011 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1015 (cond ((eq type 'after) 'string<)
1016 ((eq type 'before) 'gnus-string>)
1017 ((eq type 'at) 'string=)
1018 (t (error "Illegal match type: %s" type))))
1019 (articles gnus-scores-articles)
1021 ;; Instead of doing all the clever stuff that
1022 ;; `gnus-score-string' does to minimize searches and stuff,
1023 ;; I will assume that people generally will put so few
1024 ;; matches on numbers that any cleverness will take more
1025 ;; time than one would gain.
1028 (setq l (aref (car (car articles)) gnus-score-index))
1029 (funcall match-func match (timezone-make-date-sortable l))
1031 (and trace (setq gnus-score-trace
1034 (car-safe (rassq alist gnus-score-cache))
1038 (setcdr (car articles) (+ score (cdr (car articles))))))
1039 (setq articles (cdr articles)))
1040 ;; Update expire date
1041 (cond ((null date)) ;Permanent entry.
1042 (found ;Match, update date.
1043 (gnus-score-set 'touched '(t) alist)
1044 (setcar (nthcdr 2 kill) now))
1045 ((< date expire) ;Old entry, remove.
1046 (gnus-score-set 'touched '(t) alist)
1047 (setcdr entries (cdr rest))
1048 (setq rest entries)))
1049 (setq entries rest))))))
1051 (defun gnus-score-body (scores header now expire &optional trace)
1053 (set-buffer nntp-server-buffer)
1055 (let* ((buffer-read-only nil)
1056 (articles gnus-scores-articles)
1057 (last (mail-header-number (car (car gnus-scores-articles))))
1059 (request-func (cond ((string= "head" (downcase header))
1061 ((string= "body" (downcase header))
1063 (t 'gnus-request-article)))
1064 entries alist ofunc article)
1065 ;; Not all backends support partial fetching. In that case,
1066 ;; we just fetch the entire article.
1067 (or (gnus-check-backend-function
1068 (and (string-match "^gnus-" (symbol-name request-func))
1069 (intern (substring (symbol-name request-func)
1071 gnus-newsgroup-name)
1073 (setq ofunc request-func)
1074 (setq request-func 'gnus-request-article)))
1076 (setq article (mail-header-number (car (car articles))))
1077 (gnus-message 7 "Scoring on article %s of %s..." article last)
1078 (if (not (funcall request-func article gnus-newsgroup-name))
1081 (goto-char (point-min))
1082 ;; If just parts of the article is to be searched, but the
1083 ;; backend didn't support partial fetching, we just narrow
1084 ;; to the relevant parts.
1086 (if (eq ofunc 'gnus-request-head)
1089 (or (search-forward "\n\n" nil t) (point-max)))
1091 (or (search-forward "\n\n" nil t) (point))
1093 (setq scores all-scores)
1096 (setq alist (car scores)
1098 entries (assoc header alist))
1099 (while (cdr entries) ;First entry is the header index.
1100 (let* ((rest (cdr entries))
1102 (match (nth 0 kill))
1103 (type (or (nth 3 kill) 's))
1104 (score (or (nth 1 kill)
1105 gnus-score-interactive-default-score))
1109 (not (or (eq type 'R) (eq type 'S)
1110 (eq type 'Regexp) (eq type 'String))))
1112 (cond ((or (eq type 'r) (eq type 'R)
1113 (eq type 'regexp) (eq type 'Regexp))
1115 ((or (eq type 's) (eq type 'S)
1116 (eq type 'string) (eq type 'String))
1119 (error "Illegal match type: %s" type)))))
1120 (goto-char (point-min))
1121 (if (funcall search-func match nil t)
1122 ;; Found a match, update scores.
1124 (setcdr (car articles) (+ score (cdr (car articles))))
1126 (and trace (setq gnus-score-trace
1130 (rassq alist gnus-score-cache))
1132 gnus-score-trace)))))
1133 ;; Update expire date
1134 (cond ((null date)) ;Permanent entry.
1135 (found ;Match, update date.
1136 (gnus-score-set 'touched '(t) alist)
1137 (setcar (nthcdr 2 kill) now))
1138 ((< date expire) ;Old entry, remove.
1139 (gnus-score-set 'touched '(t) alist)
1140 (setcdr entries (cdr rest))
1141 (setq rest entries)))
1142 (setq entries rest)))))
1143 (setq articles (cdr articles)))))))
1147 (defun gnus-score-followup (scores header now expire &optional trace)
1148 ;; Insert the unique article headers in the buffer.
1149 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1150 (current-score-file gnus-current-score-file)
1152 ;; gnus-score-index is used as a free variable.
1153 alike last this art entries alist articles)
1155 ;; Change score file to the adaptive score file. All entries that
1156 ;; this function makes will be put into this file.
1157 (gnus-score-load-file (gnus-score-file-name
1158 gnus-newsgroup-name gnus-adaptive-file-suffix))
1160 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1161 articles gnus-scores-articles)
1165 (setq art (car articles)
1166 this (aref (car art) gnus-score-index)
1167 articles (cdr articles))
1168 (if (equal last this)
1169 (setq alike (cons art alike))
1173 (put-text-property (1- (point)) (point) 'articles alike)))
1174 (setq alike (list art)
1176 (and last ; Bwadr, duplicate code.
1179 (put-text-property (1- (point)) (point) 'articles alike)))
1183 (setq alist (car scores)
1185 entries (assoc header alist))
1186 (while (cdr entries) ;First entry is the header index.
1187 (let* ((rest (cdr entries))
1189 (match (nth 0 kill))
1190 (type (or (nth 3 kill) 's))
1191 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1194 (mt (aref (symbol-name type) 0))
1196 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1199 (cond ((= dmt ?r) 're-search-forward)
1200 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1201 (t (error "Illegal match type: %s" type))))
1203 (goto-char (point-min))
1205 (while (funcall search-func match nil t)
1206 (and (= (progn (beginning-of-line) (point))
1207 (match-beginning 0))
1208 (= (progn (end-of-line) (point))
1211 (setq found (setq arts (get-text-property
1212 (point) 'articles)))
1213 ;; Found a match, update scores.
1215 (setq art (car arts)
1217 (gnus-score-add-followups
1218 (car art) score all-scores)))))
1219 (while (funcall search-func match nil t)
1221 (setq found (setq arts (get-text-property (point) 'articles)))
1222 ;; Found a match, update scores.
1224 (setq art (car arts)
1226 (gnus-score-add-followups (car art) score all-scores))))
1227 ;; Update expire date
1228 (cond ((null date)) ;Permanent entry.
1229 (found ;Match, update date.
1230 (gnus-score-set 'touched '(t) alist)
1231 (setcar (nthcdr 2 kill) now))
1232 ((< date expire) ;Old entry, remove.
1233 (gnus-score-set 'touched '(t) alist)
1234 (setcdr entries (cdr rest))
1235 (setq rest entries)))
1236 (setq entries rest))))
1237 ;; We change the score file back to the previous one.
1238 (gnus-score-load-file current-score-file)))
1240 (defun gnus-score-add-followups (header score scores)
1242 (set-buffer gnus-summary-buffer)
1243 (let* ((id (mail-header-id header))
1244 (scores (car scores))
1246 ;; Don't enter a score if there already is one.
1248 (setq entry (car scores))
1249 (and (equal "references" (car entry))
1250 (or (null (nth 3 (car (cdr entry))))
1251 (eq 's (nth 3 (car (cdr entry)))))
1253 (if (assoc id entry)
1255 (setq scores (cdr scores)))
1257 (gnus-summary-score-entry
1258 "references" id 's score (current-time-string) nil t)))))
1261 (defun gnus-score-string (score-list header now expire &optional trace)
1262 ;; Score ARTICLES according to HEADER in SCORE-LIST.
1263 ;; Update matches entries to NOW and remove unmatched entried older
1266 ;; Insert the unique article headers in the buffer.
1267 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1268 ;; gnus-score-index is used as a free variable.
1269 alike last this art entries alist articles scores fuzzy)
1271 ;; Sorting the articles costs os O(N*log N) but will allow us to
1272 ;; only match with each unique header. Thus the actual matching
1273 ;; will be O(M*U) where M is the number of strings to match with,
1274 ;; and U is the number of unique headers. It is assumed (but
1275 ;; untested) this will be a net win because of the large constant
1276 ;; factor involved with string matching.
1277 (setq gnus-scores-articles (sort gnus-scores-articles 'gnus-score-string<)
1278 articles gnus-scores-articles)
1282 (setq art (car articles)
1283 this (aref (car art) gnus-score-index)
1284 articles (cdr articles))
1285 (if (equal last this)
1286 ;; O(N*H) cons-cells used here, where H is the number of
1288 (setq alike (cons art alike))
1291 ;; Insert the line, with a text property on the
1292 ;; terminating newline refering to the articles with
1295 (put-text-property (1- (point)) (point) 'articles alike)))
1296 (setq alike (list art)
1298 (and last ; Bwadr, duplicate code.
1301 (put-text-property (1- (point)) (point) 'articles alike)))
1303 ;; Find ordinary matches.
1304 (setq scores score-list)
1306 (setq alist (car scores)
1308 entries (assoc header alist))
1309 (while (cdr entries) ;First entry is the header index.
1310 (let* ((rest (cdr entries))
1312 (match (nth 0 kill))
1313 (type (or (nth 3 kill) 's))
1314 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1317 (mt (aref (symbol-name type) 0))
1319 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1322 (cond ((= dmt ?r) 're-search-forward)
1323 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1324 (t (error "Illegal match type: %s" type))))
1328 ;; Do non-fuzzy matching.
1329 (goto-char (point-min))
1331 ;; Do exact matching.
1332 (while (and (not (eobp))
1333 (funcall search-func match nil t))
1334 (and (= (progn (beginning-of-line) (point))
1335 (match-beginning 0))
1336 (= (progn (end-of-line) (point))
1339 (setq found (setq arts (get-text-property
1340 (point) 'articles)))
1341 ;; Found a match, update scores.
1344 (setq art (car arts)
1346 (setcdr art (+ score (cdr art)))
1347 (setq gnus-score-trace
1351 (rassq alist gnus-score-cache))
1355 (setq art (car arts)
1357 (setcdr art (+ score (cdr art)))))))
1359 ;; Do regexp and substring matching.
1360 (and (string= match "") (setq match "\n"))
1361 (while (and (not (eobp))
1362 (funcall search-func match nil t))
1363 (goto-char (match-beginning 0))
1365 (setq found (setq arts (get-text-property (point) 'articles)))
1366 ;; Found a match, update scores.
1369 (setq art (car arts)
1371 (setcdr art (+ score (cdr art)))
1372 (setq gnus-score-trace
1376 (rassq alist gnus-score-cache))
1380 (setq art (car arts)
1382 (setcdr art (+ score (cdr art)))))
1384 ;; Update expire date
1385 (cond ((null date)) ;Permanent entry.
1386 (found ;Match, update date.
1387 (gnus-score-set 'touched '(t) alist)
1388 (setcar (nthcdr 2 kill) now))
1389 ((< date expire) ;Old entry, remove.
1390 (gnus-score-set 'touched '(t) alist)
1391 (setcdr entries (cdr rest))
1392 (setq rest entries))))
1393 (setq entries rest))))
1395 ;; Find fuzzy matches.
1396 (setq scores (and fuzzy score-list))
1397 (if fuzzy (gnus-simplify-buffer-fuzzy))
1399 (setq alist (car scores)
1401 entries (assoc header alist))
1402 (while (cdr entries) ;First entry is the header index.
1403 (let* ((rest (cdr entries))
1405 (match (nth 0 kill))
1406 (type (or (nth 3 kill) 's))
1407 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1410 (mt (aref (symbol-name type) 0))
1412 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1415 (cond ((= dmt ?r) 're-search-forward)
1416 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1417 (t (error "Illegal match type: %s" type))))
1421 (goto-char (point-min))
1422 (while (and (not (eobp))
1423 (funcall search-func match nil t))
1424 (and (= (progn (beginning-of-line) (point))
1425 (match-beginning 0))
1426 (= (progn (end-of-line) (point))
1429 (setq found (setq arts (get-text-property
1430 (point) 'articles)))
1431 ;; Found a match, update scores.
1434 (setq art (car arts)
1436 (setcdr art (+ score (cdr art)))
1437 (setq gnus-score-trace
1441 (rassq alist gnus-score-cache))
1445 (setq art (car arts)
1447 (setcdr art (+ score (cdr art)))))))
1449 ;; Update expire date
1450 (cond ((null date)) ;Permanent entry.
1451 (found ;Match, update date.
1452 (gnus-score-set 'touched '(t) alist)
1453 (setcar (nthcdr 2 kill) now))
1454 ((< date expire) ;Old entry, remove.
1455 (gnus-score-set 'touched '(t) alist)
1456 (setcdr entries (cdr rest))
1457 (setq rest entries))))
1458 (setq entries rest))))))
1460 (defun gnus-score-string< (a1 a2)
1461 ;; Compare headers in articles A2 and A2.
1462 ;; The header index used is the free variable `gnus-score-index'.
1463 (string-lessp (aref (car a1) gnus-score-index)
1464 (aref (car a2) gnus-score-index)))
1466 (defun gnus-score-build-cons (article)
1467 ;; Build a `gnus-newsgroup-scored' type cons from ARTICLE.
1468 (cons (mail-header-number (car article)) (cdr article)))
1470 (defconst gnus-header-index
1471 ;; Name to index alist.
1472 '(("number" 0 gnus-score-integer)
1473 ("subject" 1 gnus-score-string)
1474 ("from" 2 gnus-score-string)
1475 ("date" 3 gnus-score-date)
1476 ("message-id" 4 gnus-score-string)
1477 ("references" 5 gnus-score-string)
1478 ("chars" 6 gnus-score-integer)
1479 ("lines" 7 gnus-score-integer)
1480 ("xref" 8 gnus-score-string)
1481 ("head" -1 gnus-score-body)
1482 ("body" -1 gnus-score-body)
1483 ("all" -1 gnus-score-body)
1484 ("followup" 2 gnus-score-followup)))
1486 (defun gnus-current-score-file-nondirectory (&optional score-file)
1487 (let ((score-file (or score-file gnus-current-score-file)))
1489 (gnus-short-group-name (file-name-nondirectory score-file))
1492 (defun gnus-score-adaptive ()
1494 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
1496 (date (current-time-string))
1498 ;; First we transform the adaptive rule alist into something
1499 ;; that's faster to process.
1501 (setq elem (car malist))
1502 (if (symbolp (car elem))
1503 (setcar elem (symbol-value (car elem))))
1504 (setq elem (cdr elem))
1507 (cons (symbol-name (car (car elem))) (cdr (car elem))))
1510 (concat "gnus-header-"
1511 (downcase (symbol-name (car (car elem)))))))
1512 (setq elem (cdr elem)))
1513 (setq malist (cdr malist)))
1514 ;; We change the score file to the adaptive score file.
1515 (gnus-score-load-file (gnus-score-file-name
1516 gnus-newsgroup-name gnus-adaptive-file-suffix))
1517 ;; The we score away.
1518 (goto-char (point-min))
1520 (setq elem (cdr (assq (gnus-summary-article-mark) alist)))
1522 (get-text-property (point) 'gnus-pseudo))
1524 (setq headers (gnus-summary-article-header))
1525 (while (and elem headers)
1526 (setq match (funcall (car (car elem)) headers))
1527 (gnus-summary-score-entry
1528 (nth 1 (car elem)) match
1532 ((equal (nth 1 (car elem)) "date")
1535 ;; Whether we use substring or exact matches are controlled
1537 (if (or (not gnus-score-exact-adapt-limit)
1538 (< (length match) gnus-score-exact-adapt-limit))
1540 (if (equal (nth 1 (car elem)) "subject")
1542 (nth 2 (car elem)) date nil t)
1543 (setq elem (cdr elem))))
1544 (forward-line 1)))))
1546 (defun gnus-score-remove-lines-adaptive (marks)
1548 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
1550 (date (current-time-string))
1551 (cur-score gnus-current-score-file)
1553 ;; First we transform the adaptive rule alist into something
1554 ;; that's faster to process.
1556 (setq elem (car malist))
1557 (if (symbolp (car elem))
1558 (setcar elem (symbol-value (car elem))))
1559 (setq elem (cdr elem))
1562 (cons (symbol-name (car (car elem))) (cdr (car elem))))
1565 (concat "gnus-header-"
1566 (downcase (symbol-name (car (car elem)))))))
1567 (setq elem (cdr elem)))
1568 (setq malist (cdr malist)))
1569 ;; The we score away.
1570 (goto-char (point-min))
1571 ;; We change the score file to the adaptive score file.
1572 (gnus-score-load-file (gnus-score-file-name
1573 gnus-newsgroup-name gnus-adaptive-file-suffix))
1574 (while (re-search-forward marks nil t)
1576 (setq elem (cdr (assq (gnus-summary-article-mark) alist)))
1578 (get-text-property (gnus-point-at-bol) 'gnus-pseudo))
1580 (setq headers (gnus-summary-article-header))
1582 (setq match (funcall (car (car elem)) headers))
1583 (gnus-summary-score-entry
1584 (nth 1 (car elem)) match
1585 (if (or (not gnus-score-exact-adapt-limit)
1586 (< (length match) gnus-score-exact-adapt-limit))
1588 (nth 2 (car elem)) date nil t)
1589 (setq elem (cdr elem)))))
1590 ;; Switch back to the old score file.
1591 (gnus-score-load-file cur-score))))
1597 (defvar gnus-score-mode-map nil)
1598 (defvar gnus-score-mode-hook nil)
1600 (if gnus-score-mode-map
1602 (setq gnus-score-mode-map (copy-keymap emacs-lisp-mode-map))
1603 (define-key gnus-score-mode-map "\C-c\C-c" 'gnus-score-edit-done)
1604 (define-key gnus-score-mode-map "\C-c\C-d" 'gnus-score-edit-insert-date))
1606 (defun gnus-score-mode ()
1607 "Mode for editing score files.
1608 This mode is an extended emacs-lisp mode.
1610 \\{gnus-score-mode-map}"
1612 (kill-all-local-variables)
1613 (use-local-map gnus-score-mode-map)
1614 (set-syntax-table emacs-lisp-mode-syntax-table)
1615 (setq major-mode 'gnus-score-mode)
1616 (setq mode-name "Score")
1617 (lisp-mode-variables nil)
1618 (run-hooks 'emacs-lisp-mode-hook 'gnus-score-mode-hook))
1620 (defun gnus-score-edit-insert-date ()
1621 "Insert date in numerical format."
1623 (insert (int-to-string (gnus-day-number (current-time-string)))))
1625 (defun gnus-score-edit-done ()
1626 "Save the score file and return to the summary buffer."
1628 (let ((bufnam (buffer-file-name (current-buffer)))
1629 (winconf gnus-prev-winconf))
1630 (gnus-make-directory (file-name-directory (buffer-file-name)))
1632 (kill-buffer (current-buffer))
1633 (gnus-score-remove-from-cache bufnam)
1634 (gnus-score-load-file bufnam)
1635 (and winconf (set-window-configuration winconf))))
1637 (defun gnus-score-find-trace ()
1638 "Find all score rules applied to this article."
1640 (let ((gnus-newsgroup-headers
1641 (list (gnus-summary-article-header)))
1642 (gnus-newsgroup-scored nil)
1643 (buf (current-buffer))
1645 (setq gnus-score-trace nil)
1646 (gnus-possibly-score-headers 'trace)
1647 (or (setq trace gnus-score-trace)
1648 (error "No score rules apply to the current article."))
1649 (pop-to-buffer "*Gnus Scores*")
1650 (gnus-add-current-to-buffer-list)
1653 (insert (format "%S -> %s\n" (cdr (car trace))
1654 (file-name-nondirectory (car (car trace)))))
1655 (setq trace (cdr trace)))
1656 (goto-char (point-min))
1657 (pop-to-buffer buf)))
1659 (defun gnus-score-flush-cache ()
1660 "Flush the cache of score files."
1662 (setq gnus-score-cache nil)
1663 (gnus-message 6 "The score cache is now flushed"))
1665 (defun gnus-score-close ()
1666 "Clear all internal score variables."
1667 (setq gnus-score-cache nil
1668 gnus-internal-global-score-files nil))
1670 (provide 'gnus-score)
1672 ;;; gnus-score.el ends here