Import Oort Gnus v0.16.
[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', which see.")
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                       (string :format "%v"))))
234      "post style.
235 See `gnus-posting-styles'."))
236   "Alist of valid group or topic parameters.
237
238 Each entry has the form (NAME TYPE DOC), where NAME is the parameter
239 itself (a symbol), TYPE is the parameters type (a sexp widget), and
240 DOC is a documentation string for the parameter.")
241
242 (defconst gnus-extra-topic-parameters
243   '((subscribe (regexp :tag "Subscribe") "\
244 If `gnus-subscribe-newsgroup-method' or
245 `gnus-subscribe-options-newsgroup-method' is set to
246 `gnus-subscribe-topics', new groups that matches this regexp will
247 automatically be subscribed to this topic")
248     (subscribe-level (integer :tag "Subscribe Level" :value 1) "\
249 If this topic parameter is set, when new groups are subscribed
250 automatically under this topic (via the `subscribe' topic parameter)
251 assign this level to the group, rather than the default level
252 set in `gnus-level-default-subscribed'"))
253   "Alist of topic parameters that are not also group parameters.
254
255 Each entry has the form (NAME TYPE DOC), where NAME is the parameter
256 itself (a symbol), TYPE is the parameters type (a sexp widget), and
257 DOC is a documentation string for the parameter.")
258
259 (defconst gnus-extra-group-parameters
260   '((uidvalidity (string :tag "IMAP uidvalidity") "\
261 Server-assigned value attached to IMAP groups, used to maintain consistency."))
262   "Alist of group parameters that are not also topic parameters.
263
264 Each entry has the form (NAME TYPE DOC), where NAME is the parameter
265 itself (a symbol), TYPE is the parameters type (a sexp widget), and
266 DOC is a documentation string for the parameter.")
267
268 (eval-and-compile
269   (defconst gnus-agent-parameters
270     '((agent-predicate
271        (sexp :tag "Selection Predicate" :value false)
272        "Predicate used to automatically select articles for downloading."
273        gnus-agent-cat-predicate)
274       (agent-score
275        (choice :tag "Score File" :value nil
276                (const file :tag "Use group's score files")
277                (repeat (list (string :format "%v" :tag "File name"))))
278        "Which score files to use when using score to select articles to fetch.
279
280     `nil'
281          All articles will be scored to zero (0).
282
283     `file'
284          The group's score files will be used to score the articles.
285
286     `List'
287          A list of score file names."
288        gnus-agent-cat-score-file)
289       (agent-short-article
290        (integer :tag "Max Length of Short Article" :value "")
291        "The SHORT predicate will evaluate to true when the article is
292 shorter than this length."  gnus-agent-cat-length-when-short)
293       (agent-long-article
294        (integer :tag "Min Length of Long Article" :value "")
295        "The LONG predicate will evaluate to true when the article is
296 longer than this length."  gnus-agent-cat-length-when-long)
297       (agent-low-score
298        (integer :tag "Low Score Limit" :value "")
299        "The LOW predicate will evaluate to true when the article scores
300 lower than this limit."  gnus-agent-cat-low-score)
301       (agent-high-score
302        (integer :tag "High Score Limit" :value "")
303        "The HIGH predicate will evaluate to true when the article scores
304 higher than this limit."  gnus-agent-cat-high-score)
305       (agent-days-until-old
306        (integer :tag "Days Until Old" :value "")
307        "The OLD predicate will evaluate to true when the fetched article
308 has been stored locally for at least this many days."
309        gnus-agent-cat-days-until-old)
310       (agent-enable-expiration
311        (radio :tag "Expire in this Group or Topic" :value nil
312 ;              (const :format "Inherit " 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     "Alist of group parameters that are not also topic parameters.
318
319 Each entry has the form (NAME TYPE DOC), where NAME is the parameter
320 itself (a symbol), TYPE is the parameters type (a sexp widget), and
321 DOC is a documentation string for the parameter."))
322
323 (defvar gnus-custom-params)
324 (defvar gnus-custom-method)
325 (defvar gnus-custom-group)
326 (defvar gnus-custom-topic)
327
328 (defun gnus-group-customize (group &optional topic)
329   "Edit the group or topic on the current line."
330   (interactive (list (gnus-group-group-name) (gnus-group-topic-name)))
331   (let (info
332         (types (mapcar (lambda (entry)
333                          `(cons :format "%v%h\n"
334                                 :doc ,(nth 2 entry)
335                                 (const :format "" ,(nth 0 entry))
336                                 ,(nth 1 entry)))
337                        (append (reverse gnus-group-parameters-more)
338                                gnus-group-parameters
339                                (if group
340                                    gnus-extra-group-parameters
341                                  gnus-extra-topic-parameters))))
342         (agent (mapcar (lambda (entry)
343                          (let ((type (nth 1 entry))
344                                vcons)
345                            (if (listp type)
346                                (setq type (copy-sequence type)))
347
348                            (setq vcons (cdr (memq :value type)))
349
350                            (if (symbolp (car vcons))
351                                (condition-case nil
352                                    (setcar vcons (symbol-value (car vcons)))
353                                  (error)))
354                            `(cons :format "%v%h\n"
355                                   :doc ,(nth 2 entry)
356                                   (const :format "" ,(nth 0 entry))
357                                   ,type)))
358                        (if gnus-agent
359                            gnus-agent-parameters))))
360     (unless (or group topic)
361       (error "No group on current line"))
362     (when (and group topic)
363       (error "Both a group an topic on current line"))
364     (unless (or topic (setq info (gnus-get-info group)))
365       (error "Killed group; can't be edited"))
366     ;; Ready.
367     (gnus-kill-buffer (gnus-get-buffer-create "*Gnus Customize*"))
368     (switch-to-buffer (gnus-get-buffer-create "*Gnus Customize*"))
369     (gnus-custom-mode)
370     (make-local-variable 'gnus-custom-group)
371     (setq gnus-custom-group group)
372     (make-local-variable 'gnus-custom-topic)
373     (setq gnus-custom-topic topic)
374     (buffer-disable-undo)
375     (widget-insert "Customize the ")
376     (if group
377         (widget-create 'info-link
378                        :help-echo "Push me to learn more."
379                        :tag "group parameters"
380                        "(gnus)Group Parameters")
381       (widget-create 'info-link
382                      :help-echo "Push me to learn more."
383                      :tag  "topic parameters"
384                      "(gnus)Topic Parameters"))
385     (widget-insert " for <")
386     (widget-insert (gnus-group-decoded-name (or group topic)))
387     (widget-insert "> and press ")
388     (widget-create 'push-button
389                    :tag "done"
390                    :help-echo "Push me when done customizing."
391                    :action 'gnus-group-customize-done)
392     (widget-insert ".\n\n")
393     (make-local-variable 'gnus-custom-params)
394
395     (let ((values (if group
396                       (gnus-info-params info)
397                     (gnus-topic-parameters topic))))
398
399       ;; The parameters in values may contain duplicates.  This is
400       ;; normally OK as assq returns the first. However, right here
401       ;; every duplicate ends up being displayed.  So, rather than
402       ;; display them, remove them from the list.
403
404       (let ((tmp (setq values (gnus-copy-sequence values)))
405             elem)
406         (while (cdr tmp)
407           (while (setq elem (assq (caar tmp) (cdr tmp)))
408             (delq elem tmp))
409           (setq tmp (cdr tmp))))
410
411       (setq gnus-custom-params
412             (apply 'widget-create 'group
413                    :value values
414                    (delq nil
415                          (list `(set :inline t
416                                      :greedy t
417                                      :tag "Parameters"
418                                      :format "%t:\n%h%v"
419                                      :doc "\
420 These special parameters are recognized by Gnus.
421 Check the [ ] for the parameters you want to apply to this group or
422 to the groups in this topic, then edit the value to suit your taste."
423                                      ,@types)
424                                (when gnus-agent
425                                  `(set :inline t
426                                        :greedy t
427                                        :tag "Agent Parameters"
428                                        :format "%t:\n%h%v"
429                                        :doc "\ These agent parameters are
430 recognized by Gnus.  They control article selection and expiration for
431 use in the unplugged cache.  Check the [ ] for the parameters you want
432 to apply to this group or to the groups in this topic, then edit the
433 value to suit your taste.
434
435 For those interested, group parameters override topic parameters while
436 topic parameters override agent category parameters.  Underlying
437 category parameters are the customizable variables."  ,@agent))
438                                '(repeat :inline t
439                                         :tag "Variables"
440                                         :format "%t:\n%h%v%i\n\n"
441                                         :doc "\
442 Set variables local to the group you are entering.
443
444 If you want to turn threading off in `news.answers', you could put
445 `(gnus-show-threads nil)' in the group parameters of that group.
446 `gnus-show-threads' will be made into a local variable in the summary
447 buffer you enter, and the form nil will be `eval'ed there.
448
449 This can also be used as a group-specific hook function, if you'd
450 like.  If you want to hear a beep when you enter a group, you could
451 put something like `(dummy-variable (ding))' in the parameters of that
452 group.  `dummy-variable' will be set to the result of the `(ding)'
453 form, but who cares?"
454                                         (list :format "%v" :value (nil nil)
455                                               (symbol :tag "Variable")
456                                               (sexp :tag
457                                                     "Value")))
458
459                                '(repeat :inline t
460                                         :tag "Unknown entries"
461                                         sexp))))))
462     (when group
463       (widget-insert "\n\nYou can also edit the ")
464       (widget-create 'info-link
465                      :tag "select method"
466                      :help-echo "Push me to learn more about select methods."
467                      "(gnus)Select Methods")
468       (widget-insert " for the group.\n")
469       (setq gnus-custom-method
470             (widget-create 'sexp
471                            :tag "Method"
472                            :value (gnus-info-method info))))
473     (use-local-map widget-keymap)
474     (widget-setup)
475     (buffer-enable-undo)
476     (goto-char (point-min))))
477
478 (defun gnus-group-customize-done (&rest ignore)
479   "Apply changes and bury the buffer."
480   (interactive)
481   (if gnus-custom-topic
482       (gnus-topic-set-parameters gnus-custom-topic
483                                  (widget-value gnus-custom-params))
484     (gnus-group-edit-group-done 'params gnus-custom-group
485                                 (widget-value gnus-custom-params))
486     (gnus-group-edit-group-done 'method gnus-custom-group
487                                 (widget-value gnus-custom-method)))
488   (bury-buffer))
489
490 ;;; Score Customization:
491
492 (defconst gnus-score-parameters
493   '((mark (number :tag "Mark") "\
494 The value of this entry should be a number.
495 Any articles with a score lower than this number will be marked as read.")
496
497     (expunge (number :tag "Expunge") "\
498 The value of this entry should be a number.
499 Any articles with a score lower than this number will be removed from
500 the summary buffer.")
501
502     (mark-and-expunge (number :tag "Mark-and-expunge") "\
503 The value of this entry should be a number.
504 Any articles with a score lower than this number will be marked as
505 read and removed from the summary buffer.")
506
507     (thread-mark-and-expunge (number :tag "Thread-mark-and-expunge") "\
508 The value of this entry should be a number.
509 All articles that belong to a thread that has a total score below this
510 number will be marked as read and removed from the summary buffer.
511 `gnus-thread-score-function' says how to compute the total score
512 for a thread.")
513
514     (files (repeat :inline t :tag "Files" file) "\
515 The value of this entry should be any number of file names.
516 These files are assumed to be score files as well, and will be loaded
517 the same way this one was.")
518
519     (exclude-files (repeat :inline t :tag "Exclude-files" file) "\
520 The clue of this entry should be any number of files.
521 These files will not be loaded, even though they would normally be so,
522 for some reason or other.")
523
524     (eval (sexp :tag "Eval" :value nil) "\
525 The value of this entry will be `eval'el.
526 This element will be ignored when handling global score files.")
527
528     (read-only (boolean :tag "Read-only" :value t) "\
529 Read-only score files will not be updated or saved.
530 Global score files should feature this atom.")
531
532     (orphan (number :tag "Orphan") "\
533 The value of this entry should be a number.
534 Articles that do not have parents will get this number added to their
535 scores.  Imagine you follow some high-volume newsgroup, like
536 `comp.lang.c'.  Most likely you will only follow a few of the threads,
537 also want to see any new threads.
538
539 You can do this with the following two score file entries:
540
541      (orphan -500)
542      (mark-and-expunge -100)
543
544 When you enter the group the first time, you will only see the new
545 threads.  You then raise the score of the threads that you find
546 interesting (with `I T' or `I S'), and ignore (`C y') the rest.
547 Next time you enter the group, you will see new articles in the
548 interesting threads, plus any new threads.
549
550 I.e.---the orphan score atom is for high-volume groups where there
551 exist a few interesting threads which can't be found automatically
552 by ordinary scoring rules.")
553
554     (adapt (choice :tag "Adapt"
555                    (const t)
556                    (const ignore)
557                    (sexp :format "%v"
558                          :hide-front-space t)) "\
559 This entry controls the adaptive scoring.
560 If it is t, the default adaptive scoring rules will be used.  If it
561 is `ignore', no adaptive scoring will be performed on this group.  If
562 it is a list, this list will be used as the adaptive scoring rules.
563 If it isn't present, or is something other than t or `ignore', the
564 default adaptive scoring rules will be used.  If you want to use
565 adaptive scoring on most groups, you'd set `gnus-use-adaptive-scoring'
566 to t, and insert an `(adapt ignore)' in the groups where you do not
567 want adaptive scoring.  If you only want adaptive scoring in a few
568 groups, you'd set `gnus-use-adaptive-scoring' to nil, and insert
569 `(adapt t)' in the score files of the groups where you want it.")
570
571     (adapt-file (file :tag "Adapt-file") "\
572 All adaptive score entries will go to the file named by this entry.
573 It will also be applied when entering the group.  This atom might
574 be handy if you want to adapt on several groups at once, using the
575 same adaptive file for a number of groups.")
576
577     (local (repeat :tag "Local"
578                    (group :value (nil nil)
579                           (symbol :tag "Variable")
580                           (sexp :tag "Value"))) "\
581 The value of this entry should be a list of `(VAR VALUE)' pairs.
582 Each VAR will be made buffer-local to the current summary buffer,
583 and set to the value specified.  This is a convenient, if somewhat
584 strange, way of setting variables in some groups if you don't like
585 hooks much.")
586     (touched (sexp :format "Touched\n") "Internal variable."))
587   "Alist of valid symbolic score parameters.
588
589 Each entry has the form (NAME TYPE DOC), where NAME is the parameter
590 itself (a symbol), TYPE is the parameters type (a sexp widget), and DOC is a
591 documentation string for the parameter.")
592
593 (define-widget 'gnus-score-string 'group
594   "Edit score entries for string-valued headers."
595   :convert-widget 'gnus-score-string-convert)
596
597 (defun gnus-score-string-convert (widget)
598   ;; Set args appropriately.
599   (let* ((tag (widget-get widget :tag))
600          (item `(const :format "" :value ,(downcase tag)))
601          (match '(string :tag "Match"))
602          (score '(choice :tag "Score"
603                          (const :tag "default" nil)
604                          (integer :format "%v"
605                                   :hide-front-space t)))
606          (expire '(choice :tag "Expire"
607                           (const :tag "off" nil)
608                           (integer :format "%v"
609                                    :hide-front-space t)))
610          (type '(choice :tag "Type"
611                         :value s
612                         ;; I should really create a forgiving :match
613                         ;; function for each type below, that only
614                         ;; looked at the first letter.
615                         (const :tag "Regexp" r)
616                         (const :tag "Regexp (fixed case)" R)
617                         (const :tag "Substring" s)
618                         (const :tag "Substring (fixed case)" S)
619                         (const :tag "Exact" e)
620                         (const :tag "Exact (fixed case)" E)
621                         (const :tag "Word" w)
622                         (const :tag "Word (fixed case)" W)
623                         (const :tag "default" nil)))
624          (group `(group ,match ,score ,expire ,type))
625          (doc (concat (or (widget-get widget :doc)
626                           (concat "Change score based on the " tag
627                                   " header.\n"))
628                       "
629 You can have an arbitrary number of score entries for this header,
630 each score entry has four elements:
631
632 1. The \"match element\".  This should be the string to look for in the
633    header.
634
635 2. The \"score element\".  This number should be an integer in the
636    neginf to posinf interval.  This number is added to the score
637    of the article if the match is successful.  If this element is
638    not present, the `gnus-score-interactive-default-score' number
639    will be used instead.  This is 1000 by default.
640
641 3. The \"date element\".  This date says when the last time this score
642    entry matched, which provides a mechanism for expiring the
643    score entries.  It this element is not present, the score
644    entry is permanent.  The date is represented by the number of
645    days since December 31, 1 ce.
646
647 4. The \"type element\".  This element specifies what function should
648    be used to see whether this score entry matches the article.
649
650    There are the regexp, as well as substring types, and exact match,
651    and word match types.  If this element is not present, Gnus will
652    assume that substring matching should be used.  There is case
653    sensitive variants of all match types.")))
654     (widget-put widget :args `(,item
655                                (repeat :inline t
656                                        :indent 0
657                                        :tag ,tag
658                                        :doc ,doc
659                                        :format "%t:\n%h%v%i\n\n"
660                                        (choice :format "%v"
661                                                :value ("" nil nil s)
662                                                ,group
663                                                sexp)))))
664   widget)
665
666 (define-widget 'gnus-score-integer 'group
667   "Edit score entries for integer-valued headers."
668   :convert-widget 'gnus-score-integer-convert)
669
670 (defun gnus-score-integer-convert (widget)
671   ;; Set args appropriately.
672   (let* ((tag (widget-get widget :tag))
673          (item `(const :format "" :value ,(downcase tag)))
674          (match '(integer :tag "Match"))
675          (score '(choice :tag "Score"
676                          (const :tag "default" nil)
677                          (integer :format "%v"
678                                   :hide-front-space t)))
679          (expire '(choice :tag "Expire"
680                           (const :tag "off" nil)
681                           (integer :format "%v"
682                                    :hide-front-space t)))
683          (type '(choice :tag "Type"
684                         :value <
685                         (const <)
686                         (const >)
687                         (const =)
688                         (const >=)
689                         (const <=)))
690          (group `(group ,match ,score ,expire ,type))
691          (doc (concat (or (widget-get widget :doc)
692                           (concat "Change score based on the " tag
693                                   " header.")))))
694     (widget-put widget :args `(,item
695                                (repeat :inline t
696                                        :indent 0
697                                        :tag ,tag
698                                        :doc ,doc
699                                        :format "%t:\n%h%v%i\n\n"
700                                        ,group))))
701   widget)
702
703 (define-widget 'gnus-score-date 'group
704   "Edit score entries for date-valued headers."
705   :convert-widget 'gnus-score-date-convert)
706
707 (defun gnus-score-date-convert (widget)
708   ;; Set args appropriately.
709   (let* ((tag (widget-get widget :tag))
710          (item `(const :format "" :value ,(downcase tag)))
711          (match '(string :tag "Match"))
712          (score '(choice :tag "Score"
713                          (const :tag "default" nil)
714                          (integer :format "%v"
715                                   :hide-front-space t)))
716          (expire '(choice :tag "Expire"
717                           (const :tag "off" nil)
718                           (integer :format "%v"
719                                    :hide-front-space t)))
720          (type '(choice :tag "Type"
721                         :value regexp
722                         (const regexp)
723                         (const before)
724                         (const at)
725                         (const after)))
726          (group `(group ,match ,score ,expire ,type))
727          (doc (concat (or (widget-get widget :doc)
728                           (concat "Change score based on the " tag
729                                   " header."))
730                       "
731 For the Date header we have three kinda silly match types: `before',
732 `at' and `after'.  I can't really imagine this ever being useful, but,
733 like, it would feel kinda silly not to provide this function.  Just in
734 case.  You never know.  Better safe than sorry.  Once burnt, twice
735 shy.  Don't judge a book by its cover.  Never not have sex on a first
736 date.  (I have been told that at least one person, and I quote,
737 \"found this function indispensable\", however.)
738
739 A more useful match type is `regexp'.  With it, you can match the date
740 string using a regular expression.  The date is normalized to ISO8601
741 compact format first---`YYYYMMDDTHHMMSS'.  If you want to match all
742 articles that have been posted on April 1st in every year, you could
743 use `....0401.........' as a match string, for instance.  (Note that
744 the date is kept in its original time zone, so this will match
745 articles that were posted when it was April 1st where the article was
746 posted from.  Time zones are such wholesome fun for the whole family,
747 eh?")))
748     (widget-put widget :args `(,item
749                                (repeat :inline t
750                                        :indent 0
751                                        :tag ,tag
752                                        :doc ,doc
753                                        :format "%t:\n%h%v%i\n\n"
754                                        ,group))))
755   widget)
756
757 (defvar gnus-custom-scores)
758 (defvar gnus-custom-score-alist)
759
760 (defun gnus-score-customize (file)
761   "Customize score file FILE.
762 When called interactively, FILE defaults to the current score file.
763 This can be changed using the `\\[gnus-score-change-score-file]' command."
764   (interactive (list gnus-current-score-file))
765   (unless file
766     (error (format "No score file for %s"
767                    (gnus-group-decoded-name gnus-newsgroup-name))))
768   (let ((scores (gnus-score-load file))
769         (types (mapcar (lambda (entry)
770                          `(group :format "%v%h\n"
771                                  :doc ,(nth 2 entry)
772                                  (const :format "" ,(nth 0 entry))
773                                  ,(nth 1 entry)))
774                        gnus-score-parameters)))
775     ;; Ready.
776     (kill-buffer (gnus-get-buffer-create "*Gnus Customize*"))
777     (switch-to-buffer (gnus-get-buffer-create "*Gnus Customize*"))
778     (gnus-custom-mode)
779     (make-local-variable 'gnus-custom-score-alist)
780     (setq gnus-custom-score-alist scores)
781     (widget-insert "Customize the ")
782     (widget-create 'info-link
783                    :help-echo "Push me to learn more."
784                    :tag "score entries"
785                    "(gnus)Score File Format")
786     (widget-insert " for\n\t")
787     (widget-insert file)
788     (widget-insert "\nand press ")
789     (widget-create 'push-button
790                    :tag "done"
791                    :help-echo "Push me when done customizing."
792                    :action 'gnus-score-customize-done)
793     (widget-insert ".\n
794 Check the [ ] for the entries you want to apply to this score file, then
795 edit the value to suit your taste.  Don't forget to mark the checkbox,
796 if you do all your changes will be lost.  ")
797     (widget-create 'push-button
798                    :action (lambda (&rest ignore)
799                              (require 'gnus-audio)
800                              (gnus-audio-play "Evil_Laugh.au"))
801                    "Bhahahah!")
802     (widget-insert "\n\n")
803     (make-local-variable 'gnus-custom-scores)
804     (setq gnus-custom-scores
805           (widget-create 'group
806                          :value scores
807                          `(checklist :inline t
808                                      :greedy t
809                                      (gnus-score-string :tag "From")
810                                      (gnus-score-string :tag "Subject")
811                                      (gnus-score-string :tag "References")
812                                      (gnus-score-string :tag "Xref")
813                                      (gnus-score-string :tag "Extra")
814                                      (gnus-score-string :tag "Message-ID")
815                                      (gnus-score-integer :tag "Lines")
816                                      (gnus-score-integer :tag "Chars")
817                                      (gnus-score-date :tag "Date")
818                                      (gnus-score-string :tag "Head"
819                                                         :doc "\
820 Match all headers in the article.
821
822 Using one of `Head', `Body', `All' will slow down scoring considerable.
823 ")
824                                      (gnus-score-string :tag "Body"
825                                                         :doc "\
826 Match the body sans header of the article.
827
828 Using one of `Head', `Body', `All' will slow down scoring considerable.
829 ")
830                                      (gnus-score-string :tag "All"
831                                                         :doc "\
832 Match the entire article, including both headers and body.
833
834 Using one of `Head', `Body', `All' will slow down scoring
835 considerable.
836 ")
837                                      (gnus-score-string :tag
838                                                         "Followup"
839                                                         :doc "\
840 Score all followups to the specified authors.
841
842 This entry is somewhat special, in that it will match the `From:'
843 header, and affect the score of not only the matching articles, but
844 also all followups to the matching articles.  This allows you
845 e.g. increase the score of followups to your own articles, or decrease
846 the score of followups to the articles of some known trouble-maker.
847 ")
848                                      (gnus-score-string :tag "Thread"
849                                                         :doc "\
850 Add a score entry on all articles that are part of a thread.
851
852 This match key works along the same lines as the `Followup' match key.
853 If you say that you want to score on a (sub-)thread that is started by
854 an article with a `Message-ID' X, then you add a `thread' match.  This
855 will add a new `thread' match for each article that has X in its
856 `References' header.  (These new `thread' matches will use the
857 `Message-ID's of these matching articles.)  This will ensure that you
858 can raise/lower the score of an entire thread, even though some
859 articles in the thread may not have complete `References' headers.
860 Note that using this may lead to undeterministic scores of the
861 articles in the thread.
862 ")
863                                      ,@types)
864                          '(repeat :inline t
865                                   :tag "Unknown entries"
866                                   sexp)))
867     (use-local-map widget-keymap)
868     (widget-setup)))
869
870 (defun gnus-score-customize-done (&rest ignore)
871   "Reset the score alist with the present value."
872   (let ((alist gnus-custom-score-alist)
873         (value (widget-value gnus-custom-scores)))
874     (setcar alist (car value))
875     (setcdr alist (cdr value))
876     (gnus-score-set 'touched '(t) alist))
877   (bury-buffer))
878
879 (eval-when-compile
880   (defvar category-fields nil)
881   (defvar gnus-agent-cat-predicate nil)
882   (defvar gnus-agent-cat-score-file nil)
883   (defvar gnus-agent-cat-length-when-short nil)
884   (defvar gnus-agent-cat-length-when-long nil)
885   (defvar gnus-agent-cat-low-score nil)
886   (defvar gnus-agent-cat-high-score nil)
887   (defvar gnus-agent-cat-groups nil)
888   (defvar gnus-agent-cat-enable-expiration nil)
889   (defvar gnus-agent-cat-days-until-old nil)
890   (defvar gnus-agent-cat-name nil)
891 )
892
893 (defun gnus-trim-whitespace (s)
894   (when (string-match "\\`[ \n\t]+" s)
895     (setq s (substring s (match-end 0))))
896   (when (string-match "[ \n\t]+\\'" s)
897     (setq s (substring s 0 (match-beginning 0))))
898   s)
899
900 (defmacro gnus-agent-cat-prepare-category-field (parameter)
901   (let* ((entry (assq parameter gnus-agent-parameters))
902          (field (nth 3 entry)))
903     `(let* ((type (copy-sequence
904                    (nth 1 (assq ',parameter gnus-agent-parameters))))
905             (val (,field info))
906             (deflt (if (,field defaults)
907                        (concat " [" (gnus-trim-whitespace
908                                      (pp-to-string (,field defaults))) "]"))))
909
910        (if (eq (car type) 'radio)
911            (let* ((rtype (nreverse type))
912                   (rt rtype))
913              (while (listp (or (cadr rt) 'not-list))
914                (setq rt (cdr rt)))
915
916              (setcdr rt (cons '(const :format "Inherit " nil) (cdr rt)))
917              (setq type (nreverse rtype))))
918
919        (if deflt
920            (let ((tag (cdr (memq :tag type))))
921              (if (string-match "\n" deflt)
922                  (progn (while (progn (setq deflt (replace-match "\n " t t
923                                                                  deflt))
924                                       (string-match "\n" deflt (match-end 0))))
925                         (setq deflt (concat "\n" deflt))))
926
927              (setcar tag (concat (car tag) deflt))))
928
929        (widget-insert "\n")
930
931        (set (make-local-variable ',field)
932             (if val
933                 (widget-create type :value val)
934               (widget-create type)))
935        (widget-put ,field :default val)
936        (widget-put ,field :accessor ',field)
937        (push ,field category-fields))))
938
939 (defun gnus-agent-customize-category (category)
940   "Edit the CATEGORY."
941   (interactive (list (gnus-category-name)))
942   (let ((info (assq category gnus-category-alist))
943         (defaults (list nil '(agent-predicate . false)
944                         (cons 'agent-enable-expiration
945                               gnus-agent-enable-expiration)
946                         '(agent-days-until-old . 7)
947                         (cons 'agent-length-when-short
948                               gnus-agent-short-article)
949                         (cons 'agent-length-when-long gnus-agent-long-article)
950                         (cons 'agent-low-score gnus-agent-low-score)
951                         (cons 'agent-high-score gnus-agent-high-score))))
952
953     (let ((old (get-buffer "*Gnus Agent Category Customize*")))
954       (when old
955         (gnus-kill-buffer old)))
956     (switch-to-buffer (gnus-get-buffer-create
957                        "*Gnus Agent Category Customize*"))
958
959     (let ((inhibit-read-only t))
960       (gnus-custom-mode)
961       (buffer-disable-undo)
962
963       (let* ((name (gnus-agent-cat-name info)))
964         (widget-insert "Customize the Agent Category '")
965         (widget-insert (symbol-name name))
966         (widget-insert "' and press ")
967         (widget-create
968          'push-button
969          :notify
970          '(lambda (&rest ignore)
971             (let* ((info (assq gnus-agent-cat-name gnus-category-alist))
972                    (widgets category-fields))
973               (while widgets
974                 (let* ((widget (pop widgets))
975                        (value (ignore-errors (widget-value widget))))
976                   (eval `(setf (,(widget-get widget :accessor) ',info)
977                                ',value)))))
978             (gnus-category-write)
979             (gnus-kill-buffer (current-buffer))
980             (when (get-buffer gnus-category-buffer)
981               (switch-to-buffer (get-buffer gnus-category-buffer))
982               (gnus-category-list)))
983                        "Done")
984         (widget-insert
985          "\n    Note: Empty fields default to the customizable global\
986  variables.\n\n")
987
988         (set (make-local-variable 'gnus-agent-cat-name)
989              name))
990
991       (set (make-local-variable 'category-fields) nil)
992       (gnus-agent-cat-prepare-category-field agent-predicate)
993
994       (gnus-agent-cat-prepare-category-field agent-score)
995       (gnus-agent-cat-prepare-category-field agent-short-article)
996       (gnus-agent-cat-prepare-category-field agent-long-article)
997       (gnus-agent-cat-prepare-category-field agent-low-score)
998       (gnus-agent-cat-prepare-category-field agent-high-score)
999
1000       ;; The group list is NOT handled with
1001       ;; gnus-agent-cat-prepare-category-field as I don't want the
1002       ;; group list to appear when customizing a topic.
1003       (widget-insert "\n")
1004       (set (make-local-variable 'gnus-agent-cat-groups)
1005            (widget-create
1006             `(choice
1007               :format "%[Select Member Groups%]\n%v" :value ignore
1008               (const :menu-tag "do not change" :tag "" :value ignore)
1009               (checklist :entry-format "%b %v"
1010                          :menu-tag "display group selectors"
1011                          :greedy t
1012                          :value ,(delq nil
1013                                        (mapcar
1014                                         (lambda (newsrc)
1015                                           (car (member
1016                                                 (gnus-info-group newsrc)
1017                                                 (gnus-agent-cat-groups info))))
1018                                         (cdr gnus-newsrc-alist)))
1019                          ,@(mapcar (lambda (newsrc)
1020                                      `(const ,(gnus-info-group newsrc)))
1021                                    (cdr gnus-newsrc-alist))))))
1022
1023       (widget-put gnus-agent-cat-groups :default (gnus-agent-cat-groups info))
1024       (widget-put gnus-agent-cat-groups :accessor 'gnus-agent-cat-groups)
1025       (push gnus-agent-cat-groups category-fields)
1026
1027       (widget-insert "\nExpiration Settings ")
1028
1029       (gnus-agent-cat-prepare-category-field agent-enable-expiration)
1030       (gnus-agent-cat-prepare-category-field agent-days-until-old)
1031
1032       (use-local-map widget-keymap)
1033       (widget-setup)
1034       (buffer-enable-undo))))
1035
1036 ;;; The End:
1037
1038 (provide 'gnus-cus)
1039
1040 ;;; gnus-cus.el ends here