*** empty log message ***
[gnus] / lisp / gnus-kill.el
1 ;;; gnus-kill.el --- kill commands for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'gnus)
30 (require 'gnus-art)
31 (require 'gnus-range)
32
33 (defcustom gnus-kill-file-mode-hook nil
34   "Hook for Gnus kill file mode."
35   :group 'gnus-score
36   :type 'hook)
37
38 (defcustom gnus-kill-expiry-days 7
39   "*Number of days before expiring unused kill file entries."
40   :group 'gnus-score
41   :type 'integer)
42
43 (defcustom gnus-kill-save-kill-file nil
44   "*If non-nil, will save kill files after processing them."
45   :group 'gnus-score
46   :type 'boolean)
47
48 (defcustom gnus-winconf-kill-file nil
49   "What does this do, Lars?"
50   :group 'gnus-score
51   :type 'sexp)
52
53 (defcustom gnus-kill-killed t
54   "*If non-nil, Gnus will apply kill files to already killed articles.
55 If it is nil, Gnus will never apply kill files to articles that have
56 already been through the scoring process, which might very well save lots
57 of time."
58   :group 'gnus-score
59   :type 'boolean)
60
61 \f
62
63 (defmacro gnus-raise (field expression level)
64   `(gnus-kill ,field ,expression
65               (function (gnus-summary-raise-score ,level)) t))
66
67 (defmacro gnus-lower (field expression level)
68   `(gnus-kill ,field ,expression
69               (function (gnus-summary-raise-score (- ,level))) t))
70
71 ;;;
72 ;;; Gnus Kill File Mode
73 ;;;
74
75 (defvar gnus-kill-file-mode-map nil)
76
77 (unless gnus-kill-file-mode-map
78   (gnus-define-keymap (setq gnus-kill-file-mode-map
79                             (copy-keymap emacs-lisp-mode-map))
80     "\C-c\C-k\C-s" gnus-kill-file-kill-by-subject
81     "\C-c\C-k\C-a" gnus-kill-file-kill-by-author
82     "\C-c\C-k\C-t" gnus-kill-file-kill-by-thread
83     "\C-c\C-k\C-x" gnus-kill-file-kill-by-xref
84     "\C-c\C-a" gnus-kill-file-apply-buffer
85     "\C-c\C-e" gnus-kill-file-apply-last-sexp
86     "\C-c\C-c" gnus-kill-file-exit))
87
88 (defun gnus-kill-file-mode ()
89   "Major mode for editing kill files.
90
91 If you are using this mode - you probably shouldn't.  Kill files
92 perform badly and paint with a pretty broad brush.  Score files, on
93 the other hand, are vastly faster (40x speedup) and give you more
94 control over what to do.
95
96 In addition to Emacs-Lisp Mode, the following commands are available:
97
98 \\{gnus-kill-file-mode-map}
99
100   A kill file contains Lisp expressions to be applied to a selected
101 newsgroup.  The purpose is to mark articles as read on the basis of
102 some set of regexps.  A global kill file is applied to every newsgroup,
103 and a local kill file is applied to a specified newsgroup.  Since a
104 global kill file is applied to every newsgroup, for better performance
105 use a local one.
106
107   A kill file can contain any kind of Emacs Lisp expressions expected
108 to be evaluated in the Summary buffer.  Writing Lisp programs for this
109 purpose is not so easy because the internal working of Gnus must be
110 well-known.  For this reason, Gnus provides a general function which
111 does this easily for non-Lisp programmers.
112
113   The `gnus-kill' function executes commands available in Summary Mode
114 by their key sequences.  `gnus-kill' should be called with FIELD,
115 REGEXP and optional COMMAND and ALL.  FIELD is a string representing
116 the header field or an empty string.  If FIELD is an empty string, the
117 entire article body is searched for.  REGEXP is a string which is
118 compared with FIELD value.  COMMAND is a string representing a valid
119 key sequence in Summary mode or Lisp expression.  COMMAND defaults to
120 '(gnus-summary-mark-as-read nil \"X\").  Make sure that COMMAND is
121 executed in the Summary buffer.  If the second optional argument ALL
122 is non-nil, the COMMAND is applied to articles which are already
123 marked as read or unread.  Articles which are marked are skipped over
124 by default.
125
126   For example, if you want to mark articles of which subjects contain
127 the string `AI' as read, a possible kill file may look like:
128
129         (gnus-kill \"Subject\" \"AI\")
130
131   If you want to mark articles with `D' instead of `X', you can use
132 the following expression:
133
134         (gnus-kill \"Subject\" \"AI\" \"d\")
135
136 In this example it is assumed that the command
137 `gnus-summary-mark-as-read-forward' is assigned to `d' in Summary Mode.
138
139   It is possible to delete unnecessary headers which are marked with
140 `X' in a kill file as follows:
141
142         (gnus-expunge \"X\")
143
144   If the Summary buffer is empty after applying kill files, Gnus will
145 exit the selected newsgroup normally.  If headers which are marked
146 with `D' are deleted in a kill file, it is impossible to read articles
147 which are marked as read in the previous Gnus sessions.  Marks other
148 than `D' should be used for articles which should really be deleted.
149
150 Entry to this mode calls emacs-lisp-mode-hook and
151 gnus-kill-file-mode-hook with no arguments, if that value is non-nil."
152   (interactive)
153   (kill-all-local-variables)
154   (use-local-map gnus-kill-file-mode-map)
155   (set-syntax-table emacs-lisp-mode-syntax-table)
156   (setq major-mode 'gnus-kill-file-mode)
157   (setq mode-name "Kill")
158   (lisp-mode-variables nil)
159   (run-hooks 'emacs-lisp-mode-hook 'gnus-kill-file-mode-hook))
160
161 (defun gnus-kill-file-edit-file (newsgroup)
162   "Begin editing a kill file for NEWSGROUP.
163 If NEWSGROUP is nil, the global kill file is selected."
164   (interactive "sNewsgroup: ")
165   (let ((file (gnus-newsgroup-kill-file newsgroup)))
166     (gnus-make-directory (file-name-directory file))
167     ;; Save current window configuration if this is first invocation.
168     (or (and (get-file-buffer file)
169              (get-buffer-window (get-file-buffer file)))
170         (setq gnus-winconf-kill-file (current-window-configuration)))
171     ;; Hack windows.
172     (let ((buffer (find-file-noselect file)))
173       (cond ((get-buffer-window buffer)
174              (pop-to-buffer buffer))
175             ((eq major-mode 'gnus-group-mode)
176              (gnus-configure-windows 'group) ;Take all windows.
177              (pop-to-buffer buffer))
178             ((eq major-mode 'gnus-summary-mode)
179              (gnus-configure-windows 'article)
180              (pop-to-buffer gnus-article-buffer)
181              (bury-buffer gnus-article-buffer)
182              (switch-to-buffer buffer))
183             (t                          ;No good rules.
184              (find-file-other-window file))))
185     (gnus-kill-file-mode)))
186
187 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
188 (defun gnus-kill-set-kill-buffer ()
189   (let* ((file (gnus-newsgroup-kill-file gnus-newsgroup-name))
190          (buffer (find-file-noselect file)))
191     (set-buffer buffer)
192     (gnus-kill-file-mode)
193     (bury-buffer buffer)))
194
195 (defun gnus-kill-file-enter-kill (field regexp &optional dont-move)
196   ;; Enter kill file entry.
197   ;; FIELD: String containing the name of the header field to kill.
198   ;; REGEXP: The string to kill.
199   (save-excursion
200     (let (string)
201       (unless (eq major-mode 'gnus-kill-file-mode)
202         (gnus-kill-set-kill-buffer))
203       (unless dont-move
204         (goto-char (point-max)))
205       (insert (setq string (format "(gnus-kill %S %S)\n" field regexp)))
206       (gnus-kill-file-apply-string string))))
207     
208 (defun gnus-kill-file-kill-by-subject ()
209   "Kill by subject."
210   (interactive)
211   (gnus-kill-file-enter-kill
212    "Subject" 
213    (if (vectorp gnus-current-headers)
214        (regexp-quote 
215         (gnus-simplify-subject (mail-header-subject gnus-current-headers)))
216      "")
217    t))
218   
219 (defun gnus-kill-file-kill-by-author ()
220   "Kill by author."
221   (interactive)
222   (gnus-kill-file-enter-kill
223    "From" 
224    (if (vectorp gnus-current-headers)
225        (regexp-quote (mail-header-from gnus-current-headers))
226      "") t))
227  
228 (defun gnus-kill-file-kill-by-thread ()
229   "Kill by author."
230   (interactive)
231   (gnus-kill-file-enter-kill
232    "References" 
233    (if (vectorp gnus-current-headers)
234        (regexp-quote (mail-header-id gnus-current-headers))
235      "")))
236  
237 (defun gnus-kill-file-kill-by-xref ()
238   "Kill by Xref."
239   (interactive)
240   (let ((xref (and (vectorp gnus-current-headers)
241                    (mail-header-xref gnus-current-headers)))
242         (start 0)
243         group)
244     (if xref
245         (while (string-match " \\([^ \t]+\\):" xref start)
246           (setq start (match-end 0))
247           (when (not (string= 
248                       (setq group 
249                             (substring xref (match-beginning 1) (match-end 1)))
250                       gnus-newsgroup-name))
251             (gnus-kill-file-enter-kill 
252              "Xref" (concat " " (regexp-quote group) ":") t)))
253       (gnus-kill-file-enter-kill "Xref" "" t))))
254
255 (defun gnus-kill-file-raise-followups-to-author (level)
256   "Raise score for all followups to the current author."
257   (interactive "p")
258   (let ((name (mail-header-from gnus-current-headers))
259         string)
260     (save-excursion
261       (gnus-kill-set-kill-buffer)
262       (goto-char (point-min))
263       (setq name (read-string (concat "Add " level
264                                       " to followup articles to: ")
265                               (regexp-quote name)))
266       (setq 
267        string
268        (format
269         "(gnus-kill %S %S '(gnus-summary-temporarily-raise-by-thread %S))\n"
270         "From" name level))
271       (insert string)
272       (gnus-kill-file-apply-string string))
273     (gnus-message 
274      6 "Added temporary score file entry for followups to %s." name)))
275
276 (defun gnus-kill-file-apply-buffer ()
277   "Apply current buffer to current newsgroup."
278   (interactive)
279   (if (and gnus-current-kill-article
280            (get-buffer gnus-summary-buffer))
281       ;; Assume newsgroup is selected.
282       (gnus-kill-file-apply-string (buffer-string))
283     (ding) (gnus-message 2 "No newsgroup is selected.")))
284
285 (defun gnus-kill-file-apply-string (string)
286   "Apply STRING to current newsgroup."
287   (interactive)
288   (let ((string (concat "(progn \n" string "\n)")))
289     (save-excursion
290       (save-window-excursion
291         (pop-to-buffer gnus-summary-buffer)
292         (eval (car (read-from-string string)))))))
293
294 (defun gnus-kill-file-apply-last-sexp ()
295   "Apply sexp before point in current buffer to current newsgroup."
296   (interactive)
297   (if (and gnus-current-kill-article
298            (get-buffer gnus-summary-buffer))
299       ;; Assume newsgroup is selected.
300       (let ((string
301              (buffer-substring
302               (save-excursion (forward-sexp -1) (point)) (point))))
303         (save-excursion
304           (save-window-excursion
305             (pop-to-buffer gnus-summary-buffer)
306             (eval (car (read-from-string string))))))
307     (ding) (gnus-message 2 "No newsgroup is selected.")))
308
309 (defun gnus-kill-file-exit ()
310   "Save a kill file, then return to the previous buffer."
311   (interactive)
312   (save-buffer)
313   (let ((killbuf (current-buffer)))
314     ;; We don't want to return to article buffer.
315     (when (get-buffer gnus-article-buffer)
316       (bury-buffer gnus-article-buffer))
317     ;; Delete the KILL file windows.
318     (delete-windows-on killbuf)
319     ;; Restore last window configuration if available.
320     (when gnus-winconf-kill-file
321       (set-window-configuration gnus-winconf-kill-file))
322     (setq gnus-winconf-kill-file nil)
323     ;; Kill the KILL file buffer.  Suggested by tale@pawl.rpi.edu.
324     (kill-buffer killbuf)))
325
326 ;; For kill files
327
328 (defun gnus-Newsgroup-kill-file (newsgroup)
329   "Return the name of a kill file for NEWSGROUP.
330 If NEWSGROUP is nil, return the global kill file instead."
331   (cond ((or (null newsgroup)
332              (string-equal newsgroup ""))
333          ;; The global kill file is placed at top of the directory.
334          (expand-file-name gnus-kill-file-name gnus-kill-files-directory))
335         (gnus-use-long-file-name
336          ;; Append ".KILL" to capitalized newsgroup name.
337          (expand-file-name (concat (gnus-capitalize-newsgroup newsgroup)
338                                    "." gnus-kill-file-name)
339                            gnus-kill-files-directory))
340         (t
341          ;; Place "KILL" under the hierarchical directory.
342          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
343                                    "/" gnus-kill-file-name)
344                            gnus-kill-files-directory))))
345
346 (defun gnus-expunge (marks)
347   "Remove lines marked with MARKS."
348   (save-excursion
349     (set-buffer gnus-summary-buffer)
350     (gnus-summary-limit-to-marks marks 'reverse)))
351
352 (defun gnus-apply-kill-file-unless-scored ()
353   "Apply .KILL file, unless a .SCORE file for the same newsgroup exists."
354   (cond ((file-exists-p (gnus-score-file-name gnus-newsgroup-name))
355          ;; Ignores global KILL.
356          (when (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name))
357            (gnus-message 3 "Note: Ignoring %s.KILL; preferring .SCORE"
358                          gnus-newsgroup-name))
359          0)
360         ((or (file-exists-p (gnus-newsgroup-kill-file nil))
361              (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
362          (gnus-apply-kill-file-internal))
363         (t
364          0)))
365
366 (defun gnus-apply-kill-file-internal ()
367   "Apply a kill file to the current newsgroup.
368 Returns the number of articles marked as read."
369   (let* ((kill-files (list (gnus-newsgroup-kill-file nil)
370                            (gnus-newsgroup-kill-file gnus-newsgroup-name)))
371          (unreads (length gnus-newsgroup-unreads))
372          (gnus-summary-inhibit-highlight t)
373          beg)
374     (setq gnus-newsgroup-kill-headers nil)
375     ;; If there are any previously scored articles, we remove these
376     ;; from the `gnus-newsgroup-headers' list that the score functions
377     ;; will see.  This is probably pretty wasteful when it comes to
378     ;; conses, but is, I think, faster than having to assq in every
379     ;; single score function.
380     (let ((files kill-files))
381       (while files
382         (if (file-exists-p (car files))
383             (let ((headers gnus-newsgroup-headers))
384               (if gnus-kill-killed
385                   (setq gnus-newsgroup-kill-headers
386                         (mapcar (lambda (header) (mail-header-number header))
387                                 headers))
388                 (while headers
389                   (unless (gnus-member-of-range 
390                            (mail-header-number (car headers))
391                            gnus-newsgroup-killed)
392                     (push (mail-header-number (car headers))
393                           gnus-newsgroup-kill-headers))
394                   (setq headers (cdr headers))))
395               (setq files nil))
396           (setq files (cdr files)))))
397     (if (not gnus-newsgroup-kill-headers)
398         ()
399       (save-window-excursion
400         (save-excursion
401           (while kill-files
402             (if (not (file-exists-p (car kill-files)))
403                 ()
404               (gnus-message 6 "Processing kill file %s..." (car kill-files))
405               (find-file (car kill-files))
406               (gnus-add-current-to-buffer-list)
407               (goto-char (point-min))
408
409               (if (consp (ignore-errors (read (current-buffer))))
410                   (gnus-kill-parse-gnus-kill-file)
411                 (gnus-kill-parse-rn-kill-file))
412             
413               (gnus-message 
414                6 "Processing kill file %s...done" (car kill-files)))
415             (setq kill-files (cdr kill-files)))))
416
417       (gnus-set-mode-line 'summary)
418
419       (if beg
420           (let ((nunreads (- unreads (length gnus-newsgroup-unreads))))
421             (or (eq nunreads 0)
422                 (gnus-message 6 "Marked %d articles as read" nunreads))
423             nunreads)
424         0))))
425
426 ;; Parse a Gnus killfile.
427 (defun gnus-score-insert-help (string alist idx)
428   (save-excursion
429     (pop-to-buffer "*Score Help*")
430     (buffer-disable-undo (current-buffer))
431     (erase-buffer)
432     (insert string ":\n\n")
433     (while alist
434       (insert (format " %c: %s\n" (caar alist) (nth idx (car alist))))
435       (setq alist (cdr alist)))))
436
437 (defun gnus-kill-parse-gnus-kill-file ()
438   (goto-char (point-min))
439   (gnus-kill-file-mode)
440   (let (beg form)
441     (while (progn 
442              (setq beg (point))
443              (setq form (ignore-errors (read (current-buffer)))))
444       (unless (listp form)
445         (error "Illegal kill entry (possibly rn kill file?): %s" form))
446       (if (or (eq (car form) 'gnus-kill)
447               (eq (car form) 'gnus-raise)
448               (eq (car form) 'gnus-lower))
449           (progn
450             (delete-region beg (point))
451             (insert (or (eval form) "")))
452         (save-excursion
453           (set-buffer gnus-summary-buffer)
454           (ignore-errors (eval form)))))
455     (and (buffer-modified-p)
456          gnus-kill-save-kill-file
457          (save-buffer))
458     (set-buffer-modified-p nil)))
459
460 ;; Parse an rn killfile.
461 (defun gnus-kill-parse-rn-kill-file ()
462   (goto-char (point-min))
463   (gnus-kill-file-mode)
464   (let ((mod-to-header
465          '((?a . "")
466            (?h . "")
467            (?f . "from")
468            (?: . "subject")))
469         (com-to-com
470          '((?m . " ")
471            (?j . "X")))
472         pattern modifier commands)
473     (while (not (eobp))
474       (if (not (looking-at "[ \t]*/\\([^/]*\\)/\\([ahfcH]\\)?:\\([a-z=:]*\\)"))
475           ()
476         (setq pattern (buffer-substring (match-beginning 1) (match-end 1)))
477         (setq modifier (if (match-beginning 2) (char-after (match-beginning 2))
478                          ?s))
479         (setq commands (buffer-substring (match-beginning 3) (match-end 3)))
480
481         ;; The "f:+" command marks everything *but* the matches as read,
482         ;; so we simply first match everything as read, and then unmark
483         ;; PATTERN later. 
484         (when (string-match "\\+" commands)
485           (gnus-kill "from" ".")
486           (setq commands "m"))
487
488         (gnus-kill 
489          (or (cdr (assq modifier mod-to-header)) "subject")
490          pattern 
491          (if (string-match "m" commands)
492              '(gnus-summary-mark-as-unread nil " ")
493            '(gnus-summary-mark-as-read nil "X"))
494          nil t))
495       (forward-line 1))))
496
497 ;; Kill changes and new format by suggested by JWZ and Sudish Joseph
498 ;; <joseph@cis.ohio-state.edu>.  
499 (defun gnus-kill (field regexp &optional exe-command all silent)
500   "If FIELD of an article matches REGEXP, execute COMMAND.
501 Optional 1st argument COMMAND is default to
502         (gnus-summary-mark-as-read nil \"X\").
503 If optional 2nd argument ALL is non-nil, articles marked are also applied to.
504 If FIELD is an empty string (or nil), entire article body is searched for.
505 COMMAND must be a lisp expression or a string representing a key sequence."
506   ;; We don't want to change current point nor window configuration.
507   (let ((old-buffer (current-buffer)))
508     (save-excursion
509       (save-window-excursion
510         ;; Selected window must be summary buffer to execute keyboard
511         ;; macros correctly.  See command_loop_1.
512         (switch-to-buffer gnus-summary-buffer 'norecord)
513         (goto-char (point-min))         ;From the beginning.
514         (let ((kill-list regexp)
515               (date (current-time-string))
516               (command (or exe-command '(gnus-summary-mark-as-read 
517                                          nil gnus-kill-file-mark)))
518               kill kdate prev)
519           (if (listp kill-list)
520               ;; It is a list.
521               (if (not (consp (cdr kill-list)))
522                   ;; It's on the form (regexp . date).
523                   (if (zerop (gnus-execute field (car kill-list)
524                                            command nil (not all)))
525                       (when (> (gnus-days-between date (cdr kill-list))
526                                gnus-kill-expiry-days)
527                         (setq regexp nil))
528                     (setcdr kill-list date))
529                 (while (setq kill (car kill-list))
530                   (if (consp kill)
531                       ;; It's a temporary kill.
532                       (progn
533                         (setq kdate (cdr kill))
534                         (if (zerop (gnus-execute 
535                                     field (car kill) command nil (not all)))
536                             (when (> (gnus-days-between date kdate)
537                                      gnus-kill-expiry-days)
538                               ;; Time limit has been exceeded, so we
539                               ;; remove the match.
540                               (if prev
541                                   (setcdr prev (cdr kill-list))
542                                 (setq regexp (cdr regexp))))
543                           ;; Successful kill.  Set the date to today.
544                           (setcdr kill date)))
545                     ;; It's a permanent kill.
546                     (gnus-execute field kill command nil (not all)))
547                   (setq prev kill-list)
548                   (setq kill-list (cdr kill-list))))
549             (gnus-execute field kill-list command nil (not all))))))
550     (switch-to-buffer old-buffer)
551     (when (and (eq major-mode 'gnus-kill-file-mode) regexp (not silent))
552       (gnus-pp-gnus-kill
553        (nconc (list 'gnus-kill field 
554                     (if (consp regexp) (list 'quote regexp) regexp))
555               (when (or exe-command all)
556                 (list (list 'quote exe-command)))
557               (if all (list t) nil))))))
558
559 (defun gnus-pp-gnus-kill (object)
560   (if (or (not (consp (nth 2 object)))
561           (not (consp (cdr (nth 2 object))))
562           (and (eq 'quote (car (nth 2 object)))
563                (not (consp (cdadr (nth 2 object))))))
564       (concat "\n" (gnus-prin1-to-string object))
565     (save-excursion
566       (set-buffer (get-buffer-create "*Gnus PP*"))
567       (buffer-disable-undo (current-buffer))
568       (erase-buffer)
569       (insert (format "\n(%S %S\n  '(" (nth 0 object) (nth 1 object)))
570       (let ((klist (cadr (nth 2 object)))
571             (first t))
572         (while klist
573           (insert (if first (progn (setq first nil) "")  "\n    ")
574                   (gnus-prin1-to-string (car klist)))
575           (setq klist (cdr klist))))
576       (insert ")")
577       (and (nth 3 object)
578            (insert "\n  " 
579                    (if (and (consp (nth 3 object))
580                             (not (eq 'quote (car (nth 3 object)))))
581                        "'" "")
582                    (gnus-prin1-to-string (nth 3 object))))
583       (when (nth 4 object)
584         (insert "\n  t"))
585       (insert ")")
586       (prog1
587           (buffer-substring (point-min) (point-max))
588         (kill-buffer (current-buffer))))))
589
590 (defun gnus-execute-1 (function regexp form header)
591   (save-excursion
592     (let (did-kill)
593       (if (null header)
594           nil                           ;Nothing to do.
595         (if function
596             ;; Compare with header field.
597             (let (value)
598               (and header
599                    (progn
600                      (setq value (funcall function header))
601                      ;; Number (Lines:) or symbol must be converted to string.
602                      (unless (stringp value)
603                        (setq value (gnus-prin1-to-string value)))
604                      (setq did-kill (string-match regexp value)))
605                    (cond ((stringp form) ;Keyboard macro.
606                           (execute-kbd-macro form))
607                          ((gnus-functionp form)
608                           (funcall form))
609                          (t
610                           (eval form)))))
611           ;; Search article body.
612           (let ((gnus-current-article nil) ;Save article pointer.
613                 (gnus-last-article nil)
614                 (gnus-break-pages nil)  ;No need to break pages.
615                 (gnus-mark-article-hook nil)) ;Inhibit marking as read.
616             (gnus-message 
617              6 "Searching for article: %d..." (mail-header-number header))
618             (gnus-article-setup-buffer)
619             (gnus-article-prepare (mail-header-number header) t)
620             (when (save-excursion
621                     (set-buffer gnus-article-buffer)
622                     (goto-char (point-min))
623                     (setq did-kill (re-search-forward regexp nil t)))
624               (cond ((stringp form)     ;Keyboard macro.
625                      (execute-kbd-macro form))
626                     ((gnus-functionp form)
627                      (funcall form))
628                     (t
629                      (eval form)))))))
630       did-kill)))
631
632 (defun gnus-execute (field regexp form &optional backward unread)
633   "If FIELD of article header matches REGEXP, execute lisp FORM (or a string).
634 If FIELD is an empty string (or nil), entire article body is searched for.
635 If optional 1st argument BACKWARD is non-nil, do backward instead.
636 If optional 2nd argument UNREAD is non-nil, articles which are
637 marked as read or ticked are ignored."
638   (save-excursion
639     (let ((killed-no 0)
640           function article header)
641       (cond 
642        ;; Search body.
643        ((or (null field)
644             (string-equal field ""))
645         (setq function nil))
646        ;; Get access function of header field.
647        ((fboundp
648          (setq function 
649                (intern-soft 
650                 (concat "mail-header-" (downcase field)))))
651         (setq function `(lambda (h) (,function h))))
652        ;; Signal error.
653        (t
654         (error "Unknown header field: \"%s\"" field)))
655       ;; Starting from the current article.
656       (while (or
657               ;; First article.
658               (and (not article)
659                    (setq article (gnus-summary-article-number)))
660               ;; Find later articles.
661               (setq article 
662                     (gnus-summary-search-forward unread nil backward)))
663         (and (or (null gnus-newsgroup-kill-headers)
664                  (memq article gnus-newsgroup-kill-headers))
665              (vectorp (setq header (gnus-summary-article-header article)))
666              (gnus-execute-1 function regexp form header)
667              (setq killed-no (1+ killed-no))))
668       ;; Return the number of killed articles.
669       killed-no)))
670
671 ;;;###autoload
672 (defalias 'gnus-batch-kill 'gnus-batch-score)
673 ;;;###autoload
674 (defun gnus-batch-score ()
675   "Run batched scoring.
676 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
677 Newsgroups is a list of strings in Bnews format.  If you want to score
678 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
679 score the alt hierarchy, you'd say \"!alt.all\"."
680   (interactive)
681   (let* ((gnus-newsrc-options-n    
682           (gnus-newsrc-parse-options
683            (concat "options -n "
684                    (mapconcat 'identity command-line-args-left " "))))
685          (gnus-expert-user t)
686          (nnmail-spool-file nil)
687          (gnus-use-dribble-file nil)
688          (gnus-batch-mode t)
689          group newsrc entry
690          ;; Disable verbose message.
691          gnus-novice-user gnus-large-newsgroup
692          gnus-options-subscribe gnus-auto-subscribed-groups
693          gnus-options-not-subscribe)
694     ;; Eat all arguments.
695     (setq command-line-args-left nil)
696     (gnus-slave)
697     ;; Apply kills to specified newsgroups in command line arguments.
698     (setq newsrc (cdr gnus-newsrc-alist))
699     (while (setq group (car (pop newsrc)))
700       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
701       (when (and (<= (gnus-info-level (car newsrc)) gnus-level-subscribed)
702                  (and (car entry)
703                       (or (eq (car entry) t)
704                           (not (zerop (car entry)))))
705                  ;;(eq (gnus-matches-options-n group) 'subscribe)
706                  )
707         (gnus-summary-read-group group nil t nil t)
708         (when (eq (current-buffer) (get-buffer gnus-summary-buffer))
709           (gnus-summary-exit))))
710     ;; Exit Emacs.
711     (switch-to-buffer gnus-group-buffer)
712     (gnus-group-save-newsrc)))
713
714 (provide 'gnus-kill)
715
716 ;;; gnus-kill.el ends here