Synch No Gnus 200512180014.
[elisp/gnus.git-] / lisp / gnus-cus.el
1 ;;; gnus-cus.el --- customization commands for Gnus
2
3 ;; Copyright (C) 1996, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005 Free Software Foundation, Inc.
5
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
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.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (require 'wid-edit)
31 (require 'gnus)
32 (require 'gnus-agent)
33 (require 'gnus-score)
34 (require 'gnus-topic)
35 (require 'gnus-art)
36
37 ;;; Widgets:
38
39 (defvar gnus-custom-map
40   (let ((map (make-keymap)))
41     (set-keymap-parent map widget-keymap)
42     (suppress-keymap map)
43     (define-key map [mouse-1] 'widget-move-and-invoke)
44     map)
45   "Keymap for editing Gnus customization buffers.")
46
47 (defun gnus-custom-mode ()
48   "Major mode for editing Gnus customization buffers.
49
50 The following commands are available:
51
52 \\[widget-forward]              Move to next button or editable field.
53 \\[widget-backward]             Move to previous button or editable field.
54 \\[widget-button-click]         Activate button under the mouse pointer.
55 \\[widget-button-press]         Activate button under point.
56
57 Entry to this mode calls the value of `gnus-custom-mode-hook'
58 if that value is non-nil."
59   (kill-all-local-variables)
60   (setq major-mode 'gnus-custom-mode
61         mode-name "Gnus Customize")
62   (use-local-map gnus-custom-map)
63   ;; Emacs 21 stuff:
64   (when (and (facep 'custom-button-face)
65              (facep 'custom-button-pressed-face))
66     (set (make-local-variable 'widget-button-face)
67          'custom-button-face)
68     (set (make-local-variable 'widget-button-pressed-face)
69          'custom-button-pressed-face)
70     (set (make-local-variable 'widget-mouse-face)
71          'custom-button-pressed-face))
72   (when (and (boundp 'custom-raised-buttons)
73              (symbol-value 'custom-raised-buttons))
74     (set (make-local-variable 'widget-push-button-prefix) "")
75     (set (make-local-variable 'widget-push-button-suffix) "")
76     (set (make-local-variable 'widget-link-prefix) "")
77     (set (make-local-variable 'widget-link-suffix) ""))
78   (gnus-run-mode-hooks 'gnus-custom-mode-hook))
79
80 ;;; Group Customization:
81
82 (defconst gnus-group-parameters
83   '((extra-aliases (choice
84                     :tag "Extra Aliases"
85                     (list
86                      :tag "List"
87                      (editable-list
88                       :inline t
89                       (gnus-email-address :tag "Address")))
90                     (gnus-email-address :tag "Address")) "\
91 Store messages posted from or to this address in this group.
92
93 You must be using gnus-group-split for this to work.  The VALUE of the
94 nnmail-split-fancy SPLIT generated for this group will match these
95 addresses.")
96
97     (split-regexp (regexp :tag "gnus-group-split Regular Expression") "\
98 Like gnus-group-split Address, but expects a regular expression.")
99
100     (split-exclude (list :tag "gnus-group-split Restricts"
101                          (editable-list
102                           :inline t (regexp :tag "Restrict"))) "\
103 Regular expression that cancels gnus-group-split matches.
104
105 Each entry is added to the nnmail-split-fancy SPLIT as a separate
106 RESTRICT clause.")
107
108     (split-spec (choice :tag "gnus-group-split Overrider"
109                         (sexp :tag "Fancy Split")
110                         (const :tag "Catch All" catch-all)
111                         (const :tag "Ignore" nil)) "\
112 Override all other gnus-group-split fields.
113
114 In `Fancy Split', you can enter any nnmail-split-fancy SPLIT.  Note
115 that the name of this group won't be automatically assumed, you have
116 to add it to the SPLITs yourself.  This means you can use such splits
117 to split messages to other groups too.
118
119 If you select `Catch All', this group will get postings for any
120 messages not matched in any other group.  It overrides the variable
121 gnus-group-split-default-catch-all-group.
122
123 Selecting `Ignore' forces no SPLIT to be generated for this group,
124 disabling all other gnus-group-split fields.")
125
126     (broken-reply-to (const :tag "Broken Reply To" t) "\
127 Ignore `Reply-To' headers in this group.
128
129 That can be useful if you're reading a mailing list group where the
130 listserv has inserted `Reply-To' headers that point back to the
131 listserv itself.  This is broken behavior.  So there!")
132
133     (to-group (string :tag "To Group") "\
134 All posts will be sent to the specified group.")
135
136     (gcc-self (choice :tag  "GCC"
137                       :value t
138                       (const :tag "To current group" t)
139                       (const none)
140                       (string :format "%v" :hide-front-space t)) "\
141 Specify default value for GCC header.
142
143 If this symbol is present in the group parameter list and set to t,
144 new composed messages will be `Gcc''d to the current group.  If it is
145 present and set to `none', no `Gcc:' header will be generated, if it
146 is present and a string, this string will be inserted literally as a
147 `gcc' header (this symbol takes precedence over any default `Gcc'
148 rules as described later).")
149
150     (expiry-wait (choice :tag  "Expire Wait"
151                          :value never
152                          (const never)
153                          (const immediate)
154                          (number :hide-front-space t
155                                  :format "%v")) "\
156 When to expire.
157
158 Overrides any `nnmail-expiry-wait' and `nnmail-expiry-wait-function'
159 when expiring expirable messages.  The value can either be a number of
160 days (not necessarily an integer) or the symbols `never' or
161 `immediate'.")
162
163     (expiry-target (choice :tag "Expiry Target"
164                            :value delete
165                            (const delete)
166                            (function :format "%v" nnmail-)
167                            string) "\
168 Where expired messages end up.
169
170 Overrides `nnmail-expiry-target'.")
171
172     (score-file (file :tag "Score File") "\
173 Make the specified file into the current score file.
174 This means that all score commands you issue will end up in this file.")
175
176     (adapt-file (file :tag "Adapt File") "\
177 Make the specified file into the current adaptive file.
178 All adaptive score entries will be put into this file.")
179
180     (admin-address (gnus-email-address :tag "Admin Address") "\
181 Administration address for a mailing list.
182
183 When unsubscribing to a mailing list you should never send the
184 unsubscription notice to the mailing list itself.  Instead, you'd
185 send messages to the administrative address.  This parameter allows
186 you to put the admin address somewhere convenient.")
187
188     (display (choice :tag "Display"
189                      :value default
190                      (const all)
191                      (integer)
192                      (const default)
193                      (sexp  :tag "Other")) "\
194 Which articles to display on entering the group.
195
196 `all'
197      Display all articles, both read and unread.
198
199 `integer'
200      Display the last NUMBER articles in the group.  This is the same as
201      entering the group with C-u NUMBER.
202
203 `default'
204      Display the default visible articles, which normally includes
205      unread and ticked articles.
206
207 `Other'
208      Display the articles that satisfy the S-expression. The S-expression
209      should be in an array form.")
210
211     (comment (string :tag  "Comment") "\
212 An arbitrary comment on the group.")
213
214     (visible (const :tag "Permanently visible" t) "\
215 Always display this group, even when there are no unread articles in it.")
216
217     (highlight-words
218      (choice :tag "Highlight words"
219              :value nil
220              (repeat (list (regexp :tag "Highlight regexp")
221                            (number :tag "Group for entire word" 0)
222                            (number :tag "Group for displayed part" 0)
223                            (symbol :tag "Face"
224                                    gnus-emphasis-highlight-words))))
225      "highlight regexps.
226 See `gnus-emphasis-alist'.")
227
228     (posting-style
229      (choice :tag "Posting style"
230              :value nil
231              (repeat (list
232                       (choice :tag "Type"
233                               :value nil
234                               (const signature)
235                               (const signature-file)
236                               (const organization)
237                               (const address)
238                               (const name)
239                               (const body)
240                               (const import))
241                       (string :format "%v"))))
242      "post style.
243 See `gnus-posting-styles'."))
244   "Alist of valid group or topic parameters.
245
246 Each entry has the form (NAME TYPE DOC), where NAME is the parameter
247 itself (a symbol), TYPE is the parameters type (a sexp widget), and
248 DOC is a documentation string for the parameter.")
249
250 (defconst gnus-extra-topic-parameters
251   '((subscribe (regexp :tag "Subscribe") "\
252 If `gnus-subscribe-newsgroup-method' or
253 `gnus-subscribe-options-newsgroup-method' is set to
254 `gnus-subscribe-topics', new groups that matches this regexp will
255 automatically be subscribed to this topic")
256     (subscribe-level (integer :tag "Subscribe Level" :value 1) "\
257 If this topic parameter is set, when new groups are subscribed
258 automatically under this topic (via the `subscribe' topic parameter)
259 assign this level to the group, rather than the default level
260 set in `gnus-level-default-subscribed'"))
261   "Alist of topic parameters that are not also group parameters.
262
263 Each entry has the form (NAME TYPE DOC), where NAME is the parameter
264 itself (a symbol), TYPE is the parameters type (a sexp widget), and
265 DOC is a documentation string for the parameter.")
266
267 (defconst gnus-extra-group-parameters
268   '((uidvalidity (string :tag "IMAP uidvalidity") "\
269 Server-assigned value attached to IMAP groups, used to maintain consistency."))
270   "Alist of group parameters that are not also topic parameters.
271
272 Each entry has the form (NAME TYPE DOC), where NAME is the parameter
273 itself (a symbol), TYPE is the parameters type (a sexp widget), and
274 DOC is a documentation string for the parameter.")
275
276 (eval-and-compile
277   (defconst gnus-agent-parameters
278     '((agent-predicate
279        (sexp :tag "Selection Predicate" :value false)
280        "Predicate used to automatically select articles for downloading."
281        gnus-agent-cat-predicate)
282       (agent-score
283        (choice :tag "Score File" :value nil
284                (const file :tag "Use group's score files")
285                (repeat (list (string :format "%v" :tag "File name"))))
286        "Which score files to use when using score to select articles to fetch.
287
288     `nil'
289          All articles will be scored to zero (0).
290
291     `file'
292          The group's score files will be used to score the articles.
293
294     `List'
295          A list of score file names."
296        gnus-agent-cat-score-file)
297       (agent-short-article
298        (integer :tag "Max Length of Short Article" :value "")
299        "The SHORT predicate will evaluate to true when the article is
300 shorter than this length."  gnus-agent-cat-length-when-short)
301       (agent-long-article
302        (integer :tag "Min Length of Long Article" :value "")
303        "The LONG predicate will evaluate to true when the article is
304 longer than this length."  gnus-agent-cat-length-when-long)
305       (agent-low-score
306        (integer :tag "Low Score Limit" :value "")
307        "The LOW predicate will evaluate to true when the article scores
308 lower than this limit."  gnus-agent-cat-low-score)
309       (agent-high-score
310        (integer :tag "High Score Limit" :value "")
311        "The HIGH predicate will evaluate to true when the article scores
312 higher than this limit."  gnus-agent-cat-high-score)
313       (agent-days-until-old
314        (integer :tag "Days Until Old" :value "")
315        "The OLD predicate will evaluate to true when the fetched article
316 has been stored locally for at least this many days."
317        gnus-agent-cat-days-until-old)
318       (agent-enable-expiration
319        (radio :tag "Expire in this Group or Topic" :value nil
320               (const :format "Enable " ENABLE)
321               (const :format "Disable " DISABLE))
322        "\nEnable, or disable, agent expiration in this group or topic."
323        gnus-agent-cat-enable-expiration)
324       (agent-enable-undownloaded-faces
325        (boolean :tag "Enable Agent Faces")
326        "Have the summary buffer use the agent's undownloaded faces.
327 These faces, when enabled, act as a warning that an article has not
328 been fetched into either the agent nor the cache.  This is of most use
329 to users who use the agent as a cache (i.e. they only operate on
330 articles that have been downloaded).  Leave disabled to display normal
331 article faces even when the article hasn't been downloaded."
332 gnus-agent-cat-enable-undownloaded-faces))
333     "Alist of group parameters that are not also topic parameters.
334
335 Each entry has the form (NAME TYPE DOC ACCESSOR), where NAME is the
336 parameter itself (a symbol), TYPE is the parameters type (a sexp
337 widget), DOC is a documentation string for the parameter, and ACCESSOR
338 is a function (symbol) that extracts the current value from the
339 category."))
340
341 (defvar gnus-custom-params)
342 (defvar gnus-custom-method)
343 (defvar gnus-custom-group)
344 (defvar gnus-custom-topic)
345
346 (defun gnus-group-customize (group &optional topic)
347   "Edit the group or topic on the current line."
348   (interactive (list (gnus-group-group-name) (gnus-group-topic-name)))
349   (let (info
350         (types (mapcar (lambda (entry)
351                          `(cons :format "%v%h\n"
352                                 :doc ,(nth 2 entry)
353                                 (const :format "" ,(nth 0 entry))
354                                 ,(nth 1 entry)))
355                        (append (reverse gnus-group-parameters-more)
356                                gnus-group-parameters
357                                (if group
358                                    gnus-extra-group-parameters
359                                  gnus-extra-topic-parameters))))
360         (agent (mapcar (lambda (entry)
361                          (let ((type (nth 1 entry))
362                                vcons)
363                            (if (listp type)
364                                (setq type (copy-sequence type)))
365
366                            (setq vcons (cdr (memq :value type)))
367
368                            (if (symbolp (car vcons))
369                                (condition-case nil
370                                    (setcar vcons (symbol-value (car vcons)))
371                                  (error)))
372                            `(cons :format "%v%h\n"
373                                   :doc ,(nth 2 entry)
374                                   (const :format "" ,(nth 0 entry))
375                                   ,type)))
376                        (if gnus-agent
377                            gnus-agent-parameters))))
378     (unless (or group topic)
379       (error "No group on current line"))
380     (when (and group topic)
381       (error "Both a group an topic on current line"))
382     (unless (or topic (setq info (gnus-get-info group)))
383       (error "Killed group; can't be edited"))
384     ;; Ready.
385     (gnus-kill-buffer (gnus-get-buffer-create "*Gnus Customize*"))
386     (switch-to-buffer (gnus-get-buffer-create "*Gnus Customize*"))
387     (gnus-custom-mode)
388     (make-local-variable 'gnus-custom-group)
389     (setq gnus-custom-group group)
390     (make-local-variable 'gnus-custom-topic)
391     (setq gnus-custom-topic topic)
392     (buffer-disable-undo)
393     (widget-insert "Customize the ")
394     (if group
395         (widget-create 'info-link
396                        :help-echo "Push me to learn more."
397                        :tag "group parameters"
398                        "(gnus)Group Parameters")
399       (widget-create 'info-link
400                      :help-echo "Push me to learn more."
401                      :tag  "topic parameters"
402                      "(gnus)Topic Parameters"))
403     (widget-insert " for <")
404     (widget-insert (gnus-group-decoded-name (or group topic)))
405     (widget-insert "> and press ")
406     (widget-create 'push-button
407                    :tag "done"
408                    :help-echo "Push me when done customizing."
409                    :action 'gnus-group-customize-done)
410     (widget-insert ".\n\n")
411     (make-local-variable 'gnus-custom-params)
412
413     (let ((values (if group
414                       (gnus-info-params info)
415                     (gnus-topic-parameters topic))))
416
417       ;; The parameters in values may contain duplicates.  This is
418       ;; normally OK as assq returns the first. However, right here
419       ;; every duplicate ends up being displayed.  So, rather than
420       ;; display them, remove them from the list.
421
422       (let ((tmp (setq values (gnus-copy-sequence values)))
423             elem)
424         (while (cdr tmp)
425           (while (setq elem (assq (caar tmp) (cdr tmp)))
426             (delq elem tmp))
427           (setq tmp (cdr tmp))))
428
429       (setq gnus-custom-params
430             (apply 'widget-create 'group
431                    :value values
432                    (delq nil
433                          (list `(set :inline t
434                                      :greedy t
435                                      :tag "Parameters"
436                                      :format "%t:\n%h%v"
437                                      :doc "\
438 These special parameters are recognized by Gnus.
439 Check the [ ] for the parameters you want to apply to this group or
440 to the groups in this topic, then edit the value to suit your taste."
441                                      ,@types)
442                                (when gnus-agent
443                                  `(set :inline t
444                                        :greedy t
445                                        :tag "Agent Parameters"
446                                        :format "%t:\n%h%v"
447                                        :doc "\ These agent parameters are
448 recognized by Gnus.  They control article selection and expiration for
449 use in the unplugged cache.  Check the [ ] for the parameters you want
450 to apply to this group or to the groups in this topic, then edit the
451 value to suit your taste.
452
453 For those interested, group parameters override topic parameters while
454 topic parameters override agent category parameters.  Underlying
455 category parameters are the customizable variables."  ,@agent))
456                                '(repeat :inline t
457                                         :tag "Variables"
458                                         :format "%t:\n%h%v%i\n\n"
459                                         :doc "\
460 Set variables local to the group you are entering.
461
462 If you want to turn threading off in `news.answers', you could put
463 `(gnus-show-threads nil)' in the group parameters of that group.
464 `gnus-show-threads' will be made into a local variable in the summary
465 buffer you enter, and the form nil will be `eval'ed there.
466
467 This can also be used as a group-specific hook function, if you'd
468 like.  If you want to hear a beep when you enter a group, you could
469 put something like `(dummy-variable (ding))' in the parameters of that
470 group.  `dummy-variable' will be set to the result of the `(ding)'
471 form, but who cares?"
472                                         (list :format "%v" :value (nil nil)
473                                               (symbol :tag "Variable")
474                                               (sexp :tag
475                                                     "Value")))
476
477                                '(repeat :inline t
478                                         :tag "Unknown entries"
479                                         sexp))))))
480     (when group
481       (widget-insert "\n\nYou can also edit the ")
482       (widget-create 'info-link
483                      :tag "select method"
484                      :help-echo "Push me to learn more about select methods."
485                      "(gnus)Select Methods")
486       (widget-insert " for the group.\n")
487       (setq gnus-custom-method
488             (widget-create 'sexp
489                            :tag "Method"
490                            :value (gnus-info-method info))))
491     (use-local-map gnus-custom-map)
492     (widget-setup)
493     (buffer-enable-undo)
494     (goto-char (point-min))))
495
496 (defun gnus-group-customize-done (&rest ignore)
497   "Apply changes and bury the buffer."
498   (interactive)
499   (if gnus-custom-topic
500       (gnus-topic-set-parameters gnus-custom-topic
501                                  (widget-value gnus-custom-params))
502     (gnus-group-edit-group-done 'params gnus-custom-group
503                                 (widget-value gnus-custom-params))
504     (gnus-group-edit-group-done 'method gnus-custom-group
505                                 (widget-value gnus-custom-method)))
506   (bury-buffer))
507
508 ;;; Score Customization:
509
510 (defconst gnus-score-parameters
511   '((mark (number :tag "Mark") "\
512 The value of this entry should be a number.
513 Any articles with a score lower than this number will be marked as read.")
514
515     (expunge (number :tag "Expunge") "\
516 The value of this entry should be a number.
517 Any articles with a score lower than this number will be removed from
518 the summary buffer.")
519
520     (mark-and-expunge (number :tag "Mark-and-expunge") "\
521 The value of this entry should be a number.
522 Any articles with a score lower than this number will be marked as
523 read and removed from the summary buffer.")
524
525     (thread-mark-and-expunge (number :tag "Thread-mark-and-expunge") "\
526 The value of this entry should be a number.
527 All articles that belong to a thread that has a total score below this
528 number will be marked as read and removed from the summary buffer.
529 `gnus-thread-score-function' says how to compute the total score
530 for a thread.")
531
532     (files (repeat :inline t :tag "Files" file) "\
533 The value of this entry should be any number of file names.
534 These files are assumed to be score files as well, and will be loaded
535 the same way this one was.")
536
537     (exclude-files (repeat :inline t :tag "Exclude-files" file) "\
538 The clue of this entry should be any number of files.
539 These files will not be loaded, even though they would normally be so,
540 for some reason or other.")
541
542     (eval (sexp :tag "Eval" :value nil) "\
543 The value of this entry will be `eval'el.
544 This element will be ignored when handling global score files.")
545
546     (read-only (boolean :tag "Read-only" :value t) "\
547 Read-only score files will not be updated or saved.
548 Global score files should feature this atom.")
549
550     (orphan (number :tag "Orphan") "\
551 The value of this entry should be a number.
552 Articles that do not have parents will get this number added to their
553 scores.  Imagine you follow some high-volume newsgroup, like
554 `comp.lang.c'.  Most likely you will only follow a few of the threads,
555 also want to see any new threads.
556
557 You can do this with the following two score file entries:
558
559      (orphan -500)
560      (mark-and-expunge -100)
561
562 When you enter the group the first time, you will only see the new
563 threads.  You then raise the score of the threads that you find
564 interesting (with `I T' or `I S'), and ignore (`C y') the rest.
565 Next time you enter the group, you will see new articles in the
566 interesting threads, plus any new threads.
567
568 I.e.---the orphan score atom is for high-volume groups where there
569 exist a few interesting threads which can't be found automatically
570 by ordinary scoring rules.")
571
572     (adapt (choice :tag "Adapt"
573                    (const t)
574                    (const ignore)
575                    (sexp :format "%v"
576                          :hide-front-space t)) "\
577 This entry controls the adaptive scoring.
578 If it is t, the default adaptive scoring rules will be used.  If it
579 is `ignore', no adaptive scoring will be performed on this group.  If
580 it is a list, this list will be used as the adaptive scoring rules.
581 If it isn't present, or is something other than t or `ignore', the
582 default adaptive scoring rules will be used.  If you want to use
583 adaptive scoring on most groups, you'd set `gnus-use-adaptive-scoring'
584 to t, and insert an `(adapt ignore)' in the groups where you do not
585 want adaptive scoring.  If you only want adaptive scoring in a few
586 groups, you'd set `gnus-use-adaptive-scoring' to nil, and insert
587 `(adapt t)' in the score files of the groups where you want it.")
588
589     (adapt-file (file :tag "Adapt-file") "\
590 All adaptive score entries will go to the file named by this entry.
591 It will also be applied when entering the group.  This atom might
592 be handy if you want to adapt on several groups at once, using the
593 same adaptive file for a number of groups.")
594
595     (local (repeat :tag "Local"
596                    (group :value (nil nil)
597                           (symbol :tag "Variable")
598                           (sexp :tag "Value"))) "\
599 The value of this entry should be a list of `(VAR VALUE)' pairs.
600 Each VAR will be made buffer-local to the current summary buffer,
601 and set to the value specified.  This is a convenient, if somewhat
602 strange, way of setting variables in some groups if you don't like
603 hooks much.")
604     (touched (sexp :format "Touched\n") "Internal variable."))
605   "Alist of valid symbolic score parameters.
606
607 Each entry has the form (NAME TYPE DOC), where NAME is the parameter
608 itself (a symbol), TYPE is the parameters type (a sexp widget), and DOC is a
609 documentation string for the parameter.")
610
611 (define-widget 'gnus-score-string 'group
612   "Edit score entries for string-valued headers."
613   :convert-widget 'gnus-score-string-convert)
614
615 (defun gnus-score-string-convert (widget)
616   ;; Set args appropriately.
617   (let* ((tag (widget-get widget :tag))
618          (item `(const :format "" :value ,(downcase tag)))
619          (match '(string :tag "Match"))
620          (score '(choice :tag "Score"
621                          (const :tag "default" nil)
622                          (integer :format "%v"
623                                   :hide-front-space t)))
624          (expire '(choice :tag "Expire"
625                           (const :tag "off" nil)
626                           (integer :format "%v"
627                                    :hide-front-space t)))
628          (type '(choice :tag "Type"
629                         :value s
630                         ;; I should really create a forgiving :match
631                         ;; function for each type below, that only
632                         ;; looked at the first letter.
633                         (const :tag "Regexp" r)
634                         (const :tag "Regexp (fixed case)" R)
635                         (const :tag "Substring" s)
636                         (const :tag "Substring (fixed case)" S)
637                         (const :tag "Exact" e)
638                         (const :tag "Exact (fixed case)" E)
639                         (const :tag "Word" w)
640                         (const :tag "Word (fixed case)" W)
641                         (const :tag "default" nil)))
642          (group `(group ,match ,score ,expire ,type))
643          (doc (concat (or (widget-get widget :doc)
644                           (concat "Change score based on the " tag
645                                   " header.\n"))
646                       "
647 You can have an arbitrary number of score entries for this header,
648 each score entry has four elements:
649
650 1. The \"match element\".  This should be the string to look for in the
651    header.
652
653 2. The \"score element\".  This number should be an integer in the
654    neginf to posinf interval.  This number is added to the score
655    of the article if the match is successful.  If this element is
656    not present, the `gnus-score-interactive-default-score' number
657    will be used instead.  This is 1000 by default.
658
659 3. The \"date element\".  This date says when the last time this score
660    entry matched, which provides a mechanism for expiring the
661    score entries.  It this element is not present, the score
662    entry is permanent.  The date is represented by the number of
663    days since December 31, 1 ce.
664
665 4. The \"type element\".  This element specifies what function should
666    be used to see whether this score entry matches the article.
667
668    There are the regexp, as well as substring types, and exact match,
669    and word match types.  If this element is not present, Gnus will
670    assume that substring matching should be used.  There is case
671    sensitive variants of all match types.")))
672     (widget-put widget :args `(,item
673                                (repeat :inline t
674                                        :indent 0
675                                        :tag ,tag
676                                        :doc ,doc
677                                        :format "%t:\n%h%v%i\n\n"
678                                        (choice :format "%v"
679                                                :value ("" nil nil s)
680                                                ,group
681                                                sexp)))))
682   widget)
683
684 (define-widget 'gnus-score-integer 'group
685   "Edit score entries for integer-valued headers."
686   :convert-widget 'gnus-score-integer-convert)
687
688 (defun gnus-score-integer-convert (widget)
689   ;; Set args appropriately.
690   (let* ((tag (widget-get widget :tag))
691          (item `(const :format "" :value ,(downcase tag)))
692          (match '(integer :tag "Match"))
693          (score '(choice :tag "Score"
694                          (const :tag "default" nil)
695                          (integer :format "%v"
696                                   :hide-front-space t)))
697          (expire '(choice :tag "Expire"
698                           (const :tag "off" nil)
699                           (integer :format "%v"
700                                    :hide-front-space t)))
701          (type '(choice :tag "Type"
702                         :value <
703                         (const <)
704                         (const >)
705                         (const =)
706                         (const >=)
707                         (const <=)))
708          (group `(group ,match ,score ,expire ,type))
709          (doc (concat (or (widget-get widget :doc)
710                           (concat "Change score based on the " tag
711                                   " header.")))))
712     (widget-put widget :args `(,item
713                                (repeat :inline t
714                                        :indent 0
715                                        :tag ,tag
716                                        :doc ,doc
717                                        :format "%t:\n%h%v%i\n\n"
718                                        ,group))))
719   widget)
720
721 (define-widget 'gnus-score-date 'group
722   "Edit score entries for date-valued headers."
723   :convert-widget 'gnus-score-date-convert)
724
725 (defun gnus-score-date-convert (widget)
726   ;; Set args appropriately.
727   (let* ((tag (widget-get widget :tag))
728          (item `(const :format "" :value ,(downcase tag)))
729          (match '(string :tag "Match"))
730          (score '(choice :tag "Score"
731                          (const :tag "default" nil)
732                          (integer :format "%v"
733                                   :hide-front-space t)))
734          (expire '(choice :tag "Expire"
735                           (const :tag "off" nil)
736                           (integer :format "%v"
737                                    :hide-front-space t)))
738          (type '(choice :tag "Type"
739                         :value regexp
740                         (const regexp)
741                         (const before)
742                         (const at)
743                         (const after)))
744          (group `(group ,match ,score ,expire ,type))
745          (doc (concat (or (widget-get widget :doc)
746                           (concat "Change score based on the " tag
747                                   " header."))
748                       "
749 For the Date header we have three kinda silly match types: `before',
750 `at' and `after'.  I can't really imagine this ever being useful, but,
751 like, it would feel kinda silly not to provide this function.  Just in
752 case.  You never know.  Better safe than sorry.  Once burnt, twice
753 shy.  Don't judge a book by its cover.  Never not have sex on a first
754 date.  (I have been told that at least one person, and I quote,
755 \"found this function indispensable\", however.)
756
757 A more useful match type is `regexp'.  With it, you can match the date
758 string using a regular expression.  The date is normalized to ISO8601
759 compact format first---`YYYYMMDDTHHMMSS'.  If you want to match all
760 articles that have been posted on April 1st in every year, you could
761 use `....0401.........' as a match string, for instance.  (Note that
762 the date is kept in its original time zone, so this will match
763 articles that were posted when it was April 1st where the article was
764 posted from.  Time zones are such wholesome fun for the whole family,
765 eh?")))
766     (widget-put widget :args `(,item
767                                (repeat :inline t
768                                        :indent 0
769                                        :tag ,tag
770                                        :doc ,doc
771                                        :format "%t:\n%h%v%i\n\n"
772                                        ,group))))
773   widget)
774
775 (defvar gnus-custom-scores)
776 (defvar gnus-custom-score-alist)
777
778 (defun gnus-score-customize (file)
779   "Customize score file FILE.
780 When called interactively, FILE defaults to the current score file.
781 This can be changed using the `\\[gnus-score-change-score-file]' command."
782   (interactive (list gnus-current-score-file))
783   (unless file
784     (error "No score file for %s"
785            (gnus-group-decoded-name gnus-newsgroup-name)))
786   (let ((scores (gnus-score-load file))
787         (types (mapcar (lambda (entry)
788                          `(group :format "%v%h\n"
789                                  :doc ,(nth 2 entry)
790                                  (const :format "" ,(nth 0 entry))
791                                  ,(nth 1 entry)))
792                        gnus-score-parameters)))
793     ;; Ready.
794     (kill-buffer (gnus-get-buffer-create "*Gnus Customize*"))
795     (switch-to-buffer (gnus-get-buffer-create "*Gnus Customize*"))
796     (gnus-custom-mode)
797     (make-local-variable 'gnus-custom-score-alist)
798     (setq gnus-custom-score-alist scores)
799     (widget-insert "Customize the ")
800     (widget-create 'info-link
801                    :help-echo "Push me to learn more."
802                    :tag "score entries"
803                    "(gnus)Score File Format")
804     (widget-insert " for\n\t")
805     (widget-insert file)
806     (widget-insert "\nand press ")
807     (widget-create 'push-button
808                    :tag "done"
809                    :help-echo "Push me when done customizing."
810                    :action 'gnus-score-customize-done)
811     (widget-insert ".\n
812 Check the [ ] for the entries you want to apply to this score file, then
813 edit the value to suit your taste.  Don't forget to mark the checkbox,
814 if you do all your changes will be lost.  ")
815     (widget-create 'push-button
816                    :action (lambda (&rest ignore)
817                              (require 'gnus-audio)
818                              (gnus-audio-play "Evil_Laugh.au"))
819                    "Bhahahah!")
820     (widget-insert "\n\n")
821     (make-local-variable 'gnus-custom-scores)
822     (setq gnus-custom-scores
823           (widget-create 'group
824                          :value scores
825                          `(checklist :inline t
826                                      :greedy t
827                                      (gnus-score-string :tag "From")
828                                      (gnus-score-string :tag "Subject")
829                                      (gnus-score-string :tag "References")
830                                      (gnus-score-string :tag "Xref")
831                                      (gnus-score-string :tag "Extra")
832                                      (gnus-score-string :tag "Message-ID")
833                                      (gnus-score-integer :tag "Lines")
834                                      (gnus-score-integer :tag "Chars")
835                                      (gnus-score-date :tag "Date")
836                                      (gnus-score-string :tag "Head"
837                                                         :doc "\
838 Match all headers in the article.
839
840 Using one of `Head', `Body', `All' will slow down scoring considerable.
841 ")
842                                      (gnus-score-string :tag "Body"
843                                                         :doc "\
844 Match the body sans header of the article.
845
846 Using one of `Head', `Body', `All' will slow down scoring considerable.
847 ")
848                                      (gnus-score-string :tag "All"
849                                                         :doc "\
850 Match the entire article, including both headers and body.
851
852 Using one of `Head', `Body', `All' will slow down scoring
853 considerable.
854 ")
855                                      (gnus-score-string :tag
856                                                         "Followup"
857                                                         :doc "\
858 Score all followups to the specified authors.
859
860 This entry is somewhat special, in that it will match the `From:'
861 header, and affect the score of not only the matching articles, but
862 also all followups to the matching articles.  This allows you
863 e.g. increase the score of followups to your own articles, or decrease
864 the score of followups to the articles of some known trouble-maker.
865 ")
866                                      (gnus-score-string :tag "Thread"
867                                                         :doc "\
868 Add a score entry on all articles that are part of a thread.
869
870 This match key works along the same lines as the `Followup' match key.
871 If you say that you want to score on a (sub-)thread that is started by
872 an article with a `Message-ID' X, then you add a `thread' match.  This
873 will add a new `thread' match for each article that has X in its
874 `References' header.  (These new `thread' matches will use the
875 `Message-ID's of these matching articles.)  This will ensure that you
876 can raise/lower the score of an entire thread, even though some
877 articles in the thread may not have complete `References' headers.
878 Note that using this may lead to undeterministic scores of the
879 articles in the thread.
880 ")
881                                      ,@types)
882                          '(repeat :inline t
883                                   :tag "Unknown entries"
884                                   sexp)))
885     (use-local-map gnus-custom-map)
886     (widget-setup)))
887
888 (defun gnus-score-customize-done (&rest ignore)
889   "Reset the score alist with the present value."
890   (let ((alist gnus-custom-score-alist)
891         (value (widget-value gnus-custom-scores)))
892     (setcar alist (car value))
893     (setcdr alist (cdr value))
894     (gnus-score-set 'touched '(t) alist))
895   (bury-buffer))
896
897 (eval-when-compile
898   (defvar category-fields nil)
899   (defvar gnus-agent-cat-name)
900   (defvar gnus-agent-cat-score-file)
901   (defvar gnus-agent-cat-length-when-short)
902   (defvar gnus-agent-cat-length-when-long)
903   (defvar gnus-agent-cat-low-score)
904   (defvar gnus-agent-cat-high-score)
905   (defvar gnus-agent-cat-enable-expiration)
906   (defvar gnus-agent-cat-days-until-old)
907   (defvar gnus-agent-cat-predicate)
908   (defvar gnus-agent-cat-groups)
909   (defvar gnus-agent-cat-enable-undownloaded-faces)
910 )
911
912 (defun gnus-trim-whitespace (s)
913   (when (string-match "\\`[ \n\t]+" s)
914     (setq s (substring s (match-end 0))))
915   (when (string-match "[ \n\t]+\\'" s)
916     (setq s (substring s 0 (match-beginning 0))))
917   s)
918
919 (defmacro gnus-agent-cat-prepare-category-field (parameter)
920   (let* ((entry (assq parameter gnus-agent-parameters))
921          (field (nth 3 entry)))
922     `(let* ((type (copy-sequence
923                    (nth 1 (assq ',parameter gnus-agent-parameters))))
924             (val (,field info))
925             (deflt (if (,field defaults)
926                        (concat " [" (gnus-trim-whitespace
927                                      (gnus-pp-to-string (,field defaults)))
928                                "]")))
929             symb)
930
931        (if (eq (car type) 'radio)
932            (let* ((rtype (nreverse type))
933                   (rt rtype))
934              (while (listp (or (cadr rt) 'not-list))
935                (setq rt (cdr rt)))
936
937              (setcdr rt (cons '(const :format "Inherit " nil) (cdr rt)))
938              (setq type (nreverse rtype))))
939
940        (if deflt
941            (let ((tag (cdr (memq :tag type))))
942              (when (string-match "\n" deflt)
943                (while (progn (setq deflt (replace-match "\n " t t
944                                                         deflt))
945                              (string-match "\n" deflt (match-end 0))))
946                (setq deflt (concat "\n" deflt)))
947
948              (setcar tag (concat (car tag) deflt))))
949
950        (widget-insert "\n")
951
952        (setq val (if val
953                      (widget-create type :value val)
954                    (widget-create type))
955              symb (set (make-local-variable ',field) val))
956
957        (widget-put symb :default val)
958        (widget-put symb :accessor ',field)
959        (push symb category-fields))))
960
961 (defun gnus-agent-customize-category (category)
962   "Edit the CATEGORY."
963   (interactive (list (gnus-category-name)))
964   (let ((info (assq category gnus-category-alist))
965         (defaults (list nil '(agent-predicate . false)
966                         (cons 'agent-enable-expiration
967                               gnus-agent-enable-expiration)
968                         '(agent-days-until-old . 7)
969                         (cons 'agent-length-when-short
970                               gnus-agent-short-article)
971                         (cons 'agent-length-when-long gnus-agent-long-article)
972                         (cons 'agent-low-score gnus-agent-low-score)
973                         (cons 'agent-high-score gnus-agent-high-score))))
974
975     (let ((old (get-buffer "*Gnus Agent Category Customize*")))
976       (when old
977         (gnus-kill-buffer old)))
978     (switch-to-buffer (gnus-get-buffer-create
979                        "*Gnus Agent Category Customize*"))
980
981     (let ((inhibit-read-only t))
982       (gnus-custom-mode)
983       (buffer-disable-undo)
984
985       (let* ((name (gnus-agent-cat-name info)))
986         (widget-insert "Customize the Agent Category '")
987         (widget-insert (symbol-name name))
988         (widget-insert "' and press ")
989         (widget-create
990          'push-button
991          :notify
992          '(lambda (&rest ignore)
993             (let* ((info (assq gnus-agent-cat-name gnus-category-alist))
994                    (widgets category-fields))
995               (while widgets
996                 (let* ((widget (pop widgets))
997                        (value (condition-case nil (widget-value widget) (error))))
998                   (eval `(setf (,(widget-get widget :accessor) ',info)
999                                ',value)))))
1000             (gnus-category-write)
1001             (gnus-kill-buffer (current-buffer))
1002             (when (get-buffer gnus-category-buffer)
1003               (switch-to-buffer (get-buffer gnus-category-buffer))
1004               (gnus-category-list)))
1005                        "Done")
1006         (widget-insert
1007          "\n    Note: Empty fields default to the customizable global\
1008  variables.\n\n")
1009
1010         (set (make-local-variable 'gnus-agent-cat-name)
1011              name))
1012
1013       (set (make-local-variable 'category-fields) nil)
1014       (gnus-agent-cat-prepare-category-field agent-predicate)
1015
1016       (gnus-agent-cat-prepare-category-field agent-score)
1017       (gnus-agent-cat-prepare-category-field agent-short-article)
1018       (gnus-agent-cat-prepare-category-field agent-long-article)
1019       (gnus-agent-cat-prepare-category-field agent-low-score)
1020       (gnus-agent-cat-prepare-category-field agent-high-score)
1021
1022       ;; The group list is NOT handled with
1023       ;; gnus-agent-cat-prepare-category-field as I don't want the
1024       ;; group list to appear when customizing a topic.
1025       (widget-insert "\n")
1026
1027       (let ((symb
1028              (set
1029               (make-local-variable 'gnus-agent-cat-groups)
1030               (widget-create
1031                `(choice
1032                  :format "%[Select Member Groups%]\n%v" :value ignore
1033                  (const :menu-tag "do not change" :tag "" :value ignore)
1034                  (checklist :entry-format "%b %v"
1035                             :menu-tag "display group selectors"
1036                             :greedy t
1037                             :value
1038                             ,(delq nil
1039                                    (mapcar
1040                                     (lambda (newsrc)
1041                                       (car (member
1042                                             (gnus-info-group newsrc)
1043                                             (gnus-agent-cat-groups info))))
1044                                     (cdr gnus-newsrc-alist)))
1045                             ,@(mapcar (lambda (newsrc)
1046                                         `(const ,(gnus-info-group newsrc)))
1047                                       (cdr gnus-newsrc-alist))))))))
1048
1049       (widget-put symb :default (gnus-agent-cat-groups info))
1050       (widget-put symb :accessor 'gnus-agent-cat-groups)
1051       (push symb category-fields))
1052
1053       (widget-insert "\nExpiration Settings ")
1054
1055       (gnus-agent-cat-prepare-category-field agent-enable-expiration)
1056       (gnus-agent-cat-prepare-category-field agent-days-until-old)
1057
1058       (widget-insert "\nVisual Settings ")
1059
1060       (gnus-agent-cat-prepare-category-field agent-enable-undownloaded-faces)
1061
1062       (use-local-map gnus-custom-map)
1063       (widget-setup)
1064       (buffer-enable-undo))))
1065
1066 ;;; The End:
1067
1068 (provide 'gnus-cus)
1069
1070 ;;; gnus-cus.el ends here