2001-08-19 Simon Josefsson <jas@extundo.com>
[gnus] / lisp / gnus-score.el
1 ;;; gnus-score.el --- scoring code for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        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 2, or (at your option)
14 ;; 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; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-sum)
34 (require 'gnus-range)
35 (require 'message)
36 (require 'score-mode)
37
38 (defcustom gnus-global-score-files nil
39   "List of global score files and directories.
40 Set this variable if you want to use people's score files.  One entry
41 for each score file or each score file directory.  Gnus will decide
42 by itself what score files are applicable to which group.
43
44 Say you want to use the single score file
45 \"/ftp.gnus.org@ftp:/pub/larsi/ding/score/soc.motss.SCORE\" and all
46 score files in the \"/ftp.some-where:/pub/score\" directory.
47
48  (setq gnus-global-score-files
49        '(\"/ftp.gnus.org:/pub/larsi/ding/score/soc.motss.SCORE\"
50          \"/ftp.some-where:/pub/score\"))"
51   :group 'gnus-score-files
52   :type '(repeat file))
53
54 (defcustom gnus-score-file-single-match-alist nil
55   "Alist mapping regexps to lists of score files.
56 Each element of this alist should be of the form
57         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
58
59 If the name of a group is matched by REGEXP, the corresponding scorefiles
60 will be used for that group.
61 The first match found is used, subsequent matching entries are ignored (to
62 use multiple matches, see gnus-score-file-multiple-match-alist).
63
64 These score files are loaded in addition to any files returned by
65 gnus-score-find-score-files-function (which see)."
66   :group 'gnus-score-files
67   :type '(repeat (cons regexp (repeat file))))
68
69 (defcustom gnus-score-file-multiple-match-alist nil
70   "Alist mapping regexps to lists of score files.
71 Each element of this alist should be of the form
72         (\"REGEXP\" [ \"SCORE-FILE-1\" ] [ \"SCORE-FILE-2\" ] ... )
73
74 If the name of a group is matched by REGEXP, the corresponding scorefiles
75 will be used for that group.
76 If multiple REGEXPs match a group, the score files corresponding to each
77 match will be used (for only one match to be used, see
78 gnus-score-file-single-match-alist).
79
80 These score files are loaded in addition to any files returned by
81 gnus-score-find-score-files-function (which see)."
82   :group 'gnus-score-files
83   :type '(repeat (cons regexp (repeat file))))
84
85 (defcustom gnus-score-file-suffix "SCORE"
86   "Suffix of the score files."
87   :group 'gnus-score-files
88   :type 'string)
89
90 (defcustom gnus-adaptive-file-suffix "ADAPT"
91   "Suffix of the adaptive score files."
92   :group 'gnus-score-files
93   :group 'gnus-score-adapt
94   :type 'string)
95
96 (defcustom gnus-score-find-score-files-function 'gnus-score-find-bnews
97   "Function used to find score files.
98 The function will be called with the group name as the argument, and
99 should return a list of score files to apply to that group.  The score
100 files do not actually have to exist.
101
102 Predefined values are:
103
104 gnus-score-find-single: Only apply the group's own score file.
105 gnus-score-find-hierarchical: Also apply score files from parent groups.
106 gnus-score-find-bnews: Apply score files whose names matches.
107
108 See the documentation to these functions for more information.
109
110 This variable can also be a list of functions to be called.  Each
111 function is given the group name as argument and should either return
112 a list of score files, or a list of score alists.
113
114 If functions other than these pre-defined functions are used,
115 the `a' symbolic prefix to the score commands will always use
116 \"all.SCORE\"."
117   :group 'gnus-score-files
118   :type '(radio (function-item gnus-score-find-single)
119                 (function-item gnus-score-find-hierarchical)
120                 (function-item gnus-score-find-bnews)
121                 (repeat :tag "List of functions"
122                         (choice (function :tag "Other" :value 'ignore)
123                                 (function-item gnus-score-find-single)
124                                 (function-item gnus-score-find-hierarchical)
125                                 (function-item gnus-score-find-bnews)))
126                 (function :tag "Other" :value 'ignore)))
127
128 (defcustom gnus-score-interactive-default-score 1000
129   "*Scoring commands will raise/lower the score with this number as the default."
130   :group 'gnus-score-default
131   :type 'integer)
132
133 (defcustom gnus-score-expiry-days 7
134   "*Number of days before unused score file entries are expired.
135 If this variable is nil, no score file entries will be expired."
136   :group 'gnus-score-expire
137   :type '(choice (const :tag "never" nil)
138                  number))
139
140 (defcustom gnus-update-score-entry-dates t
141   "*In non-nil, update matching score entry dates.
142 If this variable is nil, then score entries that provide matches
143 will be expired along with non-matching score entries."
144   :group 'gnus-score-expire
145   :type 'boolean)
146
147 (defcustom gnus-decay-scores nil
148   "*If non-nil, decay non-permanent scores."
149   :group 'gnus-score-decay
150   :type 'boolean)
151
152 (defcustom gnus-decay-score-function 'gnus-decay-score
153   "*Function called to decay a score.
154 It is called with one parameter -- the score to be decayed."
155   :group 'gnus-score-decay
156   :type '(radio (function-item gnus-decay-score)
157                 (function :tag "Other")))
158
159 (defcustom gnus-score-decay-constant 3
160   "*Decay all \"small\" scores with this amount."
161   :group 'gnus-score-decay
162   :type 'integer)
163
164 (defcustom gnus-score-decay-scale .05
165   "*Decay all \"big\" scores with this factor."
166   :group 'gnus-score-decay
167   :type 'number)
168
169 (defcustom gnus-home-score-file nil
170   "Variable to control where interactive score entries are to go.
171 It can be:
172
173  * A string
174    This file file will be used as the home score file.
175
176  * A function
177    The result of this function will be used as the home score file.
178    The function will be passed the name of the group as its
179    parameter.
180
181  * A list
182    The elements in this list can be:
183
184    * `(regexp file-name ...)'
185      If the `regexp' matches the group name, the first `file-name' will
186      will be used as the home score file.  (Multiple filenames are
187      allowed so that one may use gnus-score-file-single-match-alist to
188      set this variable.)
189
190    * A function.
191      If the function returns non-nil, the result will be used
192      as the home score file.  The function will be passed the
193      name of the group as its parameter.
194
195    * A string.  Use the string as the home score file.
196
197    The list will be traversed from the beginning towards the end looking
198    for matches."
199   :group 'gnus-score-files
200   :type '(choice string
201                  (repeat (choice string
202                                  (cons regexp (repeat file))
203                                  (function :value fun)))
204                  (function-item gnus-hierarchial-home-score-file)
205                  (function-item gnus-current-home-score-file)
206                  (function :value fun)))
207
208 (defcustom gnus-home-adapt-file nil
209   "Variable to control where new adaptive score entries are to go.
210 This variable allows the same syntax as `gnus-home-score-file'."
211   :group 'gnus-score-adapt
212   :group 'gnus-score-files
213   :type '(choice string
214                  (repeat (choice string
215                                  (cons regexp (repeat file))
216                                  (function :value fun)))
217                  (function :value fun)))
218
219 (defcustom gnus-default-adaptive-score-alist
220   '((gnus-kill-file-mark)
221     (gnus-unread-mark)
222     (gnus-read-mark (from 3) (subject 30))
223     (gnus-catchup-mark (subject -10))
224     (gnus-killed-mark (from -1) (subject -20))
225     (gnus-del-mark (from -2) (subject -15)))
226   "*Alist of marks and scores."
227   :group 'gnus-score-adapt
228   :type '(repeat (cons (symbol :tag "Mark")
229                        (repeat (list (choice :tag "Header"
230                                              (const from)
231                                              (const subject)
232                                              (symbol :tag "other"))
233                                      (integer :tag "Score"))))))
234
235 (defcustom gnus-adaptive-word-length-limit nil
236   "*Words of a length lesser than this limit will be ignored when doing adaptive scoring."
237   :group 'gnus-score-adapt
238   :type 'integer)
239
240 (defcustom gnus-ignored-adaptive-words nil
241   "List of words to be ignored when doing adaptive word scoring."
242   :group 'gnus-score-adapt
243   :type '(repeat string))
244
245 (defcustom gnus-default-ignored-adaptive-words
246   '("a" "i" "the" "to" "of" "and" "in" "is" "it" "for" "that" "if" "you"
247     "this" "be" "on" "with" "not" "have" "are" "or" "as" "from" "can"
248     "but" "by" "at" "an" "will" "no" "all" "was" "do" "there" "my" "one"
249     "so" "we" "they" "what" "would" "any" "which" "about" "get" "your"
250     "use" "some" "me" "then" "name" "like" "out" "when" "up" "time"
251     "other" "more" "only" "just" "end" "also" "know" "how" "new" "should"
252     "been" "than" "them" "he" "who" "make" "may" "people" "these" "now"
253     "their" "here" "into" "first" "could" "way" "had" "see" "work" "well"
254     "were" "two" "very" "where" "while" "us" "because" "good" "same"
255     "even" "much" "most" "many" "such" "long" "his" "over" "last" "since"
256     "right" "before" "our" "without" "too" "those" "why" "must" "part"
257     "being" "current" "back" "still" "go" "point" "value" "each" "did"
258     "both" "true" "off" "say" "another" "state" "might" "under" "start"
259     "try" "re")
260   "*Default list of words to be ignored when doing adaptive word scoring."
261   :group 'gnus-score-adapt
262   :type '(repeat string))
263
264 (defcustom gnus-default-adaptive-word-score-alist
265   `((,gnus-read-mark . 30)
266     (,gnus-catchup-mark . -10)
267     (,gnus-killed-mark . -20)
268     (,gnus-del-mark . -15))
269   "*Alist of marks and scores."
270   :group 'gnus-score-adapt
271   :type '(repeat (cons (character :tag "Mark")
272                        (integer :tag "Score"))))
273
274 (defcustom gnus-adaptive-word-minimum nil
275   "If a number, this is the minimum score value that can be assigned to a word."
276   :group 'gnus-score-adapt
277   :type '(choice (const nil) integer))
278
279 (defcustom gnus-adaptive-word-no-group-words nil
280   "If t, don't adaptively score words included in the group name."
281   :group 'gnus-score-adapt
282   :type 'boolean)
283
284 (defcustom gnus-score-mimic-keymap nil
285   "*Have the score entry functions pretend that they are a keymap."
286   :group 'gnus-score-default
287   :type 'boolean)
288
289 (defcustom gnus-score-exact-adapt-limit 10
290   "*Number that says how long a match has to be before using substring matching.
291 When doing adaptive scoring, one normally uses fuzzy or substring
292 matching.  However, if the header one matches is short, the possibility
293 for false positives is great, so if the length of the match is less
294 than this variable, exact matching will be used.
295
296 If this variable is nil, exact matching will always be used."
297   :group 'gnus-score-adapt
298   :type '(choice (const nil) integer))
299
300 (defcustom gnus-score-uncacheable-files "ADAPT$"
301   "All score files that match this regexp will not be cached."
302   :group 'gnus-score-adapt
303   :group 'gnus-score-files
304   :type 'regexp)
305
306 (defcustom gnus-score-default-header nil
307   "Default header when entering new scores.
308
309 Should be one of the following symbols.
310
311  a: from
312  s: subject
313  b: body
314  h: head
315  i: message-id
316  t: references
317  x: xref
318  e: `extra' (non-standard overview)
319  l: lines
320  d: date
321  f: followup
322
323 If nil, the user will be asked for a header."
324   :group 'gnus-score-default
325   :type '(choice (const :tag "from" a)
326                  (const :tag "subject" s)
327                  (const :tag "body" b)
328                  (const :tag "head" h)
329                  (const :tag "message-id" i)
330                  (const :tag "references" t)
331                  (const :tag "xref" x)
332                  (const :tag "extra" e)
333                  (const :tag "lines" l)
334                  (const :tag "date" d)
335                  (const :tag "followup" f)
336                  (const :tag "ask" nil)))
337
338 (defcustom gnus-score-default-type nil
339   "Default match type when entering new scores.
340
341 Should be one of the following symbols.
342
343  s: substring
344  e: exact string
345  f: fuzzy string
346  r: regexp string
347  b: before date
348  a: after date
349  n: this date
350  <: less than number
351  >: greater than number
352  =: equal to number
353
354 If nil, the user will be asked for a match type."
355   :group 'gnus-score-default
356   :type '(choice (const :tag "substring" s)
357                  (const :tag "exact string" e)
358                  (const :tag "fuzzy string" f)
359                  (const :tag "regexp string" r)
360                  (const :tag "before date" b)
361                  (const :tag "after date" a)
362                  (const :tag "this date" n)
363                  (const :tag "less than number" <)
364                  (const :tag "greater than number" >)
365                  (const :tag "equal than number" =)
366                  (const :tag "ask" nil)))
367
368 (defcustom gnus-score-default-fold nil
369   "Use case folding for new score file entries iff not nil."
370   :group 'gnus-score-default
371   :type 'boolean)
372
373 (defcustom gnus-score-default-duration nil
374   "Default duration of effect when entering new scores.
375
376 Should be one of the following symbols.
377
378  t: temporary
379  p: permanent
380  i: immediate
381
382 If nil, the user will be asked for a duration."
383   :group 'gnus-score-default
384   :type '(choice (const :tag "temporary" t)
385                  (const :tag "permanent" p)
386                  (const :tag "immediate" i)
387                  (const :tag "ask" nil)))
388
389 (defcustom gnus-score-after-write-file-function nil
390   "Function called with the name of the score file just written to disk."
391   :group 'gnus-score-files
392   :type '(choice (const nil) function))
393
394 (defcustom gnus-score-thread-simplify nil
395   "If non-nil, subjects will simplified as in threading."
396   :group 'gnus-score-various
397   :type 'boolean)
398
399 \f
400
401 ;; Internal variables.
402
403 (defvar gnus-score-use-all-scores t
404   "If nil, only `gnus-score-find-score-files-function' is used.")
405
406 (defvar gnus-adaptive-word-syntax-table
407   (let ((table (copy-syntax-table (standard-syntax-table)))
408         (numbers '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)))
409     (while numbers
410       (modify-syntax-entry (pop numbers) " " table))
411     (modify-syntax-entry ?' "w" table)
412     table)
413   "Syntax table used when doing adaptive word scoring.")
414
415 (defvar gnus-scores-exclude-files nil)
416 (defvar gnus-internal-global-score-files nil)
417 (defvar gnus-score-file-list nil)
418
419 (defvar gnus-short-name-score-file-cache nil)
420
421 (defvar gnus-score-help-winconf nil)
422 (defvar gnus-adaptive-score-alist gnus-default-adaptive-score-alist)
423 (defvar gnus-adaptive-word-score-alist gnus-default-adaptive-word-score-alist)
424 (defvar gnus-score-trace nil)
425 (defvar gnus-score-edit-buffer nil)
426
427 (defvar gnus-score-alist nil
428   "Alist containing score information.
429 The keys can be symbols or strings.  The following symbols are defined.
430
431 touched: If this alist has been modified.
432 mark:    Automatically mark articles below this.
433 expunge: Automatically expunge articles below this.
434 files:   List of other score files to load when loading this one.
435 eval:    Sexp to be evaluated when the score file is loaded.
436
437 String entries have the form (HEADER (MATCH TYPE SCORE DATE) ...)
438 where HEADER is the header being scored, MATCH is the string we are
439 looking for, TYPE is a flag indicating whether it should use regexp or
440 substring matching, SCORE is the score to add and DATE is the date
441 of the last successful match.")
442
443 (defvar gnus-score-cache nil)
444 (defvar gnus-scores-articles nil)
445 (defvar gnus-score-index nil)
446
447
448 (defconst gnus-header-index
449   ;; Name to index alist.
450   '(("number" 0 gnus-score-integer)
451     ("subject" 1 gnus-score-string)
452     ("from" 2 gnus-score-string)
453     ("date" 3 gnus-score-date)
454     ("message-id" 4 gnus-score-string)
455     ("references" 5 gnus-score-string)
456     ("chars" 6 gnus-score-integer)
457     ("lines" 7 gnus-score-integer)
458     ("xref" 8 gnus-score-string)
459     ("extra" 9 gnus-score-string)
460     ("head" -1 gnus-score-body)
461     ("body" -1 gnus-score-body)
462     ("all" -1 gnus-score-body)
463     ("followup" 2 gnus-score-followup)
464     ("thread" 5 gnus-score-thread)))
465
466 ;;; Summary mode score maps.
467
468 (gnus-define-keys (gnus-summary-score-map "V" gnus-summary-mode-map)
469   "s" gnus-summary-set-score
470   "S" gnus-summary-current-score
471   "c" gnus-score-change-score-file
472   "C" gnus-score-customize
473   "m" gnus-score-set-mark-below
474   "x" gnus-score-set-expunge-below
475   "R" gnus-summary-rescore
476   "e" gnus-score-edit-current-scores
477   "f" gnus-score-edit-file
478   "F" gnus-score-flush-cache
479   "t" gnus-score-find-trace
480   "w" gnus-score-find-favourite-words)
481
482 ;; Summary score file commands
483
484 ;; Much modification of the kill (ahem, score) code and lots of the
485 ;; functions are written by Per Abrahamsen <amanda@iesd.auc.dk>.
486
487 (defun gnus-summary-lower-score (&optional score symp)
488   "Make a score entry based on the current article.
489 The user will be prompted for header to score on, match type,
490 permanence, and the string to be used.  The numerical prefix will be
491 used as score."
492   (interactive (gnus-interactive "P\ny"))
493   (gnus-summary-increase-score (- (gnus-score-delta-default score)) symp))
494
495 (defun gnus-score-kill-help-buffer ()
496   (when (get-buffer "*Score Help*")
497     (kill-buffer "*Score Help*")
498     (when gnus-score-help-winconf
499       (set-window-configuration gnus-score-help-winconf))))
500
501 (defun gnus-summary-increase-score (&optional score symp)
502   "Make a score entry based on the current article.
503 The user will be prompted for header to score on, match type,
504 permanence, and the string to be used.  The numerical prefix will be
505 used as score."
506   (interactive (gnus-interactive "P\ny"))
507   (let* ((nscore (gnus-score-delta-default score))
508          (prefix (if (< nscore 0) ?L ?I))
509          (increase (> nscore 0))
510          (char-to-header
511           '((?a "from" nil nil string)
512             (?s "subject" nil nil string)
513             (?b "body" "" nil body-string)
514             (?h "head" "" nil body-string)
515             (?i "message-id" nil nil string)
516             (?r "references" "message-id" nil string)
517             (?x "xref" nil nil string)
518             (?e "extra" nil nil string)
519             (?l "lines" nil nil number)
520             (?d "date" nil nil date)
521             (?f "followup" nil nil string)
522             (?t "thread" "message-id" nil string)))
523          (char-to-type
524           '((?s s "substring" string)
525             (?e e "exact string" string)
526             (?f f "fuzzy string" string)
527             (?r r "regexp string" string)
528             (?z s "substring" body-string)
529             (?p r "regexp string" body-string)
530             (?b before "before date" date)
531             (?a after "after date" date)
532             (?n at "this date" date)
533             (?< < "less than number" number)
534             (?> > "greater than number" number)
535             (?= = "equal to number" number)))
536          (current-score-file gnus-current-score-file)
537          (char-to-perm
538           (list (list ?t (current-time-string) "temporary")
539                 '(?p perm "permanent") '(?i now "immediate")))
540          (mimic gnus-score-mimic-keymap)
541          (hchar (and gnus-score-default-header
542                      (aref (symbol-name gnus-score-default-header) 0)))
543          (tchar (and gnus-score-default-type
544                      (aref (symbol-name gnus-score-default-type) 0)))
545          (pchar (and gnus-score-default-duration
546                      (aref (symbol-name gnus-score-default-duration) 0)))
547          entry temporary type match extra)
548
549     (unwind-protect
550         (progn
551
552           ;; First we read the header to score.
553           (while (not hchar)
554             (if mimic
555                 (progn
556                   (sit-for 1)
557                   (message "%c-" prefix))
558               (message "%s header (%s?): " (if increase "Increase" "Lower")
559                        (mapconcat (lambda (s) (char-to-string (car s)))
560                                   char-to-header "")))
561             (setq hchar (read-char))
562             (when (or (= hchar ??) (= hchar ?\C-h))
563               (setq hchar nil)
564               (gnus-score-insert-help "Match on header" char-to-header 1)))
565
566           (gnus-score-kill-help-buffer)
567           (unless (setq entry (assq (downcase hchar) char-to-header))
568             (if mimic (error "%c %c" prefix hchar)
569               (error "Invalid header type")))
570
571           (when (/= (downcase hchar) hchar)
572             ;; This was a majuscule, so we end reading and set the defaults.
573             (if mimic (message "%c %c" prefix hchar) (message ""))
574             (setq tchar (or tchar ?s)
575                   pchar (or pchar ?t)))
576
577           (let ((legal-types
578                  (delq nil
579                        (mapcar (lambda (s)
580                                  (if (eq (nth 4 entry)
581                                          (nth 3 s))
582                                      s nil))
583                                char-to-type))))
584             ;; We continue reading - the type.
585             (while (not tchar)
586               (if mimic
587                   (progn
588                     (sit-for 1) (message "%c %c-" prefix hchar))
589                 (message "%s header '%s' with match type (%s?): "
590                          (if increase "Increase" "Lower")
591                          (nth 1 entry)
592                          (mapconcat (lambda (s) (char-to-string (car s)))
593                                     legal-types "")))
594               (setq tchar (read-char))
595               (when (or (= tchar ??) (= tchar ?\C-h))
596                 (setq tchar nil)
597                 (gnus-score-insert-help "Match type" legal-types 2)))
598
599             (gnus-score-kill-help-buffer)
600             (unless (setq type (nth 1 (assq (downcase tchar) legal-types)))
601               (if mimic (error "%c %c" prefix hchar)
602                 (error "Invalid match type"))))
603
604           (when (/= (downcase tchar) tchar)
605             ;; It was a majuscule, so we end reading and use the default.
606             (if mimic (message "%c %c %c" prefix hchar tchar)
607               (message ""))
608             (setq pchar (or pchar ?t)))
609
610           ;; We continue reading.
611           (while (not pchar)
612             (if mimic
613                 (progn
614                   (sit-for 1) (message "%c %c %c-" prefix hchar tchar))
615               (message "%s permanence (%s?): " (if increase "Increase" "Lower")
616                        (mapconcat (lambda (s) (char-to-string (car s)))
617                                   char-to-perm "")))
618             (setq pchar (read-char))
619             (when (or (= pchar ??) (= pchar ?\C-h))
620               (setq pchar nil)
621               (gnus-score-insert-help "Match permanence" char-to-perm 2)))
622
623           (gnus-score-kill-help-buffer)
624           (if mimic (message "%c %c %c" prefix hchar tchar pchar)
625             (message ""))
626           (unless (setq temporary (cadr (assq pchar char-to-perm)))
627             ;; Deal with der(r)ided superannuated paradigms.
628             (when (and (eq (1+ prefix) 77)
629                        (eq (+ hchar 12) 109)
630                        (eq (1- tchar) 113)
631                        (eq (- pchar 4) 111))
632               (error "You rang?"))
633             (if mimic
634                 (error "%c %c %c %c" prefix hchar tchar pchar)
635               (error "Invalid match duration"))))
636       ;; Always kill the score help buffer.
637       (gnus-score-kill-help-buffer))
638
639     ;; If scoring an extra (non-standard overview) header,
640     ;; we must find out which header is in question.
641     (setq extra
642           (and gnus-extra-headers
643                (equal (nth 1 entry) "extra")
644                (intern                  ; need symbol
645                 (gnus-completing-read
646                  (symbol-name (car gnus-extra-headers)) ; default response
647                  "Score extra header:"  ; prompt
648                  (mapcar (lambda (x)    ; completion list
649                            (cons (symbol-name x) x))
650                          gnus-extra-headers)
651                  nil                    ; no completion limit
652                  t))))                  ; require match
653     ;; extra is now nil or a symbol.
654
655     ;; We have all the data, so we enter this score.
656     (setq match (if (string= (nth 2 entry) "") ""
657                   (gnus-summary-header (or (nth 2 entry) (nth 1 entry))
658                                        nil extra)))
659
660     ;; Modify the match, perhaps.
661     (cond
662      ((equal (nth 1 entry) "xref")
663       (when (string-match "^Xref: *" match)
664         (setq match (substring match (match-end 0))))
665       (when (string-match "^[^:]* +" match)
666         (setq match (substring match (match-end 0))))))
667
668     (when (memq type '(r R regexp Regexp))
669       (setq match (regexp-quote match)))
670
671     ;; Change score file to the "all.SCORE" file.
672     (when (eq symp 'a)
673       (save-excursion
674         (set-buffer gnus-summary-buffer)
675         (gnus-score-load-file
676          ;; This is a kludge; yes...
677          (cond
678           ((eq gnus-score-find-score-files-function
679                'gnus-score-find-hierarchical)
680            (gnus-score-file-name ""))
681           ((eq gnus-score-find-score-files-function 'gnus-score-find-single)
682            current-score-file)
683           (t
684            (gnus-score-file-name "all"))))))
685
686     (gnus-summary-score-entry
687      (nth 1 entry)                      ; Header
688      match                              ; Match
689      type                               ; Type
690      (if (eq score 's) nil score)       ; Score
691      (if (eq temporary 'perm)           ; Temp
692          nil
693        temporary)
694      (not (nth 3 entry))                ; Prompt
695      nil                                ; not silent
696      extra)                             ; non-standard overview.
697
698     (when (eq symp 'a)
699       ;; We change the score file back to the previous one.
700       (save-excursion
701         (set-buffer gnus-summary-buffer)
702         (gnus-score-load-file current-score-file)))))
703
704 (defun gnus-score-insert-help (string alist idx)
705   (setq gnus-score-help-winconf (current-window-configuration))
706   (save-excursion
707     (set-buffer (gnus-get-buffer-create "*Score Help*"))
708     (buffer-disable-undo)
709     (delete-windows-on (current-buffer))
710     (erase-buffer)
711     (insert string ":\n\n")
712     (let ((max -1)
713           (list alist)
714           (i 0)
715           n width pad format)
716       ;; find the longest string to display
717       (while list
718         (setq n (length (nth idx (car list))))
719         (unless (> max n)
720           (setq max n))
721         (setq list (cdr list)))
722       (setq max (+ max 4))              ; %c, `:', SPACE, a SPACE at end
723       (setq n (/ (1- (window-width)) max)) ; items per line
724       (setq width (/ (1- (window-width)) n)) ; width of each item
725       ;; insert `n' items, each in a field of width `width'
726       (while alist
727         (if (< i n)
728             ()
729           (setq i 0)
730           (delete-char -1)              ; the `\n' takes a char
731           (insert "\n"))
732         (setq pad (- width 3))
733         (setq format (concat "%c: %-" (int-to-string pad) "s"))
734         (insert (format format (caar alist) (nth idx (car alist))))
735         (setq alist (cdr alist))
736         (setq i (1+ i))))
737     ;; display ourselves in a small window at the bottom
738     (gnus-appt-select-lowest-window)
739     (split-window)
740     (pop-to-buffer "*Score Help*")
741     (let ((window-min-height 1))
742       (shrink-window-if-larger-than-buffer))
743     (select-window (get-buffer-window gnus-summary-buffer t))))
744
745 (defun gnus-summary-header (header &optional no-err extra)
746   ;; Return HEADER for current articles, or error.
747   (let ((article (gnus-summary-article-number))
748         headers)
749     (if article
750         (if (and (setq headers (gnus-summary-article-header article))
751                  (vectorp headers))
752             (if extra                   ; `header' must be "extra"
753                 (or (cdr (assq extra (mail-header-extra headers))) "")
754               (aref headers (nth 1 (assoc header gnus-header-index))))
755           (if no-err
756               nil
757             (error "Pseudo-articles can't be scored")))
758       (if no-err
759           (error "No article on current line")
760         nil))))
761
762 (defun gnus-newsgroup-score-alist ()
763   (or
764    (let ((param-file (gnus-group-find-parameter
765                       gnus-newsgroup-name 'score-file)))
766      (when param-file
767        (gnus-score-load param-file)))
768    (gnus-score-load
769     (gnus-score-file-name gnus-newsgroup-name)))
770   gnus-score-alist)
771
772 (defsubst gnus-score-get (symbol &optional alist)
773   ;; Get SYMBOL's definition in ALIST.
774   (cdr (assoc symbol
775               (or alist
776                   gnus-score-alist
777                   (gnus-newsgroup-score-alist)))))
778
779 (defun gnus-summary-score-entry (header match type score date
780                                         &optional prompt silent extra)
781   "Enter score file entry.
782 HEADER is the header being scored.
783 MATCH is the string we are looking for.
784 TYPE is the match type: substring, regexp, exact, fuzzy.
785 SCORE is the score to add.
786 DATE is the expire date, or nil for no expire, or 'now for immediate expire.
787 If optional argument `PROMPT' is non-nil, allow user to edit match.
788 If optional argument `SILENT' is nil, show effect of score entry.
789 If optional argument `EXTRA' is non-nil, it's a non-standard overview header."
790   ;; Regexp is the default type.
791   (when (eq type t)
792     (setq type 'r))
793   ;; Simplify matches...
794   (cond ((or (eq type 'r) (eq type 's) (eq type nil))
795          (setq match (if match (gnus-simplify-subject-re match) "")))
796         ((eq type 'f)
797          (setq match (gnus-simplify-subject-fuzzy match))))
798   (let ((score (gnus-score-delta-default score))
799         (header (downcase header))
800         new)
801     (set-text-properties 0 (length header) nil header)
802     (when prompt
803       (setq match (read-string
804                    (format "Match %s on %s, %s: "
805                            (cond ((eq date 'now)
806                                   "now")
807                                  ((stringp date)
808                                   "temp")
809                                  (t "permanent"))
810                            header
811                            (if (< score 0) "lower" "raise"))
812                    (if (numberp match)
813                        (int-to-string match)
814                      match))))
815
816     ;; If this is an integer comparison, we transform from string to int.
817     (if (eq (nth 2 (assoc header gnus-header-index)) 'gnus-score-integer)
818         (if (stringp match)
819             (setq match (string-to-int match)))
820       (set-text-properties 0 (length match) nil match))
821
822     (unless (eq date 'now)
823       ;; Add the score entry to the score file.
824       (when (= score gnus-score-interactive-default-score)
825         (setq score nil))
826       (let ((old (gnus-score-get header))
827             elem)
828         (setq new
829               (cond
830                (extra
831                 (list match score
832                       (and date (if (numberp date) date
833                                   (date-to-day date)))
834                       type (symbol-name extra)))
835                (type
836                 (list match score
837                       (and date (if (numberp date) date
838                                   (date-to-day date)))
839                       type))
840                (date (list match score (date-to-day date)))
841                (score (list match score))
842                (t (list match))))
843         ;; We see whether we can collapse some score entries.
844         ;; This isn't quite correct, because there may be more elements
845         ;; later on with the same key that have matching elems...  Hm.
846         (if (and old
847                  (setq elem (assoc match old))
848                  (eq (nth 3 elem) (nth 3 new))
849                  (or (and (numberp (nth 2 elem)) (numberp (nth 2 new)))
850                      (and (not (nth 2 elem)) (not (nth 2 new)))))
851             ;; Yup, we just add this new score to the old elem.
852             (setcar (cdr elem) (+ (or (nth 1 elem)
853                                       gnus-score-interactive-default-score)
854                                   (or (nth 1 new)
855                                       gnus-score-interactive-default-score)))
856           ;; Nope, we have to add a new elem.
857           (gnus-score-set header (if old (cons new old) (list new)) nil t))
858         (gnus-score-set 'touched '(t))))
859
860     ;; Score the current buffer.
861     (unless silent
862       (if (and (>= (nth 1 (assoc header gnus-header-index)) 0)
863                (eq (nth 2 (assoc header gnus-header-index))
864                    'gnus-score-string))
865           (gnus-summary-score-effect header match type score extra)
866         (gnus-summary-rescore)))
867
868     ;; Return the new scoring rule.
869     new))
870
871 (defun gnus-summary-score-effect (header match type score extra)
872   "Simulate the effect of a score file entry.
873 HEADER is the header being scored.
874 MATCH is the string we are looking for.
875 TYPE is the score type.
876 SCORE is the score to add.
877 EXTRA is the possible non-standard header."
878   (interactive (list (completing-read "Header: "
879                                       gnus-header-index
880                                       (lambda (x) (fboundp (nth 2 x)))
881                                       t)
882                      (read-string "Match: ")
883                      (y-or-n-p "Use regexp match? ")
884                      (prefix-numeric-value current-prefix-arg)))
885   (save-excursion
886     (unless (and (stringp match) (> (length match) 0))
887       (error "No match"))
888     (goto-char (point-min))
889     (let ((regexp (cond ((eq type 'f)
890                          (gnus-simplify-subject-fuzzy match))
891                         ((eq type 'r)
892                          match)
893                         ((eq type 'e)
894                          (concat "\\`" (regexp-quote match) "\\'"))
895                         (t
896                          (regexp-quote match)))))
897       (while (not (eobp))
898         (let ((content (gnus-summary-header header 'noerr extra))
899               (case-fold-search t))
900           (and content
901                (when (if (eq type 'f)
902                          (string-equal (gnus-simplify-subject-fuzzy content)
903                                        regexp)
904                        (string-match regexp content))
905                  (gnus-summary-raise-score score))))
906         (beginning-of-line 2))))
907   (gnus-set-mode-line 'summary))
908
909 (defun gnus-summary-score-crossposting (score date)
910   ;; Enter score file entry for current crossposting.
911   ;; SCORE is the score to add.
912   ;; DATE is the expire date.
913   (let ((xref (gnus-summary-header "xref"))
914         (start 0)
915         group)
916     (unless xref
917       (error "This article is not crossposted"))
918     (while (string-match " \\([^ \t]+\\):" xref start)
919       (setq start (match-end 0))
920       (when (not (string=
921                   (setq group
922                         (substring xref (match-beginning 1) (match-end 1)))
923                   gnus-newsgroup-name))
924         (gnus-summary-score-entry
925          "xref" (concat " " group ":") nil score date t)))))
926
927 \f
928 ;;;
929 ;;; Gnus Score Files
930 ;;;
931
932 ;; All score code written by Per Abrahamsen <abraham@iesd.auc.dk>.
933
934 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
935 (defun gnus-score-set-mark-below (score)
936   "Automatically mark articles with score below SCORE as read."
937   (interactive
938    (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
939              (string-to-int (read-string "Mark below: ")))))
940   (setq score (or score gnus-summary-default-score 0))
941   (gnus-score-set 'mark (list score))
942   (gnus-score-set 'touched '(t))
943   (setq gnus-summary-mark-below score)
944   (gnus-score-update-lines))
945
946 (defun gnus-score-update-lines ()
947   "Update all lines in the summary buffer."
948   (save-excursion
949     (goto-char (point-min))
950     (while (not (eobp))
951       (gnus-summary-update-line)
952       (forward-line 1))))
953
954 (defun gnus-score-update-all-lines ()
955   "Update all lines in the summary buffer, even the hidden ones."
956   (save-excursion
957     (goto-char (point-min))
958     (let (hidden)
959       (while (not (eobp))
960         (when (gnus-summary-show-thread)
961           (push (point) hidden))
962         (gnus-summary-update-line)
963         (forward-line 1))
964       ;; Re-hide the hidden threads.
965       (while hidden
966         (goto-char (pop hidden))
967         (gnus-summary-hide-thread)))))
968
969 (defun gnus-score-set-expunge-below (score)
970   "Automatically expunge articles with score below SCORE."
971   (interactive
972    (list (or (and current-prefix-arg (prefix-numeric-value current-prefix-arg))
973              (string-to-int (read-string "Set expunge below: ")))))
974   (setq score (or score gnus-summary-default-score 0))
975   (gnus-score-set 'expunge (list score))
976   (gnus-score-set 'touched '(t)))
977
978 (defun gnus-score-followup-article (&optional score)
979   "Add SCORE to all followups to the article in the current buffer."
980   (interactive "P")
981   (setq score (gnus-score-delta-default score))
982   (when (gnus-buffer-live-p gnus-summary-buffer)
983     (save-excursion
984       (save-restriction
985         (message-narrow-to-headers)
986         (let ((id (mail-fetch-field "message-id")))
987           (when id
988             (set-buffer gnus-summary-buffer)
989             (gnus-summary-score-entry
990              "references" (concat id "[ \t]*$") 'r
991              score (current-time-string) nil t)))))))
992
993 (defun gnus-score-followup-thread (&optional score)
994   "Add SCORE to all later articles in the thread the current buffer is part of."
995   (interactive "P")
996   (setq score (gnus-score-delta-default score))
997   (when (gnus-buffer-live-p gnus-summary-buffer)
998     (save-excursion
999       (save-restriction
1000         (goto-char (point-min))
1001         (let ((id (mail-fetch-field "message-id")))
1002           (when id
1003             (set-buffer gnus-summary-buffer)
1004             (gnus-summary-score-entry
1005              "references" id 's
1006              score (current-time-string))))))))
1007
1008 (defun gnus-score-set (symbol value &optional alist warn)
1009   ;; Set SYMBOL to VALUE in ALIST.
1010   (let* ((alist
1011           (or alist
1012               gnus-score-alist
1013               (gnus-newsgroup-score-alist)))
1014          (entry (assoc symbol alist)))
1015     (cond ((gnus-score-get 'read-only alist)
1016            ;; This is a read-only score file, so we do nothing.
1017            (when warn
1018              (gnus-message 4 "Note: read-only score file; entry discarded")))
1019           (entry
1020            (setcdr entry value))
1021           ((null alist)
1022            (error "Empty alist"))
1023           (t
1024            (setcdr alist
1025                    (cons (cons symbol value) (cdr alist)))))))
1026
1027 (defun gnus-summary-raise-score (n)
1028   "Raise the score of the current article by N."
1029   (interactive "p")
1030   (gnus-summary-set-score (+ (gnus-summary-article-score)
1031                              (or n gnus-score-interactive-default-score ))))
1032
1033 (defun gnus-summary-set-score (n)
1034   "Set the score of the current article to N."
1035   (interactive "p")
1036   (save-excursion
1037     (gnus-summary-show-thread)
1038     (let ((buffer-read-only nil))
1039       ;; Set score.
1040       (gnus-summary-update-mark
1041        (if (= n (or gnus-summary-default-score 0)) ?  ;Whitespace
1042          (if (< n (or gnus-summary-default-score 0))
1043              gnus-score-below-mark gnus-score-over-mark))
1044        'score))
1045     (let* ((article (gnus-summary-article-number))
1046            (score (assq article gnus-newsgroup-scored)))
1047       (if score (setcdr score n)
1048         (push (cons article n) gnus-newsgroup-scored)))
1049     (gnus-summary-update-line)))
1050
1051 (defun gnus-summary-current-score ()
1052   "Return the score of the current article."
1053   (interactive)
1054   (gnus-message 1 "%s" (gnus-summary-article-score)))
1055
1056 (defun gnus-score-change-score-file (file)
1057   "Change current score alist."
1058   (interactive
1059    (list (read-file-name "Change to score file: " gnus-kill-files-directory)))
1060   (gnus-score-load-file file)
1061   (gnus-set-mode-line 'summary))
1062
1063 (defvar gnus-score-edit-exit-function)
1064 (defun gnus-score-edit-current-scores (file)
1065   "Edit the current score alist."
1066   (interactive (list gnus-current-score-file))
1067   (if (not gnus-current-score-file)
1068       (error "No current score file")
1069     (let ((winconf (current-window-configuration)))
1070       (when (buffer-name gnus-summary-buffer)
1071         (gnus-score-save))
1072       (gnus-make-directory (file-name-directory file))
1073       (setq gnus-score-edit-buffer (find-file-noselect file))
1074       (gnus-configure-windows 'edit-score)
1075       (gnus-score-mode)
1076       (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1077       (make-local-variable 'gnus-prev-winconf)
1078       (setq gnus-prev-winconf winconf))
1079     (gnus-message
1080      4 (substitute-command-keys
1081         "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits"))))
1082
1083 (defun gnus-score-edit-file (file)
1084   "Edit a score file."
1085   (interactive
1086    (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
1087   (gnus-make-directory (file-name-directory file))
1088   (when (buffer-name gnus-summary-buffer)
1089     (gnus-score-save))
1090   (let ((winconf (current-window-configuration)))
1091     (setq gnus-score-edit-buffer (find-file-noselect file))
1092     (gnus-configure-windows 'edit-score)
1093     (gnus-score-mode)
1094     (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1095     (make-local-variable 'gnus-prev-winconf)
1096     (setq gnus-prev-winconf winconf))
1097   (gnus-message
1098    4 (substitute-command-keys
1099       "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
1100
1101 (defun gnus-score-load-file (file)
1102   ;; Load score file FILE.  Returns a list a retrieved score-alists.
1103   (let* ((file (expand-file-name
1104                 (or (and (string-match
1105                           (concat "^" (regexp-quote
1106                                        (expand-file-name
1107                                         gnus-kill-files-directory)))
1108                           (expand-file-name file))
1109                          file)
1110                     (expand-file-name file gnus-kill-files-directory))))
1111          (cached (assoc file gnus-score-cache))
1112          (global (member file gnus-internal-global-score-files))
1113          lists alist)
1114     (if cached
1115         ;; The score file was already loaded.
1116         (setq alist (cdr cached))
1117       ;; We load the score file.
1118       (setq gnus-score-alist nil)
1119       (setq alist (gnus-score-load-score-alist file))
1120       ;; We add '(touched) to the alist to signify that it hasn't been
1121       ;; touched (yet).
1122       (unless (assq 'touched alist)
1123         (push (list 'touched nil) alist))
1124       ;; If it is a global score file, we make it read-only.
1125       (and global
1126            (not (assq 'read-only alist))
1127            (push (list 'read-only t) alist))
1128       (push (cons file alist) gnus-score-cache))
1129     (let ((a alist)
1130           found)
1131       (while a
1132         ;; Downcase all header names.
1133         (cond
1134          ((stringp (caar a))
1135           (setcar (car a) (downcase (caar a)))
1136           (setq found t))
1137          ;; Advanced scoring.
1138          ((consp (caar a))
1139           (setq found t)))
1140         (pop a))
1141       ;; If there are actual scores in the alist, we add it to the
1142       ;; return value of this function.
1143       (when found
1144         (setq lists (list alist))))
1145     ;; Treat the other possible atoms in the score alist.
1146     (let ((mark (car (gnus-score-get 'mark alist)))
1147           (expunge (car (gnus-score-get 'expunge alist)))
1148           (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
1149           (files (gnus-score-get 'files alist))
1150           (exclude-files (gnus-score-get 'exclude-files alist))
1151           (orphan (car (gnus-score-get 'orphan alist)))
1152           (adapt (gnus-score-get 'adapt alist))
1153           (thread-mark-and-expunge
1154            (car (gnus-score-get 'thread-mark-and-expunge alist)))
1155           (adapt-file (car (gnus-score-get 'adapt-file alist)))
1156           (local (gnus-score-get 'local alist))
1157           (decay (car (gnus-score-get 'decay alist)))
1158           (eval (car (gnus-score-get 'eval alist))))
1159       ;; Perform possible decays.
1160       (when (and gnus-decay-scores
1161                  (or cached (file-exists-p file))
1162                  (or (not decay)
1163                      (gnus-decay-scores alist decay)))
1164         (gnus-score-set 'touched '(t) alist)
1165         (gnus-score-set 'decay (list (time-to-days (current-time))) alist))
1166       ;; We do not respect eval and files atoms from global score
1167       ;; files.
1168       (when (and files (not global))
1169         (setq lists (apply 'append lists
1170                            (mapcar (lambda (file)
1171                                      (gnus-score-load-file file))
1172                                    (if adapt-file (cons adapt-file files)
1173                                      files)))))
1174       (when (and eval (not global))
1175         (eval eval))
1176       ;; We then expand any exclude-file directives.
1177       (setq gnus-scores-exclude-files
1178             (nconc
1179              (apply
1180               'nconc
1181               (mapcar
1182                (lambda (sfile)
1183                  (list
1184                   (expand-file-name sfile (file-name-directory file))
1185                   (expand-file-name sfile gnus-kill-files-directory)))
1186                exclude-files))
1187              gnus-scores-exclude-files))
1188       (when local
1189         (save-excursion
1190           (set-buffer gnus-summary-buffer)
1191           (while local
1192             (and (consp (car local))
1193                  (symbolp (caar local))
1194                  (progn
1195                    (make-local-variable (caar local))
1196                    (set (caar local) (nth 1 (car local)))))
1197             (setq local (cdr local)))))
1198       (when orphan
1199         (setq gnus-orphan-score orphan))
1200       (setq gnus-adaptive-score-alist
1201             (cond ((equal adapt '(t))
1202                    (setq gnus-newsgroup-adaptive t)
1203                    gnus-default-adaptive-score-alist)
1204                   ((equal adapt '(ignore))
1205                    (setq gnus-newsgroup-adaptive nil))
1206                   ((consp adapt)
1207                    (setq gnus-newsgroup-adaptive t)
1208                    adapt)
1209                   (t
1210                    ;;(setq gnus-newsgroup-adaptive gnus-use-adaptive-scoring)
1211                    gnus-default-adaptive-score-alist)))
1212       (setq gnus-thread-expunge-below
1213             (or thread-mark-and-expunge gnus-thread-expunge-below))
1214       (setq gnus-summary-mark-below
1215             (or mark mark-and-expunge gnus-summary-mark-below))
1216       (setq gnus-summary-expunge-below
1217             (or expunge mark-and-expunge gnus-summary-expunge-below))
1218       (setq gnus-newsgroup-adaptive-score-file
1219             (or adapt-file gnus-newsgroup-adaptive-score-file)))
1220     (setq gnus-current-score-file file)
1221     (setq gnus-score-alist alist)
1222     lists))
1223
1224 (defun gnus-score-load (file)
1225   ;; Load score FILE.
1226   (let ((cache (assoc file gnus-score-cache)))
1227     (if cache
1228         (setq gnus-score-alist (cdr cache))
1229       (setq gnus-score-alist nil)
1230       (gnus-score-load-score-alist file)
1231       (unless gnus-score-alist
1232         (setq gnus-score-alist (copy-alist '((touched nil)))))
1233       (push (cons file gnus-score-alist) gnus-score-cache))))
1234
1235 (defun gnus-score-remove-from-cache (file)
1236   (setq gnus-score-cache
1237         (delq (assoc file gnus-score-cache) gnus-score-cache)))
1238
1239 (defun gnus-score-load-score-alist (file)
1240   "Read score FILE."
1241   (let (alist)
1242     (if (not (file-readable-p file))
1243         ;; Couldn't read file.
1244         (setq gnus-score-alist nil)
1245       ;; Read file.
1246       (with-temp-buffer
1247         (let ((coding-system-for-read score-mode-coding-system))
1248           (insert-file-contents file))
1249         (goto-char (point-min))
1250         ;; Only do the loading if the score file isn't empty.
1251         (when (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
1252           (setq alist
1253                 (condition-case ()
1254                     (read (current-buffer))
1255                   (error
1256                    (gnus-error 3.2 "Problem with score file %s" file))))))
1257       (cond
1258        ((and alist
1259              (atom alist))
1260         ;; Bogus score file.
1261         (error "Invalid syntax with score file %s" file))
1262        ((eq (car alist) 'setq)
1263         ;; This is an old-style score file.
1264         (setq gnus-score-alist (gnus-score-transform-old-to-new alist)))
1265        (t
1266         (setq gnus-score-alist alist)))
1267       ;; Check the syntax of the score file.
1268       (setq gnus-score-alist
1269             (gnus-score-check-syntax gnus-score-alist file)))))
1270
1271 (defun gnus-score-check-syntax (alist file)
1272   "Check the syntax of the score ALIST."
1273   (cond
1274    ((null alist)
1275     nil)
1276    ((not (consp alist))
1277     (gnus-message 1 "Score file is not a list: %s" file)
1278     (ding)
1279     nil)
1280    (t
1281     (let ((a alist)
1282           sr err s type)
1283       (while (and a (not err))
1284         (setq
1285          err
1286          (cond
1287           ((not (listp (car a)))
1288            (format "Invalid score element %s in %s" (car a) file))
1289           ((stringp (caar a))
1290            (cond
1291             ((not (listp (setq sr (cdar a))))
1292              (format "Invalid header match %s in %s" (nth 1 (car a)) file))
1293             (t
1294              (setq type (caar a))
1295              (while (and sr (not err))
1296                (setq s (pop sr))
1297                (setq
1298                 err
1299                 (cond
1300                  ((if (member (downcase type) '("lines" "chars"))
1301                       (not (numberp (car s)))
1302                     (not (stringp (car s))))
1303                   (format "Invalid match %s in %s" (car s) file))
1304                  ((and (cadr s) (not (integerp (cadr s))))
1305                   (format "Non-integer score %s in %s" (cadr s) file))
1306                  ((and (caddr s) (not (integerp (caddr s))))
1307                   (format "Non-integer date %s in %s" (caddr s) file))
1308                  ((and (cadddr s) (not (symbolp (cadddr s))))
1309                   (format "Non-symbol match type %s in %s" (cadddr s) file)))))
1310              err)))))
1311         (setq a (cdr a)))
1312       (if err
1313           (progn
1314             (ding)
1315             (gnus-message 3 err)
1316             (sit-for 2)
1317             nil)
1318         alist)))))
1319
1320 (defun gnus-score-transform-old-to-new (alist)
1321   (let* ((alist (nth 2 alist))
1322          out entry)
1323     (when (eq (car alist) 'quote)
1324       (setq alist (nth 1 alist)))
1325     (while alist
1326       (setq entry (car alist))
1327       (if (stringp (car entry))
1328           (let ((scor (cdr entry)))
1329             (push entry out)
1330             (while scor
1331               (setcar scor
1332                       (list (caar scor) (nth 2 (car scor))
1333                             (and (nth 3 (car scor))
1334                                  (date-to-day (nth 3 (car scor))))
1335                             (if (nth 1 (car scor)) 'r 's)))
1336               (setq scor (cdr scor))))
1337         (push (if (not (listp (cdr entry)))
1338                   (list (car entry) (cdr entry))
1339                 entry)
1340               out))
1341       (setq alist (cdr alist)))
1342     (cons (list 'touched t) (nreverse out))))
1343
1344 (defun gnus-score-save ()
1345   ;; Save all score information.
1346   (let ((cache gnus-score-cache)
1347         entry score file)
1348     (save-excursion
1349       (setq gnus-score-alist nil)
1350       (nnheader-set-temp-buffer " *Gnus Scores*")
1351       (while cache
1352         (current-buffer)
1353         (setq entry (pop cache)
1354               file (nnheader-translate-file-chars (car entry) t)
1355               score (cdr entry))
1356         (if (or (not (equal (gnus-score-get 'touched score) '(t)))
1357                 (gnus-score-get 'read-only score)
1358                 (and (file-exists-p file)
1359                      (not (file-writable-p file))))
1360             ()
1361           (setq score (setcdr entry (gnus-delete-alist 'touched score)))
1362           (erase-buffer)
1363           (let (emacs-lisp-mode-hook)
1364             (if (string-match
1365                  (concat (regexp-quote gnus-adaptive-file-suffix) "$")
1366                  file)
1367                 ;; This is an adaptive score file, so we do not run
1368                 ;; it through `pp'.  These files can get huge, and
1369                 ;; are not meant to be edited by human hands.
1370                 (gnus-prin1 score)
1371               ;; This is a normal score file, so we print it very
1372               ;; prettily.
1373               (let ((lisp-mode-syntax-table score-mode-syntax-table))
1374                 (pp score (current-buffer)))))
1375           (gnus-make-directory (file-name-directory file))
1376           ;; If the score file is empty, we delete it.
1377           (if (zerop (buffer-size))
1378               (delete-file file)
1379             ;; There are scores, so we write the file.
1380             (when (file-writable-p file)
1381               (let ((coding-system-for-write score-mode-coding-system))
1382                 (gnus-write-buffer file))
1383               (when gnus-score-after-write-file-function
1384                 (funcall gnus-score-after-write-file-function file)))))
1385         (and gnus-score-uncacheable-files
1386              (string-match gnus-score-uncacheable-files file)
1387              (gnus-score-remove-from-cache file)))
1388       (kill-buffer (current-buffer)))))
1389
1390 (defun gnus-score-load-files (score-files)
1391   "Load all score files in SCORE-FILES."
1392   ;; Load the score files.
1393   (let (scores)
1394     (while score-files
1395       (if (stringp (car score-files))
1396           ;; It is a string, which means that it's a score file name,
1397           ;; so we load the score file and add the score alist to
1398           ;; the list of alists.
1399           (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
1400         ;; It is an alist, so we just add it to the list directly.
1401         (setq scores (nconc (car score-files) scores)))
1402       (setq score-files (cdr score-files)))