1 ;;; gnus-kill.el --- kill commands for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
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)
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.
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.
29 (eval-when-compile (require 'cl))
34 (defcustom gnus-kill-file-mode-hook nil
35 "Hook for Gnus kill file mode."
36 :group 'gnus-score-kill
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
45 (defcustom gnus-kill-save-kill-file nil
46 "*If non-nil, will save kill files after processing them."
47 :group 'gnus-score-kill
50 (defcustom gnus-winconf-kill-file nil
51 "What does this do, Lars?"
52 :group 'gnus-score-kill
55 (defcustom gnus-kill-killed t
56 "*If non-nil, Gnus will apply kill files to already killed articles.
57 If it is nil, Gnus will never apply kill files to articles that have
58 already been through the scoring process, which might very well save lots
60 :group 'gnus-score-kill
65 (defmacro gnus-raise (field expression level)
66 `(gnus-kill ,field ,expression
67 (function (gnus-summary-raise-score ,level)) t))
69 (defmacro gnus-lower (field expression level)
70 `(gnus-kill ,field ,expression
71 (function (gnus-summary-raise-score (- ,level))) t))
74 ;;; Gnus Kill File Mode
77 (defvar gnus-kill-file-mode-map nil)
79 (unless gnus-kill-file-mode-map
80 (gnus-define-keymap (setq gnus-kill-file-mode-map
81 (copy-keymap emacs-lisp-mode-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))
90 (defun gnus-kill-file-mode ()
91 "Major mode for editing kill files.
93 If you are using this mode - you probably shouldn't. Kill files
94 perform badly and paint with a pretty broad brush. Score files, on
95 the other hand, are vastly faster (40x speedup) and give you more
96 control over what to do.
98 In addition to Emacs-Lisp Mode, the following commands are available:
100 \\{gnus-kill-file-mode-map}
102 A kill file contains Lisp expressions to be applied to a selected
103 newsgroup. The purpose is to mark articles as read on the basis of
104 some set of regexps. A global kill file is applied to every newsgroup,
105 and a local kill file is applied to a specified newsgroup. Since a
106 global kill file is applied to every newsgroup, for better performance
109 A kill file can contain any kind of Emacs Lisp expressions expected
110 to be evaluated in the Summary buffer. Writing Lisp programs for this
111 purpose is not so easy because the internal working of Gnus must be
112 well-known. For this reason, Gnus provides a general function which
113 does this easily for non-Lisp programmers.
115 The `gnus-kill' function executes commands available in Summary Mode
116 by their key sequences. `gnus-kill' should be called with FIELD,
117 REGEXP and optional COMMAND and ALL. FIELD is a string representing
118 the header field or an empty string. If FIELD is an empty string, the
119 entire article body is searched for. REGEXP is a string which is
120 compared with FIELD value. COMMAND is a string representing a valid
121 key sequence in Summary mode or Lisp expression. COMMAND defaults to
122 '(gnus-summary-mark-as-read nil \"X\"). Make sure that COMMAND is
123 executed in the Summary buffer. If the second optional argument ALL
124 is non-nil, the COMMAND is applied to articles which are already
125 marked as read or unread. Articles which are marked are skipped over
128 For example, if you want to mark articles of which subjects contain
129 the string `AI' as read, a possible kill file may look like:
131 (gnus-kill \"Subject\" \"AI\")
133 If you want to mark articles with `D' instead of `X', you can use
134 the following expression:
136 (gnus-kill \"Subject\" \"AI\" \"d\")
138 In this example it is assumed that the command
139 `gnus-summary-mark-as-read-forward' is assigned to `d' in Summary Mode.
141 It is possible to delete unnecessary headers which are marked with
142 `X' in a kill file as follows:
146 If the Summary buffer is empty after applying kill files, Gnus will
147 exit the selected newsgroup normally. If headers which are marked
148 with `D' are deleted in a kill file, it is impossible to read articles
149 which are marked as read in the previous Gnus sessions. Marks other
150 than `D' should be used for articles which should really be deleted.
152 Entry to this mode calls emacs-lisp-mode-hook and
153 gnus-kill-file-mode-hook with no arguments, if that value is non-nil."
155 (kill-all-local-variables)
156 (use-local-map gnus-kill-file-mode-map)
157 (set-syntax-table emacs-lisp-mode-syntax-table)
158 (setq major-mode 'gnus-kill-file-mode)
159 (setq mode-name "Kill")
160 (lisp-mode-variables nil)
161 (gnus-run-hooks 'emacs-lisp-mode-hook 'gnus-kill-file-mode-hook))
163 (defun gnus-kill-file-edit-file (newsgroup)
164 "Begin editing a kill file for NEWSGROUP.
165 If NEWSGROUP is nil, the global kill file is selected."
166 (interactive "sNewsgroup: ")
167 (let ((file (gnus-newsgroup-kill-file newsgroup)))
168 (gnus-make-directory (file-name-directory file))
169 ;; Save current window configuration if this is first invocation.
170 (or (and (get-file-buffer file)
171 (get-buffer-window (get-file-buffer file)))
172 (setq gnus-winconf-kill-file (current-window-configuration)))
174 (let ((buffer (find-file-noselect file)))
175 (cond ((get-buffer-window buffer)
176 (pop-to-buffer buffer))
177 ((eq major-mode 'gnus-group-mode)
178 (gnus-configure-windows 'group) ;Take all windows.
179 (pop-to-buffer buffer))
180 ((eq major-mode 'gnus-summary-mode)
181 (gnus-configure-windows 'article)
182 (pop-to-buffer gnus-article-buffer)
183 (bury-buffer gnus-article-buffer)
184 (switch-to-buffer buffer))
186 (find-file-other-window file))))
187 (gnus-kill-file-mode)))
189 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
190 (defun gnus-kill-set-kill-buffer ()
191 (let* ((file (gnus-newsgroup-kill-file gnus-newsgroup-name))
192 (buffer (find-file-noselect file)))
194 (gnus-kill-file-mode)
195 (bury-buffer buffer)))
197 (defun gnus-kill-file-enter-kill (field regexp &optional dont-move)
198 ;; Enter kill file entry.
199 ;; FIELD: String containing the name of the header field to kill.
200 ;; REGEXP: The string to kill.
203 (unless (eq major-mode 'gnus-kill-file-mode)
204 (gnus-kill-set-kill-buffer))
206 (goto-char (point-max)))
207 (insert (setq string (format "(gnus-kill %S %S)\n" field regexp)))
208 (gnus-kill-file-apply-string string))))
210 (defun gnus-kill-file-kill-by-subject ()
213 (gnus-kill-file-enter-kill
215 (if (vectorp gnus-current-headers)
217 (gnus-simplify-subject (mail-header-subject gnus-current-headers)))
221 (defun gnus-kill-file-kill-by-author ()
224 (gnus-kill-file-enter-kill
226 (if (vectorp gnus-current-headers)
227 (regexp-quote (mail-header-from gnus-current-headers))
230 (defun gnus-kill-file-kill-by-thread ()
233 (gnus-kill-file-enter-kill
235 (if (vectorp gnus-current-headers)
236 (regexp-quote (mail-header-id gnus-current-headers))
239 (defun gnus-kill-file-kill-by-xref ()
242 (let ((xref (and (vectorp gnus-current-headers)
243 (mail-header-xref gnus-current-headers)))
247 (while (string-match " \\([^ \t]+\\):" xref start)
248 (setq start (match-end 0))
251 (substring xref (match-beginning 1) (match-end 1)))
252 gnus-newsgroup-name))
253 (gnus-kill-file-enter-kill
254 "Xref" (concat " " (regexp-quote group) ":") t)))
255 (gnus-kill-file-enter-kill "Xref" "" t))))
257 (defun gnus-kill-file-raise-followups-to-author (level)
258 "Raise score for all followups to the current author."
260 (let ((name (mail-header-from gnus-current-headers))
263 (gnus-kill-set-kill-buffer)
264 (goto-char (point-min))
265 (setq name (read-string (concat "Add " level
266 " to followup articles to: ")
267 (regexp-quote name)))
271 "(gnus-kill %S %S '(gnus-summary-temporarily-raise-by-thread %S))\n"
274 (gnus-kill-file-apply-string string))
276 6 "Added temporary score file entry for followups to %s." name)))
278 (defun gnus-kill-file-apply-buffer ()
279 "Apply current buffer to current newsgroup."
281 (if (and gnus-current-kill-article
282 (get-buffer gnus-summary-buffer))
283 ;; Assume newsgroup is selected.
284 (gnus-kill-file-apply-string (buffer-string))
285 (ding) (gnus-message 2 "No newsgroup is selected.")))
287 (defun gnus-kill-file-apply-string (string)
288 "Apply STRING to current newsgroup."
290 (let ((string (concat "(progn \n" string "\n)")))
292 (save-window-excursion
293 (pop-to-buffer gnus-summary-buffer)
294 (eval (car (read-from-string string)))))))
296 (defun gnus-kill-file-apply-last-sexp ()
297 "Apply sexp before point in current buffer to current newsgroup."
299 (if (and gnus-current-kill-article
300 (get-buffer gnus-summary-buffer))
301 ;; Assume newsgroup is selected.
304 (save-excursion (forward-sexp -1) (point)) (point))))
306 (save-window-excursion
307 (pop-to-buffer gnus-summary-buffer)
308 (eval (car (read-from-string string))))))
309 (ding) (gnus-message 2 "No newsgroup is selected.")))
311 (defun gnus-kill-file-exit ()
312 "Save a kill file, then return to the previous buffer."
315 (let ((killbuf (current-buffer)))
316 ;; We don't want to return to article buffer.
317 (when (get-buffer gnus-article-buffer)
318 (bury-buffer gnus-article-buffer))
319 ;; Delete the KILL file windows.
320 (delete-windows-on killbuf)
321 ;; Restore last window configuration if available.
322 (when gnus-winconf-kill-file
323 (set-window-configuration gnus-winconf-kill-file))
324 (setq gnus-winconf-kill-file nil)
325 ;; Kill the KILL file buffer. Suggested by tale@pawl.rpi.edu.
326 (kill-buffer killbuf)))
330 (defun gnus-Newsgroup-kill-file (newsgroup)
331 "Return the name of a kill file for NEWSGROUP.
332 If NEWSGROUP is nil, return the global kill file instead."
333 (cond ((or (null newsgroup)
334 (string-equal newsgroup ""))
335 ;; The global kill file is placed at top of the directory.
336 (expand-file-name gnus-kill-file-name gnus-kill-files-directory))
337 (gnus-use-long-file-name
338 ;; Append ".KILL" to capitalized newsgroup name.
339 (expand-file-name (concat (gnus-capitalize-newsgroup newsgroup)
340 "." gnus-kill-file-name)
341 gnus-kill-files-directory))
343 ;; Place "KILL" under the hierarchical directory.
344 (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
345 "/" gnus-kill-file-name)
346 gnus-kill-files-directory))))
348 (defun gnus-expunge (marks)
349 "Remove lines marked with MARKS."
351 (set-buffer gnus-summary-buffer)
352 (gnus-summary-limit-to-marks marks 'reverse)))
354 (defun gnus-apply-kill-file-unless-scored ()
355 "Apply .KILL file, unless a .SCORE file for the same newsgroup exists."
356 (cond ((file-exists-p (gnus-score-file-name gnus-newsgroup-name))
357 ;; Ignores global KILL.
358 (when (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name))
359 (gnus-message 3 "Note: Ignoring %s.KILL; preferring .SCORE"
360 gnus-newsgroup-name))
362 ((or (file-exists-p (gnus-newsgroup-kill-file nil))
363 (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
364 (gnus-apply-kill-file-internal))
368 (defun gnus-apply-kill-file-internal ()
369 "Apply a kill file to the current newsgroup.
370 Returns the number of articles marked as read."
371 (let* ((kill-files (list (gnus-newsgroup-kill-file nil)
372 (gnus-newsgroup-kill-file gnus-newsgroup-name)))
373 (unreads (length gnus-newsgroup-unreads))
374 (gnus-summary-inhibit-highlight t)
376 (setq gnus-newsgroup-kill-headers nil)
377 ;; If there are any previously scored articles, we remove these
378 ;; from the `gnus-newsgroup-headers' list that the score functions
379 ;; will see. This is probably pretty wasteful when it comes to
380 ;; conses, but is, I think, faster than having to assq in every
381 ;; single score function.
382 (let ((files kill-files))
384 (if (file-exists-p (car files))
385 (let ((headers gnus-newsgroup-headers))
387 (setq gnus-newsgroup-kill-headers
388 (mapcar (lambda (header) (mail-header-number header))
391 (unless (gnus-member-of-range
392 (mail-header-number (car headers))
393 gnus-newsgroup-killed)
394 (push (mail-header-number (car headers))
395 gnus-newsgroup-kill-headers))
396 (setq headers (cdr headers))))
398 (setq files (cdr files)))))
399 (if (not gnus-newsgroup-kill-headers)
401 (save-window-excursion
404 (if (not (file-exists-p (car kill-files)))
406 (gnus-message 6 "Processing kill file %s..." (car kill-files))
407 (find-file (car kill-files))
408 (goto-char (point-min))
410 (if (consp (ignore-errors (read (current-buffer))))
411 (gnus-kill-parse-gnus-kill-file)
412 (gnus-kill-parse-rn-kill-file))
415 6 "Processing kill file %s...done" (car kill-files)))
416 (setq kill-files (cdr kill-files)))))
418 (gnus-set-mode-line 'summary)
421 (let ((nunreads (- unreads (length gnus-newsgroup-unreads))))
423 (gnus-message 6 "Marked %d articles as read" nunreads))
427 ;; Parse a Gnus killfile.
428 (defun gnus-score-insert-help (string alist idx)
430 (pop-to-buffer "*Score Help*")
431 (buffer-disable-undo)
433 (insert string ":\n\n")
435 (insert (format " %c: %s\n" (caar alist) (nth idx (car alist))))
436 (setq alist (cdr alist)))))
438 (defun gnus-kill-parse-gnus-kill-file ()
439 (goto-char (point-min))
440 (gnus-kill-file-mode)
444 (setq form (ignore-errors (read (current-buffer)))))
446 (error "Invalid kill entry (possibly rn kill file?): %s" form))
447 (if (or (eq (car form) 'gnus-kill)
448 (eq (car form) 'gnus-raise)
449 (eq (car form) 'gnus-lower))
451 (delete-region beg (point))
452 (insert (or (eval form) "")))
454 (set-buffer gnus-summary-buffer)
455 (ignore-errors (eval form)))))
456 (and (buffer-modified-p)
457 gnus-kill-save-kill-file
459 (set-buffer-modified-p nil)))
461 ;; Parse an rn killfile.
462 (defun gnus-kill-parse-rn-kill-file ()
463 (goto-char (point-min))
464 (gnus-kill-file-mode)
473 pattern modifier commands)
475 (if (not (looking-at "[ \t]*/\\([^/]*\\)/\\([ahfcH]\\)?:\\([a-z=:]*\\)"))
477 (setq pattern (buffer-substring (match-beginning 1) (match-end 1)))
478 (setq modifier (if (match-beginning 2) (char-after (match-beginning 2))
480 (setq commands (buffer-substring (match-beginning 3) (match-end 3)))
482 ;; The "f:+" command marks everything *but* the matches as read,
483 ;; so we simply first match everything as read, and then unmark
485 (when (string-match "\\+" commands)
486 (gnus-kill "from" ".")
490 (or (cdr (assq modifier mod-to-header)) "subject")
492 (if (string-match "m" commands)
493 '(gnus-summary-mark-as-unread nil " ")
494 '(gnus-summary-mark-as-read nil "X"))
498 ;; Kill changes and new format by suggested by JWZ and Sudish Joseph
499 ;; <joseph@cis.ohio-state.edu>.
500 (defun gnus-kill (field regexp &optional exe-command all silent)
501 "If FIELD of an article matches REGEXP, execute COMMAND.
502 Optional 1st argument COMMAND is default to
503 (gnus-summary-mark-as-read nil \"X\").
504 If optional 2nd argument ALL is non-nil, articles marked are also applied to.
505 If FIELD is an empty string (or nil), entire article body is searched for.
506 COMMAND must be a lisp expression or a string representing a key sequence."
507 ;; We don't want to change current point nor window configuration.
508 (let ((old-buffer (current-buffer)))
510 (save-window-excursion
511 ;; Selected window must be summary buffer to execute keyboard
512 ;; macros correctly. See command_loop_1.
513 (switch-to-buffer gnus-summary-buffer 'norecord)
514 (goto-char (point-min)) ;From the beginning.
515 (let ((kill-list regexp)
516 (date (current-time-string))
517 (command (or exe-command '(gnus-summary-mark-as-read
518 nil gnus-kill-file-mark)))
520 (if (listp kill-list)
522 (if (not (consp (cdr kill-list)))
523 ;; It's on the form (regexp . date).
524 (if (zerop (gnus-execute field (car kill-list)
525 command nil (not all)))
526 (when (> (days-between date (cdr kill-list))
527 gnus-kill-expiry-days)
529 (setcdr kill-list date))
530 (while (setq kill (car kill-list))
532 ;; It's a temporary kill.
534 (setq kdate (cdr kill))
535 (if (zerop (gnus-execute
536 field (car kill) command nil (not all)))
537 (when (> (days-between date kdate)
538 gnus-kill-expiry-days)
539 ;; Time limit has been exceeded, so we
542 (setcdr prev (cdr kill-list))
543 (setq regexp (cdr regexp))))
544 ;; Successful kill. Set the date to today.
546 ;; It's a permanent kill.
547 (gnus-execute field kill command nil (not all)))
548 (setq prev kill-list)
549 (setq kill-list (cdr kill-list))))
550 (gnus-execute field kill-list command nil (not all))))))
551 (switch-to-buffer old-buffer)
552 (when (and (eq major-mode 'gnus-kill-file-mode) regexp (not silent))
554 (nconc (list 'gnus-kill field
555 (if (consp regexp) (list 'quote regexp) regexp))
556 (when (or exe-command all)
557 (list (list 'quote exe-command)))
558 (if all (list t) nil))))))
560 (defun gnus-pp-gnus-kill (object)
561 (if (or (not (consp (nth 2 object)))
562 (not (consp (cdr (nth 2 object))))
563 (and (eq 'quote (car (nth 2 object)))
564 (not (consp (cdadr (nth 2 object))))))
565 (concat "\n" (gnus-prin1-to-string object))
567 (set-buffer (gnus-get-buffer-create "*Gnus PP*"))
568 (buffer-disable-undo)
570 (insert (format "\n(%S %S\n '(" (nth 0 object) (nth 1 object)))
571 (let ((klist (cadr (nth 2 object)))
574 (insert (if first (progn (setq first nil) "") "\n ")
575 (gnus-prin1-to-string (car klist)))
576 (setq klist (cdr klist))))
580 (if (and (consp (nth 3 object))
581 (not (eq 'quote (car (nth 3 object)))))
583 (gnus-prin1-to-string (nth 3 object))))
588 (buffer-substring (point-min) (point-max))
589 (kill-buffer (current-buffer))))))
591 (defun gnus-execute-1 (function regexp form header)
597 ;; Compare with header field.
601 (setq value (funcall function header))
602 ;; Number (Lines:) or symbol must be converted to string.
603 (unless (stringp value)
604 (setq value (gnus-prin1-to-string value)))
605 (setq did-kill (string-match regexp value)))
606 (cond ((stringp form) ;Keyboard macro.
607 (execute-kbd-macro form))
608 ((gnus-functionp form)
612 ;; Search article body.
613 (let ((gnus-current-article nil) ;Save article pointer.
614 (gnus-last-article nil)
615 (gnus-break-pages nil) ;No need to break pages.
616 (gnus-mark-article-hook nil)) ;Inhibit marking as read.
618 6 "Searching for article: %d..." (mail-header-number header))
619 (gnus-article-setup-buffer)
620 (gnus-article-prepare (mail-header-number header) t)
621 (when (save-excursion
622 (set-buffer gnus-article-buffer)
623 (goto-char (point-min))
624 (setq did-kill (re-search-forward regexp nil t)))
625 (cond ((stringp form) ;Keyboard macro.
626 (execute-kbd-macro form))
627 ((gnus-functionp form)
633 (defun gnus-execute (field regexp form &optional backward unread)
634 "If FIELD of article header matches REGEXP, execute lisp FORM (or a string).
635 If FIELD is an empty string (or nil), entire article body is searched for.
636 If optional 1st argument BACKWARD is non-nil, do backward instead.
637 If optional 2nd argument UNREAD is non-nil, articles which are
638 marked as read or ticked are ignored."
641 function article header)
645 (string-equal field ""))
647 ;; Get access function of header field.
651 (concat "mail-header-" (downcase field)))))
652 (setq function `(lambda (h) (,function h))))
655 (error "Unknown header field: \"%s\"" field)))
656 ;; Starting from the current article.
660 (setq article (gnus-summary-article-number)))
661 ;; Find later articles.
663 (gnus-summary-search-forward unread nil backward)))
664 (and (or (null gnus-newsgroup-kill-headers)
665 (memq article gnus-newsgroup-kill-headers))
666 (vectorp (setq header (gnus-summary-article-header article)))
667 (gnus-execute-1 function regexp form header)
668 (setq killed-no (1+ killed-no))))
669 ;; Return the number of killed articles.
673 (defalias 'gnus-batch-kill 'gnus-batch-score)
675 (defun gnus-batch-score ()
676 "Run batched scoring.
677 Usage: emacs -batch -l ~/.emacs -l gnus -f gnus-batch-score"
679 (let* ((gnus-newsrc-options-n
680 (gnus-newsrc-parse-options
681 (concat "options -n "
682 (mapconcat 'identity command-line-args-left " "))))
684 (nnmail-spool-file nil)
685 (gnus-use-dribble-file nil)
687 info group newsrc entry
688 ;; Disable verbose message.
689 gnus-novice-user gnus-large-newsgroup
690 gnus-options-subscribe gnus-auto-subscribed-groups
691 gnus-options-not-subscribe)
692 ;; Eat all arguments.
693 (setq command-line-args-left nil)
695 ;; Apply kills to specified newsgroups in command line arguments.
696 (setq newsrc (cdr gnus-newsrc-alist))
697 (while (setq info (pop newsrc))
698 (setq group (gnus-info-group info)
699 entry (gnus-gethash group gnus-newsrc-hashtb))
700 (when (and (<= (gnus-info-level info) gnus-level-subscribed)
702 (or (eq (car entry) t)
703 (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))))
709 (switch-to-buffer gnus-group-buffer)
710 (gnus-group-save-newsrc)))
714 ;;; gnus-kill.el ends here