Regenerate ldefs-boot.el
[bpt/emacs.git] / lisp / gnus / gnus-score.el
CommitLineData
527fcc2f 1;;; gnus-score.el --- scoring code for Gnus
e84b4b86 2
ba318903 3;; Copyright (C) 1995-2014 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
3eb9b5fd
G
1074(defun gnus-summary-current-score (arg)
1075 "Return the score of the current article.
1076 With prefix ARG, return the total score of the current (sub)thread."
1077 (interactive "P")
1078 (gnus-message 1 "%s" (if arg
1079 (gnus-thread-total-score
1080 (gnus-id-to-thread
1081 (mail-header-id (gnus-summary-article-header))))
1082 (gnus-summary-article-score))))
eec82323
LMI
1083
1084(defun gnus-score-change-score-file (file)
1085 "Change current score alist."
1086 (interactive
1087 (list (read-file-name "Change to score file: " gnus-kill-files-directory)))
1088 (gnus-score-load-file file)
1089 (gnus-set-mode-line 'summary))
1090
1091(defvar gnus-score-edit-exit-function)
1092(defun gnus-score-edit-current-scores (file)
1093 "Edit the current score alist."
1094 (interactive (list gnus-current-score-file))
6748645f
LMI
1095 (if (not gnus-current-score-file)
1096 (error "No current score file")
1097 (let ((winconf (current-window-configuration)))
1098 (when (buffer-name gnus-summary-buffer)
1099 (gnus-score-save))
1100 (gnus-make-directory (file-name-directory file))
1101 (setq gnus-score-edit-buffer (find-file-noselect file))
1102 (gnus-configure-windows 'edit-score)
1103 (gnus-score-mode)
1104 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1105 (make-local-variable 'gnus-prev-winconf)
1106 (setq gnus-prev-winconf winconf))
1107 (gnus-message
bdaa75c7
LMI
1108 4 "%s" (substitute-command-keys
1109 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits"))))
eec82323 1110
01c52d31
MB
1111(defun gnus-score-edit-all-score ()
1112 "Edit the all.SCORE file."
1113 (interactive)
1114 (find-file (gnus-score-file-name "all"))
1115 (gnus-score-mode)
1116 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1117 (gnus-message
1118 4 (substitute-command-keys
1119 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
1120
eec82323
LMI
1121(defun gnus-score-edit-file (file)
1122 "Edit a score file."
1123 (interactive
1124 (list (read-file-name "Edit score file: " gnus-kill-files-directory)))
1125 (gnus-make-directory (file-name-directory file))
1126 (when (buffer-name gnus-summary-buffer)
1127 (gnus-score-save))
1128 (let ((winconf (current-window-configuration)))
1129 (setq gnus-score-edit-buffer (find-file-noselect file))
1130 (gnus-configure-windows 'edit-score)
1131 (gnus-score-mode)
1132 (setq gnus-score-edit-exit-function 'gnus-score-edit-done)
1133 (make-local-variable 'gnus-prev-winconf)
1134 (setq gnus-prev-winconf winconf))
1135 (gnus-message
bdaa75c7
LMI
1136 4 "%s" (substitute-command-keys
1137 "\\<gnus-score-mode-map>\\[gnus-score-edit-exit] to save edits")))
eec82323 1138
23f87bed
MB
1139(defun gnus-score-edit-file-at-point (&optional format)
1140 "Edit score file at point in Score Trace buffers.
1141If FORMAT, also format the current score file."
1142 (let* ((rule (save-excursion
1143 (beginning-of-line)
1144 (read (current-buffer))))
1145 (sep "[ \n\r\t]*")
1146 ;; Must be synced with `gnus-score-find-trace':
1147 (reg " -> +")
1148 (file (save-excursion
1149 (end-of-line)
01c52d31
MB
1150 (if (and (re-search-backward reg (point-at-bol) t)
1151 (re-search-forward reg (point-at-eol) t))
1152 (buffer-substring (point) (point-at-eol))
23f87bed
MB
1153 nil))))
1154 (if (or (not file)
1155 (string-match "\\<\\(non-file rule\\|A file\\)\\>" file)
1156 ;; (see `gnus-score-find-trace' and `gnus-score-advanced')
1157 (string= "" file))
1158 (gnus-error 3 "Can't find a score file in current line.")
1159 (gnus-score-edit-file file)
1160 (when format
1161 (gnus-score-pretty-print))
1162 (when (consp rule) ;; the rule exists
1163 (setq rule (mapconcat #'(lambda (obj)
1164 (regexp-quote (format "%S" obj)))
1165 rule
1166 sep))
1167 (goto-char (point-min))
1168 (re-search-forward rule nil t)
1169 ;; make it easy to use `kill-sexp':
1170 (goto-char (1- (match-beginning 0)))))))
1171
eec82323
LMI
1172(defun gnus-score-load-file (file)
1173 ;; Load score file FILE. Returns a list a retrieved score-alists.
1174 (let* ((file (expand-file-name
1175 (or (and (string-match
6748645f
LMI
1176 (concat "^" (regexp-quote
1177 (expand-file-name
1178 gnus-kill-files-directory)))
eec82323
LMI
1179 (expand-file-name file))
1180 file)
59896c4c 1181 (expand-file-name file gnus-kill-files-directory))))
eec82323
LMI
1182 (cached (assoc file gnus-score-cache))
1183 (global (member file gnus-internal-global-score-files))
1184 lists alist)
1185 (if cached
1186 ;; The score file was already loaded.
1187 (setq alist (cdr cached))
1188 ;; We load the score file.
1189 (setq gnus-score-alist nil)
1190 (setq alist (gnus-score-load-score-alist file))
1191 ;; We add '(touched) to the alist to signify that it hasn't been
1192 ;; touched (yet).
1193 (unless (assq 'touched alist)
1194 (push (list 'touched nil) alist))
1195 ;; If it is a global score file, we make it read-only.
1196 (and global
1197 (not (assq 'read-only alist))
1198 (push (list 'read-only t) alist))
1199 (push (cons file alist) gnus-score-cache))
1200 (let ((a alist)
1201 found)
1202 (while a
1203 ;; Downcase all header names.
6748645f
LMI
1204 (cond
1205 ((stringp (caar a))
eec82323
LMI
1206 (setcar (car a) (downcase (caar a)))
1207 (setq found t))
6748645f
LMI
1208 ;; Advanced scoring.
1209 ((consp (caar a))
1210 (setq found t)))
eec82323
LMI
1211 (pop a))
1212 ;; If there are actual scores in the alist, we add it to the
1213 ;; return value of this function.
1214 (when found
1215 (setq lists (list alist))))
1216 ;; Treat the other possible atoms in the score alist.
1217 (let ((mark (car (gnus-score-get 'mark alist)))
1218 (expunge (car (gnus-score-get 'expunge alist)))
1219 (mark-and-expunge (car (gnus-score-get 'mark-and-expunge alist)))
1220 (files (gnus-score-get 'files alist))
1221 (exclude-files (gnus-score-get 'exclude-files alist))
23f87bed 1222 (orphan (car (gnus-score-get 'orphan alist)))
eec82323
LMI
1223 (adapt (gnus-score-get 'adapt alist))
1224 (thread-mark-and-expunge
1225 (car (gnus-score-get 'thread-mark-and-expunge alist)))
1226 (adapt-file (car (gnus-score-get 'adapt-file alist)))
1227 (local (gnus-score-get 'local alist))
1228 (decay (car (gnus-score-get 'decay alist)))
1229 (eval (car (gnus-score-get 'eval alist))))
1230 ;; Perform possible decays.
01c52d31
MB
1231 (when (and (if (stringp gnus-decay-scores)
1232 (string-match gnus-decay-scores file)
1233 gnus-decay-scores)
6748645f
LMI
1234 (or cached (file-exists-p file))
1235 (or (not decay)
1236 (gnus-decay-scores alist decay)))
1237 (gnus-score-set 'touched '(t) alist)
16409b0b 1238 (gnus-score-set 'decay (list (time-to-days (current-time))) alist))
eec82323
LMI
1239 ;; We do not respect eval and files atoms from global score
1240 ;; files.
6748645f
LMI
1241 (when (and files (not global))
1242 (setq lists (apply 'append lists
01c52d31 1243 (mapcar 'gnus-score-load-file
6748645f
LMI
1244 (if adapt-file (cons adapt-file files)
1245 files)))))
1246 (when (and eval (not global))
1247 (eval eval))
eec82323
LMI
1248 ;; We then expand any exclude-file directives.
1249 (setq gnus-scores-exclude-files
1250 (nconc
6748645f
LMI
1251 (apply
1252 'nconc
1253 (mapcar
1254 (lambda (sfile)
1255 (list
1256 (expand-file-name sfile (file-name-directory file))
1257 (expand-file-name sfile gnus-kill-files-directory)))
1258 exclude-files))
eec82323 1259 gnus-scores-exclude-files))
6748645f 1260 (when local
20a673b2 1261 (with-current-buffer gnus-summary-buffer
eec82323
LMI
1262 (while local
1263 (and (consp (car local))
1264 (symbolp (caar local))
1265 (progn
1266 (make-local-variable (caar local))
1267 (set (caar local) (nth 1 (car local)))))
1268 (setq local (cdr local)))))
1269 (when orphan
1270 (setq gnus-orphan-score orphan))
1271 (setq gnus-adaptive-score-alist
1272 (cond ((equal adapt '(t))
1273 (setq gnus-newsgroup-adaptive t)
1274 gnus-default-adaptive-score-alist)
1275 ((equal adapt '(ignore))
1276 (setq gnus-newsgroup-adaptive nil))
1277 ((consp adapt)
1278 (setq gnus-newsgroup-adaptive t)
1279 adapt)
1280 (t
eec82323
LMI
1281 gnus-default-adaptive-score-alist)))
1282 (setq gnus-thread-expunge-below
1283 (or thread-mark-and-expunge gnus-thread-expunge-below))
1284 (setq gnus-summary-mark-below
1285 (or mark mark-and-expunge gnus-summary-mark-below))
1286 (setq gnus-summary-expunge-below
1287 (or expunge mark-and-expunge gnus-summary-expunge-below))
1288 (setq gnus-newsgroup-adaptive-score-file
1289 (or adapt-file gnus-newsgroup-adaptive-score-file)))
1290 (setq gnus-current-score-file file)
1291 (setq gnus-score-alist alist)
1292 lists))
1293
1294(defun gnus-score-load (file)
1295 ;; Load score FILE.
1296 (let ((cache (assoc file gnus-score-cache)))
1297 (if cache
1298 (setq gnus-score-alist (cdr cache))
1299 (setq gnus-score-alist nil)
1300 (gnus-score-load-score-alist file)
1301 (unless gnus-score-alist
1302 (setq gnus-score-alist (copy-alist '((touched nil)))))
1303 (push (cons file gnus-score-alist) gnus-score-cache))))
1304
1305(defun gnus-score-remove-from-cache (file)
1306 (setq gnus-score-cache
1307 (delq (assoc file gnus-score-cache) gnus-score-cache)))
1308
1309(defun gnus-score-load-score-alist (file)
1310 "Read score FILE."
1311 (let (alist)
1312 (if (not (file-readable-p file))
1313 ;; Couldn't read file.
1314 (setq gnus-score-alist nil)
1315 ;; Read file.
16409b0b
GM
1316 (with-temp-buffer
1317 (let ((coding-system-for-read score-mode-coding-system))
1318 (insert-file-contents file))
eec82323
LMI
1319 (goto-char (point-min))
1320 ;; Only do the loading if the score file isn't empty.
1321 (when (save-excursion (re-search-forward "[()0-9a-zA-Z]" nil t))
1322 (setq alist
1323 (condition-case ()
1324 (read (current-buffer))
1325 (error
1326 (gnus-error 3.2 "Problem with score file %s" file))))))
6748645f
LMI
1327 (cond
1328 ((and alist
1329 (atom alist))
1330 ;; Bogus score file.
1331 (error "Invalid syntax with score file %s" file))
1332 ((eq (car alist) 'setq)
1333 ;; This is an old-style score file.
1334 (setq gnus-score-alist (gnus-score-transform-old-to-new alist)))
1335 (t
1336 (setq gnus-score-alist alist)))
eec82323
LMI
1337 ;; Check the syntax of the score file.
1338 (setq gnus-score-alist
1339 (gnus-score-check-syntax gnus-score-alist file)))))
1340
1341(defun gnus-score-check-syntax (alist file)
1342 "Check the syntax of the score ALIST."
1343 (cond
1344 ((null alist)
1345 nil)
1346 ((not (consp alist))
1347 (gnus-message 1 "Score file is not a list: %s" file)
1348 (ding)
1349 nil)
1350 (t
1351 (let ((a alist)
1352 sr err s type)
1353 (while (and a (not err))
1354 (setq
1355 err
1356 (cond
1357 ((not (listp (car a)))
16409b0b 1358 (format "Invalid score element %s in %s" (car a) file))
eec82323
LMI
1359 ((stringp (caar a))
1360 (cond
1361 ((not (listp (setq sr (cdar a))))
16409b0b 1362 (format "Invalid header match %s in %s" (nth 1 (car a)) file))
eec82323
LMI
1363 (t
1364 (setq type (caar a))
1365 (while (and sr (not err))
1366 (setq s (pop sr))
1367 (setq
1368 err
1369 (cond
1370 ((if (member (downcase type) '("lines" "chars"))
1371 (not (numberp (car s)))
1372 (not (stringp (car s))))
16409b0b 1373 (format "Invalid match %s in %s" (car s) file))
eec82323
LMI
1374 ((and (cadr s) (not (integerp (cadr s))))
1375 (format "Non-integer score %s in %s" (cadr s) file))
1376 ((and (caddr s) (not (integerp (caddr s))))
1377 (format "Non-integer date %s in %s" (caddr s) file))
1378 ((and (cadddr s) (not (symbolp (cadddr s))))
1379 (format "Non-symbol match type %s in %s" (cadddr s) file)))))
1380 err)))))
1381 (setq a (cdr a)))
1382 (if err
1383 (progn
1384 (ding)
bdaa75c7 1385 (gnus-message 3 "%s" err)
eec82323
LMI
1386 (sit-for 2)
1387 nil)
1388 alist)))))
1389
1390(defun gnus-score-transform-old-to-new (alist)
1391 (let* ((alist (nth 2 alist))
1392 out entry)
1393 (when (eq (car alist) 'quote)
1394 (setq alist (nth 1 alist)))
1395 (while alist
1396 (setq entry (car alist))
1397 (if (stringp (car entry))
1398 (let ((scor (cdr entry)))
1399 (push entry out)
1400 (while scor
1401 (setcar scor
1402 (list (caar scor) (nth 2 (car scor))
1403 (and (nth 3 (car scor))
16409b0b 1404 (date-to-day (nth 3 (car scor))))
eec82323
LMI
1405 (if (nth 1 (car scor)) 'r 's)))
1406 (setq scor (cdr scor))))
1407 (push (if (not (listp (cdr entry)))
1408 (list (car entry) (cdr entry))
1409 entry)
1410 out))
1411 (setq alist (cdr alist)))
1412 (cons (list 'touched t) (nreverse out))))
1413
1414(defun gnus-score-save ()
1415 ;; Save all score information.
1416 (let ((cache gnus-score-cache)
1417 entry score file)
1418 (save-excursion
1419 (setq gnus-score-alist nil)
1420 (nnheader-set-temp-buffer " *Gnus Scores*")
1421 (while cache
1422 (current-buffer)
1423 (setq entry (pop cache)
16409b0b 1424 file (nnheader-translate-file-chars (car entry) t)
eec82323
LMI
1425 score (cdr entry))
1426 (if (or (not (equal (gnus-score-get 'touched score) '(t)))
1427 (gnus-score-get 'read-only score)
1428 (and (file-exists-p file)
1429 (not (file-writable-p file))))
1430 ()
6748645f 1431 (setq score (setcdr entry (gnus-delete-alist 'touched score)))
eec82323
LMI
1432 (erase-buffer)
1433 (let (emacs-lisp-mode-hook)
01c52d31
MB
1434 (if (and (not gnus-adaptive-pretty-print)
1435 (string-match
1436 (concat (regexp-quote gnus-adaptive-file-suffix) "$")
1437 file))
1438 ;; This is an adaptive score file, so we do not run it through
1439 ;; `pp' unless requested. These files can get huge, and are
1440 ;; not meant to be edited by human hands.
eec82323
LMI
1441 (gnus-prin1 score)
1442 ;; This is a normal score file, so we print it very
1443 ;; prettily.
6748645f 1444 (let ((lisp-mode-syntax-table score-mode-syntax-table))
23f87bed 1445 (gnus-pp score))))
eec82323
LMI
1446 (gnus-make-directory (file-name-directory file))
1447 ;; If the score file is empty, we delete it.
1448 (if (zerop (buffer-size))
1449 (delete-file file)
1450 ;; There are scores, so we write the file.
1451 (when (file-writable-p file)
16409b0b
GM
1452 (let ((coding-system-for-write score-mode-coding-system))
1453 (gnus-write-buffer file))
eec82323
LMI
1454 (when gnus-score-after-write-file-function
1455 (funcall gnus-score-after-write-file-function file)))))
1456 (and gnus-score-uncacheable-files
1457 (string-match gnus-score-uncacheable-files file)
1458 (gnus-score-remove-from-cache file)))
1459 (kill-buffer (current-buffer)))))
1460
1461(defun gnus-score-load-files (score-files)
1462 "Load all score files in SCORE-FILES."
1463 ;; Load the score files.
1464 (let (scores)
1465 (while score-files
1466 (if (stringp (car score-files))
1467 ;; It is a string, which means that it's a score file name,
1468 ;; so we load the score file and add the score alist to
1469 ;; the list of alists.
1470 (setq scores (nconc (gnus-score-load-file (car score-files)) scores))
1471 ;; It is an alist, so we just add it to the list directly.
1472 (setq scores (nconc (car score-files) scores)))
1473 (setq score-files (cdr score-files)))
1474 ;; Prune the score files that are to be excluded, if any.
1475 (when gnus-scores-exclude-files
1476 (let ((s scores)
1477 c)
1478 (while s
1479 (and (setq c (rassq (car s) gnus-score-cache))
1480 (member (car c) gnus-scores-exclude-files)
1481 (setq scores (delq (car s) scores)))
1482 (setq s (cdr s)))))
1483 scores))
1484
1485(defun gnus-score-headers (score-files &optional trace)
1486 ;; Score `gnus-newsgroup-headers'.
1487 (let (scores news)
1488 ;; PLM: probably this is not the best place to clear orphan-score
1489 (setq gnus-orphan-score nil
1490 gnus-scores-articles nil
1491 gnus-scores-exclude-files nil
1492 scores (gnus-score-load-files score-files))
1493 (setq news scores)
1494 ;; Do the scoring.
1495 (while news
1496 (setq scores news
1497 news nil)
1498 (when (and gnus-summary-default-score
1499 scores)
1500 (let* ((entries gnus-header-index)
16409b0b 1501 (now (date-to-day (current-time-string)))
eec82323
LMI
1502 (expire (and gnus-score-expiry-days
1503 (- now gnus-score-expiry-days)))
1504 (headers gnus-newsgroup-headers)
1505 (current-score-file gnus-current-score-file)
1506 entry header new)
23f87bed 1507 (gnus-message 7 "Scoring...")
eec82323
LMI
1508 ;; Create articles, an alist of the form `(HEADER . SCORE)'.
1509 (while (setq header (pop headers))
1510 ;; WARNING: The assq makes the function O(N*S) while it could
1511 ;; be written as O(N+S), where N is (length gnus-newsgroup-headers)
1512 ;; and S is (length gnus-newsgroup-scored).
1513 (unless (assq (mail-header-number header) gnus-newsgroup-scored)
1514 (setq gnus-scores-articles ;Total of 2 * N cons-cells used.
1515 (cons (cons header (or gnus-summary-default-score 0))
1516 gnus-scores-articles))))
1517
20a673b2 1518 (with-current-buffer (gnus-get-buffer-create "*Headers*")
16409b0b 1519 (buffer-disable-undo)
6748645f
LMI
1520 (when (gnus-buffer-live-p gnus-summary-buffer)
1521 (message-clone-locals gnus-summary-buffer))
eec82323
LMI
1522
1523 ;; Set the global variant of this variable.
1524 (setq gnus-current-score-file current-score-file)
1525 ;; score orphans
1526 (when gnus-orphan-score
1527 (setq gnus-score-index
1528 (nth 1 (assoc "references" gnus-header-index)))
1529 (gnus-score-orphans gnus-orphan-score))
1530 ;; Run each header through the score process.
1531 (while entries
1532 (setq entry (pop entries)
1533 header (nth 0 entry)
1534 gnus-score-index (nth 1 (assoc header gnus-header-index)))
1535 (when (< 0 (apply 'max (mapcar
1536 (lambda (score)
1537 (length (gnus-score-get header score)))
1538 scores)))
01c52d31 1539 (when (if (and gnus-inhibit-slow-scoring
9b3ebcb6
MB
1540 (or (eq gnus-inhibit-slow-scoring t)
1541 (and (stringp gnus-inhibit-slow-scoring)
01c52d31
MB
1542 ;; Always true here?
1543 ;; (stringp gnus-newsgroup-name)
9b3ebcb6
MB
1544 (string-match
1545 gnus-inhibit-slow-scoring
1546 gnus-newsgroup-name)))
01c52d31
MB
1547 (> 0 (nth 1 (assoc header gnus-header-index))))
1548 (progn
1549 (gnus-message
1550 7 "Scoring on headers or body skipped.")
1551 nil)
9b3ebcb6 1552 ;; Call the scoring function for this type of "header".
01c52d31
MB
1553 (setq new (funcall (nth 2 entry) scores header
1554 now expire trace)))
eec82323 1555 (push new news))))
16409b0b
GM
1556 (when (gnus-buffer-live-p gnus-summary-buffer)
1557 (let ((scored gnus-newsgroup-scored))
1558 (with-current-buffer gnus-summary-buffer
1559 (setq gnus-newsgroup-scored scored))))
eec82323 1560 ;; Remove the buffer.
23f87bed 1561 (gnus-kill-buffer (current-buffer)))
eec82323
LMI
1562
1563 ;; Add articles to `gnus-newsgroup-scored'.
1564 (while gnus-scores-articles
1565 (when (or (/= gnus-summary-default-score
1566 (cdar gnus-scores-articles))
1567 gnus-save-score)
1568 (push (cons (mail-header-number (caar gnus-scores-articles))
1569 (cdar gnus-scores-articles))
1570 gnus-newsgroup-scored))
1571 (setq gnus-scores-articles (cdr gnus-scores-articles)))
1572
1573 (let (score)
1574 (while (setq score (pop scores))
1575 (while score
16409b0b 1576 (when (consp (caar score))
eec82323
LMI
1577 (gnus-score-advanced (car score) trace))
1578 (pop score))))
1579
23f87bed 1580 (gnus-message 7 "Scoring...done"))))))
eec82323 1581
16409b0b 1582(defun gnus-score-lower-thread (thread score-adjust)
676a7cc9 1583 "Lower the score on THREAD with SCORE-ADJUST.
16409b0b
GM
1584THREAD is expected to contain a list of the form `(PARENT [CHILD1
1585CHILD2 ...])' where PARENT is a header array and each CHILD is a list
03c1a35e 1586of the same form as THREAD. The empty list nil is valid. For each
16409b0b 1587article in the tree, the score of the corresponding entry in
39bb8e56 1588`gnus-newsgroup-scored' is adjusted by SCORE-ADJUST."
16409b0b
GM
1589 (while thread
1590 (let ((head (car thread)))
1591 (if (listp head)
1592 ;; handle a child and its descendants
1593 (gnus-score-lower-thread head score-adjust)
1594 ;; handle the parent
1595 (let* ((article (mail-header-number head))
1596 (score (assq article gnus-newsgroup-scored)))
1597 (if score (setcdr score (+ (cdr score) score-adjust))
1598 (push (cons article score-adjust) gnus-newsgroup-scored)))))
1599 (setq thread (cdr thread))))
eec82323 1600
eec82323 1601(defun gnus-score-orphans (score)
16409b0b
GM
1602 "Score orphans.
1603A root is an article with no references. An orphan is an article
1604which has references, but is not connected via its references to a
1605root article. This function finds all the orphans, and adjusts their
39bb8e56 1606score in `gnus-newsgroup-scored' by SCORE."
23f87bed
MB
1607 ;; gnus-make-threads produces a list, where each entry is a "thread"
1608 ;; as described in the gnus-score-lower-thread docs. This function
1609 ;; will be called again (after limiting has been done) if the display
1610 ;; is threaded. It would be nice to somehow save this info and use
1611 ;; it later.
1612 (dolist (thread (gnus-make-threads))
1613 (let ((id (aref (car thread) gnus-score-index)))
1614 ;; If the parent of the thread is not a root, lower the score of
1615 ;; it and its descendants. Note that some roots seem to satisfy
1616 ;; (eq id nil) and some (eq id ""); not sure why.
1617 (when (and id
1618 (not (string= id "")))
1619 (gnus-score-lower-thread thread score)))))
eec82323
LMI
1620
1621(defun gnus-score-integer (scores header now expire &optional trace)
1622 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1623 entries alist)
eec82323
LMI
1624 ;; Find matches.
1625 (while scores
1626 (setq alist (car scores)
1627 scores (cdr scores)
1628 entries (assoc header alist))
1629 (while (cdr entries) ;First entry is the header index.
1630 (let* ((rest (cdr entries))
1631 (kill (car rest))
1632 (match (nth 0 kill))
1633 (type (or (nth 3 kill) '>))
1634 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1635 (date (nth 2 kill))
1636 (found nil)
1637 (match-func (if (or (eq type '>) (eq type '<) (eq type '<=)
1638 (eq type '>=) (eq type '=))
1639 type
16409b0b 1640 (error "Invalid match type: %s" type)))
eec82323
LMI
1641 (articles gnus-scores-articles))
1642 ;; Instead of doing all the clever stuff that
1643 ;; `gnus-score-string' does to minimize searches and stuff,
1644 ;; I will assume that people generally will put so few
1645 ;; matches on numbers that any cleverness will take more
1646 ;; time than one would gain.
1647 (while articles
1648 (when (funcall match-func
1649 (or (aref (caar articles) gnus-score-index) 0)
1650 match)
1651 (when trace
1652 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1653 gnus-score-trace))
1654 (setq found t)
1655 (setcdr (car articles) (+ score (cdar articles))))
1656 (setq articles (cdr articles)))
1657 ;; Update expire date
1658 (cond ((null date)) ;Permanent entry.
1659 ((and found gnus-update-score-entry-dates) ;Match, update date.
1660 (gnus-score-set 'touched '(t) alist)
1661 (setcar (nthcdr 2 kill) now))
1662 ((and expire (< date expire)) ;Old entry, remove.
1663 (gnus-score-set 'touched '(t) alist)
1664 (setcdr entries (cdr rest))
1665 (setq rest entries)))
1666 (setq entries rest)))))
1667 nil)
1668
1669(defun gnus-score-date (scores header now expire &optional trace)
1670 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1671 entries alist match match-func article)
eec82323
LMI
1672 ;; Find matches.
1673 (while scores
1674 (setq alist (car scores)
1675 scores (cdr scores)
1676 entries (assoc header alist))
1677 (while (cdr entries) ;First entry is the header index.
1678 (let* ((rest (cdr entries))
1679 (kill (car rest))
1680 (type (or (nth 3 kill) 'before))
1681 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1682 (date (nth 2 kill))
1683 (found nil)
1684 (articles gnus-scores-articles)
1685 l)
1686 (cond
1687 ((eq type 'after)
1688 (setq match-func 'string<
1689 match (gnus-date-iso8601 (nth 0 kill))))
1690 ((eq type 'before)
1691 (setq match-func 'gnus-string>
1692 match (gnus-date-iso8601 (nth 0 kill))))
1693 ((eq type 'at)
1694 (setq match-func 'string=
1695 match (gnus-date-iso8601 (nth 0 kill))))
1696 ((eq type 'regexp)
1697 (setq match-func 'string-match
1698 match (nth 0 kill)))
16409b0b 1699 (t (error "Invalid match type: %s" type)))
eec82323
LMI
1700 ;; Instead of doing all the clever stuff that
1701 ;; `gnus-score-string' does to minimize searches and stuff,
1702 ;; I will assume that people generally will put so few
1703 ;; matches on numbers that any cleverness will take more
1704 ;; time than one would gain.
1705 (while (setq article (pop articles))
1706 (when (and
1707 (setq l (aref (car article) gnus-score-index))
1708 (funcall match-func match (gnus-date-iso8601 l)))
1709 (when trace
1710 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
1711 gnus-score-trace))
1712 (setq found t)
1713 (setcdr article (+ score (cdr article)))))
1714 ;; Update expire date
1715 (cond ((null date)) ;Permanent entry.
1716 ((and found gnus-update-score-entry-dates) ;Match, update date.
1717 (gnus-score-set 'touched '(t) alist)
1718 (setcar (nthcdr 2 kill) now))
1719 ((and expire (< date expire)) ;Old entry, remove.
1720 (gnus-score-set 'touched '(t) alist)
1721 (setcdr entries (cdr rest))
1722 (setq rest entries)))
1723 (setq entries rest)))))
1724 nil)
1725
350a1888 1726(defun gnus-score-decode-text-parts ()
46a2cc44
KY
1727 (gmm-labels
1728 ((mm-text-parts
1729 (handle)
1730 (cond ((stringp (car handle))
da946239
KY
1731 (let ((parts (apply #'append
1732 (mapcar #'mm-text-parts (cdr handle)))))
46a2cc44
KY
1733 (if (equal "multipart/alternative" (car handle))
1734 ;; pick the first supported alternative
1735 (list (car parts))
1736 parts)))
1737
1738 ((bufferp (car handle))
1739 (when (string-match "^text/" (mm-handle-media-type handle))
1740 (list handle)))
1741
da946239 1742 (t (apply #'append (mapcar #'mm-text-parts handle)))))
46a2cc44
KY
1743 (my-mm-display-part
1744 (handle)
1745 (when handle
1746 (save-restriction
1747 (narrow-to-region (point) (point))
1748 (mm-display-inline handle)
1749 (goto-char (point-max))))))
350a1888
G
1750
1751 (let (;(mm-text-html-renderer 'w3m-standalone)
46a2cc44 1752 (handles (mm-dissect-buffer t)))
350a1888 1753 (save-excursion
46a2cc44
KY
1754 (article-goto-body)
1755 (delete-region (point) (point-max))
1756 (mapc #'my-mm-display-part (mm-text-parts handles))
1757 handles))))
350a1888 1758
eec82323 1759(defun gnus-score-body (scores header now expire &optional trace)
350a1888
G
1760 (if gnus-agent-fetching
1761 nil
1762 (save-excursion
1763 (setq gnus-scores-articles
1764 (sort gnus-scores-articles
1765 (lambda (a1 a2)
1766 (< (mail-header-number (car a1))
1767 (mail-header-number (car a2))))))
1768 (set-buffer nntp-server-buffer)
1769 (save-restriction
1770 (let* ((buffer-read-only nil)
1771 (articles gnus-scores-articles)
1772 (all-scores scores)
1773 (request-func (cond ((string= "head" header)
1774 'gnus-request-head)
350a1888 1775 ((string= "body" header)
a2cfe8a3 1776 'gnus-request-body)
350a1888
G
1777 (t 'gnus-request-article)))
1778 entries alist ofunc article last)
1779 (when articles
1780 (setq last (mail-header-number (caar (last articles))))
1781 ;; Not all backends support partial fetching. In that case,
1782 ;; we just fetch the entire article.
a2cfe8a3
JT
1783 ;; When scoring by body, we need to peek at the headers to detect
1784 ;; the content encoding
1785 (unless (or (gnus-check-backend-function
1786 (and (string-match "^gnus-" (symbol-name request-func))
1787 (intern (substring (symbol-name request-func)
1788 (match-end 0))))
1789 gnus-newsgroup-name)
1790 (string= "body" header))
350a1888
G
1791 (setq ofunc request-func)
1792 (setq request-func 'gnus-request-article))
1793 (while articles
1794 (setq article (mail-header-number (caar articles)))
1795 (gnus-message 7 "Scoring article %s of %s..." article last)
1796 (widen)
1797 (let (handles)
1798 (when (funcall request-func article gnus-newsgroup-name)
1799 (when (string= "body" header)
1800 (setq handles (gnus-score-decode-text-parts)))
1801 (goto-char (point-min))
1802 ;; If just parts of the article is to be searched, but the
1803 ;; backend didn't support partial fetching, we just narrow
1804 ;; to the relevant parts.
1805 (when ofunc
1806 (if (eq ofunc 'gnus-request-head)
1807 (narrow-to-region
1808 (point)
1809 (or (search-forward "\n\n" nil t) (point-max)))
1810 (narrow-to-region
1811 (or (search-forward "\n\n" nil t) (point))
1812 (point-max))))
1813 (setq scores all-scores)
1814 ;; Find matches.
1815 (while scores
1816 (setq alist (pop scores)
1817 entries (assoc header alist))
1818 (while (cdr entries) ;First entry is the header index.
1819 (let* ((rest (cdr entries))
1820 (kill (car rest))
1821 (match (nth 0 kill))
1822 (type (or (nth 3 kill) 's))
1823 (score (or (nth 1 kill)
1824 gnus-score-interactive-default-score))
1825 (date (nth 2 kill))
1826 (found nil)
1827 (case-fold-search
1828 (not (or (eq type 'R) (eq type 'S)
1829 (eq type 'Regexp) (eq type 'String))))
1830 (search-func
1831 (cond ((or (eq type 'r) (eq type 'R)
1832 (eq type 'regexp) (eq type 'Regexp))
1833 're-search-forward)
1834 ((or (eq type 's) (eq type 'S)
1835 (eq type 'string) (eq type 'String))
1836 'search-forward)
1837 (t
1838 (error "Invalid match type: %s" type)))))
1839 (goto-char (point-min))
1840 (when (funcall search-func match nil t)
1841 ;; Found a match, update scores.
1842 (setcdr (car articles) (+ score (cdar articles)))
1843 (setq found t)
1844 (when trace
1845 (push
1846 (cons (car-safe (rassq alist gnus-score-cache))
1847 kill)
1848 gnus-score-trace)))
1849 ;; Update expire date
1850 (unless trace
1851 (cond
1852 ((null date)) ;Permanent entry.
1853 ((and found gnus-update-score-entry-dates)
1854 ;; Match, update date.
1855 (gnus-score-set 'touched '(t) alist)
1856 (setcar (nthcdr 2 kill) now))
1857 ((and expire (< date expire)) ;Old entry, remove.
1858 (gnus-score-set 'touched '(t) alist)
1859 (setcdr entries (cdr rest))
1860 (setq rest entries))))
1861 (setq entries rest))))
1862 (when handles (mm-destroy-parts handles))))
1863 (setq articles (cdr articles)))))))
1864 nil))
eec82323
LMI
1865
1866(defun gnus-score-thread (scores header now expire &optional trace)
1867 (gnus-score-followup scores header now expire trace t))
1868
1869(defun gnus-score-followup (scores header now expire &optional trace thread)
59896c4c
DL
1870 (if gnus-agent-fetching
1871 ;; FIXME: It seems doable in fetching mode.
1872 nil
1873 ;; Insert the unique article headers in the buffer.
1874 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
1875 (current-score-file gnus-current-score-file)
1876 (all-scores scores)
1877 ;; gnus-score-index is used as a free variable.
1878 alike last this art entries alist articles
1879 new news)
676a7cc9 1880
59896c4c
DL
1881 ;; Change score file to the adaptive score file. All entries that
1882 ;; this function makes will be put into this file.
20a673b2 1883 (with-current-buffer gnus-summary-buffer
59896c4c
DL
1884 (gnus-score-load-file
1885 (or gnus-newsgroup-adaptive-score-file
1886 (gnus-score-file-name
1887 gnus-newsgroup-name gnus-adaptive-file-suffix))))
eec82323 1888
676a7cc9 1889 (setq gnus-scores-articles (sort gnus-scores-articles
59896c4c
DL
1890 'gnus-score-string<)
1891 articles gnus-scores-articles)
eec82323 1892
59896c4c
DL
1893 (erase-buffer)
1894 (while articles
1895 (setq art (car articles)
1896 this (aref (car art) gnus-score-index)
1897 articles (cdr articles))
1898 (if (equal last this)
1899 (push art alike)
1900 (when last
1901 (insert last ?\n)
1902 (put-text-property (1- (point)) (point) 'articles alike))
1903 (setq alike (list art)
1904 last this)))
23f87bed 1905 (when last ; Bwadr, duplicate code.
59896c4c
DL
1906 (insert last ?\n)
1907 (put-text-property (1- (point)) (point) 'articles alike))
1908
1909 ;; Find matches.
1910 (while scores
1911 (setq alist (car scores)
1912 scores (cdr scores)
1913 entries (assoc header alist))
23f87bed 1914 (while (cdr entries) ;First entry is the header index.
59896c4c
DL
1915 (let* ((rest (cdr entries))
1916 (kill (car rest))
1917 (match (nth 0 kill))
1918 (type (or (nth 3 kill) 's))
1919 (score (or (nth 1 kill) gnus-score-interactive-default-score))
1920 (date (nth 2 kill))
1921 (found nil)
1922 (mt (aref (symbol-name type) 0))
1923 (case-fold-search
1924 (not (or (= mt ?R) (= mt ?S) (= mt ?E) (= mt ?F))))
1925 (dmt (downcase mt))
1926 (search-func
1927 (cond ((= dmt ?r) 're-search-forward)
1928 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
1929 (t (error "Invalid match type: %s" type))))
1930 arts art)
1931 (goto-char (point-min))
1932 (if (= dmt ?e)
1933 (while (funcall search-func match nil t)
01c52d31 1934 (and (= (point-at-bol)
59896c4c
DL
1935 (match-beginning 0))
1936 (= (progn (end-of-line) (point))
1937 (match-end 0))
1938 (progn
1939 (setq found (setq arts (get-text-property
1940 (point) 'articles)))
1941 ;; Found a match, update scores.
1942 (while arts
1943 (setq art (car arts)
1944 arts (cdr arts))
1945 (gnus-score-add-followups
1946 (car art) score all-scores thread))))
1947 (end-of-line))
eec82323 1948 (while (funcall search-func match nil t)
59896c4c
DL
1949 (end-of-line)
1950 (setq found (setq arts (get-text-property (point) 'articles)))
1951 ;; Found a match, update scores.
1952 (while (setq art (pop arts))
23f87bed
MB
1953 (setcdr art (+ score (cdr art)))
1954 (when trace
1955 (push (cons
1956 (car-safe (rassq alist gnus-score-cache))
1957 kill)
1958 gnus-score-trace))
59896c4c
DL
1959 (when (setq new (gnus-score-add-followups
1960 (car art) score all-scores thread))
1961 (push new news)))))
1962 ;; Update expire date
1963 (cond ((null date)) ;Permanent entry.
676a7cc9 1964 ((and found gnus-update-score-entry-dates)
59896c4c
DL
1965 ;Match, update date.
1966 (gnus-score-set 'touched '(t) alist)
1967 (setcar (nthcdr 2 kill) now))
1968 ((and expire (< date expire)) ;Old entry, remove.
1969 (gnus-score-set 'touched '(t) alist)
1970 (setcdr entries (cdr rest))
1971 (setq rest entries)))
1972 (setq entries rest))))
1973 ;; We change the score file back to the previous one.
20a673b2 1974 (with-current-buffer gnus-summary-buffer
59896c4c
DL
1975 (gnus-score-load-file current-score-file))
1976 (list (cons "references" news)))))
eec82323
LMI
1977
1978(defun gnus-score-add-followups (header score scores &optional thread)
1979 "Add a score entry to the adapt file."
20a673b2 1980 (with-current-buffer gnus-summary-buffer
eec82323
LMI
1981 (let* ((id (mail-header-id header))
1982 (scores (car scores))
1983 entry dont)
1984 ;; Don't enter a score if there already is one.
1985 (while (setq entry (pop scores))
1986 (and (equal "references" (car entry))
1987 (or (null (nth 3 (cadr entry)))
1988 (eq 's (nth 3 (cadr entry))))
1989 (assoc id entry)
1990 (setq dont t)))
1991 (unless dont
1992 (gnus-summary-score-entry
1993 (if thread "thread" "references")
1994 id 's score (current-time-string) nil t)))))
1995
1996(defun gnus-score-string (score-list header now expire &optional trace)
1997 ;; Score ARTICLES according to HEADER in SCORE-LIST.
1998 ;; Update matching entries to NOW and remove unmatched entries older
1999 ;; than EXPIRE.
2000
2001 ;; Insert the unique article headers in the buffer.
2002 (let ((gnus-score-index (nth 1 (assoc header gnus-header-index)))
2003 ;; gnus-score-index is used as a free variable.
23f87bed
MB
2004 (simplify (and gnus-score-thread-simplify
2005 (string= "subject" header)))
eec82323
LMI
2006 alike last this art entries alist articles
2007 fuzzies arts words kill)
2008
2009 ;; Sorting the articles costs os O(N*log N) but will allow us to
2010 ;; only match with each unique header. Thus the actual matching
2011 ;; will be O(M*U) where M is the number of strings to match with,
2012 ;; and U is the number of unique headers. It is assumed (but
2013 ;; untested) this will be a net win because of the large constant
2014 ;; factor involved with string matching.
16409b0b
GM
2015 (setq gnus-scores-articles
2016 ;; We cannot string-sort the extra headers list. *sigh*
2017 (if (= gnus-score-index 9)
2018 gnus-scores-articles
2019 (sort gnus-scores-articles 'gnus-score-string<))
eec82323
LMI
2020 articles gnus-scores-articles)
2021
2022 (erase-buffer)
2023 (while (setq art (pop articles))
2024 (setq this (aref (car art) gnus-score-index))
16409b0b
GM
2025
2026 ;; If we're working with non-standard headers, we are stuck
2027 ;; with working on them as a group. What a hassle.
2028 ;; Just wait 'til you see what horrors we commit against `match'...
2029 (if (= gnus-score-index 9)
23f87bed 2030 (setq this (gnus-prin1-to-string this))) ; ick.
16409b0b 2031
6748645f
LMI
2032 (if simplify
2033 (setq this (gnus-map-function gnus-simplify-subject-functions this)))
eec82323
LMI
2034 (if (equal last this)
2035 ;; O(N*H) cons-cells used here, where H is the number of
2036 ;; headers.
2037 (push art alike)
2038 (when last
2039 ;; Insert the line, with a text property on the
2040 ;; terminating newline referring to the articles with
2041 ;; this line.
2042 (insert last ?\n)
2043 (put-text-property (1- (point)) (point) 'articles alike))
2044 (setq alike (list art)
2045 last this)))
2046 (when last ; Bwadr, duplicate code.
2047 (insert last ?\n)
2048 (put-text-property (1- (point)) (point) 'articles alike))
2049
2050 ;; Go through all the score alists and pick out the entries
2051 ;; for this header.
2052 (while score-list
2053 (setq alist (pop score-list)
2054 ;; There's only one instance of this header for
2055 ;; each score alist.
2056 entries (assoc header alist))
2057 (while (cdr entries) ;First entry is the header index.
2058 (let* ((kill (cadr entries))
eec82323
LMI
2059 (type (or (nth 3 kill) 's))
2060 (score (or (nth 1 kill) gnus-score-interactive-default-score))
2061 (date (nth 2 kill))
16409b0b 2062 (extra (nth 4 kill)) ; non-standard header; string.
eec82323
LMI
2063 (found nil)
2064 (mt (aref (symbol-name type) 0))
2065 (case-fold-search (not (memq mt '(?R ?S ?E ?F))))
2066 (dmt (downcase mt))
16409b0b 2067 ;; Assume user already simplified regexp and fuzzies
6748645f 2068 (match (if (and simplify (not (memq dmt '(?f ?r))))
23f87bed
MB
2069 (gnus-map-function
2070 gnus-simplify-subject-functions
2071 (nth 0 kill))
2072 (nth 0 kill)))
eec82323
LMI
2073 (search-func
2074 (cond ((= dmt ?r) 're-search-forward)
2075 ((or (= dmt ?e) (= dmt ?s) (= dmt ?f)) 'search-forward)
2076 ((= dmt ?w) nil)
16409b0b
GM
2077 (t (error "Invalid match type: %s" type)))))
2078
2079 ;; Evil hackery to make match usable in non-standard headers.
2080 (when extra
5ec44091
KY
2081 (setq match (concat "[ (](" extra " \\. \"\\([^\"]*\\\\\"\\)*[^\"]*"
2082 (if (eq search-func 're-search-forward)
2083 match
2084 (regexp-quote match))
2085 "\\([^\"]*\\\\\"\\)*[^\"]*\")[ )]")
16409b0b
GM
2086 search-func 're-search-forward)) ; XXX danger?!?
2087
eec82323
LMI
2088 (cond
2089 ;; Fuzzy matches. We save these for later.
2090 ((= dmt ?f)
6748645f
LMI
2091 (push (cons entries alist) fuzzies)
2092 (setq entries (cdr entries)))
eec82323
LMI
2093 ;; Word matches. Save these for even later.
2094 ((= dmt ?w)
6748645f
LMI
2095 (push (cons entries alist) words)
2096 (setq entries (cdr entries)))
eec82323
LMI
2097 ;; Exact matches.
2098 ((= dmt ?e)
2099 ;; Do exact matching.
2100 (goto-char (point-min))
2101 (while (and (not (eobp))
2102 (funcall search-func match nil t))
2103 ;; Is it really exact?
2104 (and (eolp)
01c52d31 2105 (= (point-at-bol) (match-beginning 0))
eec82323
LMI
2106 ;; Yup.
2107 (progn
2108 (setq found (setq arts (get-text-property
2109 (point) 'articles)))
2110 ;; Found a match, update scores.
2111 (if trace
2112 (while (setq art (pop arts))
2113 (setcdr art (+ score (cdr art)))
2114 (push
2115 (cons
2116 (car-safe (rassq alist gnus-score-cache))
2117 kill)
2118 gnus-score-trace))
2119 (while (setq art (pop arts))
2120 (setcdr art (+ score (cdr art)))))))
6748645f
LMI
2121 (forward-line 1))
2122 ;; Update expiry date
2123 (if trace
2124 (setq entries (cdr entries))
2125 (cond
2126 ;; Permanent entry.
2127 ((null date)
2128 (setq entries (cdr entries)))
2129 ;; We have a match, so we update the date.
2130 ((and found gnus-update-score-entry-dates)
2131 (gnus-score-set 'touched '(t) alist)
2132 (setcar (nthcdr 2 kill) now)
2133 (setq entries (cdr entries)))
2134 ;; This entry has expired, so we remove it.
2135 ((and expire (< date expire))
2136 (gnus-score-set 'touched '(t) alist)
2137 (setcdr entries (cddr entries)))
2138 ;; No match; go to next entry.
2139 (t
2140 (setq entries (cdr entries))))))
eec82323
LMI
2141 ;; Regexp and substring matching.
2142 (t
2143 (goto-char (point-min))
2144 (when (string= match "")
2145 (setq match "\n"))
2146 (while (and (not (eobp))
2147 (funcall search-func match nil t))
2148 (goto-char (match-beginning 0))
2149 (end-of-line)
2150 (setq found (setq arts (get-text-property (point) 'articles)))
2151 ;; Found a match, update scores.
2152 (if trace
2153 (while (setq art (pop arts))
2154 (setcdr art (+ score (cdr art)))
2155 (push (cons (car-safe (rassq alist gnus-score-cache)) kill)
2156 gnus-score-trace))
2157 (while (setq art (pop arts))
2158 (setcdr art (+ score (cdr art)))))
6748645f
LMI
2159 (forward-line 1))
2160 ;; Update expiry date
2161 (if trace
2162 (setq entries (cdr entries))
2163 (cond
2164 ;; Permanent entry.
2165 ((null date)
2166 (setq entries (cdr entries)))
2167 ;; We have a match, so we update the date.
2168 ((and found gnus-update-score-entry-dates)
2169 (gnus-score-set 'touched '(t) alist)
2170 (setcar (nthcdr 2 kill) now)
2171 (setq entries (cdr entries)))
2172 ;; This entry has expired, so we remove it.
2173 ((and expire (< date expire))
2174 (gnus-score-set 'touched '(t) alist)
2175 (setcdr entries (cddr entries)))
2176 ;; No match; go to next entry.
2177 (t
2178 (setq entries (cdr entries))))))))))
eec82323
LMI
2179
2180 ;; Find fuzzy matches.
2181 (when fuzzies
2182 ;; Simplify the entire buffer for easy matching.
a5954fa5 2183 (gnus-simplify-buffer-fuzzy gnus-simplify-subject-fuzzy-regexp)
eec82323
LMI
2184 (while (setq kill (cadaar fuzzies))
2185 (let* ((match (nth 0 kill))
2186 (type (nth 3 kill))
2187 (score (or (nth 1 kill) gnus-score-interactive-default-score))
2188 (date (nth 2 kill))
2189 (mt (aref (symbol-name type) 0))
2190 (case-fold-search (not (= mt ?F)))
2191 found)
2192 (goto-char (point-min))
2193 (while (and (not (eobp))
2194 (search-forward match nil t))
01c52d31 2195 (when (and (= (point-at-bol) (match-beginning 0))
eec82323
LMI
2196 (eolp))
2197 (setq found (setq arts (get-text-property (point) 'articles)))
2198 (if trace
2199 (while (setq art (pop arts))
2200 (setcdr art (+ score (cdr art)))
2201 (push (cons
2202 (car-safe (rassq (cdar fuzzies) gnus-score-cache))
2203 kill)
2204 gnus-score-trace))
2205 ;; Found a match, update scores.
2206 (while (setq art (pop arts))
2207 (setcdr art (+ score (cdr art))))))
2208 (forward-line 1))
2209 ;; Update expiry date
6748645f
LMI
2210 (if (not trace)
2211 (cond
2212 ;; Permanent.
2213 ((null date)
16409b0b 2214 ;; Do nothing.
6748645f
LMI
2215 )
2216 ;; Match, update date.
2217 ((and found gnus-update-score-entry-dates)
2218 (gnus-score-set 'touched '(t) (cdar fuzzies))
2219 (setcar (nthcdr 2 kill) now))
2220 ;; Old entry, remove.
2221 ((and expire (< date expire))
2222 (gnus-score-set 'touched '(t) (cdar fuzzies))
2223 (setcdr (caar fuzzies) (cddaar fuzzies)))))
eec82323
LMI
2224 (setq fuzzies (cdr fuzzies)))))
2225
2226 (when words
2227 ;; Enter all words into the hashtb.
2228 (let ((hashtb (gnus-make-hashtable
2229 (* 10 (count-lines (point-min) (point-max))))))
2230 (gnus-enter-score-words-into-hashtb hashtb)
2231 (while (setq kill (cadaar words))
2232 (let* ((score (or (nth 1 kill) gnus-score-interactive-default-score))
2233 (date (nth 2 kill))
2234 found)
2235 (when (setq arts (intern-soft (nth 0 kill) hashtb))
2236 (setq arts (symbol-value arts))
2237 (setq found t)
2238 (if trace
2239 (while (setq art (pop arts))
2240 (setcdr art (+ score (cdr art)))
2241 (push (cons
2242 (car-safe (rassq (cdar words) gnus-score-cache))
2243 kill)
2244 gnus-score-trace))
2245 ;; Found a match, update scores.
2246 (while (setq art (pop arts))
2247 (setcdr art (+ score (cdr art))))))
2248 ;; Update expiry date
6748645f
LMI
2249 (if (not trace)
2250 (cond
2251 ;; Permanent.
2252 ((null date)
16409b0b 2253 ;; Do nothing.
6748645f
LMI
2254 )
2255 ;; Match, update date.
2256 ((and found gnus-update-score-entry-dates)
2257 (gnus-score-set 'touched '(t) (cdar words))
2258 (setcar (nthcdr 2 kill) now))
2259 ;; Old entry, remove.
2260 ((and expire (< date expire))
2261 (gnus-score-set 'touched '(t) (cdar words))
2262 (setcdr (caar words) (cddaar words)))))
eec82323
LMI
2263 (setq words (cdr words))))))
2264 nil))
2265
2266(defun gnus-enter-score-words-into-hashtb (hashtb)
2267 ;; Find all the words in the buffer and enter them into
2268 ;; the hashtable.
01c52d31 2269 (let (word val)
eec82323 2270 (goto-char (point-min))
01c52d31
MB
2271 (with-syntax-table gnus-adaptive-word-syntax-table
2272 (while (re-search-forward "\\b\\w+\\b" nil t)
2273 (setq val
2274 (gnus-gethash
2275 (setq word (downcase (buffer-substring
2276 (match-beginning 0) (match-end 0))))
2277 hashtb))
2278 (gnus-sethash
2279 word
2280 (append (get-text-property (point-at-eol) 'articles) val)
2281 hashtb)))
eec82323
LMI
2282 ;; Make all the ignorable words ignored.
2283 (let ((ignored (append gnus-ignored-adaptive-words
6748645f
LMI
2284 (if gnus-adaptive-word-no-group-words
2285 (message-tokenize-header
2286 (gnus-group-real-name gnus-newsgroup-name)
2287 "."))
eec82323
LMI
2288 gnus-default-ignored-adaptive-words)))
2289 (while ignored
2290 (gnus-sethash (pop ignored) nil hashtb)))))
2291
2292(defun gnus-score-string< (a1 a2)
2293 ;; Compare headers in articles A2 and A2.
2294 ;; The header index used is the free variable `gnus-score-index'.
2295 (string-lessp (aref (car a1) gnus-score-index)
2296 (aref (car a2) gnus-score-index)))
2297
2298(defun gnus-current-score-file-nondirectory (&optional score-file)
2299 (let ((score-file (or score-file gnus-current-score-file)))
2300 (if score-file
2301 (gnus-short-group-name (file-name-nondirectory score-file))
2302 "none")))
2303
2304(defun gnus-score-adaptive ()
2305 "Create adaptive score rules for this newsgroup."
2306 (when gnus-newsgroup-adaptive
2307 ;; We change the score file to the adaptive score file.
20a673b2 2308 (with-current-buffer gnus-summary-buffer
eec82323
LMI
2309 (gnus-score-load-file
2310 (or gnus-newsgroup-adaptive-score-file
6748645f 2311 (gnus-home-score-file gnus-newsgroup-name t)
eec82323
LMI
2312 (gnus-score-file-name
2313 gnus-newsgroup-name gnus-adaptive-file-suffix))))
2314 ;; Perform ordinary line scoring.
2315 (when (or (not (listp gnus-newsgroup-adaptive))
2316 (memq 'line gnus-newsgroup-adaptive))
2317 (save-excursion
2318 (let* ((malist (gnus-copy-sequence gnus-adaptive-score-alist))
2319 (alist malist)
2320 (date (current-time-string))
2321 (data gnus-newsgroup-data)
6748645f 2322 elem headers match func)
eec82323
LMI
2323 ;; First we transform the adaptive rule alist into something
2324 ;; that's faster to process.
2325 (while malist
2326 (setq elem (car malist))
2327 (when (symbolp (car elem))
2328 (setcar elem (symbol-value (car elem))))
2329 (setq elem (cdr elem))
2330 (while elem
6748645f
LMI
2331 (when (fboundp
2332 (setq func
2333 (intern
eec82323
LMI
2334 (concat "mail-header-"
2335 (if (eq (caar elem) 'followup)
2336 "message-id"
6748645f
LMI
2337 (downcase (symbol-name (caar elem))))))))
2338 (setcdr (car elem)
2339 (cons (if (eq (caar elem) 'followup)
2340 "references"
2341 (symbol-name (caar elem)))
2342 (cdar elem)))
2343 (setcar (car elem)
2344 `(lambda (h)
2345 (,func h))))
eec82323
LMI
2346 (setq elem (cdr elem)))
2347 (setq malist (cdr malist)))
2348 ;; Then we score away.
2349 (while data
2350 (setq elem (cdr (assq (gnus-data-mark (car data)) alist)))
2351 (if (or (not elem)
2352 (gnus-data-pseudo-p (car data)))
2353 ()
2354 (when (setq headers (gnus-data-header (car data)))
2355 (while elem
2356 (setq match (funcall (caar elem) headers))
2357 (gnus-summary-score-entry
2358 (nth 1 (car elem)) match
2359 (cond
2360 ((numberp match)
2361 '=)
2362 ((equal (nth 1 (car elem)) "date")
2363 'a)
2364 (t
2365 ;; Whether we use substring or exact matches is
2366 ;; controlled here.
2367 (if (or (not gnus-score-exact-adapt-limit)
2368 (< (length match) gnus-score-exact-adapt-limit))
2369 'e
2370 (if (equal (nth 1 (car elem)) "subject")
2371 'f 's))))
2372 (nth 2 (car elem)) date nil t)
2373 (setq elem (cdr elem)))))
2374 (setq data (cdr data))))))
2375
2376 ;; Perform adaptive word scoring.
2377 (when (and (listp gnus-newsgroup-adaptive)
2378 (memq 'word gnus-newsgroup-adaptive))
16409b0b 2379 (with-temp-buffer
eec82323 2380 (let* ((hashtb (gnus-make-hashtable 1000))
16409b0b 2381 (date (date-to-day (current-time-string)))
eec82323 2382 (data gnus-newsgroup-data)
eec82323 2383 word d score val)
01c52d31
MB
2384 (with-syntax-table gnus-adaptive-word-syntax-table
2385 ;; Go through all articles.
2386 (while (setq d (pop data))
2387 (when (and
2388 (not (gnus-data-pseudo-p d))
2389 (setq score
2390 (cdr (assq
2391 (gnus-data-mark d)
2392 gnus-adaptive-word-score-alist))))
2393 ;; This article has a mark that should lead to
2394 ;; adaptive word rules, so we insert the subject
2395 ;; and find all words in that string.
2396 (insert (mail-header-subject (gnus-data-header d)))
2397 (downcase-region (point-min) (point-max))
2398 (goto-char (point-min))
2399 (while (re-search-forward "\\b\\w+\\b" nil t)
2400 ;; Put the word and score into the hashtb.
2401 (setq val (gnus-gethash (setq word (match-string 0))
2402 hashtb))
2403 (when (or (not gnus-adaptive-word-length-limit)
2404 (> (length word)
2405 gnus-adaptive-word-length-limit))
2406 (setq val (+ score (or val 0)))
2407 (if (and gnus-adaptive-word-minimum
2408 (< val gnus-adaptive-word-minimum))
2409 (setq val gnus-adaptive-word-minimum))
2410 (gnus-sethash word val hashtb)))
2411 (erase-buffer))))
eec82323
LMI
2412 ;; Make all the ignorable words ignored.
2413 (let ((ignored (append gnus-ignored-adaptive-words
6748645f
LMI
2414 (if gnus-adaptive-word-no-group-words
2415 (message-tokenize-header
16409b0b 2416 (gnus-group-real-name
6748645f
LMI
2417 gnus-newsgroup-name)
2418 "."))
eec82323
LMI
2419 gnus-default-ignored-adaptive-words)))
2420 (while ignored
2421 (gnus-sethash (pop ignored) nil hashtb)))
2422 ;; Now we have all the words and scores, so we
2423 ;; add these rules to the ADAPT file.
2424 (set-buffer gnus-summary-buffer)
2425 (mapatoms
2426 (lambda (word)
2427 (when (symbol-value word)
2428 (gnus-summary-score-entry
2429 "subject" (symbol-name word) 'w (symbol-value word)
2430 date nil t)))
2431 hashtb))))))
2432
2433(defun gnus-score-edit-done ()
2434 (let ((bufnam (buffer-file-name (current-buffer)))
2435 (winconf gnus-prev-winconf))
2436 (when winconf
2437 (set-window-configuration winconf))
2438 (gnus-score-remove-from-cache bufnam)
01c52d31
MB
2439 (gnus-score-load-file bufnam)
2440 (run-hooks 'gnus-score-edit-done-hook)))
eec82323
LMI
2441
2442(defun gnus-score-find-trace ()
2443 "Find all score rules that applies to the current article."
2444 (interactive)
2445 (let ((old-scored gnus-newsgroup-scored))
2446 (let ((gnus-newsgroup-headers
2447 (list (gnus-summary-article-header)))
2448 (gnus-newsgroup-scored nil)
23f87bed
MB
2449 ;; Must be synced with `gnus-score-edit-file-at-point':
2450 (frmt "%S [%s] -> %s\n")
2451 trace
2452 file)
eec82323
LMI
2453 (save-excursion
2454 (nnheader-set-temp-buffer "*Score Trace*"))
2455 (setq gnus-score-trace nil)
2456 (gnus-possibly-score-headers 'trace)
2457 (if (not (setq trace gnus-score-trace))
2458 (gnus-error
2459 1 "No score rules apply to the current article (default score %d)."
2460 gnus-summary-default-score)
2461 (set-buffer "*Score Trace*")
23f87bed
MB
2462 ;; Use a keymap instead?
2463 (local-set-key "q"
2464 (lambda ()
2465 (interactive)
2466 (bury-buffer nil)
2467 (gnus-summary-expand-window)))
01c52d31
MB
2468 (local-set-key "k"
2469 (lambda ()
2470 (interactive)
2471 (kill-buffer (current-buffer))
2472 (gnus-summary-expand-window)))
23f87bed
MB
2473 (local-set-key "e" (lambda ()
2474 "Run `gnus-score-edit-file-at-point'."
2475 (interactive)
2476 (gnus-score-edit-file-at-point)))
2477 (local-set-key "f" (lambda ()
2478 "Run `gnus-score-edit-file-at-point'."
2479 (interactive)
2480 (gnus-score-edit-file-at-point 'format)))
2481 (local-set-key "t" 'toggle-truncate-lines)
16409b0b 2482 (setq truncate-lines t)
23f87bed
MB
2483 (dolist (entry trace)
2484 (setq file (or (car entry)
2485 ;; Must be synced with
2486 ;; `gnus-score-edit-file-at-point':
2487 "(non-file rule)"))
2488 (insert
2489 (format frmt
2490 (cdr entry)
2491 ;; Don't use `file-name-sans-extension' to see .SCORE and
2492 ;; .ADAPT directly:
2493 (file-name-nondirectory file)
2494 (abbreviate-file-name file))))
89167438
MB
2495 (insert
2496 (format "\nTotal score: %d"
030cca00
MB
2497 (apply '+ (mapcar
2498 (lambda (s)
2499 (or (caddr s)
2500 gnus-score-interactive-default-score))
2501 trace))))
23f87bed
MB
2502 (insert
2503 "\n\nQuick help:
2504
2505Type `e' to edit score file corresponding to the score rule on current line,
2506`f' to format (pretty print) the score file and edit it,
2507`t' toggle to truncate long lines in this buffer,
01c52d31 2508`q' to quit, `k' to kill score trace buffer.
23f87bed
MB
2509
2510The first sexp on each line is the score rule, followed by the file name of
2511the score file and its full name, including the directory.")
eec82323
LMI
2512 (goto-char (point-min))
2513 (gnus-configure-windows 'score-trace)))
2514 (set-buffer gnus-summary-buffer)
2515 (setq gnus-newsgroup-scored old-scored)))
2516
2517(defun gnus-score-find-favourite-words ()
2518 "List words used in scoring."
2519 (interactive)
2520 (let ((alists (gnus-score-load-files (gnus-all-score-files)))
2521 alist rule rules kill)
2522 ;; Go through all the score alists for this group
2523 ;; and find all `w' rules.
2524 (while (setq alist (pop alists))
2525 (while (setq rule (pop alist))
2526 (when (and (stringp (car rule))
2527 (equal "subject" (downcase (pop rule))))
2528 (while (setq kill (pop rule))
2529 (when (memq (nth 3 kill) '(w W word Word))
2530 (push (cons (or (nth 1 kill)
2531 gnus-score-interactive-default-score)
2532 (car kill))
2533 rules))))))
2534 (setq rules (sort rules (lambda (r1 r2)
2535 (string-lessp (cdr r1) (cdr r2)))))
2536 ;; Add up words that have appeared several times.
2537 (let ((r rules))
2538 (while (cdr r)
2539 (if (equal (cdar r) (cdadr r))
2540 (progn
2541 (setcar (car r) (+ (caar r) (caadr r)))
2542 (setcdr r (cddr r)))
2543 (pop r))))
2544 ;; Insert the words.
2545 (nnheader-set-temp-buffer "*Score Words*")
2546 (if (not (setq rules (sort rules (lambda (r1 r2) (> (car r1) (car r2))))))
2547 (gnus-error 3 "No word score rules")
2548 (while rules
2549 (insert (format "%-5d: %s\n" (caar rules) (cdar rules)))
2550 (pop rules))
eec82323
LMI
2551 (goto-char (point-min))
2552 (gnus-configure-windows 'score-words))))
2553
2554(defun gnus-summary-rescore ()
2555 "Redo the entire scoring process in the current summary."
2556 (interactive)
2557 (gnus-score-save)
2558 (setq gnus-score-cache nil)
2559 (setq gnus-newsgroup-scored nil)
2560 (gnus-possibly-score-headers)
2561 (gnus-score-update-all-lines))
2562
2563(defun gnus-score-flush-cache ()
2564 "Flush the cache of score files."
2565 (interactive)
2566 (gnus-score-save)
2567 (setq gnus-score-cache nil
2568 gnus-score-alist nil
2569 gnus-short-name-score-file-cache nil)
2570 (gnus-message 6 "The score cache is now flushed"))
2571
2572(gnus-add-shutdown 'gnus-score-close 'gnus)
2573
2574(defvar gnus-score-file-alist-cache nil)
2575
2576(defun gnus-score-close ()
2577 "Clear all internal score variables."
2578 (setq gnus-score-cache nil
2579 gnus-internal-global-score-files nil
2580 gnus-score-file-list nil
2581 gnus-score-file-alist-cache nil))
2582
2583;; Summary score marking commands.
2584
2585(defun gnus-summary-raise-same-subject-and-select (score)
2586 "Raise articles which has the same subject with SCORE and select the next."
2587 (interactive "p")
2588 (let ((subject (gnus-summary-article-subject)))
2589 (gnus-summary-raise-score score)
2590 (while (gnus-summary-find-subject subject)
2591 (gnus-summary-raise-score score))
2592 (gnus-summary-next-article t)))
2593
2594(defun gnus-summary-raise-same-subject (score)
2595 "Raise articles which has the same subject with SCORE."
2596 (interactive "p")
2597 (let ((subject (gnus-summary-article-subject)))
2598 (gnus-summary-raise-score score)
2599 (while (gnus-summary-find-subject subject)
2600 (gnus-summary-raise-score score))
2601 (gnus-summary-next-subject 1 t)))
2602
16409b0b 2603(defun gnus-score-delta-default (level)
eec82323
LMI
2604 (if level (prefix-numeric-value level)
2605 gnus-score-interactive-default-score))
2606
2607(defun gnus-summary-raise-thread (&optional score)
2608 "Raise the score of the articles in the current thread with SCORE."
2609 (interactive "P")
16409b0b 2610 (setq score (gnus-score-delta-default score))
eec82323
LMI
2611 (let (e)
2612 (save-excursion
2613 (let ((articles (gnus-summary-articles-in-thread)))
2614 (while articles
2615 (gnus-summary-goto-subject (car articles))
2616 (gnus-summary-raise-score score)
2617 (setq articles (cdr articles))))
2618 (setq e (point)))
2619 (let ((gnus-summary-check-current t))
2620 (unless (zerop (gnus-summary-next-subject 1 t))
2621 (goto-char e))))
2622 (gnus-summary-recenter)
2623 (gnus-summary-position-point)
2624 (gnus-set-mode-line 'summary))
2625
2626(defun gnus-summary-lower-same-subject-and-select (score)
2627 "Raise articles which has the same subject with SCORE and select the next."
2628 (interactive "p")
2629 (gnus-summary-raise-same-subject-and-select (- score)))
2630
2631(defun gnus-summary-lower-same-subject (score)
2632 "Raise articles which has the same subject with SCORE."
2633 (interactive "p")
2634 (gnus-summary-raise-same-subject (- score)))
2635
2636(defun gnus-summary-lower-thread (&optional score)
2637 "Lower score of articles in the current thread with SCORE."
2638 (interactive "P")
23f87bed 2639 (gnus-summary-raise-thread (- (gnus-score-delta-default score))))
eec82323
LMI
2640
2641;;; Finding score files.
2642
2643(defun gnus-score-score-files (group)
2644 "Return a list of all possible score files."
2645 ;; Search and set any global score files.
2646 (when gnus-global-score-files
2647 (unless gnus-internal-global-score-files
2648 (gnus-score-search-global-directories gnus-global-score-files)))
2649 ;; Fix the kill-file dir variable.
2650 (setq gnus-kill-files-directory
2651 (file-name-as-directory gnus-kill-files-directory))
2652 ;; If we can't read it, there are no score files.
2653 (if (not (file-exists-p (expand-file-name gnus-kill-files-directory)))
2654 (setq gnus-score-file-list nil)
2655 (if (not (gnus-use-long-file-name 'not-score))
2656 ;; We do not use long file names, so we have to do some
2657 ;; directory traversing.
2658 (setq gnus-score-file-list
2659 (cons nil
2660 (or gnus-short-name-score-file-cache
2661 (prog2
2662 (gnus-message 6 "Finding all score files...")
2663 (setq gnus-short-name-score-file-cache
2664 (gnus-score-score-files-1
2665 gnus-kill-files-directory))
2666 (gnus-message 6 "Finding all score files...done")))))
2667 ;; We want long file names.
2668 (when (or (not gnus-score-file-list)
2669 (not (car gnus-score-file-list))
2670 (gnus-file-newer-than gnus-kill-files-directory
2671 (car gnus-score-file-list)))
2672 (setq gnus-score-file-list
2673 (cons (nth 5 (file-attributes gnus-kill-files-directory))
2674 (nreverse
2675 (directory-files
2676 gnus-kill-files-directory t
2677 (gnus-score-file-regexp)))))))
2678 (cdr gnus-score-file-list)))
2679
2680(defun gnus-score-score-files-1 (dir)
2681 "Return all possible score files under DIR."
2682 (let ((files (list (expand-file-name dir)))
2683 (regexp (gnus-score-file-regexp))
2684 (case-fold-search nil)
2685 seen out file)
2686 (while (setq file (pop files))
2687 (cond
16409b0b
GM
2688 ;; Ignore files that start with a dot.
2689 ((string-match "^\\." (file-name-nondirectory file))
eec82323
LMI
2690 nil)
2691 ;; Add subtrees of directory to also be searched.
2692 ((and (file-directory-p file)
2693 (not (member (file-truename file) seen)))
2694 (push (file-truename file) seen)
2695 (setq files (nconc (directory-files file t nil t) files)))
2696 ;; Add files to the list of score files.
2697 ((string-match regexp file)
2698 (push file out))))
2699 (or out
2700 ;; Return a dummy value.
23f87bed
MB
2701 (list (expand-file-name "this.file.does.not.exist.SCORE"
2702 gnus-kill-files-directory)))))
eec82323
LMI
2703
2704(defun gnus-score-file-regexp ()
2705 "Return a regexp that match all score files."
2706 (concat "\\(" (regexp-quote gnus-score-file-suffix )
2707 "\\|" (regexp-quote gnus-adaptive-file-suffix) "\\)\\'"))
2708
2709(defun gnus-score-find-bnews (group)
2710 "Return a list of score files for GROUP.
2711The score files are those files in the ~/News/ directory which matches
2712GROUP using BNews sys file syntax."
2713 (let* ((sfiles (append (gnus-score-score-files group)
2714 gnus-internal-global-score-files))
2715 (kill-dir (file-name-as-directory
2716 (expand-file-name gnus-kill-files-directory)))
2717 (klen (length kill-dir))
2718 (score-regexp (gnus-score-file-regexp))
2719 (trans (cdr (assq ?: nnheader-file-name-translation-alist)))
16409b0b 2720 (group-trans (nnheader-translate-file-chars group t))
eec82323 2721 ofiles not-match regexp)
20a673b2 2722 (with-current-buffer (gnus-get-buffer-create "*gnus score files*")
16409b0b 2723 (buffer-disable-undo)
eec82323
LMI
2724 ;; Go through all score file names and create regexp with them
2725 ;; as the source.
2726 (while sfiles
2727 (erase-buffer)
2728 (insert (car sfiles))
2729 (goto-char (point-min))
2730 ;; First remove the suffix itself.
2731 (when (re-search-forward (concat "." score-regexp) nil t)
2732 (replace-match "" t t)
2733 (goto-char (point-min))
2734 (if (looking-at (regexp-quote kill-dir))
2735 ;; If the file name was just "SCORE", `klen' is one character
2736 ;; too much.
2737 (delete-char (min (1- (point-max)) klen))
2738 (goto-char (point-max))
47b63dfa
SZ
2739 (if (re-search-backward gnus-directory-sep-char-regexp nil t)
2740 (delete-region (1+ (point)) (point-min))
2741 (gnus-message 1 "Can't find directory separator in %s"
2742 (car sfiles))))
eec82323
LMI
2743 ;; If short file names were used, we have to translate slashes.
2744 (goto-char (point-min))
2745 (let ((regexp (concat
59896c4c 2746 "[/:" (if trans (char-to-string trans)) "]")))
eec82323
LMI
2747 (while (re-search-forward regexp nil t)
2748 (replace-match "." t t)))
2749 ;; Kludge to get rid of "nntp+" problems.
2750 (goto-char (point-min))
2751 (when (looking-at "nn[a-z]+\\+")
2752 (search-forward "+")
2753 (forward-char -1)
2754 (insert "\\")
2755 (forward-char 1))
2756 ;; Kludge to deal with "++".
2757 (while (search-forward "+" nil t)
2758 (replace-match "\\+" t t))
2759 ;; Translate "all" to ".*".
2760 (goto-char (point-min))
2761 (while (search-forward "all" nil t)
2762 (replace-match ".*" t t))
2763 (goto-char (point-min))
2764 ;; Deal with "not."s.
23f87bed
MB
2765 (if (looking-at "not.")
2766 (progn
2767 (setq not-match t)
2768 (setq regexp
2769 (concat "^" (buffer-substring 5 (point-max)) "$")))
2770 (setq regexp (concat "^" (buffer-substring 1 (point-max)) "$"))
2771 (setq not-match nil))
eec82323
LMI
2772 ;; Finally - if this resulting regexp matches the group name,
2773 ;; we add this score file to the list of score files
2774 ;; applicable to this group.
2775 (when (or (and not-match
47b63dfa 2776 (ignore-errors
16409b0b 2777 (not (string-match regexp group-trans))))
47b63dfa
SZ
2778 (and (not not-match)
2779 (ignore-errors (string-match regexp group-trans))))
eec82323
LMI
2780 (push (car sfiles) ofiles)))
2781 (setq sfiles (cdr sfiles)))
23f87bed 2782 (gnus-kill-buffer (current-buffer))
eec82323
LMI
2783 ;; Slight kludge here - the last score file returned should be
2784 ;; the local score file, whether it exists or not. This is so
2785 ;; that any score commands the user enters will go to the right
2786 ;; file, and not end up in some global score file.
2787 (let ((localscore (gnus-score-file-name group)))
2788 (setq ofiles (cons localscore (delete localscore ofiles))))
2789 (gnus-sort-score-files (nreverse ofiles)))))
2790
2791(defun gnus-score-find-single (group)
2792 "Return list containing the score file for GROUP."
2793 (list (or gnus-newsgroup-adaptive-score-file
2794 (gnus-score-file-name group gnus-adaptive-file-suffix))
2795 (gnus-score-file-name group)))
2796
2797(defun gnus-score-find-hierarchical (group)
2798 "Return list of score files for GROUP.
2799This includes the score file for the group and all its parents."
2800 (let* ((prefix (gnus-group-real-prefix group))
2801 (all (list nil))
2802 (group (gnus-group-real-name group))
2803 (start 0))
2804 (while (string-match "\\." group (1+ start))
2805 (setq start (match-beginning 0))
2806 (push (substring group 0 start) all))
2807 (push group all)
2808 (setq all
2809 (nconc
2810 (mapcar (lambda (group)
2811 (gnus-score-file-name group gnus-adaptive-file-suffix))
2812 (setq all (nreverse all)))
2813 (mapcar 'gnus-score-file-name all)))
2814 (if (equal prefix "")
2815 all
2816 (mapcar
2817 (lambda (file)
2818 (nnheader-translate-file-chars
2819 (concat (file-name-directory file) prefix
2820 (file-name-nondirectory file))))
2821 all))))
2822
2823(defun gnus-score-file-rank (file)
2824 "Return a number that says how specific score FILE is.
2825Destroys the current buffer."
2826 (if (member file gnus-internal-global-score-files)
2827 0
2828 (when (string-match
2829 (concat "^" (regexp-quote
2830 (expand-file-name
2831 (file-name-as-directory gnus-kill-files-directory))))
2832 file)
2833 (setq file (substring file (match-end 0))))
2834 (insert file)
2835 (goto-char (point-min))
2836 (let ((beg (point))
2837 elems)
2838 (while (re-search-forward "[./]" nil t)
2839 (push (buffer-substring beg (1- (point)))
2840 elems))
2841 (erase-buffer)
2842 (setq elems (delete "all" elems))
2843 (length elems))))
2844
2845(defun gnus-sort-score-files (files)
2846 "Sort FILES so that the most general files come first."
16409b0b 2847 (with-temp-buffer
eec82323
LMI
2848 (let ((alist
2849 (mapcar
2850 (lambda (file)
2851 (cons (inline (gnus-score-file-rank file)) file))
2852 files)))
01c52d31 2853 (mapcar 'cdr (sort alist 'car-less-than-car)))))
eec82323
LMI
2854
2855(defun gnus-score-find-alist (group)
2856 "Return list of score files for GROUP.
39bb8e56 2857The list is determined from the variable `gnus-score-file-alist'."
eec82323
LMI
2858 (let ((alist gnus-score-file-multiple-match-alist)
2859 score-files)
2860 ;; if this group has been seen before, return the cached entry
2861 (if (setq score-files (assoc group gnus-score-file-alist-cache))
2862 (cdr score-files) ;ensures caching groups with no matches
2863 ;; handle the multiple match alist
2864 (while alist
2865 (when (string-match (caar alist) group)
29dbb8f7 2866 (setq score-files (append (cdar alist) score-files)))
eec82323
LMI
2867 (setq alist (cdr alist)))
2868 (setq alist gnus-score-file-single-match-alist)
2869 ;; handle the single match alist
2870 (while alist
2871 (when (string-match (caar alist) group)
2872 ;; progn used just in case ("regexp") has no files
2873 ;; and score-files is still nil. -sj
2874 ;; this can be construed as a "stop searching here" feature :>
2875 ;; and used to simplify regexps in the single-alist
29dbb8f7 2876 (setq score-files (append (cdar alist) score-files))
eec82323
LMI
2877 (setq alist nil))
2878 (setq alist (cdr alist)))
2879 ;; cache the score files
2880 (push (cons group score-files) gnus-score-file-alist-cache)
2881 score-files)))
2882
2883(defun gnus-all-score-files (&optional group)
2884 "Return a list of all score files for the current group."
2885 (let ((funcs gnus-score-find-score-files-function)
2886 (group (or group gnus-newsgroup-name))
2887 score-files)
6748645f
LMI
2888 (when group
2889 ;; Make sure funcs is a list.
2890 (and funcs
2891 (not (listp funcs))
2892 (setq funcs (list funcs)))
59896c4c
DL
2893 (when gnus-score-use-all-scores
2894 ;; Get the initial score files for this group.
2895 (when funcs
1bca59fe 2896 (setq score-files (copy-sequence (gnus-score-find-alist group))))
59896c4c
DL
2897 ;; Add any home adapt files.
2898 (let ((home (gnus-home-score-file group t)))
2899 (when home
2900 (push home score-files)
2901 (setq gnus-newsgroup-adaptive-score-file home)))
2902 ;; Check whether there is a `adapt-file' group parameter.
2903 (let ((param-file (gnus-group-find-parameter group 'adapt-file)))
2904 (when param-file
2905 (push param-file score-files)
2906 (setq gnus-newsgroup-adaptive-score-file param-file))))
6748645f
LMI
2907 ;; Go through all the functions for finding score files (or actual
2908 ;; scores) and add them to a list.
2909 (while funcs
23f87bed 2910 (when (functionp (car funcs))
6748645f 2911 (setq score-files
23f87bed
MB
2912 (append score-files
2913 (nreverse (funcall (car funcs) group)))))
6748645f 2914 (setq funcs (cdr funcs)))
59896c4c
DL
2915 (when gnus-score-use-all-scores
2916 ;; Add any home score files.
2917 (let ((home (gnus-home-score-file group)))
2918 (when home
2919 (push home score-files)))
2920 ;; Check whether there is a `score-file' group parameter.
2921 (let ((param-file (gnus-group-find-parameter group 'score-file)))
2922 (when param-file
2923 (push param-file score-files))))
6748645f
LMI
2924 ;; Expand all files names.
2925 (let ((files score-files))
2926 (while files
2927 (when (stringp (car files))
2928 (setcar files (expand-file-name
2929 (car files) gnus-kill-files-directory)))
2930 (pop files)))
2931 (setq score-files (nreverse score-files))
2932 ;; Remove any duplicate score files.
2933 (while (and score-files
2934 (member (car score-files) (cdr score-files)))
2935 (pop score-files))
2936 (let ((files score-files))
2937 (while (cdr files)
2938 (if (member (cadr files) (cddr files))
2939 (setcdr files (cddr files))
2940 (pop files))))
2941 ;; Do the scoring if there are any score files for this group.
2942 score-files)))
eec82323
LMI
2943
2944(defun gnus-possibly-score-headers (&optional trace)
2945 "Do scoring if scoring is required."
2946 (let ((score-files (gnus-all-score-files)))
2947 (when score-files
2948 (gnus-score-headers score-files trace))))
2949
2950(defun gnus-score-file-name (newsgroup &optional suffix)
2951 "Return the name of a score file for NEWSGROUP."
2952 (let ((suffix (or suffix gnus-score-file-suffix)))
2953 (nnheader-translate-file-chars
2954 (cond
2955 ((or (null newsgroup)
2956 (string-equal newsgroup ""))
2957 ;; The global score file is placed at top of the directory.
6748645f 2958 (expand-file-name suffix gnus-kill-files-directory))
eec82323
LMI
2959 ((gnus-use-long-file-name 'not-score)
2960 ;; Append ".SCORE" to newsgroup name.
2961 (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
2962 "." suffix)
2963 gnus-kill-files-directory))
2964 (t
2965 ;; Place "SCORE" under the hierarchical directory.
2966 (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
2967 "/" suffix)
2968 gnus-kill-files-directory))))))
2969
2970(defun gnus-score-search-global-directories (files)
2971 "Scan all global score directories for score files."
2972 ;; Set the variable `gnus-internal-global-score-files' to all
2973 ;; available global score files.
2974 (interactive (list gnus-global-score-files))
2975 (let (out)
2976 (while files
6748645f 2977 ;; #### /$ Unix-specific?
23f87bed 2978 (if (file-directory-p (car files))
eec82323
LMI
2979 (setq out (nconc (directory-files
2980 (car files) t
2981 (concat (gnus-score-file-regexp) "$"))))
2982 (push (car files) out))
2983 (setq files (cdr files)))
2984 (setq gnus-internal-global-score-files out)))
2985
2986(defun gnus-score-default-fold-toggle ()
2987 "Toggle folding for new score file entries."
2988 (interactive)
2989 (setq gnus-score-default-fold (not gnus-score-default-fold))
2990 (if gnus-score-default-fold
2991 (gnus-message 1 "New score file entries will be case insensitive.")
2992 (gnus-message 1 "New score file entries will be case sensitive.")))
2993
2994;;; Home score file.
2995
2996(defun gnus-home-score-file (group &optional adapt)
2997 "Return the home score file for GROUP.
2998If ADAPT, return the home adaptive file instead."
2999 (let ((list (if adapt gnus-home-adapt-file gnus-home-score-file))
3000 elem found)
3001 ;; Make sure we have a list.
3002 (unless (listp list)
3003 (setq list (list list)))
3004 ;; Go through the list and look for matches.
3005 (while (and (not found)
3006 (setq elem (pop list)))
3007 (setq found
3008 (cond
3009 ;; Simple string.
3010 ((stringp elem)
3011 elem)
3012 ;; Function.
23f87bed 3013 ((functionp elem)
eec82323 3014 (funcall elem group))
16409b0b 3015 ;; Regexp-file cons.
eec82323 3016 ((consp elem)
6748645f 3017 (when (string-match (gnus-globalify-regexp (car elem)) group)
16409b0b 3018 (replace-match (cadr elem) t nil group))))))
eec82323 3019 (when found
23f87bed 3020 (setq found (nnheader-translate-file-chars found))
16409b0b 3021 (if (file-name-absolute-p found)
23f87bed
MB
3022 found
3023 (nnheader-concat gnus-kill-files-directory found)))))
eec82323
LMI
3024
3025(defun gnus-hierarchial-home-score-file (group)
3026 "Return the score file of the top-level hierarchy of GROUP."
3027 (if (string-match "^[^.]+\\." group)
3028 (concat (match-string 0 group) gnus-score-file-suffix)
3029 ;; Group name without any dots.
3030 (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
3031 gnus-score-file-suffix)))
3032
3033(defun gnus-hierarchial-home-adapt-file (group)
3034 "Return the adapt file of the top-level hierarchy of GROUP."
3035 (if (string-match "^[^.]+\\." group)
3036 (concat (match-string 0 group) gnus-adaptive-file-suffix)
3037 ;; Group name without any dots.
3038 (concat group (if (gnus-use-long-file-name 'not-score) "." "/")
3039 gnus-adaptive-file-suffix)))
3040
6748645f
LMI
3041(defun gnus-current-home-score-file (group)
3042 "Return the \"current\" regular score file."
29dbb8f7 3043 (car (gnus-score-find-alist group)))
6748645f 3044
eec82323
LMI
3045;;;
3046;;; Score decays
3047;;;
3048
3049(defun gnus-decay-score (score)
a8151ef7 3050 "Decay SCORE according to `gnus-score-decay-constant' and `gnus-score-decay-scale'."
23f87bed
MB
3051 (let ((n (- score
3052 (* (if (< score 0) -1 1)
3053 (min (abs score)
3054 (max gnus-score-decay-constant
3055 (* (abs score)
3056 gnus-score-decay-scale)))))))
3057 (if (and (featurep 'xemacs)
44e97401 3058 ;; XEmacs's floor can handle only the floating point
23f87bed
MB
3059 ;; number below the half of the maximum integer.
3060 (> (abs n) (lsh -1 -2)))
3061 (string-to-number
3062 (car (split-string (number-to-string n) "\\.")))
3063 (floor n))))
eec82323
LMI
3064
3065(defun gnus-decay-scores (alist day)
3066 "Decay non-permanent scores in ALIST."
16409b0b 3067 (let ((times (- (time-to-days (current-time)) day))
eec82323
LMI
3068 kill entry updated score n)
3069 (unless (zerop times) ;Done decays today already?
3070 (while (setq entry (pop alist))
3071 (when (stringp (car entry))
3072 (setq entry (cdr entry))
3073 (while (setq kill (pop entry))
3074 (when (nth 2 kill)
3075 (setq updated t)
a8151ef7
LMI
3076 (setq score (or (nth 1 kill)
3077 gnus-score-interactive-default-score)
eec82323
LMI
3078 n times)
3079 (while (natnump (decf n))
3080 (setq score (funcall gnus-decay-score-function score)))
16409b0b 3081 (setcdr kill (cons score
a8151ef7 3082 (cdr (cdr kill)))))))))
eec82323
LMI
3083 ;; Return whether this score file needs to be saved. By Je-haysuss!
3084 updated))
3085
3086(provide 'gnus-score)
3087
3088;;; gnus-score.el ends here