Commit | Line | Data |
---|---|---|
eec82323 | 1 | ;;; gnus-kill.el --- kill commands for Gnus |
23f87bed | 2 | ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003 |
16409b0b | 3 | ;; 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 | ||
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 | ||
5ab7173c RS |
30 | (eval-when-compile (require 'cl)) |
31 | ||
eec82323 LMI |
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 | |
16409b0b GM |
53 | "What does this do, Lars? |
54 | I don't know, Per." | |
eec82323 LMI |
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) | |
6748645f | 164 | (gnus-run-hooks 'emacs-lisp-mode-hook 'gnus-kill-file-mode-hook)) |
eec82323 LMI |
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)) | |
23f87bed MB |
360 | ;; Ignores global KILL. |
361 | (when (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)) | |
eec82323 LMI |
362 | (gnus-message 3 "Note: Ignoring %s.KILL; preferring .SCORE" |
363 | gnus-newsgroup-name)) | |
23f87bed MB |
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))) | |
eec82323 LMI |
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)) | |
23f87bed | 401 | (setq files (cdr files))))) |
eec82323 LMI |
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)) | |
eec82323 LMI |
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. | |
eec82323 LMI |
431 | (defun gnus-kill-parse-gnus-kill-file () |
432 | (goto-char (point-min)) | |
433 | (gnus-kill-file-mode) | |
434 | (let (beg form) | |
435 | (while (progn | |
436 | (setq beg (point)) | |
437 | (setq form (ignore-errors (read (current-buffer))))) | |
438 | (unless (listp form) | |
16409b0b | 439 | (error "Invalid kill entry (possibly rn kill file?): %s" form)) |
eec82323 LMI |
440 | (if (or (eq (car form) 'gnus-kill) |
441 | (eq (car form) 'gnus-raise) | |
442 | (eq (car form) 'gnus-lower)) | |
443 | (progn | |
444 | (delete-region beg (point)) | |
445 | (insert (or (eval form) ""))) | |
446 | (save-excursion | |
447 | (set-buffer gnus-summary-buffer) | |
448 | (ignore-errors (eval form))))) | |
449 | (and (buffer-modified-p) | |
450 | gnus-kill-save-kill-file | |
451 | (save-buffer)) | |
452 | (set-buffer-modified-p nil))) | |
453 | ||
454 | ;; Parse an rn killfile. | |
455 | (defun gnus-kill-parse-rn-kill-file () | |
456 | (goto-char (point-min)) | |
457 | (gnus-kill-file-mode) | |
458 | (let ((mod-to-header | |
459 | '((?a . "") | |
460 | (?h . "") | |
461 | (?f . "from") | |
462 | (?: . "subject"))) | |
6748645f LMI |
463 | ;;(com-to-com |
464 | ;; '((?m . " ") | |
465 | ;; (?j . "X"))) | |
eec82323 LMI |
466 | pattern modifier commands) |
467 | (while (not (eobp)) | |
468 | (if (not (looking-at "[ \t]*/\\([^/]*\\)/\\([ahfcH]\\)?:\\([a-z=:]*\\)")) | |
469 | () | |
470 | (setq pattern (buffer-substring (match-beginning 1) (match-end 1))) | |
471 | (setq modifier (if (match-beginning 2) (char-after (match-beginning 2)) | |
472 | ?s)) | |
473 | (setq commands (buffer-substring (match-beginning 3) (match-end 3))) | |
474 | ||
475 | ;; The "f:+" command marks everything *but* the matches as read, | |
476 | ;; so we simply first match everything as read, and then unmark | |
477 | ;; PATTERN later. | |
478 | (when (string-match "\\+" commands) | |
479 | (gnus-kill "from" ".") | |
480 | (setq commands "m")) | |
481 | ||
482 | (gnus-kill | |
483 | (or (cdr (assq modifier mod-to-header)) "subject") | |
484 | pattern | |
485 | (if (string-match "m" commands) | |
486 | '(gnus-summary-mark-as-unread nil " ") | |
487 | '(gnus-summary-mark-as-read nil "X")) | |
488 | nil t)) | |
489 | (forward-line 1)))) | |
490 | ||
491 | ;; Kill changes and new format by suggested by JWZ and Sudish Joseph | |
492 | ;; <joseph@cis.ohio-state.edu>. | |
493 | (defun gnus-kill (field regexp &optional exe-command all silent) | |
494 | "If FIELD of an article matches REGEXP, execute COMMAND. | |
495 | Optional 1st argument COMMAND is default to | |
496 | (gnus-summary-mark-as-read nil \"X\"). | |
497 | If optional 2nd argument ALL is non-nil, articles marked are also applied to. | |
498 | If FIELD is an empty string (or nil), entire article body is searched for. | |
499 | COMMAND must be a lisp expression or a string representing a key sequence." | |
500 | ;; We don't want to change current point nor window configuration. | |
501 | (let ((old-buffer (current-buffer))) | |
502 | (save-excursion | |
503 | (save-window-excursion | |
504 | ;; Selected window must be summary buffer to execute keyboard | |
505 | ;; macros correctly. See command_loop_1. | |
506 | (switch-to-buffer gnus-summary-buffer 'norecord) | |
507 | (goto-char (point-min)) ;From the beginning. | |
508 | (let ((kill-list regexp) | |
509 | (date (current-time-string)) | |
510 | (command (or exe-command '(gnus-summary-mark-as-read | |
511 | nil gnus-kill-file-mark))) | |
512 | kill kdate prev) | |
513 | (if (listp kill-list) | |
514 | ;; It is a list. | |
515 | (if (not (consp (cdr kill-list))) | |
b262af48 | 516 | ;; It's of the form (regexp . date). |
eec82323 LMI |
517 | (if (zerop (gnus-execute field (car kill-list) |
518 | command nil (not all))) | |
16409b0b | 519 | (when (> (days-between date (cdr kill-list)) |
eec82323 LMI |
520 | gnus-kill-expiry-days) |
521 | (setq regexp nil)) | |
522 | (setcdr kill-list date)) | |
523 | (while (setq kill (car kill-list)) | |
524 | (if (consp kill) | |
525 | ;; It's a temporary kill. | |
526 | (progn | |
527 | (setq kdate (cdr kill)) | |
528 | (if (zerop (gnus-execute | |
529 | field (car kill) command nil (not all))) | |
16409b0b | 530 | (when (> (days-between date kdate) |
eec82323 LMI |
531 | gnus-kill-expiry-days) |
532 | ;; Time limit has been exceeded, so we | |
533 | ;; remove the match. | |
534 | (if prev | |
535 | (setcdr prev (cdr kill-list)) | |
536 | (setq regexp (cdr regexp)))) | |
537 | ;; Successful kill. Set the date to today. | |
538 | (setcdr kill date))) | |
539 | ;; It's a permanent kill. | |
540 | (gnus-execute field kill command nil (not all))) | |
541 | (setq prev kill-list) | |
542 | (setq kill-list (cdr kill-list)))) | |
543 | (gnus-execute field kill-list command nil (not all)))))) | |
544 | (switch-to-buffer old-buffer) | |
545 | (when (and (eq major-mode 'gnus-kill-file-mode) regexp (not silent)) | |
546 | (gnus-pp-gnus-kill | |
547 | (nconc (list 'gnus-kill field | |
548 | (if (consp regexp) (list 'quote regexp) regexp)) | |
549 | (when (or exe-command all) | |
550 | (list (list 'quote exe-command))) | |
551 | (if all (list t) nil)))))) | |
552 | ||
553 | (defun gnus-pp-gnus-kill (object) | |
554 | (if (or (not (consp (nth 2 object))) | |
555 | (not (consp (cdr (nth 2 object)))) | |
556 | (and (eq 'quote (car (nth 2 object))) | |
557 | (not (consp (cdadr (nth 2 object)))))) | |
558 | (concat "\n" (gnus-prin1-to-string object)) | |
559 | (save-excursion | |
6748645f | 560 | (set-buffer (gnus-get-buffer-create "*Gnus PP*")) |
16409b0b | 561 | (buffer-disable-undo) |
eec82323 LMI |
562 | (erase-buffer) |
563 | (insert (format "\n(%S %S\n '(" (nth 0 object) (nth 1 object))) | |
564 | (let ((klist (cadr (nth 2 object))) | |
565 | (first t)) | |
566 | (while klist | |
567 | (insert (if first (progn (setq first nil) "") "\n ") | |
568 | (gnus-prin1-to-string (car klist))) | |
569 | (setq klist (cdr klist)))) | |
570 | (insert ")") | |
571 | (and (nth 3 object) | |
572 | (insert "\n " | |
573 | (if (and (consp (nth 3 object)) | |
574 | (not (eq 'quote (car (nth 3 object))))) | |
575 | "'" "") | |
576 | (gnus-prin1-to-string (nth 3 object)))) | |
577 | (when (nth 4 object) | |
578 | (insert "\n t")) | |
579 | (insert ")") | |
580 | (prog1 | |
23f87bed | 581 | (buffer-string) |
eec82323 LMI |
582 | (kill-buffer (current-buffer)))))) |
583 | ||
584 | (defun gnus-execute-1 (function regexp form header) | |
585 | (save-excursion | |
586 | (let (did-kill) | |
587 | (if (null header) | |
588 | nil ;Nothing to do. | |
589 | (if function | |
590 | ;; Compare with header field. | |
591 | (let (value) | |
592 | (and header | |
593 | (progn | |
594 | (setq value (funcall function header)) | |
595 | ;; Number (Lines:) or symbol must be converted to string. | |
596 | (unless (stringp value) | |
597 | (setq value (gnus-prin1-to-string value))) | |
598 | (setq did-kill (string-match regexp value))) | |
599 | (cond ((stringp form) ;Keyboard macro. | |
600 | (execute-kbd-macro form)) | |
23f87bed | 601 | ((functionp form) |
eec82323 LMI |
602 | (funcall form)) |
603 | (t | |
604 | (eval form))))) | |
605 | ;; Search article body. | |
606 | (let ((gnus-current-article nil) ;Save article pointer. | |
607 | (gnus-last-article nil) | |
608 | (gnus-break-pages nil) ;No need to break pages. | |
609 | (gnus-mark-article-hook nil)) ;Inhibit marking as read. | |
610 | (gnus-message | |
611 | 6 "Searching for article: %d..." (mail-header-number header)) | |
612 | (gnus-article-setup-buffer) | |
613 | (gnus-article-prepare (mail-header-number header) t) | |
614 | (when (save-excursion | |
615 | (set-buffer gnus-article-buffer) | |
616 | (goto-char (point-min)) | |
617 | (setq did-kill (re-search-forward regexp nil t))) | |
618 | (cond ((stringp form) ;Keyboard macro. | |
619 | (execute-kbd-macro form)) | |
23f87bed | 620 | ((functionp form) |
eec82323 LMI |
621 | (funcall form)) |
622 | (t | |
623 | (eval form))))))) | |
624 | did-kill))) | |
625 | ||
626 | (defun gnus-execute (field regexp form &optional backward unread) | |
627 | "If FIELD of article header matches REGEXP, execute lisp FORM (or a string). | |
628 | If FIELD is an empty string (or nil), entire article body is searched for. | |
629 | If optional 1st argument BACKWARD is non-nil, do backward instead. | |
630 | If optional 2nd argument UNREAD is non-nil, articles which are | |
631 | marked as read or ticked are ignored." | |
632 | (save-excursion | |
633 | (let ((killed-no 0) | |
23f87bed | 634 | function article header extras) |
eec82323 LMI |
635 | (cond |
636 | ;; Search body. | |
637 | ((or (null field) | |
638 | (string-equal field "")) | |
639 | (setq function nil)) | |
640 | ;; Get access function of header field. | |
23f87bed MB |
641 | ((cond ((fboundp |
642 | (setq function | |
643 | (intern-soft | |
644 | (concat "mail-header-" (downcase field))))) | |
645 | (setq function `(lambda (h) (,function h)))) | |
646 | ((when (setq extras | |
647 | (member (downcase field) | |
648 | (mapcar (lambda (header) | |
649 | (downcase (symbol-name header))) | |
650 | gnus-extra-headers))) | |
651 | (setq function | |
652 | `(lambda (h) | |
653 | (gnus-extra-header | |
654 | (quote ,(nth (- (length gnus-extra-headers) | |
655 | (length extras)) | |
656 | gnus-extra-headers)) | |
657 | h))))))) | |
eec82323 LMI |
658 | ;; Signal error. |
659 | (t | |
660 | (error "Unknown header field: \"%s\"" field))) | |
661 | ;; Starting from the current article. | |
662 | (while (or | |
663 | ;; First article. | |
664 | (and (not article) | |
665 | (setq article (gnus-summary-article-number))) | |
666 | ;; Find later articles. | |
667 | (setq article | |
668 | (gnus-summary-search-forward unread nil backward))) | |
669 | (and (or (null gnus-newsgroup-kill-headers) | |
670 | (memq article gnus-newsgroup-kill-headers)) | |
671 | (vectorp (setq header (gnus-summary-article-header article))) | |
672 | (gnus-execute-1 function regexp form header) | |
673 | (setq killed-no (1+ killed-no)))) | |
674 | ;; Return the number of killed articles. | |
675 | killed-no))) | |
676 | ||
677 | ;;;###autoload | |
678 | (defalias 'gnus-batch-kill 'gnus-batch-score) | |
679 | ;;;###autoload | |
680 | (defun gnus-batch-score () | |
681 | "Run batched scoring. | |
6748645f | 682 | Usage: emacs -batch -l ~/.emacs -l gnus -f gnus-batch-score" |
eec82323 LMI |
683 | (interactive) |
684 | (let* ((gnus-newsrc-options-n | |
685 | (gnus-newsrc-parse-options | |
686 | (concat "options -n " | |
687 | (mapconcat 'identity command-line-args-left " ")))) | |
688 | (gnus-expert-user t) | |
689 | (nnmail-spool-file nil) | |
16409b0b | 690 | (mail-sources nil) |
eec82323 LMI |
691 | (gnus-use-dribble-file nil) |
692 | (gnus-batch-mode t) | |
6748645f | 693 | info group newsrc entry |
eec82323 LMI |
694 | ;; Disable verbose message. |
695 | gnus-novice-user gnus-large-newsgroup | |
696 | gnus-options-subscribe gnus-auto-subscribed-groups | |
697 | gnus-options-not-subscribe) | |
698 | ;; Eat all arguments. | |
699 | (setq command-line-args-left nil) | |
700 | (gnus-slave) | |
701 | ;; Apply kills to specified newsgroups in command line arguments. | |
702 | (setq newsrc (cdr gnus-newsrc-alist)) | |
6748645f LMI |
703 | (while (setq info (pop newsrc)) |
704 | (setq group (gnus-info-group info) | |
705 | entry (gnus-gethash group gnus-newsrc-hashtb)) | |
706 | (when (and (<= (gnus-info-level info) gnus-level-subscribed) | |
eec82323 LMI |
707 | (and (car entry) |
708 | (or (eq (car entry) t) | |
6748645f | 709 | (not (zerop (car entry)))))) |
16409b0b GM |
710 | (ignore-errors |
711 | (gnus-summary-read-group group nil t nil t)) | |
eec82323 LMI |
712 | (when (eq (current-buffer) (get-buffer gnus-summary-buffer)) |
713 | (gnus-summary-exit)))) | |
714 | ;; Exit Emacs. | |
715 | (switch-to-buffer gnus-group-buffer) | |
716 | (gnus-group-save-newsrc))) | |
717 | ||
718 | (provide 'gnus-kill) | |
719 | ||
ab5796a9 | 720 | ;;; arch-tag: b30c0f53-df1a-490b-b81e-17b13474f395 |
eec82323 | 721 | ;;; gnus-kill.el ends here |