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