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