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