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