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