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