* lisp/subr.el (internal--called-interactively-p--get-frame): Avoid filling
[emacs.git] / lisp / gnus / gnus-score.el
blobda5c31325bd3016c9acfa4cdd8a4e59542a71f33
1 ;;; gnus-score.el --- scoring code for Gnus
3 ;; Copyright (C) 1995-2013 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <amanda@iesd.auc.dk>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile (require 'cl))
30 (require 'gnus)
31 (require 'gnus-sum)
32 (require 'gnus-range)
33 (require 'gnus-win)
34 (require 'message)
35 (require 'score-mode)
36 (require 'gmm-utils)
38 (defcustom gnus-global-score-files nil
39 "List of global score files and directories.
40 Set this variable if you want to use people's score files. One entry
41 for each score file or each score file directory. Gnus will decide
42 by itself what score files are applicable to which group.
44 Say you want to use the single score file
45 \"/ftp.gnus.org@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
46 score files in the \"/ftp.some-where:/pub/score\" directory.
48 (setq gnus-global-score-files
49 '(\"/ftp.gnus.org:/pub/larsi/ding/score/soc.motss.SCORE\"
50 \"/ftp.some-where:/pub/score\"))"
51 :group 'gnus-score-files
52 :type '(repeat file))
54 (defcustom gnus-score-file-single-match-alist nil
55 "Alist mapping regexps to lists of score files.
56 Each element of this alist should be of the form
57 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
59 If the name of a group is matched by REGEXP, the corresponding scorefiles
60 will be used for that group.
61 The first match found is used, subsequent matching entries are ignored (to
62 use multiple matches, see `gnus-score-file-multiple-match-alist').
64 These score files are loaded in addition to any files returned by
65 `gnus-score-find-score-files-function'."
66 :group 'gnus-score-files
67 :type '(repeat (cons regexp (repeat file))))
69 (defcustom gnus-score-file-multiple-match-alist nil
70 "Alist mapping regexps to lists of score files.
71 Each element of this alist should be of the form
72 (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
74 If the name of a group is matched by REGEXP, the corresponding scorefiles
75 will be used for that group.
76 If multiple REGEXPs match a group, the score files corresponding to each
77 match will be used (for only one match to be used, see
78 `gnus-score-file-single-match-alist').
80 These score files are loaded in addition to any files returned by
81 `gnus-score-find-score-files-function'."
82 :group 'gnus-score-files
83 :type '(repeat (cons regexp (repeat file))))
85 (defcustom gnus-score-file-suffix "SCORE"
86 "Suffix of the score files."
87 :group 'gnus-score-files
88 :type 'string)
90 (defcustom gnus-adaptive-file-suffix "ADAPT"
91 "Suffix of the adaptive score files."
92 :group 'gnus-score-files
93 :group 'gnus-score-adapt
94 :type 'string)
96 (defcustom gnus-score-find-score-files-function 'gnus-score-find-bnews
97 "Function used to find score files.
98 The function will be called with the group name as the argument, and
99 should return a list of score files to apply to that group. The score
100 files do not actually have to exist.
102 Predefined values are:
104 `gnus-score-find-single': Only apply the group's own score file.
105 `gnus-score-find-hierarchical': Also apply score files from parent groups.
106 `gnus-score-find-bnews': Apply score files whose names matches.
108 See the documentation to these functions for more information.
110 This variable can also be a list of functions to be called. Each
111 function is given the group name as argument and should either return
112 a list of score files, or a list of score alists.
114 If functions other than these pre-defined functions are used,
115 the `a' symbolic prefix to the score commands will always use
116 \"all.SCORE\"."
117 :group 'gnus-score-files
118 :type '(radio (function-item gnus-score-find-single)
119 (function-item gnus-score-find-hierarchical)
120 (function-item gnus-score-find-bnews)
121 (repeat :tag "List of functions"
122 (choice (function :tag "Other" :value 'ignore)
123 (function-item gnus-score-find-single)
124 (function-item gnus-score-find-hierarchical)
125 (function-item gnus-score-find-bnews)))
126 (function :tag "Other" :value 'ignore)))
128 (defcustom gnus-score-interactive-default-score 1000
129 "*Scoring commands will raise/lower the score with this number as the default."
130 :group 'gnus-score-default
131 :type 'integer)
133 (defcustom gnus-score-expiry-days 7
134 "*Number of days before unused score file entries are expired.
135 If this variable is nil, no score file entries will be expired."
136 :group 'gnus-score-expire
137 :type '(choice (const :tag "never" nil)
138 number))
140 (defcustom gnus-update-score-entry-dates t
141 "*If non-nil, update matching score entry dates.
142 If this variable is nil, then score entries that provide matches
143 will be expired along with non-matching score entries."
144 :group 'gnus-score-expire
145 :type 'boolean)
147 (defcustom gnus-decay-scores nil
148 "*If non-nil, decay non-permanent scores.
150 If it is a regexp, only decay score files matching regexp."
151 :group 'gnus-score-decay
152 :type `(choice (const :tag "never" nil)
153 (const :tag "always" t)
154 (const :tag "adaptive score files"
155 ,(concat "\\." gnus-adaptive-file-suffix "\\'"))
156 (regexp)))
158 (defcustom gnus-decay-score-function 'gnus-decay-score
159 "*Function called to decay a score.
160 It is called with one parameter -- the score to be decayed."
161 :group 'gnus-score-decay
162 :type '(radio (function-item gnus-decay-score)
163 (function :tag "Other")))
165 (defcustom gnus-score-decay-constant 3
166 "*Decay all \"small\" scores with this amount."
167 :group 'gnus-score-decay
168 :type 'integer)
170 (defcustom gnus-score-decay-scale .05
171 "*Decay all \"big\" scores with this factor."
172 :group 'gnus-score-decay
173 :type 'number)
175 (defcustom gnus-home-score-file nil
176 "Variable to control where interactive score entries are to go.
177 It can be:
179 * A string
180 This file will be used as the home score file.
182 * A function
183 The result of this function will be used as the home score file.
184 The function will be passed the name of the group as its
185 parameter.
187 * A list
188 The elements in this list can be:
190 * `(regexp file-name ...)'
191 If the `regexp' matches the group name, the first `file-name'
192 will be used as the home score file. (Multiple filenames are
193 allowed so that one may use gnus-score-file-single-match-alist to
194 set this variable.)
196 * A function.
197 If the function returns non-nil, the result will be used
198 as the home score file. The function will be passed the
199 name of the group as its parameter.
201 * A string. Use the string as the home score file.
203 The list will be traversed from the beginning towards the end looking
204 for matches."
205 :group 'gnus-score-files
206 :type '(choice string
207 (repeat (choice string
208 (cons regexp (repeat file))
209 function))
210 (function-item gnus-hierarchial-home-score-file)
211 (function-item gnus-current-home-score-file)
212 function))
214 (defcustom gnus-home-adapt-file nil
215 "Variable to control where new adaptive score entries are to go.
216 This variable allows the same syntax as `gnus-home-score-file'."
217 :group 'gnus-score-adapt
218 :group 'gnus-score-files
219 :type '(choice string
220 (repeat (choice string
221 (cons regexp (repeat file))
222 function))
223 function))
225 (defcustom gnus-default-adaptive-score-alist
226 `((gnus-kill-file-mark)
227 (gnus-unread-mark)
228 (gnus-read-mark
229 (from , (+ 2 gnus-score-decay-constant))
230 (subject , (+ 27 gnus-score-decay-constant)))
231 (gnus-catchup-mark
232 (subject , (+ -7 (* -1 gnus-score-decay-constant))))
233 (gnus-killed-mark
234 (from , (- -1 gnus-score-decay-constant))
235 (subject , (+ -17 (* -1 gnus-score-decay-constant))))
236 (gnus-del-mark
237 (from , (- -1 gnus-score-decay-constant))
238 (subject , (+ -12 (* -1 gnus-score-decay-constant)))))
239 "Alist of marks and scores.
240 If you use score decays, you might want to set values higher than
241 `gnus-score-decay-constant'."
242 :group 'gnus-score-adapt
243 :type '(repeat (cons (symbol :tag "Mark")
244 (repeat (list (choice :tag "Header"
245 (const from)
246 (const subject)
247 (symbol :tag "other"))
248 (integer :tag "Score"))))))
250 (defcustom gnus-adaptive-word-length-limit nil
251 "*Words of a length lesser than this limit will be ignored when doing adaptive scoring."
252 :version "22.1"
253 :group 'gnus-score-adapt
254 :type '(radio (const :format "Unlimited " nil)
255 (integer :format "Maximum length: %v")))
257 (defcustom gnus-ignored-adaptive-words nil
258 "List of words to be ignored when doing adaptive word scoring."
259 :group 'gnus-score-adapt
260 :type '(repeat string))
262 (defcustom gnus-default-ignored-adaptive-words
263 '("a" "i" "the" "to" "of" "and" "in" "is" "it" "for" "that" "if" "you"
264 "this" "be" "on" "with" "not" "have" "are" "or" "as" "from" "can"
265 "but" "by" "at" "an" "will" "no" "all" "was" "do" "there" "my" "one"
266 "so" "we" "they" "what" "would" "any" "which" "about" "get" "your"
267 "use" "some" "me" "then" "name" "like" "out" "when" "up" "time"
268 "other" "more" "only" "just" "end" "also" "know" "how" "new" "should"
269 "been" "than" "them" "he" "who" "make" "may" "people" "these" "now"
270 "their" "here" "into" "first" "could" "way" "had" "see" "work" "well"
271 "were" "two" "very" "where" "while" "us" "because" "good" "same"
272 "even" "much" "most" "many" "such" "long" "his" "over" "last" "since"
273 "right" "before" "our" "without" "too" "those" "why" "must" "part"
274 "being" "current" "back" "still" "go" "point" "value" "each" "did"
275 "both" "true" "off" "say" "another" "state" "might" "under" "start"
276 "try" "re")
277 "*Default list of words to be ignored when doing adaptive word scoring."
278 :group 'gnus-score-adapt
279 :type '(repeat string))
281 (defcustom gnus-default-adaptive-word-score-alist
282 `((,gnus-read-mark . 30)
283 (,gnus-catchup-mark . -10)
284 (,gnus-killed-mark . -20)
285 (,gnus-del-mark . -15))
286 "*Alist of marks and scores."
287 :group 'gnus-score-adapt
288 :type '(repeat (cons (character :tag "Mark")
289 (integer :tag "Score"))))
291 (defcustom gnus-adaptive-word-minimum nil
292 "If a number, this is the minimum score value that can be assigned to a word."
293 :group 'gnus-score-adapt
294 :type '(choice (const nil) integer))
296 (defcustom gnus-adaptive-word-no-group-words nil
297 "If t, don't adaptively score words included in the group name."
298 :group 'gnus-score-adapt
299 :type 'boolean)
301 (defcustom gnus-score-mimic-keymap nil
302 "*Have the score entry functions pretend that they are a keymap."
303 :group 'gnus-score-default
304 :type 'boolean)
306 (defcustom gnus-score-exact-adapt-limit 10
307 "*Number that says how long a match has to be before using substring matching.
308 When doing adaptive scoring, one normally uses fuzzy or substring
309 matching. However, if the header one matches is short, the possibility
310 for false positives is great, so if the length of the match is less
311 than this variable, exact matching will be used.
313 If this variable is nil, exact matching will always be used."
314 :group 'gnus-score-adapt
315 :type '(choice (const nil) integer))
317 (defcustom gnus-score-uncacheable-files "ADAPT$"
318 "All score files that match this regexp will not be cached."
319 :group 'gnus-score-adapt
320 :group 'gnus-score-files
321 :type 'regexp)
323 (defcustom gnus-adaptive-pretty-print nil
324 "If non-nil, adaptive score files fill are pretty printed."
325 :group 'gnus-score-files
326 :group 'gnus-score-adapt
327 :version "23.1" ;; No Gnus
328 :type 'boolean)
330 (defcustom gnus-score-default-header nil
331 "Default header when entering new scores.
333 Should be one of the following symbols.
335 a: from
336 s: subject
337 b: body
338 h: head
339 i: message-id
340 t: references
341 x: xref
342 e: `extra' (non-standard overview)
343 l: lines
344 d: date
345 f: followup
347 If nil, the user will be asked for a header."
348 :group 'gnus-score-default
349 :type '(choice (const :tag "from" a)
350 (const :tag "subject" s)
351 (const :tag "body" b)
352 (const :tag "head" h)
353 (const :tag "message-id" i)
354 (const :tag "references" t)
355 (const :tag "xref" x)
356 (const :tag "extra" e)
357 (const :tag "lines" l)
358 (const :tag "date" d)
359 (const :tag "followup" f)
360 (const :tag "ask" nil)))
362 (defcustom gnus-score-default-type nil
363 "Default match type when entering new scores.
365 Should be one of the following symbols.
367 s: substring
368 e: exact string
369 f: fuzzy string
370 r: regexp string
371 b: before date
372 a: after date
373 n: this date
374 <: less than number
375 >: greater than number
376 =: equal to number
378 If nil, the user will be asked for a match type."
379 :group 'gnus-score-default
380 :type '(choice (const :tag "substring" s)
381 (const :tag "exact string" e)
382 (const :tag "fuzzy string" f)
383 (const :tag "regexp string" r)
384 (const :tag "before date" b)
385 (const :tag "after date" a)
386 (const :tag "this date" n)
387 (const :tag "less than number" <)
388 (const :tag "greater than number" >)
389 (const :tag "equal than number" =)
390 (const :tag "ask" nil)))
392 (defcustom gnus-score-default-fold nil
393 "Non-nil means use case folding for new score file entries."
394 :group 'gnus-score-default
395 :type 'boolean)
397 (defcustom gnus-score-default-duration nil
398 "Default duration of effect when entering new scores.
400 Should be one of the following symbols.
402 t: temporary
403 p: permanent
404 i: immediate
406 If nil, the user will be asked for a duration."
407 :group 'gnus-score-default
408 :type '(choice (const :tag "temporary" t)
409 (const :tag "permanent" p)
410 (const :tag "immediate" i)
411 (const :tag "ask" nil)))
413 (defcustom gnus-score-after-write-file-function nil
414 "Function called with the name of the score file just written to disk."
415 :group 'gnus-score-files
416 :type '(choice (const nil) function))
418 (defcustom gnus-score-thread-simplify nil
419 "If non-nil, subjects will simplified as in threading."
420 :group 'gnus-score-various
421 :type 'boolean)
423 (defcustom gnus-inhibit-slow-scoring nil
424 "Inhibit slow scoring, e.g. scoring on headers or body.
426 If a regexp, scoring on headers or body is inhibited if the group
427 matches the regexp. If it is t, scoring on headers or body is
428 inhibited for all groups."
429 :group 'gnus-score-various
430 :version "23.1" ;; No Gnus
431 :type '(choice (const :tag "All" nil)
432 (const :tag "None" t)
433 regexp))
437 ;; Internal variables.
439 (defvar gnus-score-use-all-scores t
440 "If nil, only `gnus-score-find-score-files-function' is used.")
442 (defvar gnus-adaptive-word-syntax-table
443 (let ((table (copy-syntax-table (standard-syntax-table)))
444 (numbers '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)))
445 (while numbers
446 (modify-syntax-entry (pop numbers) " " table))
447 (modify-syntax-entry ?' "w" table)
448 table)
449 "Syntax table used when doing adaptive word scoring.")
451 (defvar gnus-scores-exclude-files nil)
452 (defvar gnus-internal-global-score-files nil)
453 (defvar gnus-score-file-list nil)
455 (defvar gnus-short-name-score-file-cache nil)
457 (defvar gnus-score-help-winconf nil)
458 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
459 (defvar gnus-adaptive-word-score-alist gnus-default-adaptive-word-score-alist)
460 (defvar gnus-score-trace nil)
461 (defvar gnus-score-edit-buffer nil)
463 (defvar gnus-score-alist nil
464 "Alist containing score information.
465 The keys can be symbols or strings. The following symbols are defined.
467 touched: If this alist has been modified.
468 mark: Automatically mark articles below this.
469 expunge: Automatically expunge articles below this.
470 files: List of other score files to load when loading this one.
471 eval: Sexp to be evaluated when the score file is loaded.
473 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
474 where HEADER is the header being scored, MATCH is the string we are
475 looking for, TYPE is a flag indicating whether it should use regexp or
476 substring matching, SCORE is the score to add and DATE is the date
477 of the last successful match.")
479 (defvar gnus-score-cache nil)
480 (defvar gnus-scores-articles nil)
481 (defvar gnus-score-index nil)
484 (defconst gnus-header-index
485 ;; Name to index alist.
486 '(("number" 0 gnus-score-integer)
487 ("subject" 1 gnus-score-string)
488 ("from" 2 gnus-score-string)
489 ("date" 3 gnus-score-date)
490 ("message-id" 4 gnus-score-string)
491 ("references" 5 gnus-score-string)
492 ("chars" 6 gnus-score-integer)
493 ("lines" 7 gnus-score-integer)
494 ("xref" 8 gnus-score-string)
495 ("extra" 9 gnus-score-string)
496 ("head" -1 gnus-score-body)
497 ("body" -1 gnus-score-body)
498 ("all" -1 gnus-score-body)
499 ("followup" 2 gnus-score-followup)
500 ("thread" 5 gnus-score-thread)))
502 ;;; Summary mode score maps.
504 (gnus-define-keys (gnus-summary-score-map "V" gnus-summary-mode-map)
505 "s" gnus-summary-set-score
506 "S" gnus-summary-current-score
507 "c" gnus-score-change-score-file
508 "C" gnus-score-customize
509 "m" gnus-score-set-mark-below
510 "x" gnus-score-set-expunge-below
511 "R" gnus-summary-rescore
512 "e" gnus-score-edit-current-scores
513 "f" gnus-score-edit-file
514 "F" gnus-score-flush-cache
515 "t" gnus-score-find-trace
516 "w" gnus-score-find-favourite-words)
518 ;; Summary score file commands
520 ;; Much modification of the kill (ahem, score) code and lots of the
521 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
523 (defun gnus-summary-lower-score (&optional score symp)
524 "Make a score entry based on the current article.
525 The user will be prompted for header to score on, match type,
526 permanence, and the string to be used. The numerical prefix will
527 be used as SCORE. A symbolic prefix of `a' (the SYMP parameter)
528 says to use the `all.SCORE' file for the command instead of the
529 current score file."
530 (interactive (gnus-interactive "P\ny"))
531 (gnus-summary-increase-score (- (gnus-score-delta-default score)) symp))
533 (defun gnus-score-kill-help-buffer ()
534 (when (get-buffer "*Score Help*")
535 (kill-buffer "*Score Help*")
536 (when gnus-score-help-winconf
537 (set-window-configuration gnus-score-help-winconf))))
539 (defun gnus-summary-increase-score (&optional score symp)
540 "Make a score entry based on the current article.
541 The user will be prompted for header to score on, match type,
542 permanence, and the string to be used. The numerical prefix will
543 be used as SCORE. A symbolic prefix of `a' (the SYMP parameter)
544 says to use the `all.SCORE' file for the command instead of the
545 current score file."
546 (interactive (gnus-interactive "P\ny"))
547 (let* ((nscore (gnus-score-delta-default score))
548 (prefix (if (< nscore 0) ?L ?I))
549 (increase (> nscore 0))
550 (char-to-header
551 '((?a "from" nil nil string)
552 (?s "subject" nil nil string)
553 (?b "body" "" nil body-string)
554 (?h "head" "" nil body-string)
555 (?i "message-id" nil nil string)
556 (?r "references" "message-id" nil string)
557 (?x "xref" nil nil string)
558 (?e "extra" nil nil string)
559 (?l "lines" nil nil number)
560 (?d "date" nil nil date)
561 (?f "followup" nil nil string)
562 (?t "thread" "message-id" nil string)))
563 (char-to-type
564 '((?s s "substring" string)
565 (?e e "exact string" string)
566 (?f f "fuzzy string" string)
567 (?r r "regexp string" string)
568 (?z s "substring" body-string)
569 (?p r "regexp string" body-string)
570 (?b before "before date" date)
571 (?a after "after date" date)
572 (?n at "this date" date)
573 (?< < "less than number" number)
574 (?> > "greater than number" number)
575 (?= = "equal to number" number)))
576 (current-score-file gnus-current-score-file)
577 (char-to-perm
578 (list (list ?t (current-time-string) "temporary")
579 '(?p perm "permanent") '(?i now "immediate")))
580 (mimic gnus-score-mimic-keymap)
581 (hchar (and gnus-score-default-header
582 (aref (symbol-name gnus-score-default-header) 0)))
583 (tchar (and gnus-score-default-type
584 (aref (symbol-name gnus-score-default-type) 0)))
585 (pchar (and gnus-score-default-duration
586 (aref (symbol-name gnus-score-default-duration) 0)))
587 entry temporary type match extra)
589 (unwind-protect
590 (progn
592 ;; First we read the header to score.
593 (while (not hchar)
594 (if mimic
595 (progn
596 (sit-for 1)
597 (message "%c-" prefix))
598 (message "%s header (%s?): " (if increase "Increase" "Lower")
599 (mapconcat (lambda (s) (char-to-string (car s)))
600 char-to-header "")))
601 (setq hchar (read-char))
602 (when (or (= hchar ??) (= hchar ?\C-h))
603 (setq hchar nil)
604 (gnus-score-insert-help "Match on header" char-to-header 1)))
606 (gnus-score-kill-help-buffer)
607 (unless (setq entry (assq (downcase hchar) char-to-header))
608 (if mimic (error "%c %c" prefix hchar)
609 (error "Invalid header type")))
611 (when (/= (downcase hchar) hchar)
612 ;; This was a majuscule, so we end reading and set the defaults.
613 (if mimic (message "%c %c" prefix hchar) (message ""))
614 (setq tchar (or tchar ?s)
615 pchar (or pchar ?t)))
617 (let ((legal-types
618 (delq nil
619 (mapcar (lambda (s)
620 (if (eq (nth 4 entry)
621 (nth 3 s))
622 s nil))
623 char-to-type))))
624 ;; We continue reading - the type.
625 (while (not tchar)
626 (if mimic
627 (progn
628 (sit-for 1) (message "%c %c-" prefix hchar))
629 (message "%s header '%s' with match type (%s?): "
630 (if increase "Increase" "Lower")
631 (nth 1 entry)
632 (mapconcat (lambda (s) (char-to-string (car s)))
633 legal-types "")))
634 (setq tchar (read-char))
635 (when (or (= tchar ??) (= tchar ?\C-h))
636 (setq tchar nil)
637 (gnus-score-insert-help "Match type" legal-types 2)))
639 (gnus-score-kill-help-buffer)
640 (unless (setq type (nth 1 (assq (downcase tchar) legal-types)))
641 (if mimic (error "%c %c" prefix hchar)
642 (error "Invalid match type"))))
644 (when (/= (downcase tchar) tchar)
645 ;; It was a majuscule, so we end reading and use the default.
646 (if mimic (message "%c %c %c" prefix hchar tchar)
647 (message ""))
648 (setq pchar (or pchar ?t)))
650 ;; We continue reading.
651 (while (not pchar)
652 (if mimic
653 (progn
654 (sit-for 1) (message "%c %c %c-" prefix hchar tchar))
655 (message "%s permanence (%s?): " (if increase "Increase" "Lower")
656 (mapconcat (lambda (s) (char-to-string (car s)))
657 char-to-perm "")))
658 (setq pchar (read-char))
659 (when (or (= pchar ??) (= pchar ?\C-h))
660 (setq pchar nil)
661 (gnus-score-insert-help "Match permanence" char-to-perm 2)))
663 (gnus-score-kill-help-buffer)
664 (if mimic (message "%c %c %c %c" prefix hchar tchar pchar)
665 (message ""))
666 (unless (setq temporary (cadr (assq pchar char-to-perm)))
667 ;; Deal with der(r)ided superannuated paradigms.
668 (when (and (eq (1+ prefix) 77)
669 (eq (+ hchar 12) 109)
670 (eq (1- tchar) 113)
671 (eq (- pchar 4) 111))
672 (error "You rang?"))
673 (if mimic
674 (error "%c %c %c %c" prefix hchar tchar pchar)
675 (error "Invalid match duration"))))
676 ;; Always kill the score help buffer.
677 (gnus-score-kill-help-buffer))
679 ;; If scoring an extra (non-standard overview) header,
680 ;; we must find out which header is in question.
681 (setq extra
682 (and gnus-extra-headers
683 (equal (nth 1 entry) "extra")
684 (intern ; need symbol
685 (let ((collection (mapcar 'symbol-name gnus-extra-headers)))
686 (gnus-completing-read
687 "Score extra header" ; prompt
688 collection ; completion list
689 t ; require match
690 nil ; no history
691 nil ; no initial-input
692 (car collection)))))) ; default value
693 ;; extra is now nil or a symbol.
695 ;; We have all the data, so we enter this score.
696 (setq match (if (string= (nth 2 entry) "") ""
697 (gnus-summary-header (or (nth 2 entry) (nth 1 entry))
698 nil extra)))
700 ;; Modify the match, perhaps.
701 (cond
702 ((equal (nth 1 entry) "xref")
703 (when (string-match "^Xref: *" match)
704 (setq match (substring match (match-end 0))))
705 (when (string-match "^[^:]* +" match)
706 (setq match (substring match (match-end 0))))))
708 (when (memq type '(r R regexp Regexp))
709 (setq match (regexp-quote match)))
711 ;; Change score file to the "all.SCORE" file.
712 (when (eq symp 'a)
713 (with-current-buffer gnus-summary-buffer
714 (gnus-score-load-file
715 ;; This is a kludge; yes...
716 (cond
717 ((eq gnus-score-find-score-files-function
718 'gnus-score-find-hierarchical)
719 (gnus-score-file-name ""))
720 ((eq gnus-score-find-score-files-function 'gnus-score-find-single)
721 current-score-file)
723 (gnus-score-file-name "all"))))))
725 (gnus-summary-score-entry
726 (nth 1 entry) ; Header
727 match ; Match
728 type ; Type
729 (if (eq score 's) nil score) ; Score
730 (if (eq temporary 'perm) ; Temp
732 temporary)
733 (not (nth 3 entry)) ; Prompt
734 nil ; not silent
735 extra) ; non-standard overview.
737 (when (eq symp 'a)
738 ;; We change the score file back to the previous one.
739 (with-current-buffer gnus-summary-buffer
740 (gnus-score-load-file current-score-file)))))
742 (defun gnus-score-insert-help (string alist idx)
743 (setq gnus-score-help-winconf (current-window-configuration))
744 (with-current-buffer (gnus-get-buffer-create "*Score Help*")
745 (buffer-disable-undo)
746 (delete-windows-on (current-buffer))
747 (erase-buffer)
748 (insert string ":\n\n")
749 (let ((max -1)
750 (list alist)
751 (i 0)
752 n width pad format)
753 ;; find the longest string to display
754 (while list
755 (setq n (length (nth idx (car list))))
756 (unless (> max n)
757 (setq max n))
758 (setq list (cdr list)))
759 (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
760 (setq n (/ (1- (window-width)) max)) ; items per line
761 (setq width (/ (1- (window-width)) n)) ; width of each item
762 ;; insert `n' items, each in a field of width `width'
763 (while alist
764 (if (< i n)
766 (setq i 0)
767 (delete-char -1) ; the `\n' takes a char
768 (insert "\n"))
769 (setq pad (- width 3))
770 (setq format (concat "%c: %-" (int-to-string pad) "s"))
771 (insert (format format (caar alist) (nth idx (car alist))))
772 (setq alist (cdr alist))
773 (setq i (1+ i))))
774 (goto-char (point-min))
775 ;; display ourselves in a small window at the bottom
776 (gnus-select-lowest-window)
777 (if (< (/ (window-height) 2) window-min-height)
778 (switch-to-buffer "*Score Help*")
779 (split-window)
780 (pop-to-buffer "*Score Help*"))
781 (let ((window-min-height 1))
782 (shrink-window-if-larger-than-buffer))
783 (select-window (gnus-get-buffer-window gnus-summary-buffer t))))
785 (defun gnus-summary-header (header &optional no-err extra)
786 ;; Return HEADER for current articles, or error.
787 (let ((article (gnus-summary-article-number))
788 headers)
789 (if article
790 (if (and (setq headers (gnus-summary-article-header article))
791 (vectorp headers))
792 (if extra ; `header' must be "extra"
793 (or (cdr (assq extra (mail-header-extra headers))) "")
794 (aref headers (nth 1 (assoc header gnus-header-index))))
795 (if no-err
797 (error "Pseudo-articles can't be scored")))
798 (if no-err
799 (error "No article on current line")
800 nil))))
802 (defun gnus-newsgroup-score-alist ()
804 (let ((param-file (gnus-group-find-parameter
805 gnus-newsgroup-name 'score-file)))
806 (when param-file
807 (gnus-score-load param-file)))
808 (gnus-score-load
809 (gnus-score-file-name gnus-newsgroup-name)))
810 gnus-score-alist)
812 (defsubst gnus-score-get (symbol &optional alist)
813 ;; Get SYMBOL's definition in ALIST.
814 (cdr (assoc symbol
815 (or alist
816 gnus-score-alist
817 (gnus-newsgroup-score-alist)))))
819 (defun gnus-summary-score-entry (header match type score date
820 &optional prompt silent extra)
821 "Enter score file entry.
822 HEADER is the header being scored.
823 MATCH is the string we are looking for.
824 TYPE is the match type: substring, regexp, exact, fuzzy.
825 SCORE is the score to add.
826 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
827 If optional argument `PROMPT' is non-nil, allow user to edit match.
828 If optional argument `SILENT' is nil, show effect of score entry.
829 If optional argument `EXTRA' is non-nil, it's a non-standard overview header."
830 ;; Regexp is the default type.
831 (when (eq type t)
832 (setq type 'r))
833 ;; Simplify matches...
834 (cond ((or (eq type 'r) (eq type 's) (eq type nil))
835 (setq match (if match (gnus-simplify-subject-re match) "")))
836 ((eq type 'f)
837 (setq match (gnus-simplify-subject-fuzzy match))))
838 (let ((score (gnus-score-delta-default score))
839 (header (downcase header))
840 new)
841 (set-text-properties 0 (length header) nil header)
842 (when prompt
843 (setq match (read-string
844 (format "Match %s on %s, %s: "
845 (cond ((eq date 'now)
846 "now")
847 ((stringp date)
848 "temp")
849 (t "permanent"))
850 header
851 (if (< score 0) "lower" "raise"))
852 (if (numberp match)
853 (int-to-string match)
854 match))))
856 ;; If this is an integer comparison, we transform from string to int.
857 (if (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
858 (if (stringp match)
859 (setq match (string-to-number match)))
860 (set-text-properties 0 (length match) nil match))
862 (unless (eq date 'now)
863 ;; Add the score entry to the score file.
864 (when (= score gnus-score-interactive-default-score)
865 (setq score nil))
866 (let ((old (gnus-score-get header))
867 elem)
868 (setq new
869 (cond
870 (extra
871 (list match score
872 (and date (if (numberp date) date
873 (date-to-day date)))
874 type (symbol-name extra)))
875 (type
876 (list match score
877 (and date (if (numberp date) date
878 (date-to-day date)))
879 type))
880 (date (list match score (date-to-day date)))
881 (score (list match score))
882 (t (list match))))
883 ;; We see whether we can collapse some score entries.
884 ;; This isn't quite correct, because there may be more elements
885 ;; later on with the same key that have matching elems... Hm.
886 (if (and old
887 (setq elem (assoc match old))
888 (eq (nth 3 elem) (nth 3 new))
889 (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
890 (and (not (nth 2 elem)) (not (nth 2 new)))))
891 ;; Yup, we just add this new score to the old elem.
892 (setcar (cdr elem) (+ (or (nth 1 elem)
893 gnus-score-interactive-default-score)
894 (or (nth 1 new)
895 gnus-score-interactive-default-score)))
896 ;; Nope, we have to add a new elem.
897 (gnus-score-set header (if old (cons new old) (list new)) nil t))
898 (gnus-score-set 'touched '(t))))
900 ;; Score the current buffer.
901 (unless silent
902 (if (and (>= (nth 1 (assoc header gnus-header-index)) 0)
903 (eq (nth 2 (assoc header gnus-header-index))
904 'gnus-score-string))
905 (gnus-summary-score-effect header match type score extra)
906 (gnus-summary-rescore)))
908 ;; Return the new scoring rule.
909 new))
911 (defun gnus-summary-score-effect (header match type score &optional extra)
912 "Simulate the effect of a score file entry.
913 HEADER is the header being scored.
914 MATCH is the string we are looking for.
915 TYPE is the score type.
916 SCORE is the score to add.
917 EXTRA is the possible non-standard header."
918 (interactive (list (gnus-completing-read "Header"
919 (mapcar
920 'car
921 (gnus-remove-if-not
922 (lambda (x) (fboundp (nth 2 x)))
923 gnus-header-index))
925 (read-string "Match: ")
926 (if (y-or-n-p "Use regexp match? ") 'r 's)
927 (string-to-number (read-string "Score: "))))
928 (save-excursion
929 (unless (and (stringp match) (> (length match) 0))
930 (error "No match"))
931 (goto-char (point-min))
932 (let ((regexp (cond ((eq type 'f)
933 (gnus-simplify-subject-fuzzy match))
934 ((eq type 'r)
935 match)
936 ((eq type 'e)
937 (concat "\\`" (regexp-quote match) "\\'"))
939 (regexp-quote match)))))
940 (while (not (eobp))
941 (let ((content (gnus-summary-header header 'noerr extra))
942 (case-fold-search t))
943 (and content
944 (when (if (eq type 'f)
945 (string-equal (gnus-simplify-subject-fuzzy content)
946 regexp)
947 (string-match regexp content))
948 (gnus-summary-raise-score score))))
949 (beginning-of-line 2))))
950 (gnus-set-mode-line 'summary))
953 ;;; Gnus Score Files
956 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
958 (defun gnus-score-set-mark-below (score)
959 "Automatically mark articles with score below SCORE as read."
960 (interactive
961 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
962 (string-to-number (read-string "Mark below: ")))))
963 (setq score (or score gnus-summary-default-score 0))
964 (gnus-score-set 'mark (list score))
965 (gnus-score-set 'touched '(t))
966 (setq gnus-summary-mark-below score)
967 (gnus-score-update-lines))
969 (defun gnus-score-update-lines ()
970 "Update all lines in the summary buffer."
971 (save-excursion
972 (goto-char (point-min))
973 (while (not (eobp))
974 (gnus-summary-update-line)
975 (forward-line 1))))
977 (defun gnus-score-update-all-lines ()
978 "Update all lines in the summary buffer, even the hidden ones."
979 (save-excursion
980 (goto-char (point-min))
981 (let (hidden)
982 (while (not (eobp))
983 (when (gnus-summary-show-thread)
984 (push (point) hidden))
985 (gnus-summary-update-line)
986 (forward-line 1))
987 ;; Re-hide the hidden threads.
988 (while hidden
989 (goto-char (pop hidden))
990 (gnus-summary-hide-thread)))))
992 (defun gnus-score-set-expunge-below (score)
993 "Automatically expunge articles with score below SCORE."
994 (interactive
995 (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
996 (string-to-number (read-string "Set expunge below: ")))))
997 (setq score (or score gnus-summary-default-score 0))
998 (gnus-score-set 'expunge (list score))
999 (gnus-score-set 'touched '(t)))
1001 (defun gnus-score-followup-article (&optional score)
1002 "Add SCORE to all followups to the article in the current buffer."
1003 (interactive "P")
1004 (setq score (gnus-score-delta-default score))
1005 (when (gnus-buffer-live-p gnus-summary-buffer)
1006 (save-excursion
1007 (save-restriction
1008 (message-narrow-to-headers)
1009 (let ((id (mail-fetch-field "message-id")))
1010 (when id
1011 (set-buffer gnus-summary-buffer)
1012 (gnus-summary-score-entry
1013 "references" (concat id "[ \t]*$") 'r
1014 score (current-time-string) nil t)))))))
1016 (defun gnus-score-followup-thread (&optional score)
1017 "Add SCORE to all later articles in the thread the current buffer is part of."
1018 (interactive "P")
1019 (setq score (gnus-score-delta-default score))
1020 (when (gnus-buffer-live-p gnus-summary-buffer)
1021 (save-excursion
1022 (save-restriction
1023 (goto-char (point-min))
1024 (let ((id (mail-fetch-field "message-id")))
1025 (when id
1026 (set-buffer gnus-summary-buffer)
1027 (gnus-summary-score-entry
1028 "references" id 's
1029 score (current-time-string))))))))
1031 (defun gnus-score-set (symbol value &optional alist warn)
1032 ;; Set SYMBOL to VALUE in ALIST.
1033 (let* ((alist
1034 (or alist
1035 gnus-score-alist
1036 (gnus-newsgroup-score-alist)))
1037 (entry (assoc symbol alist)))
1038 (cond ((gnus-score-get 'read-only alist)
1039 ;; This is a read-only score file, so we do nothing.
1040 (when warn
1041 (gnus-message 4 "Note: read-only score file; entry discarded")))
1042 (entry
1043 (setcdr entry value))
1044 ((null alist)
1045 (error "Empty alist"))
1047 (setcdr alist
1048 (cons (cons symbol value) (cdr alist)))))))
1050 (defun gnus-summary-raise-score (n)
1051 "Raise the score of the current article by N."
1052 (interactive "p")
1053 (gnus-summary-set-score (+ (gnus-summary-article-score)
1054 (or n gnus-score-interactive-default-score ))))
1056 (defun gnus-summary-set-score (n)
1057 "Set the score of the current article to N."
1058 (interactive "p")
1059 (save-excursion
1060 (gnus-summary-show-thread)
1061 (let ((buffer-read-only nil))
1062 ;; Set score.
1063 (gnus-summary-update-mark
1064 (if (= n (or gnus-summary-default-score 0)) ? ;Whitespace
1065 (if (< n (or gnus-summary-default-score 0))
1066 gnus-score-below-mark gnus-score-over-mark))
1067 'score))
1068 (let* ((article (gnus-summary-article-number))
1069 (score (assq article gnus-newsgroup-scored)))
1070 (if score (setcdr score n)
1071 (push (cons article n) gnus-newsgroup-scored)))
1072 (gnus-summary-update-line)))
1074 (defun gnus-summary-current-score ()
1075 "Return the score of the current article."
1076 (interactive)
1077 (gnus-message 1 "%s" (gnus-summary-article-score)))
1079 (defun gnus-score-change-score-file (file)
1080 "Change current score alist."
1081 (interactive
1082 (list (read-file-name "Change to score file: " gnus-kill-files-directory)))
1083 (gnus-score-load-file file)
1084 (gnus-set-mode-line 'summary))
1086 (defvar gnus-score-edit-exit-function)
1087 (defun gnus-score-edit-current-scores (file)
1088 "Edit the current score alist."
1089 (interactive (list gnus-current-score-file))
1090 (if (not gnus-current-score-file)
1091 (error "No current score file")
1092 (let ((winconf (current-window-configuration)))
1093 (when (buffer-name gnus-summary-buffer)
1094 (gnus-score-save))
1095 (gnus-make-directory (file-name-directory file))
1096 (setq gnus-score-edit-buffer (find-file-noselect file))
1097 (gnus-configure-windows 'edit-score)
1098 (gnus-score-mode)
1099 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1100 (make-local-variable 'gnus-prev-winconf)
1101 (setq gnus-prev-winconf winconf))
1102 (gnus-message
1103 4 "%s" (substitute-command-keys
1104 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits"))))
1106 (defun gnus-score-edit-all-score ()
1107 "Edit the all.SCORE file."
1108 (interactive)
1109 (find-file (gnus-score-file-name "all"))
1110 (gnus-score-mode)
1111 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1112 (gnus-message
1113 4 (substitute-command-keys
1114 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
1116 (defun gnus-score-edit-file (file)
1117 "Edit a score file."
1118 (interactive
1119 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
1120 (gnus-make-directory (file-name-directory file))
1121 (when (buffer-name gnus-summary-buffer)
1122 (gnus-score-save))
1123 (let ((winconf (current-window-configuration)))
1124 (setq gnus-score-edit-buffer (find-file-noselect file))
1125 (gnus-configure-windows 'edit-score)
1126 (gnus-score-mode)
1127 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1128 (make-local-variable 'gnus-prev-winconf)
1129 (setq gnus-prev-winconf winconf))
1130 (gnus-message
1131 4 "%s" (substitute-command-keys
1132 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
1134 (defun gnus-score-edit-file-at-point (&optional format)
1135 "Edit score file at point in Score Trace buffers.
1136 If FORMAT, also format the current score file."
1137 (let* ((rule (save-excursion
1138 (beginning-of-line)
1139 (read (current-buffer))))
1140 (sep "[ \n\r\t]*")
1141 ;; Must be synced with `gnus-score-find-trace':
1142 (reg " -> +")
1143 (file (save-excursion
1144 (end-of-line)
1145 (if (and (re-search-backward reg (point-at-bol) t)
1146 (re-search-forward reg (point-at-eol) t))
1147 (buffer-substring (point) (point-at-eol))
1148 nil))))
1149 (if (or (not file)
1150 (string-match "\\<\\(non-file rule\\|A file\\)\\>" file)
1151 ;; (see `gnus-score-find-trace' and `gnus-score-advanced')
1152 (string= "" file))
1153 (gnus-error 3 "Can't find a score file in current line.")
1154 (gnus-score-edit-file file)
1155 (when format
1156 (gnus-score-pretty-print))
1157 (when (consp rule) ;; the rule exists
1158 (setq rule (mapconcat #'(lambda (obj)
1159 (regexp-quote (format "%S" obj)))
1160 rule
1161 sep))
1162 (goto-char (point-min))
1163 (re-search-forward rule nil t)
1164 ;; make it easy to use `kill-sexp':
1165 (goto-char (1- (match-beginning 0)))))))
1167 (defun gnus-score-load-file (file)
1168 ;; Load score file FILE. Returns a list a retrieved score-alists.
1169 (let* ((file (expand-file-name
1170 (or (and (string-match
1171 (concat "^" (regexp-quote
1172 (expand-file-name
1173 gnus-kill-files-directory)))
1174 (expand-file-name file))
1175 file)
1176 (expand-file-name file gnus-kill-files-directory))))
1177 (cached (assoc file gnus-score-cache))
1178 (global (member file gnus-internal-global-score-files))
1179 lists alist)
1180 (if cached
1181 ;; The score file was already loaded.
1182 (setq alist (cdr cached))
1183 ;; We load the score file.
1184 (setq gnus-score-alist nil)
1185 (setq alist (gnus-score-load-score-alist file))
1186 ;; We add '(touched) to the alist to signify that it hasn't been
1187 ;; touched (yet).
1188 (unless (assq 'touched alist)
1189 (push (list 'touched nil) alist))
1190 ;; If it is a global score file, we make it read-only.
1191 (and global
1192 (not (assq 'read-only alist))
1193 (push (list 'read-only t) alist))
1194 (push (cons file alist) gnus-score-cache))
1195 (let ((a alist)
1196 found)
1197 (while a
1198 ;; Downcase all header names.
1199 (cond
1200 ((stringp (caar a))
1201 (setcar (car a) (downcase (caar a)))
1202 (setq found t))
1203 ;; Advanced scoring.
1204 ((consp (caar a))
1205 (setq found t)))
1206 (pop a))
1207 ;; If there are actual scores in the alist, we add it to the
1208 ;; return value of this function.
1209 (when found
1210 (setq lists (list alist))))
1211 ;; Treat the other possible atoms in the score alist.
1212 (let ((mark (car (gnus-score-get 'mark alist)))
1213 (expunge (car (gnus-score-get 'expunge alist)))
1214 (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
1215 (files (gnus-score-get 'files alist))
1216 (exclude-files (gnus-score-get 'exclude-files alist))
1217 (orphan (car (gnus-score-get 'orphan alist)))
1218 (adapt (gnus-score-get 'adapt alist))
1219 (thread-mark-and-expunge
1220 (car (gnus-score-get 'thread-mark-and-expunge alist)))
1221 (adapt-file (car (gnus-score-get 'adapt-file alist)))
1222 (local (gnus-score-get 'local alist))
1223 (decay (car (gnus-score-get 'decay alist)))
1224 (eval (car (gnus-score-get 'eval alist))))
1225 ;; Perform possible decays.
1226 (when (and (if (stringp gnus-decay-scores)
1227 (string-match gnus-decay-scores file)
1228 gnus-decay-scores)
1229 (or cached (file-exists-p file))
1230 (or (not decay)
1231 (gnus-decay-scores alist decay)))
1232 (gnus-score-set 'touched '(t) alist)
1233 (gnus-score-set 'decay (list (time-to-days (current-time))) alist))
1234 ;; We do not respect eval and files atoms from global score
1235 ;; files.
1236 (when (and files (not global))
1237 (setq lists (apply 'append lists
1238 (mapcar 'gnus-score-load-file
1239 (if adapt-file (cons adapt-file files)
1240 files)))))
1241 (when (and eval (not global))
1242 (eval eval))
1243 ;; We then expand any exclude-file directives.
1244 (setq gnus-scores-exclude-files
1245 (nconc
1246 (apply
1247 'nconc
1248 (mapcar
1249 (lambda (sfile)
1250 (list
1251 (expand-file-name sfile (file-name-directory file))
1252 (expand-file-name sfile gnus-kill-files-directory)))
1253 exclude-files))
1254 gnus-scores-exclude-files))
1255 (when local
1256 (with-current-buffer gnus-summary-buffer
1257 (while local
1258 (and (consp (car local))
1259 (symbolp (caar local))
1260 (progn
1261 (make-local-variable (caar local))
1262 (set (caar local) (nth 1 (car local)))))
1263 (setq local (cdr local)))))
1264 (when orphan
1265 (setq gnus-orphan-score orphan))
1266 (setq gnus-adaptive-score-alist
1267 (cond ((equal adapt '(t))
1268 (setq gnus-newsgroup-adaptive t)
1269 gnus-default-adaptive-score-alist)
1270 ((equal adapt '(ignore))
1271 (setq gnus-newsgroup-adaptive nil))
1272 ((consp adapt)
1273 (setq gnus-newsgroup-adaptive t)
1274 adapt)
1276 gnus-default-adaptive-score-alist)))
1277 (setq gnus-thread-expunge-below
1278 (or thread-mark-and-expunge gnus-thread-expunge-below))
1279 (setq gnus-summary-mark-below
1280 (or mark mark-and-expunge gnus-summary-mark-below))
1281 (setq gnus-summary-expunge-below
1282 (or expunge mark-and-expunge gnus-summary-expunge-below))
1283 (setq gnus-newsgroup-adaptive-score-file
1284 (or adapt-file gnus-newsgroup-adaptive-score-file)))
1285 (setq gnus-current-score-file file)
1286 (setq gnus-score-alist alist)
1287 lists))
1289 (defun gnus-score-load (file)
1290 ;; Load score FILE.
1291 (let ((cache (assoc file gnus-score-cache)))
1292 (if cache
1293 (setq gnus-score-alist (cdr cache))
1294 (setq gnus-score-alist nil)
1295 (gnus-score-load-score-alist file)
1296 (unless gnus-score-alist
1297 (setq gnus-score-alist (copy-alist '((touched nil)))))
1298 (push (cons file gnus-score-alist) gnus-score-cache))))
1300 (defun gnus-score-remove-from-cache (file)
1301 (setq gnus-score-cache
1302 (delq (assoc file gnus-score-cache) gnus-score-cache)))
1304 (defun gnus-score-load-score-alist (file)
1305 "Read score FILE."
1306 (let (alist)
1307 (if (not (file-readable-p file))
1308 ;; Couldn't read file.
1309 (setq gnus-score-alist nil)
1310 ;; Read file.
1311 (with-temp-buffer
1312 (let ((coding-system-for-read score-mode-coding-system))
1313 (insert-file-contents file))
1314 (goto-char (point-min))
1315 ;; Only do the loading if the score file isn't empty.
1316 (when (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
1317 (setq alist
1318 (condition-case ()
1319 (read (current-buffer))
1320 (error
1321 (gnus-error 3.2 "Problem with score file %s" file))))))
1322 (cond
1323 ((and alist
1324 (atom alist))
1325 ;; Bogus score file.
1326 (error "Invalid syntax with score file %s" file))
1327 ((eq (car alist) 'setq)
1328 ;; This is an old-style score file.
1329 (setq gnus-score-alist (gnus-score-transform-old-to-new alist)))
1331 (setq gnus-score-alist alist)))
1332 ;; Check the syntax of the score file.
1333 (setq gnus-score-alist
1334 (gnus-score-check-syntax gnus-score-alist file)))))
1336 (defun gnus-score-check-syntax (alist file)
1337 "Check the syntax of the score ALIST."
1338 (cond
1339 ((null alist)
1340 nil)
1341 ((not (consp alist))
1342 (gnus-message 1 "Score file is not a list: %s" file)
1343 (ding)
1344 nil)
1346 (let ((a alist)
1347 sr err s type)
1348 (while (and a (not err))
1349 (setq
1351 (cond
1352 ((not (listp (car a)))
1353 (format "Invalid score element %s in %s" (car a) file))
1354 ((stringp (caar a))
1355 (cond
1356 ((not (listp (setq sr (cdar a))))
1357 (format "Invalid header match %s in %s" (nth 1 (car a)) file))
1359 (setq type (caar a))
1360 (while (and sr (not err))
1361 (setq s (pop sr))
1362 (setq
1364 (cond
1365 ((if (member (downcase type) '("lines" "chars"))
1366 (not (numberp (car s)))
1367 (not (stringp (car s))))
1368 (format "Invalid match %s in %s" (car s) file))
1369 ((and (cadr s) (not (integerp (cadr s))))
1370 (format "Non-integer score %s in %s" (cadr s) file))
1371 ((and (caddr s) (not (integerp (caddr s))))
1372 (format "Non-integer date %s in %s" (caddr s) file))
1373 ((and (cadddr s) (not (symbolp (cadddr s))))
1374 (format "Non-symbol match type %s in %s" (cadddr s) file)))))
1375 err)))))
1376 (setq a (cdr a)))
1377 (if err
1378 (progn
1379 (ding)
1380 (gnus-message 3 "%s" err)
1381 (sit-for 2)
1382 nil)
1383 alist)))))
1385 (defun gnus-score-transform-old-to-new (alist)
1386 (let* ((alist (nth 2 alist))
1387 out entry)
1388 (when (eq (car alist) 'quote)
1389 (setq alist (nth 1 alist)))
1390 (while alist
1391 (setq entry (car alist))
1392 (if (stringp (car entry))
1393 (let ((scor (cdr entry)))
1394 (push entry out)
1395 (while scor
1396 (setcar scor
1397 (list (caar scor) (nth 2 (car scor))
1398 (and (nth 3 (car scor))
1399 (date-to-day (nth 3 (car scor))))
1400 (if (nth 1 (car scor)) 'r 's)))
1401 (setq scor (cdr scor))))
1402 (push (if (not (listp (cdr entry)))
1403 (list (car entry) (cdr entry))
1404 entry)
1405 out))
1406 (setq alist (cdr alist)))
1407 (cons (list 'touched t) (nreverse out))))
1409 (defun gnus-score-save ()
1410 ;; Save all score information.
1411 (let ((cache gnus-score-cache)
1412 entry score file)
1413 (save-excursion
1414 (setq gnus-score-alist nil)
1415 (nnheader-set-temp-buffer " *Gnus Scores*")
1416 (while cache
1417 (current-buffer)
1418 (setq entry (pop cache)
1419 file (nnheader-translate-file-chars (car entry) t)
1420 score (cdr entry))
1421 (if (or (not (equal (gnus-score-get 'touched score) '(t)))
1422 (gnus-score-get 'read-only score)
1423 (and (file-exists-p file)
1424 (not (file-writable-p file))))
1426 (setq score (setcdr entry (gnus-delete-alist 'touched score)))
1427 (erase-buffer)
1428 (let (emacs-lisp-mode-hook)
1429 (if (and (not gnus-adaptive-pretty-print)
1430 (string-match
1431 (concat (regexp-quote gnus-adaptive-file-suffix) "$")
1432 file))
1433 ;; This is an adaptive score file, so we do not run it through
1434 ;; `pp' unless requested. These files can get huge, and are
1435 ;; not meant to be edited by human hands.
1436 (gnus-prin1 score)
1437 ;; This is a normal score file, so we print it very
1438 ;; prettily.
1439 (let ((lisp-mode-syntax-table score-mode-syntax-table))
1440 (gnus-pp score))))
1441 (gnus-make-directory (file-name-directory file))
1442 ;; If the score file is empty, we delete it.
1443 (if (zerop (buffer-size))
1444 (delete-file file)
1445 ;; There are scores, so we write the file.
1446 (when (file-writable-p file)
1447 (let ((coding-system-for-write score-mode-coding-system))
1448 (gnus-write-buffer file))
1449 (when gnus-score-after-write-file-function
1450 (funcall gnus-score-after-write-file-function file)))))
1451 (and gnus-score-uncacheable-files
1452 (string-match gnus-score-uncacheable-files file)
1453 (gnus-score-remove-from-cache file)))
1454 (kill-buffer (current-buffer)))))
1456 (defun gnus-score-load-files (score-files)
1457 "Load all score files in SCORE-FILES."
1458 ;; Load the score files.
1459 (let (scores)
1460 (while score-files
1461 (if (stringp (car score-files))
1462 ;; It is a string, which means that it's a score file name,
1463 ;; so we load the score file and add the score alist to
1464 ;; the list of alists.
1465 (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
1466 ;; It is an alist, so we just add it to the list directly.
1467 (setq scores (nconc (car score-files) scores)))
1468 (setq score-files (cdr score-files)))
1469 ;; Prune the score files that are to be excluded, if any.
1470 (when gnus-scores-exclude-files
1471 (let ((s scores)
1473 (while s
1474 (and (setq c (rassq (car s) gnus-score-cache))
1475 (member (car c) gnus-scores-exclude-files)
1476 (setq scores (delq (car s) scores)))
1477 (setq s (cdr s)))))
1478 scores))
1480 (defun gnus-score-headers (score-files &optional trace)
1481 ;; Score `gnus-newsgroup-headers'.
1482 (let (scores news)
1483 ;; PLM: probably this is not the best place to clear orphan-score
1484 (setq gnus-orphan-score nil
1485 gnus-scores-articles nil
1486 gnus-scores-exclude-files nil
1487 scores (gnus-score-load-files score-files))
1488 (setq news scores)
1489 ;; Do the scoring.
1490 (while news
1491 (setq scores news
1492 news nil)
1493 (when (and gnus-summary-default-score
1494 scores)
1495 (let* ((entries gnus-header-index)
1496 (now (date-to-day (current-time-string)))
1497 (expire (and gnus-score-expiry-days
1498 (- now gnus-score-expiry-days)))
1499 (headers gnus-newsgroup-headers)
1500 (current-score-file gnus-current-score-file)
1501 entry header new)
1502 (gnus-message 7 "Scoring...")
1503 ;; Create articles, an alist of the form `(HEADER . SCORE)'.
1504 (while (setq header (pop headers))
1505 ;; WARNING: The assq makes the function O(N*S) while it could
1506 ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
1507 ;; and S is (length gnus-newsgroup-scored).
1508 (unless (assq (mail-header-number header) gnus-newsgroup-scored)
1509 (setq gnus-scores-articles ;Total of 2 * N cons-cells used.
1510 (cons (cons header (or gnus-summary-default-score 0))
1511 gnus-scores-articles))))
1513 (with-current-buffer (gnus-get-buffer-create "*Headers*")
1514 (buffer-disable-undo)
1515 (when (gnus-buffer-live-p gnus-summary-buffer)
1516 (message-clone-locals gnus-summary-buffer))
1518 ;; Set the global variant of this variable.
1519 (setq gnus-current-score-file current-score-file)
1520 ;; score orphans
1521 (when gnus-orphan-score
1522 (setq gnus-score-index
1523 (nth 1 (assoc "references" gnus-header-index)))
1524 (gnus-score-orphans gnus-orphan-score))
1525 ;; Run each header through the score process.
1526 (while entries
1527 (setq entry (pop entries)
1528 header (nth 0 entry)
1529 gnus-score-index (nth 1 (assoc header gnus-header-index)))
1530 (when (< 0 (apply 'max (mapcar
1531 (lambda (score)
1532 (length (gnus-score-get header score)))
1533 scores)))
1534 (when (if (and gnus-inhibit-slow-scoring
1535 (or (eq gnus-inhibit-slow-scoring t)
1536 (and (stringp gnus-inhibit-slow-scoring)
1537 ;; Always true here?
1538 ;; (stringp gnus-newsgroup-name)
1539 (string-match
1540 gnus-inhibit-slow-scoring
1541 gnus-newsgroup-name)))
1542 (> 0 (nth 1 (assoc header gnus-header-index))))
1543 (progn
1544 (gnus-message
1545 7 "Scoring on headers or body skipped.")
1546 nil)
1547 ;; Call the scoring function for this type of "header".
1548 (setq new (funcall (nth 2 entry) scores header
1549 now expire trace)))
1550 (push new news))))
1551 (when (gnus-buffer-live-p gnus-summary-buffer)
1552 (let ((scored gnus-newsgroup-scored))
1553 (with-current-buffer gnus-summary-buffer
1554 (setq gnus-newsgroup-scored scored))))
1555 ;; Remove the buffer.
1556 (gnus-kill-buffer (current-buffer)))
1558 ;; Add articles to `gnus-newsgroup-scored'.
1559 (while gnus-scores-articles
1560 (when (or (/= gnus-summary-default-score
1561 (cdar gnus-scores-articles))
1562 gnus-save-score)
1563 (push (cons (mail-header-number (caar gnus-scores-articles))
1564 (cdar gnus-scores-articles))
1565 gnus-newsgroup-scored))
1566 (setq gnus-scores-articles (cdr gnus-scores-articles)))
1568 (let (score)
1569 (while (setq score (pop scores))
1570 (while score
1571 (when (consp (caar score))
1572 (gnus-score-advanced (car score) trace))
1573 (pop score))))
1575 (gnus-message 7 "Scoring...done"))))))
1577 (defun gnus-score-lower-thread (thread score-adjust)
1578 "Lower the score on THREAD with SCORE-ADJUST.
1579 THREAD is expected to contain a list of the form `(PARENT [CHILD1
1580 CHILD2 ...])' where PARENT is a header array and each CHILD is a list
1581 of the same form as THREAD. The empty list nil is valid. For each
1582 article in the tree, the score of the corresponding entry in
1583 `gnus-newsgroup-scored' is adjusted by SCORE-ADJUST."
1584 (while thread
1585 (let ((head (car thread)))
1586 (if (listp head)
1587 ;; handle a child and its descendants
1588 (gnus-score-lower-thread head score-adjust)
1589 ;; handle the parent
1590 (let* ((article (mail-header-number head))
1591 (score (assq article gnus-newsgroup-scored)))
1592 (if score (setcdr score (+ (cdr score) score-adjust))
1593 (push (cons article score-adjust) gnus-newsgroup-scored)))))
1594 (setq thread (cdr thread))))
1596 (defun gnus-score-orphans (score)
1597 "Score orphans.
1598 A root is an article with no references. An orphan is an article
1599 which has references, but is not connected via its references to a
1600 root article. This function finds all the orphans, and adjusts their
1601 score in `gnus-newsgroup-scored' by SCORE."
1602 ;; gnus-make-threads produces a list, where each entry is a "thread"
1603 ;; as described in the gnus-score-lower-thread docs. This function
1604 ;; will be called again (after limiting has been done) if the display
1605 ;; is threaded. It would be nice to somehow save this info and use
1606 ;; it later.
1607 (dolist (thread (gnus-make-threads))
1608 (let ((id (aref (car thread) gnus-score-index)))
1609 ;; If the parent of the thread is not a root, lower the score of
1610 ;; it and its descendants. Note that some roots seem to satisfy
1611 ;; (eq id nil) and some (eq id ""); not sure why.
1612 (when (and id
1613 (not (string= id "")))
1614 (gnus-score-lower-thread thread score)))))
1616 (defun gnus-score-integer (scores header now expire &optional trace)
1617 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1618 entries alist)
1619 ;; Find matches.
1620 (while scores
1621 (setq alist (car scores)
1622 scores (cdr scores)
1623 entries (assoc header alist))
1624 (while (cdr entries) ;First entry is the header index.
1625 (let* ((rest (cdr entries))
1626 (kill (car rest))
1627 (match (nth 0 kill))
1628 (type (or (nth 3 kill) '>))
1629 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1630 (date (nth 2 kill))
1631 (found nil)
1632 (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
1633 (eq type '>=) (eq type '=))
1634 type
1635 (error "Invalid match type: %s" type)))
1636 (articles gnus-scores-articles))
1637 ;; Instead of doing all the clever stuff that
1638 ;; `gnus-score-string' does to minimize searches and stuff,
1639 ;; I will assume that people generally will put so few
1640 ;; matches on numbers that any cleverness will take more
1641 ;; time than one would gain.
1642 (while articles
1643 (when (funcall match-func
1644 (or (aref (caar articles) gnus-score-index) 0)
1645 match)
1646 (when trace
1647 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1648 gnus-score-trace))
1649 (setq found t)
1650 (setcdr (car articles) (+ score (cdar articles))))
1651 (setq articles (cdr articles)))
1652 ;; Update expire date
1653 (cond ((null date)) ;Permanent entry.
1654 ((and found gnus-update-score-entry-dates) ;Match, update date.
1655 (gnus-score-set 'touched '(t) alist)
1656 (setcar (nthcdr 2 kill) now))
1657 ((and expire (< date expire)) ;Old entry, remove.
1658 (gnus-score-set 'touched '(t) alist)
1659 (setcdr entries (cdr rest))
1660 (setq rest entries)))
1661 (setq entries rest)))))
1662 nil)
1664 (defun gnus-score-date (scores header now expire &optional trace)
1665 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1666 entries alist match match-func article)
1667 ;; Find matches.
1668 (while scores
1669 (setq alist (car scores)
1670 scores (cdr scores)
1671 entries (assoc header alist))
1672 (while (cdr entries) ;First entry is the header index.
1673 (let* ((rest (cdr entries))
1674 (kill (car rest))
1675 (type (or (nth 3 kill) 'before))
1676 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1677 (date (nth 2 kill))
1678 (found nil)
1679 (articles gnus-scores-articles)
1681 (cond
1682 ((eq type 'after)
1683 (setq match-func 'string<
1684 match (gnus-date-iso8601 (nth 0 kill))))
1685 ((eq type 'before)
1686 (setq match-func 'gnus-string>
1687 match (gnus-date-iso8601 (nth 0 kill))))
1688 ((eq type 'at)
1689 (setq match-func 'string=
1690 match (gnus-date-iso8601 (nth 0 kill))))
1691 ((eq type 'regexp)
1692 (setq match-func 'string-match
1693 match (nth 0 kill)))
1694 (t (error "Invalid match type: %s" type)))
1695 ;; Instead of doing all the clever stuff that
1696 ;; `gnus-score-string' does to minimize searches and stuff,
1697 ;; I will assume that people generally will put so few
1698 ;; matches on numbers that any cleverness will take more
1699 ;; time than one would gain.
1700 (while (setq article (pop articles))
1701 (when (and
1702 (setq l (aref (car article) gnus-score-index))
1703 (funcall match-func match (gnus-date-iso8601 l)))
1704 (when trace
1705 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1706 gnus-score-trace))
1707 (setq found t)
1708 (setcdr article (+ score (cdr article)))))
1709 ;; Update expire date
1710 (cond ((null date)) ;Permanent entry.
1711 ((and found gnus-update-score-entry-dates) ;Match, update date.
1712 (gnus-score-set 'touched '(t) alist)
1713 (setcar (nthcdr 2 kill) now))
1714 ((and expire (< date expire)) ;Old entry, remove.
1715 (gnus-score-set 'touched '(t) alist)
1716 (setcdr entries (cdr rest))
1717 (setq rest entries)))
1718 (setq entries rest)))))
1719 nil)
1721 (defun gnus-score-decode-text-parts ()
1722 (gmm-labels
1723 ((mm-text-parts
1724 (handle)
1725 (cond ((stringp (car handle))
1726 (let ((parts (apply #'append
1727 (mapcar #'mm-text-parts (cdr handle)))))
1728 (if (equal "multipart/alternative" (car handle))
1729 ;; pick the first supported alternative
1730 (list (car parts))
1731 parts)))
1733 ((bufferp (car handle))
1734 (when (string-match "^text/" (mm-handle-media-type handle))
1735 (list handle)))
1737 (t (apply #'append (mapcar #'mm-text-parts handle)))))
1738 (my-mm-display-part
1739 (handle)
1740 (when handle
1741 (save-restriction
1742 (narrow-to-region (point) (point))
1743 (mm-display-inline handle)
1744 (goto-char (point-max))))))
1746 (let (;(mm-text-html-renderer 'w3m-standalone)
1747 (handles (mm-dissect-buffer t)))
1748 (save-excursion
1749 (article-goto-body)
1750 (delete-region (point) (point-max))
1751 (mapc #'my-mm-display-part (mm-text-parts handles))
1752 handles))))
1754 (defun gnus-score-body (scores header now expire &optional trace)
1755 (if gnus-agent-fetching
1757 (save-excursion
1758 (setq gnus-scores-articles
1759 (sort gnus-scores-articles
1760 (lambda (a1 a2)
1761 (< (mail-header-number (car a1))
1762 (mail-header-number (car a2))))))
1763 (set-buffer nntp-server-buffer)
1764 (save-restriction
1765 (let* ((buffer-read-only nil)
1766 (articles gnus-scores-articles)
1767 (all-scores scores)
1768 (request-func (cond ((string= "head" header)
1769 'gnus-request-head)
1770 ((string= "body" header)
1771 'gnus-request-body)
1772 (t 'gnus-request-article)))
1773 entries alist ofunc article last)
1774 (when articles
1775 (setq last (mail-header-number (caar (last articles))))
1776 ;; Not all backends support partial fetching. In that case,
1777 ;; we just fetch the entire article.
1778 ;; When scoring by body, we need to peek at the headers to detect
1779 ;; the content encoding
1780 (unless (or (gnus-check-backend-function
1781 (and (string-match "^gnus-" (symbol-name request-func))
1782 (intern (substring (symbol-name request-func)
1783 (match-end 0))))
1784 gnus-newsgroup-name)
1785 (string= "body" header))
1786 (setq ofunc request-func)
1787 (setq request-func 'gnus-request-article))
1788 (while articles
1789 (setq article (mail-header-number (caar articles)))
1790 (gnus-message 7 "Scoring article %s of %s..." article last)
1791 (widen)
1792 (let (handles)
1793 (when (funcall request-func article gnus-newsgroup-name)
1794 (when (string= "body" header)
1795 (setq handles (gnus-score-decode-text-parts)))
1796 (goto-char (point-min))
1797 ;; If just parts of the article is to be searched, but the
1798 ;; backend didn't support partial fetching, we just narrow
1799 ;; to the relevant parts.
1800 (when ofunc
1801 (if (eq ofunc 'gnus-request-head)
1802 (narrow-to-region
1803 (point)
1804 (or (search-forward "\n\n" nil t) (point-max)))
1805 (narrow-to-region
1806 (or (search-forward "\n\n" nil t) (point))
1807 (point-max))))
1808 (setq scores all-scores)
1809 ;; Find matches.
1810 (while scores
1811 (setq alist (pop scores)
1812 entries (assoc header alist))
1813 (while (cdr entries) ;First entry is the header index.
1814 (let* ((rest (cdr entries))
1815 (kill (car rest))
1816 (match (nth 0 kill))
1817 (type (or (nth 3 kill) 's))
1818 (score (or (nth 1 kill)
1819 gnus-score-interactive-default-score))
1820 (date (nth 2 kill))
1821 (found nil)
1822 (case-fold-search
1823 (not (or (eq type 'R) (eq type 'S)
1824 (eq type 'Regexp) (eq type 'String))))
1825 (search-func
1826 (cond ((or (eq type 'r) (eq type 'R)
1827 (eq type 'regexp) (eq type 'Regexp))
1828 're-search-forward)
1829 ((or (eq type 's) (eq type 'S)
1830 (eq type 'string) (eq type 'String))
1831 'search-forward)
1833 (error "Invalid match type: %s" type)))))
1834 (goto-char (point-min))
1835 (when (funcall search-func match nil t)
1836 ;; Found a match, update scores.
1837 (setcdr (car articles) (+ score (cdar articles)))
1838 (setq found t)
1839 (when trace
1840 (push
1841 (cons (car-safe (rassq alist gnus-score-cache))
1842 kill)
1843 gnus-score-trace)))
1844 ;; Update expire date
1845 (unless trace
1846 (cond
1847 ((null date)) ;Permanent entry.
1848 ((and found gnus-update-score-entry-dates)
1849 ;; Match, update date.
1850 (gnus-score-set 'touched '(t) alist)
1851 (setcar (nthcdr 2 kill) now))
1852 ((and expire (< date expire)) ;Old entry, remove.
1853 (gnus-score-set 'touched '(t) alist)
1854 (setcdr entries (cdr rest))
1855 (setq rest entries))))
1856 (setq entries rest))))
1857 (when handles (mm-destroy-parts handles))))
1858 (setq articles (cdr articles)))))))
1859 nil))
1861 (defun gnus-score-thread (scores header now expire &optional trace)
1862 (gnus-score-followup scores header now expire trace t))
1864 (defun gnus-score-followup (scores header now expire &optional trace thread)
1865 (if gnus-agent-fetching
1866 ;; FIXME: It seems doable in fetching mode.
1868 ;; Insert the unique article headers in the buffer.
1869 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1870 (current-score-file gnus-current-score-file)
1871 (all-scores scores)
1872 ;; gnus-score-index is used as a free variable.
1873 alike last this art entries alist articles
1874 new news)
1876 ;; Change score file to the adaptive score file. All entries that
1877 ;; this function makes will be put into this file.
1878 (with-current-buffer gnus-summary-buffer
1879 (gnus-score-load-file
1880 (or gnus-newsgroup-adaptive-score-file
1881 (gnus-score-file-name
1882 gnus-newsgroup-name gnus-adaptive-file-suffix))))
1884 (setq gnus-scores-articles (sort gnus-scores-articles
1885 'gnus-score-string<)
1886 articles gnus-scores-articles)
1888 (erase-buffer)
1889 (while articles
1890 (setq art (car articles)
1891 this (aref (car art) gnus-score-index)
1892 articles (cdr articles))
1893 (if (equal last this)
1894 (push art alike)
1895 (when last
1896 (insert last ?\n)
1897 (put-text-property (1- (point)) (point) 'articles alike))
1898 (setq alike (list art)
1899 last this)))
1900 (when last ; Bwadr, duplicate code.
1901 (insert last ?\n)
1902 (put-text-property (1- (point)) (point) 'articles alike))
1904 ;; Find matches.
1905 (while scores
1906 (setq alist (car scores)
1907 scores (cdr scores)
1908 entries (assoc header alist))
1909 (while (cdr entries) ;First entry is the header index.
1910 (let* ((rest (cdr entries))
1911 (kill (car rest))
1912 (match (nth 0 kill))
1913 (type (or (nth 3 kill) 's))
1914 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1915 (date (nth 2 kill))
1916 (found nil)
1917 (mt (aref (symbol-name type) 0))
1918 (case-fold-search
1919 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1920 (dmt (downcase mt))
1921 (search-func
1922 (cond ((= dmt ?r) 're-search-forward)
1923 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1924 (t (error "Invalid match type: %s" type))))
1925 arts art)
1926 (goto-char (point-min))
1927 (if (= dmt ?e)
1928 (while (funcall search-func match nil t)
1929 (and (= (point-at-bol)
1930 (match-beginning 0))
1931 (= (progn (end-of-line) (point))
1932 (match-end 0))
1933 (progn
1934 (setq found (setq arts (get-text-property
1935 (point) 'articles)))
1936 ;; Found a match, update scores.
1937 (while arts
1938 (setq art (car arts)
1939 arts (cdr arts))
1940 (gnus-score-add-followups
1941 (car art) score all-scores thread))))
1942 (end-of-line))
1943 (while (funcall search-func match nil t)
1944 (end-of-line)
1945 (setq found (setq arts (get-text-property (point) 'articles)))
1946 ;; Found a match, update scores.
1947 (while (setq art (pop arts))
1948 (setcdr art (+ score (cdr art)))
1949 (when trace
1950 (push (cons
1951 (car-safe (rassq alist gnus-score-cache))
1952 kill)
1953 gnus-score-trace))
1954 (when (setq new (gnus-score-add-followups
1955 (car art) score all-scores thread))
1956 (push new news)))))
1957 ;; Update expire date
1958 (cond ((null date)) ;Permanent entry.
1959 ((and found gnus-update-score-entry-dates)
1960 ;Match, update date.
1961 (gnus-score-set 'touched '(t) alist)
1962 (setcar (nthcdr 2 kill) now))
1963 ((and expire (< date expire)) ;Old entry, remove.
1964 (gnus-score-set 'touched '(t) alist)
1965 (setcdr entries (cdr rest))
1966 (setq rest entries)))
1967 (setq entries rest))))
1968 ;; We change the score file back to the previous one.
1969 (with-current-buffer gnus-summary-buffer
1970 (gnus-score-load-file current-score-file))
1971 (list (cons "references" news)))))
1973 (defun gnus-score-add-followups (header score scores &optional thread)
1974 "Add a score entry to the adapt file."
1975 (with-current-buffer gnus-summary-buffer
1976 (let* ((id (mail-header-id header))
1977 (scores (car scores))
1978 entry dont)
1979 ;; Don't enter a score if there already is one.
1980 (while (setq entry (pop scores))
1981 (and (equal "references" (car entry))
1982 (or (null (nth 3 (cadr entry)))
1983 (eq 's (nth 3 (cadr entry))))
1984 (assoc id entry)
1985 (setq dont t)))
1986 (unless dont
1987 (gnus-summary-score-entry
1988 (if thread "thread" "references")
1989 id 's score (current-time-string) nil t)))))
1991 (defun gnus-score-string (score-list header now expire &optional trace)
1992 ;; Score ARTICLES according to HEADER in SCORE-LIST.
1993 ;; Update matching entries to NOW and remove unmatched entries older
1994 ;; than EXPIRE.
1996 ;; Insert the unique article headers in the buffer.
1997 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1998 ;; gnus-score-index is used as a free variable.
1999 (simplify (and gnus-score-thread-simplify
2000 (string= "subject" header)))
2001 alike last this art entries alist articles
2002 fuzzies arts words kill)
2004 ;; Sorting the articles costs os O(N*log N) but will allow us to
2005 ;; only match with each unique header. Thus the actual matching
2006 ;; will be O(M*U) where M is the number of strings to match with,
2007 ;; and U is the number of unique headers. It is assumed (but
2008 ;; untested) this will be a net win because of the large constant
2009 ;; factor involved with string matching.
2010 (setq gnus-scores-articles
2011 ;; We cannot string-sort the extra headers list. *sigh*
2012 (if (= gnus-score-index 9)
2013 gnus-scores-articles
2014 (sort gnus-scores-articles 'gnus-score-string<))
2015 articles gnus-scores-articles)
2017 (erase-buffer)
2018 (while (setq art (pop articles))
2019 (setq this (aref (car art) gnus-score-index))
2021 ;; If we're working with non-standard headers, we are stuck
2022 ;; with working on them as a group. What a hassle.
2023 ;; Just wait 'til you see what horrors we commit against `match'...
2024 (if (= gnus-score-index 9)
2025 (setq this (gnus-prin1-to-string this))) ; ick.
2027 (if simplify
2028 (setq this (gnus-map-function gnus-simplify-subject-functions this)))
2029 (if (equal last this)
2030 ;; O(N*H) cons-cells used here, where H is the number of
2031 ;; headers.
2032 (push art alike)
2033 (when last
2034 ;; Insert the line, with a text property on the
2035 ;; terminating newline referring to the articles with
2036 ;; this line.
2037 (insert last ?\n)
2038 (put-text-property (1- (point)) (point) 'articles alike))
2039 (setq alike (list art)
2040 last this)))
2041 (when last ; Bwadr, duplicate code.
2042 (insert last ?\n)
2043 (put-text-property (1- (point)) (point) 'articles alike))
2045 ;; Go through all the score alists and pick out the entries
2046 ;; for this header.
2047 (while score-list
2048 (setq alist (pop score-list)
2049 ;; There's only one instance of this header for
2050 ;; each score alist.
2051 entries (assoc header alist))
2052 (while (cdr entries) ;First entry is the header index.
2053 (let* ((kill (cadr entries))
2054 (type (or (nth 3 kill) 's))
2055 (score (or (nth 1 kill) gnus-score-interactive-default-score))
2056 (date (nth 2 kill))
2057 (extra (nth 4 kill)) ; non-standard header; string.
2058 (found nil)
2059 (mt (aref (symbol-name type) 0))
2060 (case-fold-search (not (memq mt '(?R ?S ?E ?F))))
2061 (dmt (downcase mt))
2062 ;; Assume user already simplified regexp and fuzzies
2063 (match (if (and simplify (not (memq dmt '(?f ?r))))
2064 (gnus-map-function
2065 gnus-simplify-subject-functions
2066 (nth 0 kill))
2067 (nth 0 kill)))
2068 (search-func
2069 (cond ((= dmt ?r) 're-search-forward)
2070 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
2071 ((= dmt ?w) nil)
2072 (t (error "Invalid match type: %s" type)))))
2074 ;; Evil hackery to make match usable in non-standard headers.
2075 (when extra
2076 (setq match (concat "[ (](" extra " \\. \"\\([^\"]*\\\\\"\\)*[^\"]*"
2077 (if (eq search-func 're-search-forward)
2078 match
2079 (regexp-quote match))
2080 "\\([^\"]*\\\\\"\\)*[^\"]*\")[ )]")
2081 search-func 're-search-forward)) ; XXX danger?!?
2083 (cond
2084 ;; Fuzzy matches. We save these for later.
2085 ((= dmt ?f)
2086 (push (cons entries alist) fuzzies)
2087 (setq entries (cdr entries)))
2088 ;; Word matches. Save these for even later.
2089 ((= dmt ?w)
2090 (push (cons entries alist) words)
2091 (setq entries (cdr entries)))
2092 ;; Exact matches.
2093 ((= dmt ?e)
2094 ;; Do exact matching.
2095 (goto-char (point-min))
2096 (while (and (not (eobp))
2097 (funcall search-func match nil t))
2098 ;; Is it really exact?
2099 (and (eolp)
2100 (= (point-at-bol) (match-beginning 0))
2101 ;; Yup.
2102 (progn
2103 (setq found (setq arts (get-text-property
2104 (point) 'articles)))
2105 ;; Found a match, update scores.
2106 (if trace
2107 (while (setq art (pop arts))
2108 (setcdr art (+ score (cdr art)))
2109 (push
2110 (cons
2111 (car-safe (rassq alist gnus-score-cache))
2112 kill)
2113 gnus-score-trace))
2114 (while (setq art (pop arts))
2115 (setcdr art (+ score (cdr art)))))))
2116 (forward-line 1))
2117 ;; Update expiry date
2118 (if trace
2119 (setq entries (cdr entries))
2120 (cond
2121 ;; Permanent entry.
2122 ((null date)
2123 (setq entries (cdr entries)))
2124 ;; We have a match, so we update the date.
2125 ((and found gnus-update-score-entry-dates)
2126 (gnus-score-set 'touched '(t) alist)
2127 (setcar (nthcdr 2 kill) now)
2128 (setq entries (cdr entries)))
2129 ;; This entry has expired, so we remove it.
2130 ((and expire (< date expire))
2131 (gnus-score-set 'touched '(t) alist)
2132 (setcdr entries (cddr entries)))
2133 ;; No match; go to next entry.
2135 (setq entries (cdr entries))))))
2136 ;; Regexp and substring matching.
2138 (goto-char (point-min))
2139 (when (string= match "")
2140 (setq match "\n"))
2141 (while (and (not (eobp))
2142 (funcall search-func match nil t))
2143 (goto-char (match-beginning 0))
2144 (end-of-line)
2145 (setq found (setq arts (get-text-property (point) 'articles)))
2146 ;; Found a match, update scores.
2147 (if trace
2148 (while (setq art (pop arts))
2149 (setcdr art (+ score (cdr art)))
2150 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
2151 gnus-score-trace))
2152 (while (setq art (pop arts))
2153 (setcdr art (+ score (cdr art)))))
2154 (forward-line 1))
2155 ;; Update expiry date
2156 (if trace
2157 (setq entries (cdr entries))
2158 (cond
2159 ;; Permanent entry.
2160 ((null date)
2161 (setq entries (cdr entries)))
2162 ;; We have a match, so we update the date.
2163 ((and found gnus-update-score-entry-dates)
2164 (gnus-score-set 'touched '(t) alist)
2165 (setcar (nthcdr 2 kill) now)
2166 (setq entries (cdr entries)))
2167 ;; This entry has expired, so we remove it.
2168 ((and expire (< date expire))
2169 (gnus-score-set 'touched '(t) alist)
2170 (setcdr entries (cddr entries)))
2171 ;; No match; go to next entry.
2173 (setq entries (cdr entries))))))))))
2175 ;; Find fuzzy matches.
2176 (when fuzzies
2177 ;; Simplify the entire buffer for easy matching.
2178 (gnus-simplify-buffer-fuzzy gnus-simplify-subject-fuzzy-regexp)
2179 (while (setq kill (cadaar fuzzies))
2180 (let* ((match (nth 0 kill))
2181 (type (nth 3 kill))
2182 (score (or (nth 1 kill) gnus-score-interactive-default-score))
2183 (date (nth 2 kill))
2184 (mt (aref (symbol-name type) 0))
2185 (case-fold-search (not (= mt ?F)))
2186 found)
2187 (goto-char (point-min))
2188 (while (and (not (eobp))
2189 (search-forward match nil t))
2190 (when (and (= (point-at-bol) (match-beginning 0))
2191 (eolp))
2192 (setq found (setq arts (get-text-property (point) 'articles)))
2193 (if trace
2194 (while (setq art (pop arts))
2195 (setcdr art (+ score (cdr art)))
2196 (push (cons
2197 (car-safe (rassq (cdar fuzzies) gnus-score-cache))
2198 kill)
2199 gnus-score-trace))
2200 ;; Found a match, update scores.
2201 (while (setq art (pop arts))
2202 (setcdr art (+ score (cdr art))))))
2203 (forward-line 1))
2204 ;; Update expiry date
2205 (if (not trace)
2206 (cond
2207 ;; Permanent.
2208 ((null date)
2209 ;; Do nothing.
2211 ;; Match, update date.
2212 ((and found gnus-update-score-entry-dates)
2213 (gnus-score-set 'touched '(t) (cdar fuzzies))
2214 (setcar (nthcdr 2 kill) now))
2215 ;; Old entry, remove.
2216 ((and expire (< date expire))
2217 (gnus-score-set 'touched '(t) (cdar fuzzies))
2218 (setcdr (caar fuzzies) (cddaar fuzzies)))))
2219 (setq fuzzies (cdr fuzzies)))))
2221 (when words
2222 ;; Enter all words into the hashtb.
2223 (let ((hashtb (gnus-make-hashtable
2224 (* 10 (count-lines (point-min) (point-max))))))
2225 (gnus-enter-score-words-into-hashtb hashtb)
2226 (while (setq kill (cadaar words))
2227 (let* ((score (or (nth 1 kill) gnus-score-interactive-default-score))
2228 (date (nth 2 kill))
2229 found)
2230 (when (setq arts (intern-soft (nth 0 kill) hashtb))
2231 (setq arts (symbol-value arts))
2232 (setq found t)
2233 (if trace
2234 (while (setq art (pop arts))
2235 (setcdr art (+ score (cdr art)))
2236 (push (cons
2237 (car-safe (rassq (cdar words) gnus-score-cache))
2238 kill)
2239 gnus-score-trace))
2240 ;; Found a match, update scores.
2241 (while (setq art (pop arts))
2242 (setcdr art (+ score (cdr art))))))
2243 ;; Update expiry date
2244 (if (not trace)
2245 (cond
2246 ;; Permanent.
2247 ((null date)
2248 ;; Do nothing.
2250 ;; Match, update date.
2251 ((and found gnus-update-score-entry-dates)
2252 (gnus-score-set 'touched '(t) (cdar words))
2253 (setcar (nthcdr 2 kill) now))
2254 ;; Old entry, remove.
2255 ((and expire (< date expire))
2256 (gnus-score-set 'touched '(t) (cdar words))
2257 (setcdr (caar words) (cddaar words)))))
2258 (setq words (cdr words))))))
2259 nil))
2261 (defun gnus-enter-score-words-into-hashtb (hashtb)
2262 ;; Find all the words in the buffer and enter them into
2263 ;; the hashtable.
2264 (let (word val)
2265 (goto-char (point-min))
2266 (with-syntax-table gnus-adaptive-word-syntax-table
2267 (while (re-search-forward "\\b\\w+\\b" nil t)
2268 (setq val
2269 (gnus-gethash
2270 (setq word (downcase (buffer-substring
2271 (match-beginning 0) (match-end 0))))
2272 hashtb))
2273 (gnus-sethash
2274 word
2275 (append (get-text-property (point-at-eol) 'articles) val)
2276 hashtb)))
2277 ;; Make all the ignorable words ignored.
2278 (let ((ignored (append gnus-ignored-adaptive-words
2279 (if gnus-adaptive-word-no-group-words
2280 (message-tokenize-header
2281 (gnus-group-real-name gnus-newsgroup-name)
2282 "."))
2283 gnus-default-ignored-adaptive-words)))
2284 (while ignored
2285 (gnus-sethash (pop ignored) nil hashtb)))))
2287 (defun gnus-score-string< (a1 a2)
2288 ;; Compare headers in articles A2 and A2.
2289 ;; The header index used is the free variable `gnus-score-index'.
2290 (string-lessp (aref (car a1) gnus-score-index)
2291 (aref (car a2) gnus-score-index)))
2293 (defun gnus-current-score-file-nondirectory (&optional score-file)
2294 (let ((score-file (or score-file gnus-current-score-file)))
2295 (if score-file
2296 (gnus-short-group-name (file-name-nondirectory score-file))
2297 "none")))
2299 (defun gnus-score-adaptive ()
2300 "Create adaptive score rules for this newsgroup."
2301 (when gnus-newsgroup-adaptive
2302 ;; We change the score file to the adaptive score file.
2303 (with-current-buffer gnus-summary-buffer
2304 (gnus-score-load-file
2305 (or gnus-newsgroup-adaptive-score-file
2306 (gnus-home-score-file gnus-newsgroup-name t)
2307 (gnus-score-file-name
2308 gnus-newsgroup-name gnus-adaptive-file-suffix))))
2309 ;; Perform ordinary line scoring.
2310 (when (or (not (listp gnus-newsgroup-adaptive))
2311 (memq 'line gnus-newsgroup-adaptive))
2312 (save-excursion
2313 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
2314 (alist malist)
2315 (date (current-time-string))
2316 (data gnus-newsgroup-data)
2317 elem headers match func)
2318 ;; First we transform the adaptive rule alist into something
2319 ;; that's faster to process.
2320 (while malist
2321 (setq elem (car malist))
2322 (when (symbolp (car elem))
2323 (setcar elem (symbol-value (car elem))))
2324 (setq elem (cdr elem))
2325 (while elem
2326 (when (fboundp
2327 (setq func
2328 (intern
2329 (concat "mail-header-"
2330 (if (eq (caar elem) 'followup)
2331 "message-id"
2332 (downcase (symbol-name (caar elem))))))))
2333 (setcdr (car elem)
2334 (cons (if (eq (caar elem) 'followup)
2335 "references"
2336 (symbol-name (caar elem)))
2337 (cdar elem)))
2338 (setcar (car elem)
2339 `(lambda (h)
2340 (,func h))))
2341 (setq elem (cdr elem)))
2342 (setq malist (cdr malist)))
2343 ;; Then we score away.
2344 (while data
2345 (setq elem (cdr (assq (gnus-data-mark (car data)) alist)))
2346 (if (or (not elem)
2347 (gnus-data-pseudo-p (car data)))
2349 (when (setq headers (gnus-data-header (car data)))
2350 (while elem
2351 (setq match (funcall (caar elem) headers))
2352 (gnus-summary-score-entry
2353 (nth 1 (car elem)) match
2354 (cond
2355 ((numberp match)
2357 ((equal (nth 1 (car elem)) "date")
2360 ;; Whether we use substring or exact matches is
2361 ;; controlled here.
2362 (if (or (not gnus-score-exact-adapt-limit)
2363 (< (length match) gnus-score-exact-adapt-limit))
2365 (if (equal (nth 1 (car elem)) "subject")
2366 'f 's))))
2367 (nth 2 (car elem)) date nil t)
2368 (setq elem (cdr elem)))))
2369 (setq data (cdr data))))))
2371 ;; Perform adaptive word scoring.
2372 (when (and (listp gnus-newsgroup-adaptive)
2373 (memq 'word gnus-newsgroup-adaptive))
2374 (with-temp-buffer
2375 (let* ((hashtb (gnus-make-hashtable 1000))
2376 (date (date-to-day (current-time-string)))
2377 (data gnus-newsgroup-data)
2378 word d score val)
2379 (with-syntax-table gnus-adaptive-word-syntax-table
2380 ;; Go through all articles.
2381 (while (setq d (pop data))
2382 (when (and
2383 (not (gnus-data-pseudo-p d))
2384 (setq score
2385 (cdr (assq
2386 (gnus-data-mark d)
2387 gnus-adaptive-word-score-alist))))
2388 ;; This article has a mark that should lead to
2389 ;; adaptive word rules, so we insert the subject
2390 ;; and find all words in that string.
2391 (insert (mail-header-subject (gnus-data-header d)))
2392 (downcase-region (point-min) (point-max))
2393 (goto-char (point-min))
2394 (while (re-search-forward "\\b\\w+\\b" nil t)
2395 ;; Put the word and score into the hashtb.
2396 (setq val (gnus-gethash (setq word (match-string 0))
2397 hashtb))
2398 (when (or (not gnus-adaptive-word-length-limit)
2399 (> (length word)
2400 gnus-adaptive-word-length-limit))
2401 (setq val (+ score (or val 0)))
2402 (if (and gnus-adaptive-word-minimum
2403 (< val gnus-adaptive-word-minimum))
2404 (setq val gnus-adaptive-word-minimum))
2405 (gnus-sethash word val hashtb)))
2406 (erase-buffer))))
2407 ;; Make all the ignorable words ignored.
2408 (let ((ignored (append gnus-ignored-adaptive-words
2409 (if gnus-adaptive-word-no-group-words
2410 (message-tokenize-header
2411 (gnus-group-real-name
2412 gnus-newsgroup-name)
2413 "."))
2414 gnus-default-ignored-adaptive-words)))
2415 (while ignored
2416 (gnus-sethash (pop ignored) nil hashtb)))
2417 ;; Now we have all the words and scores, so we
2418 ;; add these rules to the ADAPT file.
2419 (set-buffer gnus-summary-buffer)
2420 (mapatoms
2421 (lambda (word)
2422 (when (symbol-value word)
2423 (gnus-summary-score-entry
2424 "subject" (symbol-name word) 'w (symbol-value word)
2425 date nil t)))
2426 hashtb))))))
2428 (defun gnus-score-edit-done ()
2429 (let ((bufnam (buffer-file-name (current-buffer)))
2430 (winconf gnus-prev-winconf))
2431 (when winconf
2432 (set-window-configuration winconf))
2433 (gnus-score-remove-from-cache bufnam)
2434 (gnus-score-load-file bufnam)
2435 (run-hooks 'gnus-score-edit-done-hook)))
2437 (defun gnus-score-find-trace ()
2438 "Find all score rules that applies to the current article."
2439 (interactive)
2440 (let ((old-scored gnus-newsgroup-scored))
2441 (let ((gnus-newsgroup-headers
2442 (list (gnus-summary-article-header)))
2443 (gnus-newsgroup-scored nil)
2444 ;; Must be synced with `gnus-score-edit-file-at-point':
2445 (frmt "%S [%s] -> %s\n")
2446 trace
2447 file)
2448 (save-excursion
2449 (nnheader-set-temp-buffer "*Score Trace*"))
2450 (setq gnus-score-trace nil)
2451 (gnus-possibly-score-headers 'trace)
2452 (if (not (setq trace gnus-score-trace))
2453 (gnus-error
2454 1 "No score rules apply to the current article (default score %d)."
2455 gnus-summary-default-score)
2456 (set-buffer "*Score Trace*")
2457 ;; Use a keymap instead?
2458 (local-set-key "q"
2459 (lambda ()
2460 (interactive)
2461 (bury-buffer nil)
2462 (gnus-summary-expand-window)))
2463 (local-set-key "k"
2464 (lambda ()
2465 (interactive)
2466 (kill-buffer (current-buffer))
2467 (gnus-summary-expand-window)))
2468 (local-set-key "e" (lambda ()
2469 "Run `gnus-score-edit-file-at-point'."
2470 (interactive)
2471 (gnus-score-edit-file-at-point)))
2472 (local-set-key "f" (lambda ()
2473 "Run `gnus-score-edit-file-at-point'."
2474 (interactive)
2475 (gnus-score-edit-file-at-point 'format)))
2476 (local-set-key "t" 'toggle-truncate-lines)
2477 (setq truncate-lines t)
2478 (dolist (entry trace)
2479 (setq file (or (car entry)
2480 ;; Must be synced with
2481 ;; `gnus-score-edit-file-at-point':
2482 "(non-file rule)"))
2483 (insert
2484 (format frmt
2485 (cdr entry)
2486 ;; Don't use `file-name-sans-extension' to see .SCORE and
2487 ;; .ADAPT directly:
2488 (file-name-nondirectory file)
2489 (abbreviate-file-name file))))
2490 (insert
2491 (format "\nTotal score: %d"
2492 (apply '+ (mapcar
2493 (lambda (s)
2494 (or (caddr s)
2495 gnus-score-interactive-default-score))
2496 trace))))
2497 (insert
2498 "\n\nQuick help:
2500 Type `e' to edit score file corresponding to the score rule on current line,
2501 `f' to format (pretty print) the score file and edit it,
2502 `t' toggle to truncate long lines in this buffer,
2503 `q' to quit, `k' to kill score trace buffer.
2505 The first sexp on each line is the score rule, followed by the file name of
2506 the score file and its full name, including the directory.")
2507 (goto-char (point-min))
2508 (gnus-configure-windows 'score-trace)))
2509 (set-buffer gnus-summary-buffer)
2510 (setq gnus-newsgroup-scored old-scored)))
2512 (defun gnus-score-find-favourite-words ()
2513 "List words used in scoring."
2514 (interactive)
2515 (let ((alists (gnus-score-load-files (gnus-all-score-files)))
2516 alist rule rules kill)
2517 ;; Go through all the score alists for this group
2518 ;; and find all `w' rules.
2519 (while (setq alist (pop alists))
2520 (while (setq rule (pop alist))
2521 (when (and (stringp (car rule))
2522 (equal "subject" (downcase (pop rule))))
2523 (while (setq kill (pop rule))
2524 (when (memq (nth 3 kill) '(w W word Word))
2525 (push (cons (or (nth 1 kill)
2526 gnus-score-interactive-default-score)
2527 (car kill))
2528 rules))))))
2529 (setq rules (sort rules (lambda (r1 r2)
2530 (string-lessp (cdr r1) (cdr r2)))))
2531 ;; Add up words that have appeared several times.
2532 (let ((r rules))
2533 (while (cdr r)
2534 (if (equal (cdar r) (cdadr r))
2535 (progn
2536 (setcar (car r) (+ (caar r) (caadr r)))
2537 (setcdr r (cddr r)))
2538 (pop r))))
2539 ;; Insert the words.
2540 (nnheader-set-temp-buffer "*Score Words*")
2541 (if (not (setq rules (sort rules (lambda (r1 r2) (> (car r1) (car r2))))))
2542 (gnus-error 3 "No word score rules")
2543 (while rules
2544 (insert (format "%-5d: %s\n" (caar rules) (cdar rules)))
2545 (pop rules))
2546 (goto-char (point-min))
2547 (gnus-configure-windows 'score-words))))
2549 (defun gnus-summary-rescore ()
2550 "Redo the entire scoring process in the current summary."
2551 (interactive)
2552 (gnus-score-save)
2553 (setq gnus-score-cache nil)
2554 (setq gnus-newsgroup-scored nil)
2555 (gnus-possibly-score-headers)
2556 (gnus-score-update-all-lines))
2558 (defun gnus-score-flush-cache ()
2559 "Flush the cache of score files."
2560 (interactive)
2561 (gnus-score-save)
2562 (setq gnus-score-cache nil
2563 gnus-score-alist nil
2564 gnus-short-name-score-file-cache nil)
2565 (gnus-message 6 "The score cache is now flushed"))
2567 (gnus-add-shutdown 'gnus-score-close 'gnus)
2569 (defvar gnus-score-file-alist-cache nil)
2571 (defun gnus-score-close ()
2572 "Clear all internal score variables."
2573 (setq gnus-score-cache nil
2574 gnus-internal-global-score-files nil
2575 gnus-score-file-list nil
2576 gnus-score-file-alist-cache nil))
2578 ;; Summary score marking commands.
2580 (defun gnus-summary-raise-same-subject-and-select (score)
2581 "Raise articles which has the same subject with SCORE and select the next."
2582 (interactive "p")
2583 (let ((subject (gnus-summary-article-subject)))
2584 (gnus-summary-raise-score score)
2585 (while (gnus-summary-find-subject subject)
2586 (gnus-summary-raise-score score))
2587 (gnus-summary-next-article t)))
2589 (defun gnus-summary-raise-same-subject (score)
2590 "Raise articles which has the same subject with SCORE."
2591 (interactive "p")
2592 (let ((subject (gnus-summary-article-subject)))
2593 (gnus-summary-raise-score score)
2594 (while (gnus-summary-find-subject subject)
2595 (gnus-summary-raise-score score))
2596 (gnus-summary-next-subject 1 t)))
2598 (defun gnus-score-delta-default (level)
2599 (if level (prefix-numeric-value level)
2600 gnus-score-interactive-default-score))
2602 (defun gnus-summary-raise-thread (&optional score)
2603 "Raise the score of the articles in the current thread with SCORE."
2604 (interactive "P")
2605 (setq score (gnus-score-delta-default score))
2606 (let (e)
2607 (save-excursion
2608 (let ((articles (gnus-summary-articles-in-thread)))
2609 (while articles
2610 (gnus-summary-goto-subject (car articles))
2611 (gnus-summary-raise-score score)
2612 (setq articles (cdr articles))))
2613 (setq e (point)))
2614 (let ((gnus-summary-check-current t))
2615 (unless (zerop (gnus-summary-next-subject 1 t))
2616 (goto-char e))))
2617 (gnus-summary-recenter)
2618 (gnus-summary-position-point)
2619 (gnus-set-mode-line 'summary))
2621 (defun gnus-summary-lower-same-subject-and-select (score)
2622 "Raise articles which has the same subject with SCORE and select the next."
2623 (interactive "p")
2624 (gnus-summary-raise-same-subject-and-select (- score)))
2626 (defun gnus-summary-lower-same-subject (score)
2627 "Raise articles which has the same subject with SCORE."
2628 (interactive "p")
2629 (gnus-summary-raise-same-subject (- score)))
2631 (defun gnus-summary-lower-thread (&optional score)
2632 "Lower score of articles in the current thread with SCORE."
2633 (interactive "P")
2634 (gnus-summary-raise-thread (- (gnus-score-delta-default score))))
2636 ;;; Finding score files.
2638 (defun gnus-score-score-files (group)
2639 "Return a list of all possible score files."
2640 ;; Search and set any global score files.
2641 (when gnus-global-score-files
2642 (unless gnus-internal-global-score-files
2643 (gnus-score-search-global-directories gnus-global-score-files)))
2644 ;; Fix the kill-file dir variable.
2645 (setq gnus-kill-files-directory
2646 (file-name-as-directory gnus-kill-files-directory))
2647 ;; If we can't read it, there are no score files.
2648 (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
2649 (setq gnus-score-file-list nil)
2650 (if (not (gnus-use-long-file-name 'not-score))
2651 ;; We do not use long file names, so we have to do some
2652 ;; directory traversing.
2653 (setq gnus-score-file-list
2654 (cons nil
2655 (or gnus-short-name-score-file-cache
2656 (prog2
2657 (gnus-message 6 "Finding all score files...")
2658 (setq gnus-short-name-score-file-cache
2659 (gnus-score-score-files-1
2660 gnus-kill-files-directory))
2661 (gnus-message 6 "Finding all score files...done")))))
2662 ;; We want long file names.
2663 (when (or (not gnus-score-file-list)
2664 (not (car gnus-score-file-list))
2665 (gnus-file-newer-than gnus-kill-files-directory
2666 (car gnus-score-file-list)))
2667 (setq gnus-score-file-list
2668 (cons (nth 5 (file-attributes gnus-kill-files-directory))
2669 (nreverse
2670 (directory-files
2671 gnus-kill-files-directory t
2672 (gnus-score-file-regexp)))))))
2673 (cdr gnus-score-file-list)))
2675 (defun gnus-score-score-files-1 (dir)
2676 "Return all possible score files under DIR."
2677 (let ((files (list (expand-file-name dir)))
2678 (regexp (gnus-score-file-regexp))
2679 (case-fold-search nil)
2680 seen out file)
2681 (while (setq file (pop files))
2682 (cond
2683 ;; Ignore files that start with a dot.
2684 ((string-match "^\\." (file-name-nondirectory file))
2685 nil)
2686 ;; Add subtrees of directory to also be searched.
2687 ((and (file-directory-p file)
2688 (not (member (file-truename file) seen)))
2689 (push (file-truename file) seen)
2690 (setq files (nconc (directory-files file t nil t) files)))
2691 ;; Add files to the list of score files.
2692 ((string-match regexp file)
2693 (push file out))))
2694 (or out
2695 ;; Return a dummy value.
2696 (list (expand-file-name "this.file.does.not.exist.SCORE"
2697 gnus-kill-files-directory)))))
2699 (defun gnus-score-file-regexp ()
2700 "Return a regexp that match all score files."
2701 (concat "\\(" (regexp-quote gnus-score-file-suffix )
2702 "\\|" (regexp-quote gnus-adaptive-file-suffix) "\\)\\'"))
2704 (defun gnus-score-find-bnews (group)
2705 "Return a list of score files for GROUP.
2706 The score files are those files in the ~/News/ directory which matches
2707 GROUP using BNews sys file syntax."
2708 (let* ((sfiles (append (gnus-score-score-files group)
2709 gnus-internal-global-score-files))
2710 (kill-dir (file-name-as-directory
2711 (expand-file-name gnus-kill-files-directory)))
2712 (klen (length kill-dir))
2713 (score-regexp (gnus-score-file-regexp))
2714 (trans (cdr (assq ?: nnheader-file-name-translation-alist)))
2715 (group-trans (nnheader-translate-file-chars group t))
2716 ofiles not-match regexp)
2717 (with-current-buffer (gnus-get-buffer-create "*gnus score files*")
2718 (buffer-disable-undo)
2719 ;; Go through all score file names and create regexp with them
2720 ;; as the source.
2721 (while sfiles
2722 (erase-buffer)
2723 (insert (car sfiles))
2724 (goto-char (point-min))
2725 ;; First remove the suffix itself.
2726 (when (re-search-forward (concat "." score-regexp) nil t)
2727 (replace-match "" t t)
2728 (goto-char (point-min))
2729 (if (looking-at (regexp-quote kill-dir))
2730 ;; If the file name was just "SCORE", `klen' is one character
2731 ;; too much.
2732 (delete-char (min (1- (point-max)) klen))
2733 (goto-char (point-max))
2734 (if (re-search-backward gnus-directory-sep-char-regexp nil t)
2735 (delete-region (1+ (point)) (point-min))
2736 (gnus-message 1 "Can't find directory separator in %s"
2737 (car sfiles))))
2738 ;; If short file names were used, we have to translate slashes.
2739 (goto-char (point-min))
2740 (let ((regexp (concat
2741 "[/:" (if trans (char-to-string trans)) "]")))
2742 (while (re-search-forward regexp nil t)
2743 (replace-match "." t t)))
2744 ;; Kludge to get rid of "nntp+" problems.
2745 (goto-char (point-min))
2746 (when (looking-at "nn[a-z]+\\+")
2747 (search-forward "+")
2748 (forward-char -1)
2749 (insert "\\")
2750 (forward-char 1))
2751 ;; Kludge to deal with "++".
2752 (while (search-forward "+" nil t)
2753 (replace-match "\\+" t t))
2754 ;; Translate "all" to ".*".
2755 (goto-char (point-min))
2756 (while (search-forward "all" nil t)
2757 (replace-match ".*" t t))
2758 (goto-char (point-min))
2759 ;; Deal with "not."s.
2760 (if (looking-at "not.")
2761 (progn
2762 (setq not-match t)
2763 (setq regexp
2764 (concat "^" (buffer-substring 5 (point-max)) "$")))
2765 (setq regexp (concat "^" (buffer-substring 1 (point-max)) "$"))
2766 (setq not-match nil))
2767 ;; Finally - if this resulting regexp matches the group name,
2768 ;; we add this score file to the list of score files
2769 ;; applicable to this group.
2770 (when (or (and not-match
2771 (ignore-errors
2772 (not (string-match regexp group-trans))))
2773 (and (not not-match)
2774 (ignore-errors (string-match regexp group-trans))))
2775 (push (car sfiles) ofiles)))
2776 (setq sfiles (cdr sfiles)))
2777 (gnus-kill-buffer (current-buffer))
2778 ;; Slight kludge here - the last score file returned should be
2779 ;; the local score file, whether it exists or not. This is so
2780 ;; that any score commands the user enters will go to the right
2781 ;; file, and not end up in some global score file.
2782 (let ((localscore (gnus-score-file-name group)))
2783 (setq ofiles (cons localscore (delete localscore ofiles))))
2784 (gnus-sort-score-files (nreverse ofiles)))))
2786 (defun gnus-score-find-single (group)
2787 "Return list containing the score file for GROUP."
2788 (list (or gnus-newsgroup-adaptive-score-file
2789 (gnus-score-file-name group gnus-adaptive-file-suffix))
2790 (gnus-score-file-name group)))
2792 (defun gnus-score-find-hierarchical (group)
2793 "Return list of score files for GROUP.
2794 This includes the score file for the group and all its parents."
2795 (let* ((prefix (gnus-group-real-prefix group))
2796 (all (list nil))
2797 (group (gnus-group-real-name group))
2798 (start 0))
2799 (while (string-match "\\." group (1+ start))
2800 (setq start (match-beginning 0))
2801 (push (substring group 0 start) all))
2802 (push group all)
2803 (setq all
2804 (nconc
2805 (mapcar (lambda (group)
2806 (gnus-score-file-name group gnus-adaptive-file-suffix))
2807 (setq all (nreverse all)))
2808 (mapcar 'gnus-score-file-name all)))
2809 (if (equal prefix "")
2811 (mapcar
2812 (lambda (file)
2813 (nnheader-translate-file-chars
2814 (concat (file-name-directory file) prefix
2815 (file-name-nondirectory file))))
2816 all))))
2818 (defun gnus-score-file-rank (file)
2819 "Return a number that says how specific score FILE is.
2820 Destroys the current buffer."
2821 (if (member file gnus-internal-global-score-files)
2823 (when (string-match
2824 (concat "^" (regexp-quote
2825 (expand-file-name
2826 (file-name-as-directory gnus-kill-files-directory))))
2827 file)
2828 (setq file (substring file (match-end 0))))
2829 (insert file)
2830 (goto-char (point-min))
2831 (let ((beg (point))
2832 elems)
2833 (while (re-search-forward "[./]" nil t)
2834 (push (buffer-substring beg (1- (point)))
2835 elems))
2836 (erase-buffer)
2837 (setq elems (delete "all" elems))
2838 (length elems))))
2840 (defun gnus-sort-score-files (files)
2841 "Sort FILES so that the most general files come first."
2842 (with-temp-buffer
2843 (let ((alist
2844 (mapcar
2845 (lambda (file)
2846 (cons (inline (gnus-score-file-rank file)) file))
2847 files)))
2848 (mapcar 'cdr (sort alist 'car-less-than-car)))))
2850 (defun gnus-score-find-alist (group)
2851 "Return list of score files for GROUP.
2852 The list is determined from the variable `gnus-score-file-alist'."
2853 (let ((alist gnus-score-file-multiple-match-alist)
2854 score-files)
2855 ;; if this group has been seen before, return the cached entry
2856 (if (setq score-files (assoc group gnus-score-file-alist-cache))
2857 (cdr score-files) ;ensures caching groups with no matches
2858 ;; handle the multiple match alist
2859 (while alist
2860 (when (string-match (caar alist) group)
2861 (setq score-files (append (cdar alist) score-files)))
2862 (setq alist (cdr alist)))
2863 (setq alist gnus-score-file-single-match-alist)
2864 ;; handle the single match alist
2865 (while alist
2866 (when (string-match (caar alist) group)
2867 ;; progn used just in case ("regexp") has no files
2868 ;; and score-files is still nil. -sj
2869 ;; this can be construed as a "stop searching here" feature :>
2870 ;; and used to simplify regexps in the single-alist
2871 (setq score-files (append (cdar alist) score-files))
2872 (setq alist nil))
2873 (setq alist (cdr alist)))
2874 ;; cache the score files
2875 (push (cons group score-files) gnus-score-file-alist-cache)
2876 score-files)))
2878 (defun gnus-all-score-files (&optional group)
2879 "Return a list of all score files for the current group."
2880 (let ((funcs gnus-score-find-score-files-function)
2881 (group (or group gnus-newsgroup-name))
2882 score-files)
2883 (when group
2884 ;; Make sure funcs is a list.
2885 (and funcs
2886 (not (listp funcs))
2887 (setq funcs (list funcs)))
2888 (when gnus-score-use-all-scores
2889 ;; Get the initial score files for this group.
2890 (when funcs
2891 (setq score-files (copy-sequence (gnus-score-find-alist group))))
2892 ;; Add any home adapt files.
2893 (let ((home (gnus-home-score-file group t)))
2894 (when home
2895 (push home score-files)
2896 (setq gnus-newsgroup-adaptive-score-file home)))
2897 ;; Check whether there is a `adapt-file' group parameter.
2898 (let ((param-file (gnus-group-find-parameter group 'adapt-file)))
2899 (when param-file
2900 (push param-file score-files)
2901 (setq gnus-newsgroup-adaptive-score-file param-file))))
2902 ;; Go through all the functions for finding score files (or actual
2903 ;; scores) and add them to a list.
2904 (while funcs
2905 (when (functionp (car funcs))
2906 (setq score-files
2907 (append score-files
2908 (nreverse (funcall (car funcs) group)))))
2909 (setq funcs (cdr funcs)))
2910 (when gnus-score-use-all-scores
2911 ;; Add any home score files.
2912 (let ((home (gnus-home-score-file group)))
2913 (when home
2914 (push home score-files)))
2915 ;; Check whether there is a `score-file' group parameter.
2916 (let ((param-file (gnus-group-find-parameter group 'score-file)))
2917 (when param-file
2918 (push param-file score-files))))
2919 ;; Expand all files names.
2920 (let ((files score-files))
2921 (while files
2922 (when (stringp (car files))
2923 (setcar files (expand-file-name
2924 (car files) gnus-kill-files-directory)))
2925 (pop files)))
2926 (setq score-files (nreverse score-files))
2927 ;; Remove any duplicate score files.
2928 (while (and score-files
2929 (member (car score-files) (cdr score-files)))
2930 (pop score-files))
2931 (let ((files score-files))
2932 (while (cdr files)
2933 (if (member (cadr files) (cddr files))
2934 (setcdr files (cddr files))
2935 (pop files))))
2936 ;; Do the scoring if there are any score files for this group.
2937 score-files)))
2939 (defun gnus-possibly-score-headers (&optional trace)
2940 "Do scoring if scoring is required."
2941 (let ((score-files (gnus-all-score-files)))
2942 (when score-files
2943 (gnus-score-headers score-files trace))))
2945 (defun gnus-score-file-name (newsgroup &optional suffix)
2946 "Return the name of a score file for NEWSGROUP."
2947 (let ((suffix (or suffix gnus-score-file-suffix)))
2948 (nnheader-translate-file-chars
2949 (cond
2950 ((or (null newsgroup)
2951 (string-equal newsgroup ""))
2952 ;; The global score file is placed at top of the directory.
2953 (expand-file-name suffix gnus-kill-files-directory))
2954 ((gnus-use-long-file-name 'not-score)
2955 ;; Append ".SCORE" to newsgroup name.
2956 (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
2957 "." suffix)
2958 gnus-kill-files-directory))
2960 ;; Place "SCORE" under the hierarchical directory.
2961 (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
2962 "/" suffix)
2963 gnus-kill-files-directory))))))
2965 (defun gnus-score-search-global-directories (files)
2966 "Scan all global score directories for score files."
2967 ;; Set the variable `gnus-internal-global-score-files' to all
2968 ;; available global score files.
2969 (interactive (list gnus-global-score-files))
2970 (let (out)
2971 (while files
2972 ;; #### /$ Unix-specific?
2973 (if (file-directory-p (car files))
2974 (setq out (nconc (directory-files
2975 (car files) t
2976 (concat (gnus-score-file-regexp) "$"))))
2977 (push (car files) out))
2978 (setq files (cdr files)))
2979 (setq gnus-internal-global-score-files out)))
2981 (defun gnus-score-default-fold-toggle ()
2982 "Toggle folding for new score file entries."
2983 (interactive)
2984 (setq gnus-score-default-fold (not gnus-score-default-fold))
2985 (if gnus-score-default-fold
2986 (gnus-message 1 "New score file entries will be case insensitive.")
2987 (gnus-message 1 "New score file entries will be case sensitive.")))
2989 ;;; Home score file.
2991 (defun gnus-home-score-file (group &optional adapt)
2992 "Return the home score file for GROUP.
2993 If ADAPT, return the home adaptive file instead."
2994 (let ((list (if adapt gnus-home-adapt-file gnus-home-score-file))
2995 elem found)
2996 ;; Make sure we have a list.
2997 (unless (listp list)
2998 (setq list (list list)))
2999 ;; Go through the list and look for matches.
3000 (while (and (not found)
3001 (setq elem (pop list)))
3002 (setq found
3003 (cond
3004 ;; Simple string.
3005 ((stringp elem)
3006 elem)
3007 ;; Function.
3008 ((functionp elem)
3009 (funcall elem group))
3010 ;; Regexp-file cons.
3011 ((consp elem)
3012 (when (string-match (gnus-globalify-regexp (car elem)) group)
3013 (replace-match (cadr elem) t nil group))))))
3014 (when found
3015 (setq found (nnheader-translate-file-chars found))
3016 (if (file-name-absolute-p found)
3017 found
3018 (nnheader-concat gnus-kill-files-directory found)))))
3020 (defun gnus-hierarchial-home-score-file (group)
3021 "Return the score file of the top-level hierarchy of GROUP."
3022 (if (string-match "^[^.]+\\." group)
3023 (concat (match-string 0 group) gnus-score-file-suffix)
3024 ;; Group name without any dots.
3025 (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
3026 gnus-score-file-suffix)))
3028 (defun gnus-hierarchial-home-adapt-file (group)
3029 "Return the adapt file of the top-level hierarchy of GROUP."
3030 (if (string-match "^[^.]+\\." group)
3031 (concat (match-string 0 group) gnus-adaptive-file-suffix)
3032 ;; Group name without any dots.
3033 (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
3034 gnus-adaptive-file-suffix)))
3036 (defun gnus-current-home-score-file (group)
3037 "Return the \"current\" regular score file."
3038 (car (gnus-score-find-alist group)))
3041 ;;; Score decays
3044 (defun gnus-decay-score (score)
3045 "Decay SCORE according to `gnus-score-decay-constant' and `gnus-score-decay-scale'."
3046 (let ((n (- score
3047 (* (if (< score 0) -1 1)
3048 (min (abs score)
3049 (max gnus-score-decay-constant
3050 (* (abs score)
3051 gnus-score-decay-scale)))))))
3052 (if (and (featurep 'xemacs)
3053 ;; XEmacs's floor can handle only the floating point
3054 ;; number below the half of the maximum integer.
3055 (> (abs n) (lsh -1 -2)))
3056 (string-to-number
3057 (car (split-string (number-to-string n) "\\.")))
3058 (floor n))))
3060 (defun gnus-decay-scores (alist day)
3061 "Decay non-permanent scores in ALIST."
3062 (let ((times (- (time-to-days (current-time)) day))
3063 kill entry updated score n)
3064 (unless (zerop times) ;Done decays today already?
3065 (while (setq entry (pop alist))
3066 (when (stringp (car entry))
3067 (setq entry (cdr entry))
3068 (while (setq kill (pop entry))
3069 (when (nth 2 kill)
3070 (setq updated t)
3071 (setq score (or (nth 1 kill)
3072 gnus-score-interactive-default-score)
3073 n times)
3074 (while (natnump (decf n))
3075 (setq score (funcall gnus-decay-score-function score)))
3076 (setcdr kill (cons score
3077 (cdr (cdr kill)))))))))
3078 ;; Return whether this score file needs to be saved. By Je-haysuss!
3079 updated))
3081 (provide 'gnus-score)
3083 ;;; gnus-score.el ends here