annotate lisp/rmail/rmail-kill.el @ 72:b9518feda344 r20-0b31

Import from CVS: tag r20-0b31
author cvs
date Mon, 13 Aug 2007 09:03:46 +0200
parents 131b0175ea99
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
1 ;;; rmail-kill.el --- Mail filtering for rmail
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
2
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
3 ;; Copyright status unknown
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
4
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
5 ;; Author: Unknown
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
6 ;; Keywords: mail
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
7
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
8 ;; This file is part of XEmacs.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
9
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
10 ;; XEmacs is free software; you can redistribute it and/or modify it
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
11 ;; under the terms of the GNU General Public License as published by
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
13 ;; any later version.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
14
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
15 ;; XEmacs is distributed in the hope that it will be useful, but
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
18 ;; General Public License for more details.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
19
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
20 ;; You should have received a copy of the GNU General Public License
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
23 ;; 02111-1307, USA.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
24
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
25 ;;; Synched up with: Not in FSF.
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
26
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
27 ;;; Commentary:
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
28 ;; This is the Original Notice on this file:
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;; GNU Emacs and this file "rmail-kill.el", is distributed in the hope
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;; that it will be useful, but WITHOUT ANY WARRANTY. No author or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;; distributor accepts responsibility to anyone for the consequences
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;; of using it or for whether it serves any particular purpose or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;; works at all, unless he says so in writing. Refer to the GNU Emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;; General Public License for full details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;; Everyone is granted permission to copy, modify and redistribute GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;; Emacs and rmail-kill.el, but only under the conditions described in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;; the GNU Emacs General Public License. A copy of this license is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;; supposed to have been given to you along with GNU Emacs so you can
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;; know your rights and responsibilities. It should be in a file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;; named COPYING. Among other things, the copyright notice and this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ;; notice must be preserved on all copies.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
44 ;;; Code:
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 (setq rmail-message-filter 'rmail-maybe-execute-message
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 rmail-mode-hook '((lambda ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 (define-key rmail-mode-map "e" 'rmail-extract-rejected-message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 (define-key rmail-mode-map "b" 'rmail-beginning-of-message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (define-key rmail-mode-map "K" 'rmail-execute-messages))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 ;; a-list with each entry (rmail-field-name . pattern)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 (defvar rmail-usual-suspects
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 '(("subject" . "Smithsonian Astronomical Observatory")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 ("subject" . "MGR, Bellcore window manager, Part"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 "An alist used to kill rmail messages based on regex matches to different fields.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 The car of each entry is the name of a mail header, the cdr is a pattern.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 Case is not significant.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 See also the documentation for rmail-maybe-execute-message and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 rmail-execute-messages.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (setq kill-emacs-hook 'maybe-book-some-suspects)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (defun maybe-book-some-suspects ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 (save-window-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (find-file "~/.emacs")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (re-search-forward "^(defvar rmail-usual-suspects$")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (down-list 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 (backward-char 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (if (not (equal rmail-usual-suspects
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (save-excursion (read (current-buffer)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (switch-to-buffer-other-window "SUSPECTS")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (erase-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (mapcar '(lambda (x) (print x (current-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 rmail-usual-suspects)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (set-buffer-modified-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (if (y-or-n-p "Save the usual suspects? ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (set-buffer ".emacs")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 (kill-sexp 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 (prin1 rmail-usual-suspects (get-buffer ".emacs"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (save-buffer)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (defun rmail-maybe-execute-message (&optional suspects dont-move)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 "Kill the current message if it matches an entry in SUSPECTS.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 SUSPECTS is alist of the form of rmail-usual-suspects (which see).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 If the current message contains a mail header that matches pattern,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 it is deleted.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 This function can be used as a rmail-message-filter (which see)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (if (null suspects)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (setq suspects rmail-usual-suspects))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (while suspects
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (if (and (string-match (cdr (car suspects))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 ;; if not such field, can never match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (or (mail-fetch-field (car (car suspects))) "$^"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (not (rmail-message-deleted-p rmail-current-message)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (message "Deleted message %d" rmail-current-message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (if dont-move
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (rmail-delete-message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (rmail-delete-forward))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (setq suspects nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (setq suspects (cdr suspects)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (defun rmail-execute-messages (round-up-the-usual-suspects)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 "Kill some rmail messages based on regex matches to a kill-alist.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 With a prefix arg, use rmail-usual-suspects as the kill-alist, otherwise
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 prompt for a field name."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (let ((scene-of-the-crime rmail-current-message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (alleged-perpetrator)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (cuffed-all-suspects nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (if round-up-the-usual-suspects
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (setq alleged-perpetrator rmail-usual-suspects)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (let* ((weapon (rmail-get-current-header "Kill what field? (default Subject) " "Subject"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (default-description (or (regexp-quote (mail-fetch-field weapon))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 "some regex"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (most-wanted-notice (format "Kill messages having a \"%s\" field matching? (default %s) "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 weapon default-description))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 (suspect-description (read-string-with-default most-wanted-notice default-description)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (setq alleged-perpetrator (list (cons weapon suspect-description)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 (if (y-or-n-p "Add it to rmail-usual-suspects? ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (setq rmail-usual-suspects (append alleged-perpetrator rmail-usual-suspects)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (while (not cuffed-all-suspects)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (rmail-maybe-execute-message alleged-perpetrator 'dont-move)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 ;; rmail-next-undeleted-message returns a string when there are no more, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 ;; we also want a chance to delete that last message...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 (if (stringp alleged-perpetrator)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (setq cuffed-all-suspects t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (setq cuffed-all-suspects (rmail-next-undeleted-message 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (rmail-show-message scene-of-the-crime)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (if (rmail-message-deleted-p rmail-current-message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (rmail-next-undeleted-message 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (if (rmail-message-deleted-p rmail-current-message)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (rmail-previous-undeleted-message 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (defun rmail-get-current-header (prompt default)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (let* ((end (progn (end-of-line) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (if (re-search-forward "^\\([^ \t]*\\):" end t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (buffer-substring (match-beginning 1) (match-end 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (read-string-with-default prompt default)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (defun read-string-with-default (prompt default)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (let ((s (read-string prompt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (if (string= s "") default s)))
72
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
155
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
156 (provide 'rmail-kill)
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
157
b9518feda344 Import from CVS: tag r20-0b31
cvs
parents: 70
diff changeset
158 ;;; rmail-kill.el ends here