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