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