*** empty log message ***
[gnus] / lisp / gnus-edit.el
1 ;;; gnus-edit.el --- Gnus SCORE file editing.
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3 ;;
4 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
5 ;; Keywords: news, help
6 ;; Version: 0.1
7
8 ;;; Commentary:
9 ;;
10 ;; Type `M-x gnus-score-customize RET' to invoke.
11
12 ;;; Code:
13
14 (require 'custom)
15 (require 'gnus-score)
16
17 (autoload 'gnus-score-load "gnus-score")
18
19 (defconst gnus-score-custom-data
20   '((tag . "Score")
21     (doc . "Customization of Gnus SCORE files.
22
23 SCORE files allow you to assign a score to each article when you enter
24 a group, and automatically mark the articles as read or delete them
25 based on the score.  In the summary buffer you can use the score to
26 sort the articles by score (`C-c C-s C-s') or to jump to the unread
27 article with the highest score (`,').")
28     (type . group)
29     (data ""
30           ((header . nil)
31            (doc . "Name of SCORE file to customize.
32
33 Enter the name in the `File' field, then push the [Load] button to
34 load it.  When done editing, push the [Save] button to save the file.
35
36 Several score files may apply to each group, and several groups may
37 use the same score file.  This is controlled implicitly by the name of
38 the score file and the value of the global variable
39 `gnus-score-find-score-files-function', and explicitly by the the
40 `Files' and `Exclude Files' entries.") 
41            (compact . t)
42            (type . group)
43            (data ((tag . "Load")
44                   (type . button)
45                   (query . gnus-score-custom-load))
46                  ((tag . "Save")
47                   (type . button)
48                   (query . gnus-score-custom-save))
49                  ((name . file)
50                   (tag . "File")
51                   (directory . "~/News/")
52                   (default-file . "SCORE")
53                   (type . file))))
54           ((name . files)
55            (tag . "Files")
56            (doc . "\
57 List of score files to load when the the current score file is loaded.
58 You can use this to share score entries between multiple score files.
59
60 Push the `[INS]' button add a score file to the list, or `[DEL]' to
61 delete a score file from the list.")
62            (type . list)
63            (data ((type . repeat)
64                   (header . nil)
65                   (data (type . file)
66                         (directory . "~/News/")))))
67           ((name . exclude-files)
68            (tag . "Exclude Files")
69            (doc . "\
70 List of score files to exclude when the the current score file is loaded.
71 You can use this if you have a score file you want to share between a
72 number of newsgroups, except for the newsgroup this score file
73 matches.  [ Did anyone get that? ]
74
75 Push the `[INS]' button add a score file to the list, or `[DEL]' to
76 delete a score file from the list.")
77            (type . list)
78            (data ((type . repeat)
79                   (header . nil)
80                   (data (type . file)
81                         (directory . "~/News/")))))
82           ((name . mark)
83            (tag . "Mark")
84            (doc . "\
85 Articles below this score will be automatically marked as read.
86
87 This means that when you enter the summary buffer, the articles will
88 be shown but will already be marked as read.  You can then press `x'
89 to get rid of them entirely.
90
91 By default articles with a negative score will be marked as read.  To
92 change this, push the `Mark' button, and choose `Integer'.  You can
93 then enter a value in the `Mark' field.")
94            (type . gnus-score-custom-maybe-type))
95           ((name . expunge)
96            (tag . "Expunge")
97            (doc . "\
98 Articles below this score will not be shown in the summary buffer.")
99            (type . gnus-score-custom-maybe-type))
100           ((name . mark-and-expunge)
101            (tag . "Mark and Expunge")
102            (doc . "\
103 Articles below this score will be marked as read, but not shown.
104
105 Someone should explain me the difference between this and `expunge'
106 alone or combined with `mark'.")
107            (type . gnus-score-custom-maybe-type))
108 ;         ;; Sexp type isn't implemented yet.
109 ;         ((name . eval)
110 ;          (tag . "Eval")
111 ;          (doc . "Evaluate this expression when the entering sumamry buffer.")
112 ;          (type . sexp))
113           ;; Toggle type isn't implemented yet.
114           ((name . read-only)
115            (tag . "Read Only")
116            (doc . "Read-only score files will not be updated or saved.
117 Except from this buffer, of course!")
118            (type . toggle))
119           ((type . doc)
120            (header . nil)
121            (doc . "\
122 Each news header has an associated list of score entries.  
123 You can use the [INS] buttons to add new score entries anywhere in the
124 list, or the [DEL] buttons to delete specific score entries.
125
126 Each score entry should specify a string that should be matched with
127 the content actual header in order to determine whether the entry
128 applies to that header.  Enter that string in the `Match' field.
129
130 If the score entry matches, the articles score will be adjusted with
131 some amount.  Enter that amount in the in the `Score' field.  You
132 should specify a positive amount for score entries that matches
133 articles you find interesting, and a negative amount for score entries
134 matching articles you would rather avoid.  The final score for the
135 article will be the sum of the score of all score entries that match
136 the article. 
137
138 The score entry can be either permanent or expirable.  To make the
139 entry permanent, push the `Date' button and choose the `Permanent'
140 entry.  To make the entry expirable, choose instead the `Integer'
141 entry.  After choosing the you can enter the date the score entry was
142 last matched in the `Date' field.  The date will be automatically
143 updated each time the score entry matches an article.  When the date
144 become too old, the the score entry will be removed.
145
146 For your convenience, the date is specified as the number of days
147 elapsed since the (imaginary) Gregorian date Sunday, December 31, 1
148 BC.
149
150 Finally, you can choose what kind of match you want to perform by
151 pushing the `Type' button.  For most entries you can choose between
152 `Exact' which mean the header content must be exactly identical to the
153 match string, or `Substring' meaning the match string should be
154 somewhere in the header content, or even `Regexp' to use Emacs regular
155 expression matching.  The last choice is `Fuzzy' which is like `Exact'
156 except that whitespace derivations, a beginning `Re:' or a terminating
157 parenthetical remark are all ignored.  Each of the four types have a
158 variant which will ignore case in the comparison.  That variant is
159 indicated with a `(fold)' after its name."))
160           ((name . from)
161            (tag . "From")
162            (doc . "Scoring based on the authors email address.")
163            (type . gnus-score-custom-string-type))
164           ((name . subject)
165            (tag . "Subject")
166            (doc . "Scoring based on the articles subject.")
167            (type . gnus-score-custom-string-type))
168           ((name . followup)
169            (tag . "Followup")
170            (doc . "Scoring based on who the article is a followup to.
171
172 If you want to see all followups to your own articles, add an entry
173 with a positive score matching your email address here.  You can also
174 put an entry with a negative score matching someone who is so annoying
175 that you don't even want to see him quoted in followups.")
176            (type . gnus-score-custom-string-type))
177           ((name . xref)
178            (tag . "Xref")
179            (doc . "Scoring based on article crossposting.
180
181 If you want to score based on which newsgroups an article is posted
182 to, this is the header to use.  The syntax is a little different from
183 the `Newsgroups' header, but scoring in `Xref' is much faster.  As an
184 example, to match all crossposted articles match on `:.*:' using the
185 `Regexp' type.")
186            (type . gnus-score-custom-string-type))
187           ((name . references)
188            (tag . "References")
189            (doc . "Scoring based on article references.
190
191 The `References' header gives you an alternative way to score on
192 followups.  If you for example want to see follow all discussions
193 where people from `iesd.auc.dk' school participate, you can add a
194 substring match on `iesd.auc.dk>' on this header.")
195            (type . gnus-score-custom-string-type))
196           ((name . message-id)
197            (tag . "Message-ID")
198            (doc . "Scoring based on the articles message-id.
199
200 This isn't very useful, but Lars like completeness.  You can use it to
201 match all messaged generated by recent Gnus version with a `Substring'
202 match on `.fsf@'.")
203            (type . gnus-score-custom-string-type))
204           ((type . doc)
205            (header . nil)
206            (doc . "\
207 WARNING:  Scoring on the following three pseudo headers is very slow!
208 Scoring on any of the real headers use a technique that avoids
209 scanning the entire article, only the actual headers you score on are
210 scanned, and this scanning has been heavily optimized.  Using just a
211 single entry for one the three pseudo-headers `Head', `Body', and
212 `All' will require GNUS to retrieve and scan the entire article, which
213 can be very slow on large groups.  However, if you add one entry for
214 any of these headers, you can just as well add several.  Each
215 subsequent entry cost relatively little extra time."))
216           ((name . head)
217            (tag . "Head")
218            (doc . "Scoring based on the article header.
219
220 Instead of matching the content of a single header, the entire header
221 section of the article is matched.  You can use this to match on
222 arbitrary headers, foe example to single out TIN lusers, use a substring
223 match on `Newsreader: TIN'.  That should get 'em!")
224            (type . gnus-score-custom-string-type))
225           ((name . body)
226            (tag . "Body")
227            (doc . "Scoring based on the article body.
228
229 If you think any article that mentions `Kibo' is inherently
230 interesting, do a substring match on His name.  You Are Allowed.")
231            (type . gnus-score-custom-string-type))
232           ((name . all)
233            (tag . "All")
234            (doc . "Scoring based on the whole article.")
235            (type . gnus-score-custom-string-type))
236           ((name . date)
237            (tag . "Date")
238            (doc . "Scoring based on article date.
239
240 You can change the score of articles that have been posted before,
241 after, or at a specific date.  You should add the date in the `Match'
242 field, and then select `before', `after', or `at' by pushing the
243 `Type' button.  Imagine you want to lower the score of very old
244 articles, or want to raise the score of articles from the future (such
245 things happen!).  Then you can't use date scoring for that.  In fact,
246 I can't imagine anything you would want to use this for.   
247
248 For your convenience, the date is specified in Usenet date format.")
249            (type . gnus-score-custom-date-type))
250           ((type . doc)
251            (header . nil)
252            (doc . "\
253 The Lines and Chars headers use integer based scoring.  
254
255 This means that you should write an integer in the `Match' field, and
256 the push the `Type' field to if the `Chars' or `Lines' header should
257 be larger, equal, or smaller than the number you wrote in the match
258 field."))
259           ((name . chars)
260            (tag . "Characters")
261            (doc . "Scoring based on the number of characters in the article.")
262            (type . gnus-score-custom-integer-type))
263           ((name . lines)
264            (tag . "Lines")
265            (doc . "Scoring based on the number of lines in the article.")
266            (type . gnus-score-custom-integer-type))
267           ((name . orphan)
268            (tag . "Orphan")
269            (doc . "Score to add to articles with no parents.")
270            (type . gnus-score-custom-maybe-type)))))  
271 ;; This is to complex for me to figure out right now.
272 ;`adapt'
273 ;     This entry controls the adaptive scoring.  If it is `t', the
274 ;     default adaptive scoring rules will be used.  If it is `ignore', no
275 ;     adaptive scoring will be performed on this group.  If it is a
276 ;     list, this list will be used as the adaptive scoring rules.  If it
277 ;     isn't present, or is something other than `t' or `ignore', the
278 ;     default adaptive scoring rules will be used.  If you want to use
279 ;     adaptive scoring on most groups, you'd set
280 ;     `gnus-use-adaptive-scoring' to `t', and insert an `(adapt ignore)'
281 ;     in the groups where you do not want adaptive scoring.  If you only
282 ;     want adaptive scoring in a few groups, you'd set
283 ;     `gnus-use-adaptive-scoring' to `nil', and insert `(adapt t)' in
284 ;     the score files of the groups where you want it.
285 ;; This isn't implemented in the old version of (ding) I use.
286 ;`local'
287 ;  List of local variables to bind in the summary buffer.
288
289 (defconst gnus-score-custom-type-properties
290   '((gnus-score-custom-maybe-type
291      (type . choice)
292      (data ((type . integer)
293             (default . 0))
294            ((tag . "Default")
295             (type . const)
296             (default . nil))))
297     (gnus-score-custom-string-type
298      (type . list)
299      (data ((type . repeat)
300             (header . nil)
301             (data . ((type . list)
302                      (compact . t)
303                      (data ((tag . "Match")
304                             (width . 59)
305                             (type . string))
306                            "\n           "
307                            ((tag . "Score")
308                             (type . integer))
309                            ((tag . "Date")
310                             (type . choice)
311                             (data ((type . integer)
312                                    (default . 0)
313                                    (width . 9))
314                                   ((tag . "Permanent")
315                                    (type . const)
316                                    (default . nil))))
317                            ((tag . "Type")
318                             (type . choice)
319                             (data ((tag . "Exact")
320                                    (default . e)
321                                    (type . const))
322                                   ((tag . "Substring")
323                                    (default . s) 
324                                    (type . const))
325                                   ((tag . "Regexp")
326                                    (default . r)
327                                    (type . const))
328                                   ((tag . "Fuzzy")
329                                    (default . f)
330                                    (type . const))
331                                   ((tag . "Exact (fold)")
332                                    (default . E)
333                                    (type . const))
334                                   ((tag . "Substring (fold)")
335                                    (default . S) 
336                                    (type . const))
337                                   ((tag . "Regexp (fold)")
338                                    (default . R)
339                                    (type . const))
340                                   ((tag . "Fuzzy  (fold)")
341                                    (default . F)
342                                    (type . const))))))))))
343     (gnus-score-custom-integer-type
344      (type . list)
345      (data ((type . repeat)
346             (header . nil)
347             (data . ((type . list)
348                      (compact . t)
349                      (data ((tag . "Match")
350                             (type . integer))
351                            ((tag . "Score")
352                             (type . integer))
353                            ((tag . "Date")
354                             (type . choice)
355                             (data ((type . integer)
356                                    (default . 0)
357                                    (width . 9))
358                                   ((tag . "Permanent")
359                                    (type . const)
360                                    (default . nil))))
361                            ((tag . "Type")
362                             (type . choice)
363                             (data ((tag . "<")
364                                    (default . <)
365                                    (type . const))
366                                   ((tag . ">")
367                                    (default . >) 
368                                    (type . const))
369                                   ((tag . "=")
370                                    (default . =)
371                                    (type . const))
372                                   ((tag . ">=")
373                                    (default . >=)
374                                    (type . const))
375                                   ((tag . "<=")
376                                    (default . <=)
377                                    (type . const))))))))))
378     (gnus-score-custom-date-type
379      (type . list)
380      (data ((type . repeat)
381             (header . nil)
382             (data . ((type . list)
383                      (compact . t)
384                      (data ((tag . "Match")
385                             (width . 59)
386                             (type . string))
387                            "\n           "
388                            ((tag . "Score")
389                             (type . integer))
390                            ((tag . "Date")
391                             (type . choice)
392                             (data ((type . integer)
393                                    (default . 0)
394                                    (width . 9))
395                                   ((tag . "Permanent")
396                                    (type . const)
397                                    (default . nil))))
398                            ((tag . "Type")
399                             (type . choice)
400                             (data ((tag . "Before")
401                                    (default . before)
402                                    (type . const))
403                                   ((tag . "After")
404                                    (default . after) 
405                                    (type . const))
406                                   ((tag . "At")
407                                    (default . at)
408                                    (type . const))))))))))))
409
410 (defvar gnus-score-custom-file nil
411   "Name of SCORE file being customized.")
412
413 (defun gnus-score-customize ()
414   "Create a buffer for editing gnus SCORE files."
415   (interactive)
416   (let (gnus-score-alist)
417     (custom-buffer-create "*Score Edit*" gnus-score-custom-data
418                           gnus-score-custom-type-properties
419                           'gnus-score-custom-set
420                           'gnus-score-custom-get))
421   (make-local-variable 'gnus-score-custom-file)
422   (setq gnus-score-custom-file "SCORE")
423   (make-local-variable 'gnus-score-alist)
424   (setq gnus-score-alist nil)
425   (custom-reset-all))
426
427 (defun gnus-score-custom-get (name)
428   (if (eq name 'file)
429       gnus-score-custom-file
430     (let ((entry (assoc (symbol-name name) gnus-score-alist)))
431       (if entry 
432           (mapcar 'gnus-score-custom-sanify (cdr entry))
433         (setq entry (assoc name gnus-score-alist))
434         (if (memq name '(files))
435             (cdr entry)
436           (car (cdr entry)))))))
437
438 (defun gnus-score-custom-set (name value)
439   (cond ((eq name 'file)
440          (setq gnus-score-custom-file value))
441         ((assoc (symbol-name name) gnus-score-alist)
442          (if value
443              (setcdr (assoc (symbol-name name) gnus-score-alist) value)
444            (setq gnus-score-alist (delq (assoc (symbol-name name) 
445                                                gnus-score-alist) 
446                                         gnus-score-alist))))
447         ((assoc (symbol-name name) gnus-header-index)
448          (if value
449              (setq gnus-score-alist 
450                    (cons (cons (symbol-name name) value) gnus-score-alist))))
451         ((assoc name gnus-score-alist)
452          (cond ((null value)
453                 (setq gnus-score-alist (delq (assoc name gnus-score-alist)
454                                              gnus-score-alist)))
455                ((listp value)
456                 (setcdr (assoc name gnus-score-alist) value))
457                (t
458                 (setcdr (assoc name gnus-score-alist) (list value)))))
459         ((null value))
460         ((listp value)
461          (setq gnus-score-alist (cons (cons name value) gnus-score-alist)))
462         (t
463          (setq gnus-score-alist 
464                (cons (cons name (list value)) gnus-score-alist)))))
465
466 (defun gnus-score-custom-sanify (entry)
467   (list (nth 0 entry)
468         (or (nth 1 entry) gnus-score-interactive-default-score)
469         (nth 2 entry)
470         (if (null (nth 3 entry)) 
471             's
472           (intern (substring (symbol-name (nth 3 entry)) 0 1)))))
473
474 (defvar gnus-score-cache nil)
475
476 (defun gnus-score-custom-load ()
477   (interactive)
478   (let ((file (custom-name-value 'file)))
479     (if (eq file custom-nil)
480         (error "You must specify a file name"))
481     (setq file (expand-file-name file "~/News"))
482     (gnus-score-load file)
483     (setq gnus-score-custom-file file)
484     (custom-reset-all)
485     (message "Loaded")))
486
487 (defun gnus-score-custom-save ()
488   (interactive)
489   (custom-apply-all)
490   (gnus-score-remove-from-cache gnus-score-custom-file)
491   (let ((file gnus-score-custom-file)
492         (score gnus-score-alist)
493         emacs-lisp-mode-hook)
494     (save-excursion
495       (set-buffer (get-buffer-create "*Score*"))
496       (buffer-disable-undo (current-buffer))
497       (erase-buffer)
498       (pp score (current-buffer))
499       (gnus-make-directory (file-name-directory file))
500       (write-region (point-min) (point-max) file nil 'silent)
501       (kill-buffer (current-buffer))))
502   (message "Saved"))
503
504 (provide 'gnus-edit)
505
506 ;;; gnus-edit.el end here