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