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