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