Add 2010 to copyright years.
[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
2058 (setq match (concat "[ (](" extra " \\. \"[^)]*"
23f87bed 2059 match "[^\"]*\")[ )]")
16409b0b
GM
2060 search-func 're-search-forward)) ; XXX danger?!?
2061
eec82323
LMI
2062 (cond
2063 ;; Fuzzy matches. We save these for later.
2064 ((= dmt ?f)
6748645f
LMI
2065 (push (cons entries alist) fuzzies)
2066 (setq entries (cdr entries)))
eec82323
LMI
2067 ;; Word matches. Save these for even later.
2068 ((= dmt ?w)
6748645f
LMI
2069 (push (cons entries alist) words)
2070 (setq entries (cdr entries)))
eec82323
LMI
2071 ;; Exact matches.
2072 ((= dmt ?e)
2073 ;; Do exact matching.
2074 (goto-char (point-min))
2075 (while (and (not (eobp))
2076 (funcall search-func match nil t))
2077 ;; Is it really exact?
2078 (and (eolp)
01c52d31 2079 (= (point-at-bol) (match-beginning 0))
eec82323
LMI
2080 ;; Yup.
2081 (progn
2082 (setq found (setq arts (get-text-property
2083 (point) 'articles)))
2084 ;; Found a match, update scores.
2085 (if trace
2086 (while (setq art (pop arts))
2087 (setcdr art (+ score (cdr art)))
2088 (push
2089 (cons
2090 (car-safe (rassq alist gnus-score-cache))
2091 kill)
2092 gnus-score-trace))
2093 (while (setq art (pop arts))
2094 (setcdr art (+ score (cdr art)))))))
6748645f
LMI
2095 (forward-line 1))
2096 ;; Update expiry date
2097 (if trace
2098 (setq entries (cdr entries))
2099 (cond
2100 ;; Permanent entry.
2101 ((null date)
2102 (setq entries (cdr entries)))
2103 ;; We have a match, so we update the date.
2104 ((and found gnus-update-score-entry-dates)
2105 (gnus-score-set 'touched '(t) alist)
2106 (setcar (nthcdr 2 kill) now)
2107 (setq entries (cdr entries)))
2108 ;; This entry has expired, so we remove it.
2109 ((and expire (< date expire))
2110 (gnus-score-set 'touched '(t) alist)
2111 (setcdr entries (cddr entries)))
2112 ;; No match; go to next entry.
2113 (t
2114 (setq entries (cdr entries))))))
eec82323
LMI
2115 ;; Regexp and substring matching.
2116 (t
2117 (goto-char (point-min))
2118 (when (string= match "")
2119 (setq match "\n"))
2120 (while (and (not (eobp))
2121 (funcall search-func match nil t))
2122 (goto-char (match-beginning 0))
2123 (end-of-line)
2124 (setq found (setq arts (get-text-property (point) 'articles)))
2125 ;; Found a match, update scores.
2126 (if trace
2127 (while (setq art (pop arts))
2128 (setcdr art (+ score (cdr art)))
2129 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
2130 gnus-score-trace))
2131 (while (setq art (pop arts))
2132 (setcdr art (+ score (cdr art)))))
6748645f
LMI
2133 (forward-line 1))
2134 ;; Update expiry date
2135 (if trace
2136 (setq entries (cdr entries))
2137 (cond
2138 ;; Permanent entry.
2139 ((null date)
2140 (setq entries (cdr entries)))
2141 ;; We have a match, so we update the date.
2142 ((and found gnus-update-score-entry-dates)
2143 (gnus-score-set 'touched '(t) alist)
2144 (setcar (nthcdr 2 kill) now)
2145 (setq entries (cdr entries)))
2146 ;; This entry has expired, so we remove it.
2147 ((and expire (< date expire))
2148 (gnus-score-set 'touched '(t) alist)
2149 (setcdr entries (cddr entries)))
2150 ;; No match; go to next entry.
2151 (t
2152 (setq entries (cdr entries))))))))))
eec82323
LMI
2153
2154 ;; Find fuzzy matches.
2155 (when fuzzies
2156 ;; Simplify the entire buffer for easy matching.
2157 (gnus-simplify-buffer-fuzzy)
2158 (while (setq kill (cadaar fuzzies))
2159 (let* ((match (nth 0 kill))
2160 (type (nth 3 kill))
2161 (score (or (nth 1 kill) gnus-score-interactive-default-score))
2162 (date (nth 2 kill))
2163 (mt (aref (symbol-name type) 0))
2164 (case-fold-search (not (= mt ?F)))
2165 found)
2166 (goto-char (point-min))
2167 (while (and (not (eobp))
2168 (search-forward match nil t))
01c52d31 2169 (when (and (= (point-at-bol) (match-beginning 0))
eec82323
LMI
2170 (eolp))
2171 (setq found (setq arts (get-text-property (point) 'articles)))
2172 (if trace
2173 (while (setq art (pop arts))
2174 (setcdr art (+ score (cdr art)))
2175 (push (cons
2176 (car-safe (rassq (cdar fuzzies) gnus-score-cache))
2177 kill)
2178 gnus-score-trace))
2179 ;; Found a match, update scores.
2180 (while (setq art (pop arts))
2181 (setcdr art (+ score (cdr art))))))
2182 (forward-line 1))
2183 ;; Update expiry date
6748645f
LMI
2184 (if (not trace)
2185 (cond
2186 ;; Permanent.
2187 ((null date)
16409b0b 2188 ;; Do nothing.
6748645f
LMI
2189 )
2190 ;; Match, update date.
2191 ((and found gnus-update-score-entry-dates)
2192 (gnus-score-set 'touched '(t) (cdar fuzzies))
2193 (setcar (nthcdr 2 kill) now))
2194 ;; Old entry, remove.
2195 ((and expire (< date expire))
2196 (gnus-score-set 'touched '(t) (cdar fuzzies))
2197 (setcdr (caar fuzzies) (cddaar fuzzies)))))
eec82323
LMI
2198 (setq fuzzies (cdr fuzzies)))))
2199
2200 (when words
2201 ;; Enter all words into the hashtb.
2202 (let ((hashtb (gnus-make-hashtable
2203 (* 10 (count-lines (point-min) (point-max))))))
2204 (gnus-enter-score-words-into-hashtb hashtb)
2205 (while (setq kill (cadaar words))
2206 (let* ((score (or (nth 1 kill) gnus-score-interactive-default-score))
2207 (date (nth 2 kill))
2208 found)
2209 (when (setq arts (intern-soft (nth 0 kill) hashtb))
2210 (setq arts (symbol-value arts))
2211 (setq found t)
2212 (if trace
2213 (while (setq art (pop arts))
2214 (setcdr art (+ score (cdr art)))
2215 (push (cons
2216 (car-safe (rassq (cdar words) gnus-score-cache))
2217 kill)
2218 gnus-score-trace))
2219 ;; Found a match, update scores.
2220 (while (setq art (pop arts))
2221 (setcdr art (+ score (cdr art))))))
2222 ;; Update expiry date
6748645f
LMI
2223 (if (not trace)
2224 (cond
2225 ;; Permanent.
2226 ((null date)
16409b0b 2227 ;; Do nothing.
6748645f
LMI
2228 )
2229 ;; Match, update date.
2230 ((and found gnus-update-score-entry-dates)
2231 (gnus-score-set 'touched '(t) (cdar words))
2232 (setcar (nthcdr 2 kill) now))
2233 ;; Old entry, remove.
2234 ((and expire (< date expire))
2235 (gnus-score-set 'touched '(t) (cdar words))
2236 (setcdr (caar words) (cddaar words)))))
eec82323
LMI
2237 (setq words (cdr words))))))
2238 nil))
2239
2240(defun gnus-enter-score-words-into-hashtb (hashtb)
2241 ;; Find all the words in the buffer and enter them into
2242 ;; the hashtable.
01c52d31 2243 (let (word val)
eec82323 2244 (goto-char (point-min))
01c52d31
MB
2245 (with-syntax-table gnus-adaptive-word-syntax-table
2246 (while (re-search-forward "\\b\\w+\\b" nil t)
2247 (setq val
2248 (gnus-gethash
2249 (setq word (downcase (buffer-substring
2250 (match-beginning 0) (match-end 0))))
2251 hashtb))
2252 (gnus-sethash
2253 word
2254 (append (get-text-property (point-at-eol) 'articles) val)
2255 hashtb)))
eec82323
LMI
2256 ;; Make all the ignorable words ignored.
2257 (let ((ignored (append gnus-ignored-adaptive-words
6748645f
LMI
2258 (if gnus-adaptive-word-no-group-words
2259 (message-tokenize-header
2260 (gnus-group-real-name gnus-newsgroup-name)
2261 "."))
eec82323
LMI
2262 gnus-default-ignored-adaptive-words)))
2263 (while ignored
2264 (gnus-sethash (pop ignored) nil hashtb)))))
2265
2266(defun gnus-score-string< (a1 a2)
2267 ;; Compare headers in articles A2 and A2.
2268 ;; The header index used is the free variable `gnus-score-index'.
2269 (string-lessp (aref (car a1) gnus-score-index)
2270 (aref (car a2) gnus-score-index)))
2271
2272(defun gnus-current-score-file-nondirectory (&optional score-file)
2273 (let ((score-file (or score-file gnus-current-score-file)))
2274 (if score-file
2275 (gnus-short-group-name (file-name-nondirectory score-file))
2276 "none")))
2277
2278(defun gnus-score-adaptive ()
2279 "Create adaptive score rules for this newsgroup."
2280 (when gnus-newsgroup-adaptive
2281 ;; We change the score file to the adaptive score file.
2282 (save-excursion
2283 (set-buffer gnus-summary-buffer)
2284 (gnus-score-load-file
2285 (or gnus-newsgroup-adaptive-score-file
6748645f 2286 (gnus-home-score-file gnus-newsgroup-name t)
eec82323
LMI
2287 (gnus-score-file-name
2288 gnus-newsgroup-name gnus-adaptive-file-suffix))))
2289 ;; Perform ordinary line scoring.
2290 (when (or (not (listp gnus-newsgroup-adaptive))
2291 (memq 'line gnus-newsgroup-adaptive))
2292 (save-excursion
2293 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
2294 (alist malist)
2295 (date (current-time-string))
2296 (data gnus-newsgroup-data)
6748645f 2297 elem headers match func)
eec82323
LMI
2298 ;; First we transform the adaptive rule alist into something
2299 ;; that's faster to process.
2300 (while malist
2301 (setq elem (car malist))
2302 (when (symbolp (car elem))
2303 (setcar elem (symbol-value (car elem))))
2304 (setq elem (cdr elem))
2305 (while elem
6748645f
LMI
2306 (when (fboundp
2307 (setq func
2308 (intern
eec82323
LMI
2309 (concat "mail-header-"
2310 (if (eq (caar elem) 'followup)
2311 "message-id"
6748645f
LMI
2312 (downcase (symbol-name (caar elem))))))))
2313 (setcdr (car elem)
2314 (cons (if (eq (caar elem) 'followup)
2315 "references"
2316 (symbol-name (caar elem)))
2317 (cdar elem)))
2318 (setcar (car elem)
2319 `(lambda (h)
2320 (,func h))))
eec82323
LMI
2321 (setq elem (cdr elem)))
2322 (setq malist (cdr malist)))
2323 ;; Then we score away.
2324 (while data
2325 (setq elem (cdr (assq (gnus-data-mark (car data)) alist)))
2326 (if (or (not elem)
2327 (gnus-data-pseudo-p (car data)))
2328 ()
2329 (when (setq headers (gnus-data-header (car data)))
2330 (while elem
2331 (setq match (funcall (caar elem) headers))
2332 (gnus-summary-score-entry
2333 (nth 1 (car elem)) match
2334 (cond
2335 ((numberp match)
2336 '=)
2337 ((equal (nth 1 (car elem)) "date")
2338 'a)
2339 (t
2340 ;; Whether we use substring or exact matches is
2341 ;; controlled here.
2342 (if (or (not gnus-score-exact-adapt-limit)
2343 (< (length match) gnus-score-exact-adapt-limit))
2344 'e
2345 (if (equal (nth 1 (car elem)) "subject")
2346 'f 's))))
2347 (nth 2 (car elem)) date nil t)
2348 (setq elem (cdr elem)))))
2349 (setq data (cdr data))))))
2350
2351 ;; Perform adaptive word scoring.
2352 (when (and (listp gnus-newsgroup-adaptive)
2353 (memq 'word gnus-newsgroup-adaptive))
16409b0b 2354 (with-temp-buffer
eec82323 2355 (let* ((hashtb (gnus-make-hashtable 1000))
16409b0b 2356 (date (date-to-day (current-time-string)))
eec82323 2357 (data gnus-newsgroup-data)
eec82323 2358 word d score val)
01c52d31
MB
2359 (with-syntax-table gnus-adaptive-word-syntax-table
2360 ;; Go through all articles.
2361 (while (setq d (pop data))
2362 (when (and
2363 (not (gnus-data-pseudo-p d))
2364 (setq score
2365 (cdr (assq
2366 (gnus-data-mark d)
2367 gnus-adaptive-word-score-alist))))
2368 ;; This article has a mark that should lead to
2369 ;; adaptive word rules, so we insert the subject
2370 ;; and find all words in that string.
2371 (insert (mail-header-subject (gnus-data-header d)))
2372 (downcase-region (point-min) (point-max))
2373 (goto-char (point-min))
2374 (while (re-search-forward "\\b\\w+\\b" nil t)
2375 ;; Put the word and score into the hashtb.
2376 (setq val (gnus-gethash (setq word (match-string 0))
2377 hashtb))
2378 (when (or (not gnus-adaptive-word-length-limit)
2379 (> (length word)
2380 gnus-adaptive-word-length-limit))
2381 (setq val (+ score (or val 0)))
2382 (if (and gnus-adaptive-word-minimum
2383 (< val gnus-adaptive-word-minimum))
2384 (setq val gnus-adaptive-word-minimum))
2385 (gnus-sethash word val hashtb)))
2386 (erase-buffer))))
eec82323
LMI
2387 ;; Make all the ignorable words ignored.
2388 (let ((ignored (append gnus-ignored-adaptive-words
6748645f
LMI
2389 (if gnus-adaptive-word-no-group-words
2390 (message-tokenize-header
16409b0b 2391 (gnus-group-real-name
6748645f
LMI
2392 gnus-newsgroup-name)
2393 "."))
eec82323
LMI
2394 gnus-default-ignored-adaptive-words)))
2395 (while ignored
2396 (gnus-sethash (pop ignored) nil hashtb)))
2397 ;; Now we have all the words and scores, so we
2398 ;; add these rules to the ADAPT file.
2399 (set-buffer gnus-summary-buffer)
2400 (mapatoms
2401 (lambda (word)
2402 (when (symbol-value word)
2403 (gnus-summary-score-entry
2404 "subject" (symbol-name word) 'w (symbol-value word)
2405 date nil t)))
2406 hashtb))))))
2407
2408(defun gnus-score-edit-done ()
2409 (let ((bufnam (buffer-file-name (current-buffer)))
2410 (winconf gnus-prev-winconf))
2411 (when winconf
2412 (set-window-configuration winconf))
2413 (gnus-score-remove-from-cache bufnam)
01c52d31
MB
2414 (gnus-score-load-file bufnam)
2415 (run-hooks 'gnus-score-edit-done-hook)))
eec82323
LMI
2416
2417(defun gnus-score-find-trace ()
2418 "Find all score rules that applies to the current article."
2419 (interactive)
2420 (let ((old-scored gnus-newsgroup-scored))
2421 (let ((gnus-newsgroup-headers
2422 (list (gnus-summary-article-header)))
2423 (gnus-newsgroup-scored nil)
23f87bed
MB
2424 ;; Must be synced with `gnus-score-edit-file-at-point':
2425 (frmt "%S [%s] -> %s\n")
2426 trace
2427 file)
eec82323
LMI
2428 (save-excursion
2429 (nnheader-set-temp-buffer "*Score Trace*"))
2430 (setq gnus-score-trace nil)
2431 (gnus-possibly-score-headers 'trace)
2432 (if (not (setq trace gnus-score-trace))
2433 (gnus-error
2434 1 "No score rules apply to the current article (default score %d)."
2435 gnus-summary-default-score)
2436 (set-buffer "*Score Trace*")
23f87bed
MB
2437 ;; Use a keymap instead?
2438 (local-set-key "q"
2439 (lambda ()
2440 (interactive)
2441 (bury-buffer nil)
2442 (gnus-summary-expand-window)))
01c52d31
MB
2443 (local-set-key "k"
2444 (lambda ()
2445 (interactive)
2446 (kill-buffer (current-buffer))
2447 (gnus-summary-expand-window)))
23f87bed
MB
2448 (local-set-key "e" (lambda ()
2449 "Run `gnus-score-edit-file-at-point'."
2450 (interactive)
2451 (gnus-score-edit-file-at-point)))
2452 (local-set-key "f" (lambda ()
2453 "Run `gnus-score-edit-file-at-point'."
2454 (interactive)
2455 (gnus-score-edit-file-at-point 'format)))
2456 (local-set-key "t" 'toggle-truncate-lines)
16409b0b 2457 (setq truncate-lines t)
23f87bed
MB
2458 (dolist (entry trace)
2459 (setq file (or (car entry)
2460 ;; Must be synced with
2461 ;; `gnus-score-edit-file-at-point':
2462 "(non-file rule)"))
2463 (insert
2464 (format frmt
2465 (cdr entry)
2466 ;; Don't use `file-name-sans-extension' to see .SCORE and
2467 ;; .ADAPT directly:
2468 (file-name-nondirectory file)
2469 (abbreviate-file-name file))))
89167438
MB
2470 (insert
2471 (format "\nTotal score: %d"
030cca00
MB
2472 (apply '+ (mapcar
2473 (lambda (s)
2474 (or (caddr s)
2475 gnus-score-interactive-default-score))
2476 trace))))
23f87bed
MB
2477 (insert
2478 "\n\nQuick help:
2479
2480Type `e' to edit score file corresponding to the score rule on current line,
2481`f' to format (pretty print) the score file and edit it,
2482`t' toggle to truncate long lines in this buffer,
01c52d31 2483`q' to quit, `k' to kill score trace buffer.
23f87bed
MB
2484
2485The first sexp on each line is the score rule, followed by the file name of
2486the score file and its full name, including the directory.")
eec82323
LMI
2487 (goto-char (point-min))
2488 (gnus-configure-windows 'score-trace)))
2489 (set-buffer gnus-summary-buffer)
2490 (setq gnus-newsgroup-scored old-scored)))
2491
2492(defun gnus-score-find-favourite-words ()
2493 "List words used in scoring."
2494 (interactive)
2495 (let ((alists (gnus-score-load-files (gnus-all-score-files)))
2496 alist rule rules kill)
2497 ;; Go through all the score alists for this group
2498 ;; and find all `w' rules.
2499 (while (setq alist (pop alists))
2500 (while (setq rule (pop alist))
2501 (when (and (stringp (car rule))
2502 (equal "subject" (downcase (pop rule))))
2503 (while (setq kill (pop rule))
2504 (when (memq (nth 3 kill) '(w W word Word))
2505 (push (cons (or (nth 1 kill)
2506 gnus-score-interactive-default-score)
2507 (car kill))
2508 rules))))))
2509 (setq rules (sort rules (lambda (r1 r2)
2510 (string-lessp (cdr r1) (cdr r2)))))
2511 ;; Add up words that have appeared several times.
2512 (let ((r rules))
2513 (while (cdr r)
2514 (if (equal (cdar r) (cdadr r))
2515 (progn
2516 (setcar (car r) (+ (caar r) (caadr r)))
2517 (setcdr r (cddr r)))
2518 (pop r))))
2519 ;; Insert the words.
2520 (nnheader-set-temp-buffer "*Score Words*")
2521 (if (not (setq rules (sort rules (lambda (r1 r2) (> (car r1) (car r2))))))
2522 (gnus-error 3 "No word score rules")
2523 (while rules
2524 (insert (format "%-5d: %s\n" (caar rules) (cdar rules)))
2525 (pop rules))
eec82323
LMI
2526 (goto-char (point-min))
2527 (gnus-configure-windows 'score-words))))
2528
2529(defun gnus-summary-rescore ()
2530 "Redo the entire scoring process in the current summary."
2531 (interactive)
2532 (gnus-score-save)
2533 (setq gnus-score-cache nil)
2534 (setq gnus-newsgroup-scored nil)
2535 (gnus-possibly-score-headers)
2536 (gnus-score-update-all-lines))
2537
2538(defun gnus-score-flush-cache ()
2539 "Flush the cache of score files."
2540 (interactive)
2541 (gnus-score-save)
2542 (setq gnus-score-cache nil
2543 gnus-score-alist nil
2544 gnus-short-name-score-file-cache nil)
2545 (gnus-message 6 "The score cache is now flushed"))
2546
2547(gnus-add-shutdown 'gnus-score-close 'gnus)
2548
2549(defvar gnus-score-file-alist-cache nil)
2550
2551(defun gnus-score-close ()
2552 "Clear all internal score variables."
2553 (setq gnus-score-cache nil
2554 gnus-internal-global-score-files nil
2555 gnus-score-file-list nil
2556 gnus-score-file-alist-cache nil))
2557
2558;; Summary score marking commands.
2559
2560(defun gnus-summary-raise-same-subject-and-select (score)
2561 "Raise articles which has the same subject with SCORE and select the next."
2562 (interactive "p")
2563 (let ((subject (gnus-summary-article-subject)))
2564 (gnus-summary-raise-score score)
2565 (while (gnus-summary-find-subject subject)
2566 (gnus-summary-raise-score score))
2567 (gnus-summary-next-article t)))
2568
2569(defun gnus-summary-raise-same-subject (score)
2570 "Raise articles which has the same subject with SCORE."
2571 (interactive "p")
2572 (let ((subject (gnus-summary-article-subject)))
2573 (gnus-summary-raise-score score)
2574 (while (gnus-summary-find-subject subject)
2575 (gnus-summary-raise-score score))
2576 (gnus-summary-next-subject 1 t)))
2577
16409b0b 2578(defun gnus-score-delta-default (level)
eec82323
LMI
2579 (if level (prefix-numeric-value level)
2580 gnus-score-interactive-default-score))
2581
2582(defun gnus-summary-raise-thread (&optional score)
2583 "Raise the score of the articles in the current thread with SCORE."
2584 (interactive "P")
16409b0b 2585 (setq score (gnus-score-delta-default score))
eec82323
LMI
2586 (let (e)
2587 (save-excursion
2588 (let ((articles (gnus-summary-articles-in-thread)))
2589 (while articles
2590 (gnus-summary-goto-subject (car articles))
2591 (gnus-summary-raise-score score)
2592 (setq articles (cdr articles))))
2593 (setq e (point)))
2594 (let ((gnus-summary-check-current t))
2595 (unless (zerop (gnus-summary-next-subject 1 t))
2596 (goto-char e))))
2597 (gnus-summary-recenter)
2598 (gnus-summary-position-point)
2599 (gnus-set-mode-line 'summary))
2600
2601(defun gnus-summary-lower-same-subject-and-select (score)
2602 "Raise articles which has the same subject with SCORE and select the next."
2603 (interactive "p")
2604 (gnus-summary-raise-same-subject-and-select (- score)))
2605
2606(defun gnus-summary-lower-same-subject (score)
2607 "Raise articles which has the same subject with SCORE."
2608 (interactive "p")
2609 (gnus-summary-raise-same-subject (- score)))
2610
2611(defun gnus-summary-lower-thread (&optional score)
2612 "Lower score of articles in the current thread with SCORE."
2613 (interactive "P")
23f87bed 2614 (gnus-summary-raise-thread (- (gnus-score-delta-default score))))
eec82323
LMI
2615
2616;;; Finding score files.
2617
2618(defun gnus-score-score-files (group)
2619 "Return a list of all possible score files."
2620 ;; Search and set any global score files.
2621 (when gnus-global-score-files
2622 (unless gnus-internal-global-score-files
2623 (gnus-score-search-global-directories gnus-global-score-files)))
2624 ;; Fix the kill-file dir variable.
2625 (setq gnus-kill-files-directory
2626 (file-name-as-directory gnus-kill-files-directory))
2627 ;; If we can't read it, there are no score files.
2628 (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
2629 (setq gnus-score-file-list nil)
2630 (if (not (gnus-use-long-file-name 'not-score))
2631 ;; We do not use long file names, so we have to do some
2632 ;; directory traversing.
2633 (setq gnus-score-file-list
2634 (cons nil
2635 (or gnus-short-name-score-file-cache
2636 (prog2
2637 (gnus-message 6 "Finding all score files...")
2638 (setq gnus-short-name-score-file-cache
2639 (gnus-score-score-files-1
2640 gnus-kill-files-directory))
2641 (gnus-message 6 "Finding all score files...done")))))
2642 ;; We want long file names.
2643 (when (or (not gnus-score-file-list)
2644 (not (car gnus-score-file-list))
2645 (gnus-file-newer-than gnus-kill-files-directory
2646 (car gnus-score-file-list)))
2647 (setq gnus-score-file-list
2648 (cons (nth 5 (file-attributes gnus-kill-files-directory))
2649 (nreverse
2650 (directory-files
2651 gnus-kill-files-directory t
2652 (gnus-score-file-regexp)))))))
2653 (cdr gnus-score-file-list)))
2654
2655(defun gnus-score-score-files-1 (dir)
2656 "Return all possible score files under DIR."
2657 (let ((files (list (expand-file-name dir)))
2658 (regexp (gnus-score-file-regexp))
2659 (case-fold-search nil)
2660 seen out file)
2661 (while (setq file (pop files))
2662 (cond
16409b0b
GM
2663 ;; Ignore files that start with a dot.
2664 ((string-match "^\\." (file-name-nondirectory file))
eec82323
LMI
2665 nil)
2666 ;; Add subtrees of directory to also be searched.
2667 ((and (file-directory-p file)
2668 (not (member (file-truename file) seen)))
2669 (push (file-truename file) seen)
2670 (setq files (nconc (directory-files file t nil t) files)))
2671 ;; Add files to the list of score files.
2672 ((string-match regexp file)
2673 (push file out))))
2674 (or out
2675 ;; Return a dummy value.
23f87bed
MB
2676 (list (expand-file-name "this.file.does.not.exist.SCORE"
2677 gnus-kill-files-directory)))))
eec82323
LMI
2678
2679(defun gnus-score-file-regexp ()
2680 "Return a regexp that match all score files."
2681 (concat "\\(" (regexp-quote gnus-score-file-suffix )
2682 "\\|" (regexp-quote gnus-adaptive-file-suffix) "\\)\\'"))
2683
2684(defun gnus-score-find-bnews (group)
2685 "Return a list of score files for GROUP.
2686The score files are those files in the ~/News/ directory which matches
2687GROUP using BNews sys file syntax."
2688 (let* ((sfiles (append (gnus-score-score-files group)
2689 gnus-internal-global-score-files))
2690 (kill-dir (file-name-as-directory
2691 (expand-file-name gnus-kill-files-directory)))
2692 (klen (length kill-dir))
2693 (score-regexp (gnus-score-file-regexp))
2694 (trans (cdr (assq ?: nnheader-file-name-translation-alist)))
16409b0b 2695 (group-trans (nnheader-translate-file-chars group t))
eec82323
LMI
2696 ofiles not-match regexp)
2697 (save-excursion
6748645f 2698 (set-buffer (gnus-get-buffer-create "*gnus score files*"))
16409b0b 2699 (buffer-disable-undo)
eec82323
LMI
2700 ;; Go through all score file names and create regexp with them
2701 ;; as the source.
2702 (while sfiles
2703 (erase-buffer)
2704 (insert (car sfiles))
2705 (goto-char (point-min))
2706 ;; First remove the suffix itself.
2707 (when (re-search-forward (concat "." score-regexp) nil t)
2708 (replace-match "" t t)
2709 (goto-char (point-min))
2710 (if (looking-at (regexp-quote kill-dir))
2711 ;; If the file name was just "SCORE", `klen' is one character
2712 ;; too much.
2713 (delete-char (min (1- (point-max)) klen))
2714 (goto-char (point-max))
47b63dfa
SZ
2715 (if (re-search-backward gnus-directory-sep-char-regexp nil t)
2716 (delete-region (1+ (point)) (point-min))
2717 (gnus-message 1 "Can't find directory separator in %s"
2718 (car sfiles))))
eec82323
LMI
2719 ;; If short file names were used, we have to translate slashes.
2720 (goto-char (point-min))
2721 (let ((regexp (concat
59896c4c 2722 "[/:" (if trans (char-to-string trans)) "]")))
eec82323
LMI
2723 (while (re-search-forward regexp nil t)
2724 (replace-match "." t t)))
2725 ;; Kludge to get rid of "nntp+" problems.
2726 (goto-char (point-min))
2727 (when (looking-at "nn[a-z]+\\+")
2728 (search-forward "+")
2729 (forward-char -1)
2730 (insert "\\")
2731 (forward-char 1))
2732 ;; Kludge to deal with "++".
2733 (while (search-forward "+" nil t)
2734 (replace-match "\\+" t t))
2735 ;; Translate "all" to ".*".
2736 (goto-char (point-min))
2737 (while (search-forward "all" nil t)
2738 (replace-match ".*" t t))
2739 (goto-char (point-min))
2740 ;; Deal with "not."s.
23f87bed
MB
2741 (if (looking-at "not.")
2742 (progn
2743 (setq not-match t)
2744 (setq regexp
2745 (concat "^" (buffer-substring 5 (point-max)) "$")))
2746 (setq regexp (concat "^" (buffer-substring 1 (point-max)) "$"))
2747 (setq not-match nil))
eec82323
LMI
2748 ;; Finally - if this resulting regexp matches the group name,
2749 ;; we add this score file to the list of score files
2750 ;; applicable to this group.
2751 (when (or (and not-match
47b63dfa 2752 (ignore-errors
16409b0b 2753 (not (string-match regexp group-trans))))
47b63dfa
SZ
2754 (and (not not-match)
2755 (ignore-errors (string-match regexp group-trans))))
eec82323
LMI
2756 (push (car sfiles) ofiles)))
2757 (setq sfiles (cdr sfiles)))
23f87bed 2758 (gnus-kill-buffer (current-buffer))
eec82323
LMI
2759 ;; Slight kludge here - the last score file returned should be
2760 ;; the local score file, whether it exists or not. This is so
2761 ;; that any score commands the user enters will go to the right
2762 ;; file, and not end up in some global score file.
2763 (let ((localscore (gnus-score-file-name group)))
2764 (setq ofiles (cons localscore (delete localscore ofiles))))
2765 (gnus-sort-score-files (nreverse ofiles)))))
2766
2767(defun gnus-score-find-single (group)
2768 "Return list containing the score file for GROUP."
2769 (list (or gnus-newsgroup-adaptive-score-file
2770 (gnus-score-file-name group gnus-adaptive-file-suffix))
2771 (gnus-score-file-name group)))
2772
2773(defun gnus-score-find-hierarchical (group)
2774 "Return list of score files for GROUP.
2775This includes the score file for the group and all its parents."
2776 (let* ((prefix (gnus-group-real-prefix group))
2777 (all (list nil))
2778 (group (gnus-group-real-name group))
2779 (start 0))
2780 (while (string-match "\\." group (1+ start))
2781 (setq start (match-beginning 0))
2782 (push (substring group 0 start) all))
2783 (push group all)
2784 (setq all
2785 (nconc
2786 (mapcar (lambda (group)
2787 (gnus-score-file-name group gnus-adaptive-file-suffix))
2788 (setq all (nreverse all)))
2789 (mapcar 'gnus-score-file-name all)))
2790 (if (equal prefix "")
2791 all
2792 (mapcar
2793 (lambda (file)
2794 (nnheader-translate-file-chars
2795 (concat (file-name-directory file) prefix
2796 (file-name-nondirectory file))))
2797 all))))
2798
2799(defun gnus-score-file-rank (file)
2800 "Return a number that says how specific score FILE is.
2801Destroys the current buffer."
2802 (if (member file gnus-internal-global-score-files)
2803 0
2804 (when (string-match
2805 (concat "^" (regexp-quote
2806 (expand-file-name
2807 (file-name-as-directory gnus-kill-files-directory))))
2808 file)
2809 (setq file (substring file (match-end 0))))
2810 (insert file)
2811 (goto-char (point-min))
2812 (let ((beg (point))
2813 elems)
2814 (while (re-search-forward "[./]" nil t)
2815 (push (buffer-substring beg (1- (point)))
2816 elems))
2817 (erase-buffer)
2818 (setq elems (delete "all" elems))
2819 (length elems))))
2820
2821(defun gnus-sort-score-files (files)
2822 "Sort FILES so that the most general files come first."
16409b0b 2823 (with-temp-buffer
eec82323
LMI
2824 (let ((alist
2825 (mapcar
2826 (lambda (file)
2827 (cons (inline (gnus-score-file-rank file)) file))
2828 files)))
01c52d31 2829 (mapcar 'cdr (sort alist 'car-less-than-car)))))
eec82323
LMI
2830
2831(defun gnus-score-find-alist (group)
2832 "Return list of score files for GROUP.
39bb8e56 2833The list is determined from the variable `gnus-score-file-alist'."
eec82323
LMI
2834 (let ((alist gnus-score-file-multiple-match-alist)
2835 score-files)
2836 ;; if this group has been seen before, return the cached entry
2837 (if (setq score-files (assoc group gnus-score-file-alist-cache))
2838 (cdr score-files) ;ensures caching groups with no matches
2839 ;; handle the multiple match alist
2840 (while alist
2841 (when (string-match (caar alist) group)
2842 (setq score-files
2843 (nconc score-files (copy-sequence (cdar alist)))))
2844 (setq alist (cdr alist)))
2845 (setq alist gnus-score-file-single-match-alist)
2846 ;; handle the single match alist
2847 (while alist
2848 (when (string-match (caar alist) group)
2849 ;; progn used just in case ("regexp") has no files
2850 ;; and score-files is still nil. -sj
2851 ;; this can be construed as a "stop searching here" feature :>
2852 ;; and used to simplify regexps in the single-alist
2853 (setq score-files
2854 (nconc score-files (copy-sequence (cdar alist))))
2855 (setq alist nil))
2856 (setq alist (cdr alist)))
2857 ;; cache the score files
2858 (push (cons group score-files) gnus-score-file-alist-cache)
2859 score-files)))
2860
2861(defun gnus-all-score-files (&optional group)
2862 "Return a list of all score files for the current group."
2863 (let ((funcs gnus-score-find-score-files-function)
2864 (group (or group gnus-newsgroup-name))
2865 score-files)
6748645f
LMI
2866 (when group
2867 ;; Make sure funcs is a list.
2868 (and funcs
2869 (not (listp funcs))
2870 (setq funcs (list funcs)))
59896c4c
DL
2871 (when gnus-score-use-all-scores
2872 ;; Get the initial score files for this group.
2873 (when funcs
2874 (setq score-files (nreverse (gnus-score-find-alist group))))
2875 ;; Add any home adapt files.
2876 (let ((home (gnus-home-score-file group t)))
2877 (when home
2878 (push home score-files)
2879 (setq gnus-newsgroup-adaptive-score-file home)))
2880 ;; Check whether there is a `adapt-file' group parameter.
2881 (let ((param-file (gnus-group-find-parameter group 'adapt-file)))
2882 (when param-file
2883 (push param-file score-files)
2884 (setq gnus-newsgroup-adaptive-score-file param-file))))
6748645f
LMI
2885 ;; Go through all the functions for finding score files (or actual
2886 ;; scores) and add them to a list.
2887 (while funcs
23f87bed 2888 (when (functionp (car funcs))
6748645f 2889 (setq score-files
23f87bed
MB
2890 (append score-files
2891 (nreverse (funcall (car funcs) group)))))
6748645f 2892 (setq funcs (cdr funcs)))
59896c4c
DL
2893 (when gnus-score-use-all-scores
2894 ;; Add any home score files.
2895 (let ((home (gnus-home-score-file group)))
2896 (when home
2897 (push home score-files)))
2898 ;; Check whether there is a `score-file' group parameter.
2899 (let ((param-file (gnus-group-find-parameter group 'score-file)))
2900 (when param-file
2901 (push param-file score-files))))
6748645f
LMI
2902 ;; Expand all files names.
2903 (let ((files score-files))
2904 (while files
2905 (when (stringp (car files))
2906 (setcar files (expand-file-name
2907 (car files) gnus-kill-files-directory)))
2908 (pop files)))
2909 (setq score-files (nreverse score-files))
2910 ;; Remove any duplicate score files.
2911 (while (and score-files
2912 (member (car score-files) (cdr score-files)))
2913 (pop score-files))
2914 (let ((files score-files))
2915 (while (cdr files)
2916 (if (member (cadr files) (cddr files))
2917 (setcdr files (cddr files))
2918 (pop files))))
2919 ;; Do the scoring if there are any score files for this group.
2920 score-files)))
eec82323
LMI
2921
2922(defun gnus-possibly-score-headers (&optional trace)
2923 "Do scoring if scoring is required."
2924 (let ((score-files (gnus-all-score-files)))
2925 (when score-files
2926 (gnus-score-headers score-files trace))))
2927
2928(defun gnus-score-file-name (newsgroup &optional suffix)
2929 "Return the name of a score file for NEWSGROUP."
2930 (let ((suffix (or suffix gnus-score-file-suffix)))
2931 (nnheader-translate-file-chars
2932 (cond
2933 ((or (null newsgroup)
2934 (string-equal newsgroup ""))
2935 ;; The global score file is placed at top of the directory.
6748645f 2936 (expand-file-name suffix gnus-kill-files-directory))
eec82323
LMI
2937 ((gnus-use-long-file-name 'not-score)
2938 ;; Append ".SCORE" to newsgroup name.
2939 (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
2940 "." suffix)
2941 gnus-kill-files-directory))
2942 (t
2943 ;; Place "SCORE" under the hierarchical directory.
2944 (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
2945 "/" suffix)
2946 gnus-kill-files-directory))))))
2947
2948(defun gnus-score-search-global-directories (files)
2949 "Scan all global score directories for score files."
2950 ;; Set the variable `gnus-internal-global-score-files' to all
2951 ;; available global score files.
2952 (interactive (list gnus-global-score-files))
2953 (let (out)
2954 (while files
6748645f 2955 ;; #### /$ Unix-specific?
23f87bed 2956 (if (file-directory-p (car files))
eec82323
LMI
2957 (setq out (nconc (directory-files
2958 (car files) t
2959 (concat (gnus-score-file-regexp) "$"))))
2960 (push (car files) out))
2961 (setq files (cdr files)))
2962 (setq gnus-internal-global-score-files out)))
2963
2964(defun gnus-score-default-fold-toggle ()
2965 "Toggle folding for new score file entries."
2966 (interactive)
2967 (setq gnus-score-default-fold (not gnus-score-default-fold))
2968 (if gnus-score-default-fold
2969 (gnus-message 1 "New score file entries will be case insensitive.")
2970 (gnus-message 1 "New score file entries will be case sensitive.")))
2971
2972;;; Home score file.
2973
2974(defun gnus-home-score-file (group &optional adapt)
2975 "Return the home score file for GROUP.
2976If ADAPT, return the home adaptive file instead."
2977 (let ((list (if adapt gnus-home-adapt-file gnus-home-score-file))
2978 elem found)
2979 ;; Make sure we have a list.
2980 (unless (listp list)
2981 (setq list (list list)))
2982 ;; Go through the list and look for matches.
2983 (while (and (not found)
2984 (setq elem (pop list)))
2985 (setq found
2986 (cond
2987 ;; Simple string.
2988 ((stringp elem)
2989 elem)
2990 ;; Function.
23f87bed 2991 ((functionp elem)
eec82323 2992 (funcall elem group))
16409b0b 2993 ;; Regexp-file cons.
eec82323 2994 ((consp elem)
6748645f 2995 (when (string-match (gnus-globalify-regexp (car elem)) group)
16409b0b 2996 (replace-match (cadr elem) t nil group))))))
eec82323 2997 (when found
23f87bed 2998 (setq found (nnheader-translate-file-chars found))
16409b0b 2999 (if (file-name-absolute-p found)
23f87bed
MB
3000 found
3001 (nnheader-concat gnus-kill-files-directory found)))))
eec82323
LMI
3002
3003(defun gnus-hierarchial-home-score-file (group)
3004 "Return the score file of the top-level hierarchy of GROUP."
3005 (if (string-match "^[^.]+\\." group)
3006 (concat (match-string 0 group) gnus-score-file-suffix)
3007 ;; Group name without any dots.
3008 (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
3009 gnus-score-file-suffix)))
3010
3011(defun gnus-hierarchial-home-adapt-file (group)
3012 "Return the adapt file of the top-level hierarchy of GROUP."
3013 (if (string-match "^[^.]+\\." group)
3014 (concat (match-string 0 group) gnus-adaptive-file-suffix)
3015 ;; Group name without any dots.
3016 (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
3017 gnus-adaptive-file-suffix)))
3018
6748645f
LMI
3019(defun gnus-current-home-score-file (group)
3020 "Return the \"current\" regular score file."
3021 (car (nreverse (gnus-score-find-alist group))))
3022
eec82323
LMI
3023;;;
3024;;; Score decays
3025;;;
3026
3027(defun gnus-decay-score (score)
a8151ef7 3028 "Decay SCORE according to `gnus-score-decay-constant' and `gnus-score-decay-scale'."
23f87bed
MB
3029 (let ((n (- score
3030 (* (if (< score 0) -1 1)
3031 (min (abs score)
3032 (max gnus-score-decay-constant
3033 (* (abs score)
3034 gnus-score-decay-scale)))))))
3035 (if (and (featurep 'xemacs)
3036 ;; XEmacs' floor can handle only the floating point
3037 ;; number below the half of the maximum integer.
3038 (> (abs n) (lsh -1 -2)))
3039 (string-to-number
3040 (car (split-string (number-to-string n) "\\.")))
3041 (floor n))))
eec82323
LMI
3042
3043(defun gnus-decay-scores (alist day)
3044 "Decay non-permanent scores in ALIST."
16409b0b 3045 (let ((times (- (time-to-days (current-time)) day))
eec82323
LMI
3046 kill entry updated score n)
3047 (unless (zerop times) ;Done decays today already?
3048 (while (setq entry (pop alist))
3049 (when (stringp (car entry))
3050 (setq entry (cdr entry))
3051 (while (setq kill (pop entry))
3052 (when (nth 2 kill)
3053 (setq updated t)
a8151ef7
LMI
3054 (setq score (or (nth 1 kill)
3055 gnus-score-interactive-default-score)
eec82323
LMI
3056 n times)
3057 (while (natnump (decf n))
3058 (setq score (funcall gnus-decay-score-function score)))
16409b0b 3059 (setcdr kill (cons score
a8151ef7 3060 (cdr (cdr kill)))))))))
eec82323
LMI
3061 ;; Return whether this score file needs to be saved. By Je-haysuss!
3062 updated))
3063
6748645f
LMI
3064(defun gnus-score-regexp-bad-p (regexp)
3065 "Test whether REGEXP is safe for Gnus scoring.
3066A regexp is unsafe if it matches newline or a buffer boundary.
3067
3068If the regexp is good, return nil. If the regexp is bad, return a
3069cons cell (SYM . STRING), where the symbol SYM is `new' or `bad'.
3070In the `new' case, the string is a safe replacement for REGEXP.
3071In the `bad' case, the string is a unsafe subexpression of REGEXP,
3072and we do not have a simple replacement to suggest.
3073
23f87bed 3074See Info node `(gnus)Scoring Tips' for examples of good regular expressions."
6748645f
LMI
3075 (let (case-fold-search)
3076 (and
3077 ;; First, try a relatively fast necessary condition.
3078 ;; Notice ranges (like [^:] or [\t-\r]), \s>, \Sw, \W, \', \`:
3079 (string-match "\n\\|\\\\[SsW`']\\|\\[\\^\\|[\0-\n]-" regexp)
3080 ;; Now break the regexp into tokens, and check each:
3081 (let ((tail regexp) ; remaining regexp to check
3082 tok ; current token
3083 bad ; nil, or bad subexpression
3084 new ; nil, or replacement regexp so far
3085 end) ; length of current token
3086 (while (and (not bad)
3087 (string-match
3088 "\\`\\(\\\\[sS]?.\\|\\[\\^?]?[^]]*]\\|[^\\]\\)"
3089 tail))
3090 (setq end (match-end 0)
3091 tok (substring tail 0 end)
3092 tail (substring tail end))
3093 (if;; Is token `bad' (matching newline or buffer ends)?
3094 (or (member tok '("\n" "\\W" "\\`" "\\'"))
3095 ;; This next handles "[...]", "\\s.", and "\\S.":
3096 (and (> end 2) (string-match tok "\n")))
3097 (let ((newtok
3098 ;; Try to suggest a replacement for tok ...
3099 (cond ((string-equal tok "\\`") "^") ; or "\\(^\\)"
3100 ((string-equal tok "\\'") "$") ; or "\\($\\)"
3101 ((string-match "\\[\\^" tok) ; very common
3102 (concat (substring tok 0 -1) "\n]")))))
3103 (if newtok
3104 (setq new
3105 (concat
3106 (or new
3107 ;; good prefix so far:
3108 (substring regexp 0 (- (+ (length tail) end))))
3109 newtok))
3110 ;; No replacement idea, so give up:
3111 (setq bad tok)))
3112 ;; tok is good, may need to extend new
3113 (and new (setq new (concat new tok)))))
3114 ;; Now return a value:
3115 (cond
3116 (bad (cons 'bad bad))
3117 (new (cons 'new new))
16409b0b 3118 (t nil))))))
6748645f 3119
eec82323
LMI
3120(provide 'gnus-score)
3121
cbee283d 3122;; arch-tag: d3922589-764d-46ae-9954-9330fd192634
eec82323 3123;;; gnus-score.el ends here