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