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