Merge from emacs-24; up to 2012-05-08T15:19:18Z!monnier@iro.umontreal.ca
[bpt/emacs.git] / lisp / gnus / gnus-kill.el
CommitLineData
eec82323 1;;; gnus-kill.el --- kill commands for Gnus
e84b4b86 2
acaf905b 3;; Copyright (C) 1995-2012 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
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
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
154gnus-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)
cfcd5c91 162 (gnus-run-mode-hooks 'emacs-lisp-mode-hook 'gnus-kill-file-mode-hook))
eec82323
LMI
163
164(defun gnus-kill-file-edit-file (newsgroup)
165 "Begin editing a kill file for NEWSGROUP.
166If 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
eec82323
LMI
331(defun gnus-expunge (marks)
332 "Remove lines marked with MARKS."
20a673b2 333 (with-current-buffer gnus-summary-buffer
eec82323
LMI
334 (gnus-summary-limit-to-marks marks 'reverse)))
335
336(defun gnus-apply-kill-file-unless-scored ()
337 "Apply .KILL file, unless a .SCORE file for the same newsgroup exists."
338 (cond ((file-exists-p (gnus-score-file-name gnus-newsgroup-name))
23f87bed
MB
339 ;; Ignores global KILL.
340 (when (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name))
eec82323
LMI
341 (gnus-message 3 "Note: Ignoring %s.KILL; preferring .SCORE"
342 gnus-newsgroup-name))
23f87bed
MB
343 0)
344 ((or (file-exists-p (gnus-newsgroup-kill-file nil))
345 (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
346 (gnus-apply-kill-file-internal))
347 (t
348 0)))
eec82323
LMI
349
350(defun gnus-apply-kill-file-internal ()
351 "Apply a kill file to the current newsgroup.
352Returns the number of articles marked as read."
353 (let* ((kill-files (list (gnus-newsgroup-kill-file nil)
354 (gnus-newsgroup-kill-file gnus-newsgroup-name)))
355 (unreads (length gnus-newsgroup-unreads))
356 (gnus-summary-inhibit-highlight t)
357 beg)
358 (setq gnus-newsgroup-kill-headers nil)
359 ;; If there are any previously scored articles, we remove these
360 ;; from the `gnus-newsgroup-headers' list that the score functions
361 ;; will see. This is probably pretty wasteful when it comes to
362 ;; conses, but is, I think, faster than having to assq in every
363 ;; single score function.
364 (let ((files kill-files))
365 (while files
366 (if (file-exists-p (car files))
367 (let ((headers gnus-newsgroup-headers))
368 (if gnus-kill-killed
369 (setq gnus-newsgroup-kill-headers
370 (mapcar (lambda (header) (mail-header-number header))
371 headers))
372 (while headers
373 (unless (gnus-member-of-range
374 (mail-header-number (car headers))
375 gnus-newsgroup-killed)
376 (push (mail-header-number (car headers))
377 gnus-newsgroup-kill-headers))
378 (setq headers (cdr headers))))
379 (setq files nil))
23f87bed 380 (setq files (cdr files)))))
eec82323
LMI
381 (if (not gnus-newsgroup-kill-headers)
382 ()
383 (save-window-excursion
384 (save-excursion
385 (while kill-files
386 (if (not (file-exists-p (car kill-files)))
387 ()
388 (gnus-message 6 "Processing kill file %s..." (car kill-files))
389 (find-file (car kill-files))
eec82323
LMI
390 (goto-char (point-min))
391
392 (if (consp (ignore-errors (read (current-buffer))))
393 (gnus-kill-parse-gnus-kill-file)
394 (gnus-kill-parse-rn-kill-file))
395
396 (gnus-message
397 6 "Processing kill file %s...done" (car kill-files)))
398 (setq kill-files (cdr kill-files)))))
399
400 (gnus-set-mode-line 'summary)
401
402 (if beg
403 (let ((nunreads (- unreads (length gnus-newsgroup-unreads))))
404 (or (eq nunreads 0)
405 (gnus-message 6 "Marked %d articles as read" nunreads))
406 nunreads)
407 0))))
408
409;; Parse a Gnus killfile.
eec82323
LMI
410(defun gnus-kill-parse-gnus-kill-file ()
411 (goto-char (point-min))
412 (gnus-kill-file-mode)
413 (let (beg form)
414 (while (progn
415 (setq beg (point))
416 (setq form (ignore-errors (read (current-buffer)))))
417 (unless (listp form)
16409b0b 418 (error "Invalid kill entry (possibly rn kill file?): %s" form))
eec82323
LMI
419 (if (or (eq (car form) 'gnus-kill)
420 (eq (car form) 'gnus-raise)
421 (eq (car form) 'gnus-lower))
422 (progn
423 (delete-region beg (point))
424 (insert (or (eval form) "")))
20a673b2 425 (with-current-buffer gnus-summary-buffer
eec82323
LMI
426 (ignore-errors (eval form)))))
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")))
6748645f
LMI
441 ;;(com-to-com
442 ;; '((?m . " ")
443 ;; (?j . "X")))
eec82323
LMI
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 (when (string-match "\\+" commands)
457 (gnus-kill "from" ".")
458 (setq commands "m"))
459
460 (gnus-kill
461 (or (cdr (assq modifier mod-to-header)) "subject")
462 pattern
463 (if (string-match "m" commands)
6ffab592 464 '(gnus-summary-tick-article nil " ")
eec82323
LMI
465 '(gnus-summary-mark-as-read nil "X"))
466 nil t))
467 (forward-line 1))))
468
469;; Kill changes and new format by suggested by JWZ and Sudish Joseph
470;; <joseph@cis.ohio-state.edu>.
471(defun gnus-kill (field regexp &optional exe-command all silent)
472 "If FIELD of an article matches REGEXP, execute COMMAND.
473Optional 1st argument COMMAND is default to
474 (gnus-summary-mark-as-read nil \"X\").
475If optional 2nd argument ALL is non-nil, articles marked are also applied to.
476If FIELD is an empty string (or nil), entire article body is searched for.
01c52d31 477COMMAND must be a Lisp expression or a string representing a key sequence."
eec82323
LMI
478 ;; We don't want to change current point nor window configuration.
479 (let ((old-buffer (current-buffer)))
480 (save-excursion
481 (save-window-excursion
482 ;; Selected window must be summary buffer to execute keyboard
483 ;; macros correctly. See command_loop_1.
484 (switch-to-buffer gnus-summary-buffer 'norecord)
485 (goto-char (point-min)) ;From the beginning.
486 (let ((kill-list regexp)
487 (date (current-time-string))
488 (command (or exe-command '(gnus-summary-mark-as-read
489 nil gnus-kill-file-mark)))
490 kill kdate prev)
491 (if (listp kill-list)
492 ;; It is a list.
493 (if (not (consp (cdr kill-list)))
b262af48 494 ;; It's of the form (regexp . date).
eec82323
LMI
495 (if (zerop (gnus-execute field (car kill-list)
496 command nil (not all)))
16409b0b 497 (when (> (days-between date (cdr kill-list))
eec82323
LMI
498 gnus-kill-expiry-days)
499 (setq regexp nil))
500 (setcdr kill-list date))
501 (while (setq kill (car kill-list))
502 (if (consp kill)
503 ;; It's a temporary kill.
504 (progn
505 (setq kdate (cdr kill))
506 (if (zerop (gnus-execute
507 field (car kill) command nil (not all)))
16409b0b 508 (when (> (days-between date kdate)
eec82323
LMI
509 gnus-kill-expiry-days)
510 ;; Time limit has been exceeded, so we
511 ;; remove the match.
512 (if prev
513 (setcdr prev (cdr kill-list))
514 (setq regexp (cdr regexp))))
515 ;; Successful kill. Set the date to today.
516 (setcdr kill date)))
517 ;; It's a permanent kill.
518 (gnus-execute field kill command nil (not all)))
519 (setq prev kill-list)
520 (setq kill-list (cdr kill-list))))
521 (gnus-execute field kill-list command nil (not all))))))
522 (switch-to-buffer old-buffer)
523 (when (and (eq major-mode 'gnus-kill-file-mode) regexp (not silent))
524 (gnus-pp-gnus-kill
525 (nconc (list 'gnus-kill field
526 (if (consp regexp) (list 'quote regexp) regexp))
527 (when (or exe-command all)
528 (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 (cdadr (nth 2 object))))))
536 (concat "\n" (gnus-prin1-to-string object))
20a673b2 537 (with-current-buffer (gnus-get-buffer-create "*Gnus PP*")
16409b0b 538 (buffer-disable-undo)
eec82323
LMI
539 (erase-buffer)
540 (insert (format "\n(%S %S\n '(" (nth 0 object) (nth 1 object)))
541 (let ((klist (cadr (nth 2 object)))
542 (first t))
543 (while klist
544 (insert (if first (progn (setq first nil) "") "\n ")
545 (gnus-prin1-to-string (car klist)))
546 (setq klist (cdr klist))))
547 (insert ")")
548 (and (nth 3 object)
549 (insert "\n "
550 (if (and (consp (nth 3 object))
551 (not (eq 'quote (car (nth 3 object)))))
552 "'" "")
553 (gnus-prin1-to-string (nth 3 object))))
554 (when (nth 4 object)
555 (insert "\n t"))
556 (insert ")")
557 (prog1
23f87bed 558 (buffer-string)
eec82323
LMI
559 (kill-buffer (current-buffer))))))
560
561(defun gnus-execute-1 (function regexp form header)
562 (save-excursion
563 (let (did-kill)
564 (if (null header)
565 nil ;Nothing to do.
566 (if function
567 ;; Compare with header field.
568 (let (value)
569 (and header
570 (progn
571 (setq value (funcall function header))
572 ;; Number (Lines:) or symbol must be converted to string.
573 (unless (stringp value)
574 (setq value (gnus-prin1-to-string value)))
575 (setq did-kill (string-match regexp value)))
576 (cond ((stringp form) ;Keyboard macro.
577 (execute-kbd-macro form))
23f87bed 578 ((functionp form)
eec82323
LMI
579 (funcall form))
580 (t
581 (eval form)))))
582 ;; Search article body.
583 (let ((gnus-current-article nil) ;Save article pointer.
584 (gnus-last-article nil)
585 (gnus-break-pages nil) ;No need to break pages.
586 (gnus-mark-article-hook nil)) ;Inhibit marking as read.
587 (gnus-message
588 6 "Searching for article: %d..." (mail-header-number header))
589 (gnus-article-setup-buffer)
590 (gnus-article-prepare (mail-header-number header) t)
20a673b2 591 (when (with-current-buffer gnus-article-buffer
eec82323
LMI
592 (goto-char (point-min))
593 (setq did-kill (re-search-forward regexp nil t)))
594 (cond ((stringp form) ;Keyboard macro.
595 (execute-kbd-macro form))
23f87bed 596 ((functionp form)
eec82323
LMI
597 (funcall form))
598 (t
599 (eval form)))))))
600 did-kill)))
601
602(defun gnus-execute (field regexp form &optional backward unread)
01c52d31 603 "If FIELD of article header matches REGEXP, execute Lisp FORM (or a string).
eec82323
LMI
604If FIELD is an empty string (or nil), entire article body is searched for.
605If optional 1st argument BACKWARD is non-nil, do backward instead.
606If optional 2nd argument UNREAD is non-nil, articles which are
607marked as read or ticked are ignored."
608 (save-excursion
609 (let ((killed-no 0)
23f87bed 610 function article header extras)
eec82323
LMI
611 (cond
612 ;; Search body.
613 ((or (null field)
614 (string-equal field ""))
615 (setq function nil))
616 ;; Get access function of header field.
23f87bed
MB
617 ((cond ((fboundp
618 (setq function
619 (intern-soft
620 (concat "mail-header-" (downcase field)))))
621 (setq function `(lambda (h) (,function h))))
622 ((when (setq extras
623 (member (downcase field)
624 (mapcar (lambda (header)
625 (downcase (symbol-name header)))
626 gnus-extra-headers)))
627 (setq function
628 `(lambda (h)
629 (gnus-extra-header
630 (quote ,(nth (- (length gnus-extra-headers)
631 (length extras))
632 gnus-extra-headers))
633 h)))))))
eec82323
LMI
634 ;; Signal error.
635 (t
636 (error "Unknown header field: \"%s\"" field)))
637 ;; Starting from the current article.
638 (while (or
639 ;; First article.
640 (and (not article)
641 (setq article (gnus-summary-article-number)))
642 ;; Find later articles.
643 (setq article
644 (gnus-summary-search-forward unread nil backward)))
645 (and (or (null gnus-newsgroup-kill-headers)
646 (memq article gnus-newsgroup-kill-headers))
647 (vectorp (setq header (gnus-summary-article-header article)))
648 (gnus-execute-1 function regexp form header)
649 (setq killed-no (1+ killed-no))))
650 ;; Return the number of killed articles.
651 killed-no)))
652
653;;;###autoload
654(defalias 'gnus-batch-kill 'gnus-batch-score)
655;;;###autoload
656(defun gnus-batch-score ()
657 "Run batched scoring.
6748645f 658Usage: emacs -batch -l ~/.emacs -l gnus -f gnus-batch-score"
eec82323
LMI
659 (interactive)
660 (let* ((gnus-newsrc-options-n
661 (gnus-newsrc-parse-options
662 (concat "options -n "
663 (mapconcat 'identity command-line-args-left " "))))
664 (gnus-expert-user t)
16409b0b 665 (mail-sources nil)
eec82323
LMI
666 (gnus-use-dribble-file nil)
667 (gnus-batch-mode t)
01c52d31 668 info group newsrc unread
eec82323
LMI
669 ;; Disable verbose message.
670 gnus-novice-user gnus-large-newsgroup
671 gnus-options-subscribe gnus-auto-subscribed-groups
672 gnus-options-not-subscribe)
673 ;; Eat all arguments.
674 (setq command-line-args-left nil)
675 (gnus-slave)
676 ;; Apply kills to specified newsgroups in command line arguments.
677 (setq newsrc (cdr gnus-newsrc-alist))
6748645f
LMI
678 (while (setq info (pop newsrc))
679 (setq group (gnus-info-group info)
01c52d31 680 unread (gnus-group-unread group))
6748645f 681 (when (and (<= (gnus-info-level info) gnus-level-subscribed)
01c52d31
MB
682 (and unread
683 (or (eq unread t)
684 (not (zerop unread)))))
16409b0b
GM
685 (ignore-errors
686 (gnus-summary-read-group group nil t nil t))
eec82323
LMI
687 (when (eq (current-buffer) (get-buffer gnus-summary-buffer))
688 (gnus-summary-exit))))
689 ;; Exit Emacs.
690 (switch-to-buffer gnus-group-buffer)
691 (gnus-group-save-newsrc)))
692
693(provide 'gnus-kill)
694
695;;; gnus-kill.el ends here