*** 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 (eval-when-compile (require 'cl))
31
32 (defvar gnus-kill-file-mode-hook nil
33   "*A hook for Gnus kill file mode.")
34
35 (defvar gnus-kill-expiry-days 7
36   "*Number of days before expiring unused kill file entries.")
37
38 (defvar gnus-kill-save-kill-file nil
39   "*If non-nil, will save kill files after processing them.")
40
41 (defvar gnus-winconf-kill-file nil)
42
43 \f
44
45 (defmacro gnus-raise (field expression level)
46   (` (gnus-kill (, field) (, expression)
47                 (function (gnus-summary-raise-score (, level))) t)))
48
49 (defmacro gnus-lower (field expression level)
50   (` (gnus-kill (, field) (, expression)
51                 (function (gnus-summary-raise-score (- (, level)))) t)))
52
53 ;;;
54 ;;; Gnus Kill File Mode
55 ;;;
56
57 (defvar gnus-kill-file-mode-map nil)
58
59 (if gnus-kill-file-mode-map
60     nil
61   (setq gnus-kill-file-mode-map (copy-keymap emacs-lisp-mode-map))
62   (define-key gnus-kill-file-mode-map 
63     "\C-c\C-k\C-s" 'gnus-kill-file-kill-by-subject)
64   (define-key gnus-kill-file-mode-map
65     "\C-c\C-k\C-a" 'gnus-kill-file-kill-by-author)
66   (define-key gnus-kill-file-mode-map
67     "\C-c\C-k\C-t" 'gnus-kill-file-kill-by-thread)
68   (define-key gnus-kill-file-mode-map 
69     "\C-c\C-k\C-x" 'gnus-kill-file-kill-by-xref)
70   (define-key gnus-kill-file-mode-map
71     "\C-c\C-a" 'gnus-kill-file-apply-buffer)
72   (define-key gnus-kill-file-mode-map
73     "\C-c\C-e" 'gnus-kill-file-apply-last-sexp)
74   (define-key gnus-kill-file-mode-map 
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)
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       (or (eq major-mode 'gnus-kill-file-mode)
191           (gnus-kill-set-kill-buffer))
192       (current-buffer)
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   
207 (defun gnus-kill-file-kill-by-author ()
208   "Kill by author."
209   (interactive)
210   (gnus-kill-file-enter-kill
211    "From" 
212    (if (vectorp gnus-current-headers)
213        (regexp-quote (mail-header-from gnus-current-headers))
214      "")))
215  
216 (defun gnus-kill-file-kill-by-thread ()
217   "Kill by author."
218   (interactive "p")
219   (gnus-kill-file-enter-kill
220    "References" 
221    (if (vectorp gnus-current-headers)
222        (regexp-quote (mail-header-id gnus-current-headers))
223      "")))
224  
225 (defun gnus-kill-file-kill-by-xref ()
226   "Kill by Xref."
227   (interactive)
228   (let ((xref (and (vectorp gnus-current-headers) 
229                    (mail-header-xref gnus-current-headers)))
230         (start 0)
231         group)
232     (if xref
233         (while (string-match " \\([^ \t]+\\):" xref start)
234           (setq start (match-end 0))
235           (if (not (string= 
236                     (setq group 
237                           (substring xref (match-beginning 1) (match-end 1)))
238                     gnus-newsgroup-name))
239               (gnus-kill-file-enter-kill 
240                "Xref" (concat " " (regexp-quote group) ":"))))
241       (gnus-kill-file-enter-kill "Xref" ""))))
242
243 (defun gnus-kill-file-raise-followups-to-author (level)
244   "Raise score for all followups to the current author."
245   (interactive "p")
246   (let ((name (mail-header-from gnus-current-headers))
247         string)
248     (save-excursion
249       (gnus-kill-set-kill-buffer)
250       (goto-char (point-min))
251       (setq name (read-string (concat "Add " level
252                                       " to followup articles to: ")
253                               (regexp-quote name)))
254       (setq 
255        string
256        (format
257         "(gnus-kill %S %S '(gnus-summary-temporarily-raise-by-thread %S))\n"
258         "From" name level))
259       (insert string)
260       (gnus-kill-file-apply-string string))
261     (message "Added temporary score file entry for followups to %s." name)))
262
263 (defun gnus-kill-file-apply-buffer ()
264   "Apply current buffer to current newsgroup."
265   (interactive)
266   (if (and gnus-current-kill-article
267            (get-buffer gnus-summary-buffer))
268       ;; Assume newsgroup is selected.
269       (gnus-kill-file-apply-string (buffer-string))
270     (ding) (message "No newsgroup is selected.")))
271
272 (defun gnus-kill-file-apply-string (string)
273   "Apply STRING to current newsgroup."
274   (interactive)
275   (let ((string (concat "(progn \n" string "\n)")))
276     (save-excursion
277       (save-window-excursion
278         (pop-to-buffer gnus-summary-buffer)
279         (eval (car (read-from-string string)))))))
280
281 (defun gnus-kill-file-apply-last-sexp ()
282   "Apply sexp before point in current buffer to current newsgroup."
283   (interactive)
284   (if (and gnus-current-kill-article
285            (get-buffer gnus-summary-buffer))
286       ;; Assume newsgroup is selected.
287       (let ((string
288              (buffer-substring
289               (save-excursion (forward-sexp -1) (point)) (point))))
290         (save-excursion
291           (save-window-excursion
292             (pop-to-buffer gnus-summary-buffer)
293             (eval (car (read-from-string string))))))
294     (ding) (message "No newsgroup is selected.")))
295
296 (defun gnus-kill-file-exit ()
297   "Save a kill file, then return to the previous buffer."
298   (interactive)
299   (save-buffer)
300   (let ((killbuf (current-buffer)))
301     ;; We don't want to return to article buffer.
302     (and (get-buffer gnus-article-buffer)
303          (bury-buffer gnus-article-buffer))
304     ;; Delete the KILL file windows.
305     (delete-windows-on killbuf)
306     ;; Restore last window configuration if available.
307     (and gnus-winconf-kill-file
308          (set-window-configuration gnus-winconf-kill-file))
309     (setq gnus-winconf-kill-file nil)
310     ;; Kill the KILL file buffer.  Suggested by tale@pawl.rpi.edu.
311     (kill-buffer killbuf)))
312
313 ;; For kill files
314
315 (defun gnus-Newsgroup-kill-file (newsgroup)
316   "Return the name of a kill file for NEWSGROUP.
317 If NEWSGROUP is nil, return the global kill file instead."
318   (cond ((or (null newsgroup)
319              (string-equal newsgroup ""))
320          ;; The global kill file is placed at top of the directory.
321          (expand-file-name gnus-kill-file-name
322                            (or gnus-kill-files-directory "~/News")))
323         (gnus-use-long-file-name
324          ;; Append ".KILL" to capitalized newsgroup name.
325          (expand-file-name (concat (gnus-capitalize-newsgroup newsgroup)
326                                    "." gnus-kill-file-name)
327                            (or gnus-kill-files-directory "~/News")))
328         (t
329          ;; Place "KILL" under the hierarchical directory.
330          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
331                                    "/" gnus-kill-file-name)
332                            (or gnus-kill-files-directory "~/News")))))
333
334 (defun gnus-expunge (marks)
335   "Remove lines marked with MARKS."
336   (save-excursion
337     (set-buffer gnus-summary-buffer)
338     (gnus-summary-limit-to-marks marks 'reverse)))
339
340 (defun gnus-apply-kill-file-internal ()
341   "Apply a kill file to the current newsgroup.
342 Returns the number of articles marked as read."
343   (let* ((kill-files (list (gnus-newsgroup-kill-file nil)
344                            (gnus-newsgroup-kill-file gnus-newsgroup-name)))
345          (unreads (length gnus-newsgroup-unreads))
346          (gnus-summary-inhibit-highlight t)
347          beg)
348     (setq gnus-newsgroup-kill-headers nil)
349     ;; If there are any previously scored articles, we remove these
350     ;; from the `gnus-newsgroup-headers' list that the score functions
351     ;; will see. This is probably pretty wasteful when it comes to
352     ;; conses, but is, I think, faster than having to assq in every
353     ;; single score function.
354     (let ((files kill-files))
355       (while files
356         (if (file-exists-p (car files))
357             (let ((headers gnus-newsgroup-headers))
358               (if gnus-kill-killed
359                   (setq gnus-newsgroup-kill-headers
360                         (mapcar (lambda (header) (mail-header-number header))
361                                 headers))
362                 (while headers
363                   (or (gnus-member-of-range 
364                        (mail-header-number (car headers)) 
365                        gnus-newsgroup-killed)
366                       (setq gnus-newsgroup-kill-headers 
367                             (cons (mail-header-number (car headers))
368                                   gnus-newsgroup-kill-headers)))
369                   (setq headers (cdr headers))))
370               (setq files nil))
371           (setq files (cdr files)))))
372     (if (not gnus-newsgroup-kill-headers)
373         ()
374       (save-window-excursion
375         (save-excursion
376           (while kill-files
377             (if (not (file-exists-p (car kill-files)))
378                 ()
379               (message "Processing kill file %s..." (car kill-files))
380               (find-file (car kill-files))
381               (gnus-add-current-to-buffer-list)
382               (goto-char (point-min))
383
384               (if (consp (condition-case nil (read (current-buffer)) 
385                            (error nil)))
386                   (gnus-kill-parse-gnus-kill-file)
387                 (gnus-kill-parse-rn-kill-file))
388             
389               (message "Processing kill file %s...done" (car kill-files)))
390             (setq kill-files (cdr kill-files)))))
391
392       (gnus-set-mode-line 'summary)
393
394       (if beg
395           (let ((nunreads (- unreads (length gnus-newsgroup-unreads))))
396             (or (eq nunreads 0)
397                 (message "Marked %d articles as read" nunreads))
398             nunreads)
399         0))))
400
401 ;; Parse a Gnus killfile.
402 (defun gnus-score-insert-help (string alist idx)
403   (save-excursion
404     (pop-to-buffer "*Score Help*")
405     (buffer-disable-undo (current-buffer))
406     (erase-buffer)
407     (insert string ":\n\n")
408     (while alist
409       (insert (format " %c: %s\n" (car (car alist)) (nth idx (car alist))))
410       (setq alist (cdr alist)))))
411
412 (defun gnus-kill-parse-gnus-kill-file ()
413   (goto-char (point-min))
414   (gnus-kill-file-mode)
415   (let (beg form)
416     (while (progn 
417              (setq beg (point))
418              (setq form (condition-case () (read (current-buffer))
419                           (error nil))))
420       (or (listp form)
421           (error "Illegal kill entry (possibly rn kill file?): %s" form))
422       (if (or (eq (car form) 'gnus-kill)
423               (eq (car form) 'gnus-raise)
424               (eq (car form) 'gnus-lower))
425           (progn
426             (delete-region beg (point))
427             (insert (or (eval form) "")))
428         (save-excursion
429           (set-buffer gnus-summary-buffer)
430           (condition-case () (eval form) (error nil)))))
431     (and (buffer-modified-p) 
432          gnus-kill-save-kill-file
433          (save-buffer))
434     (set-buffer-modified-p nil)))
435
436 ;; Parse an rn killfile.
437 (defun gnus-kill-parse-rn-kill-file ()
438   (goto-char (point-min))
439   (gnus-kill-file-mode)
440   (let ((mod-to-header
441          '((?a . "")
442            (?h . "")
443            (?f . "from")
444            (?: . "subject")))
445         (com-to-com
446          '((?m . " ")
447            (?j . "X")))
448         pattern modifier commands)
449     (while (not (eobp))
450       (if (not (looking-at "[ \t]*/\\([^/]*\\)/\\([ahfcH]\\)?:\\([a-z=:]*\\)"))
451           ()
452         (setq pattern (buffer-substring (match-beginning 1) (match-end 1)))
453         (setq modifier (if (match-beginning 2) (char-after (match-beginning 2))
454                          ?s))
455         (setq commands (buffer-substring (match-beginning 3) (match-end 3)))
456
457         ;; The "f:+" command marks everything *but* the matches as read,
458         ;; so we simply first match everything as read, and then unmark
459         ;; PATTERN later. 
460         (and (string-match "\\+" commands)
461              (progn
462                (gnus-kill "from" ".")
463                (setq commands "m")))
464
465         (gnus-kill 
466          (or (cdr (assq modifier mod-to-header)) "subject")
467          pattern 
468          (if (string-match "m" commands) 
469              '(gnus-summary-mark-as-unread nil " ")
470            '(gnus-summary-mark-as-read nil "X")) 
471          nil t))
472       (forward-line 1))))
473
474 ;; Kill changes and new format by suggested by JWZ and Sudish Joseph
475 ;; <joseph@cis.ohio-state.edu>.  
476 (defun gnus-kill (field regexp &optional exe-command all silent)
477   "If FIELD of an article matches REGEXP, execute COMMAND.
478 Optional 1st argument COMMAND is default to
479         (gnus-summary-mark-as-read nil \"X\").
480 If optional 2nd argument ALL is non-nil, articles marked are also applied to.
481 If FIELD is an empty string (or nil), entire article body is searched for.
482 COMMAND must be a lisp expression or a string representing a key sequence."
483   ;; We don't want to change current point nor window configuration.
484   (let ((old-buffer (current-buffer)))
485     (save-excursion
486       (save-window-excursion
487         ;; Selected window must be summary buffer to execute keyboard
488         ;; macros correctly. See command_loop_1.
489         (switch-to-buffer gnus-summary-buffer 'norecord)
490         (goto-char (point-min))         ;From the beginning.
491         (let ((kill-list regexp)
492               (date (current-time-string))
493               (command (or exe-command '(gnus-summary-mark-as-read 
494                                          nil gnus-kill-file-mark)))
495               kill kdate prev)
496           (if (listp kill-list)
497               ;; It is a list.
498               (if (not (consp (cdr kill-list)))
499                   ;; It's on the form (regexp . date).
500                   (if (zerop (gnus-execute field (car kill-list) 
501                                            command nil (not all)))
502                       (if (> (gnus-days-between date (cdr kill-list))
503                              gnus-kill-expiry-days)
504                           (setq regexp nil))
505                     (setcdr kill-list date))
506                 (while (setq kill (car kill-list))
507                   (if (consp kill)
508                       ;; It's a temporary kill.
509                       (progn
510                         (setq kdate (cdr kill))
511                         (if (zerop (gnus-execute 
512                                     field (car kill) command nil (not all)))
513                             (if (> (gnus-days-between date kdate)
514                                    gnus-kill-expiry-days)
515                                 ;; Time limit has been exceeded, so we
516                                 ;; remove the match.
517                                 (if prev
518                                     (setcdr prev (cdr kill-list))
519                                   (setq regexp (cdr regexp))))
520                           ;; Successful kill. Set the date to today.
521                           (setcdr kill date)))
522                     ;; It's a permanent kill.
523                     (gnus-execute field kill command nil (not all)))
524                   (setq prev kill-list)
525                   (setq kill-list (cdr kill-list))))
526             (gnus-execute field kill-list command nil (not all))))))
527     (switch-to-buffer old-buffer)
528     (if (and (eq major-mode 'gnus-kill-file-mode) regexp (not silent))
529         (gnus-pp-gnus-kill
530          (nconc (list 'gnus-kill field 
531                       (if (consp regexp) (list 'quote regexp) regexp))
532                 (if (or exe-command all) (list (list 'quote exe-command)))
533                 (if all (list t) nil))))))
534
535 (defun gnus-pp-gnus-kill (object)
536   (if (or (not (consp (nth 2 object)))
537           (not (consp (cdr (nth 2 object))))
538           (and (eq 'quote (car (nth 2 object)))
539                (not (consp (cdr (car (cdr (nth 2 object))))))))
540       (concat "\n" (prin1-to-string object))
541     (save-excursion
542       (set-buffer (get-buffer-create "*Gnus PP*"))
543       (buffer-disable-undo (current-buffer))
544       (erase-buffer)
545       (insert (format "\n(%S %S\n  '(" (nth 0 object) (nth 1 object)))
546       (let ((klist (car (cdr (nth 2 object))))
547             (first t))
548         (while klist
549           (insert (if first (progn (setq first nil) "")  "\n    ")
550                   (prin1-to-string (car klist)))
551           (setq klist (cdr klist))))
552       (insert ")")
553       (and (nth 3 object)
554            (insert "\n  " 
555                    (if (and (consp (nth 3 object))
556                             (not (eq 'quote (car (nth 3 object))))) 
557                        "'" "")
558                    (prin1-to-string (nth 3 object))))
559       (and (nth 4 object)
560            (insert "\n  t"))
561       (insert ")")
562       (prog1
563           (buffer-substring (point-min) (point-max))
564         (kill-buffer (current-buffer))))))
565
566 (defun gnus-execute-1 (function regexp form header)
567   (save-excursion
568     (let (did-kill)
569       (if (null header)
570           nil                           ;Nothing to do.
571         (if function
572             ;; Compare with header field.
573             (let (value)
574               (and header
575                    (progn
576                      (setq value (funcall function header))
577                      ;; Number (Lines:) or symbol must be converted to string.
578                      (or (stringp value)
579                          (setq value (prin1-to-string value)))
580                      (setq did-kill (string-match regexp value)))
581                    (cond ((stringp form)        ;Keyboard macro.
582                           (execute-kbd-macro form))
583                          ((gnus-functionp form)
584                           (funcall form))
585                          (t
586                           (eval form)))))
587           ;; Search article body.
588           (let ((gnus-current-article nil) ;Save article pointer.
589                 (gnus-last-article nil)
590                 (gnus-break-pages nil)  ;No need to break pages.
591                 (gnus-mark-article-hook nil)) ;Inhibit marking as read.
592             (message "Searching for article: %d..." (mail-header-number header))
593             (gnus-article-setup-buffer)
594             (gnus-article-prepare (mail-header-number header) t)
595             (if (save-excursion
596                   (set-buffer gnus-article-buffer)
597                   (goto-char (point-min))
598                   (setq did-kill (re-search-forward regexp nil t)))
599                 (if (stringp form)      ;Keyboard macro.
600                     (execute-kbd-macro form)
601                   (eval form))))))
602       did-kill)))
603
604 (defun gnus-execute (field regexp form &optional backward ignore-marked)
605   "If FIELD of article header matches REGEXP, execute lisp FORM (or a string).
606 If FIELD is an empty string (or nil), entire article body is searched for.
607 If optional 1st argument BACKWARD is non-nil, do backward instead.
608 If optional 2nd argument IGNORE-MARKED is non-nil, articles which are
609 marked as read or ticked are ignored."
610   (save-excursion
611     (let ((killed-no 0)
612           function article header)
613       (cond 
614        ;; Search body.
615        ((or (null field) 
616             (string-equal field ""))
617         (setq function nil))
618        ;; Get access function of header field.
619        ((fboundp
620          (setq function 
621                (intern-soft 
622                 (concat "mail-header-" (downcase field)))))
623         (setq function `(lambda (h) (,function h))))
624        ;; Signal error.
625        (t
626         (error "Unknown header field: \"%s\"" field)))
627       ;; Starting from the current article.
628       (while (or (and (not article)
629                       (setq article (gnus-summary-article-number))
630                       t)
631                  (setq article 
632                        (gnus-summary-search-forward 
633                         (not ignore-marked) nil backward)))
634         (and (or (null gnus-newsgroup-kill-headers)
635                  (memq article gnus-newsgroup-kill-headers))
636              (vectorp (setq header (gnus-summary-article-header article)))
637              (gnus-execute-1 function regexp form header)
638              (setq killed-no (1+ killed-no))))
639       killed-no)))
640
641 (provide 'gnus-kill)
642
643 ;;; gnus-kill.el ends here