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