comparison lisp/rmail/rmail-kill.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children b82b59fe008d
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;; GNU Emacs and this file "rmail-kill.el", is distributed in the hope
2 ;; that it will be useful, but WITHOUT ANY WARRANTY. No author or
3 ;; distributor accepts responsibility to anyone for the consequences
4 ;; of using it or for whether it serves any particular purpose or
5 ;; works at all, unless he says so in writing. Refer to the GNU Emacs
6 ;; General Public License for full details.
7
8 ;; Everyone is granted permission to copy, modify and redistribute GNU
9 ;; Emacs and rmail-kill.el, but only under the conditions described in
10 ;; the GNU Emacs General Public License. A copy of this license is
11 ;; supposed to have been given to you along with GNU Emacs so you can
12 ;; know your rights and responsibilities. It should be in a file
13 ;; named COPYING. Among other things, the copyright notice and this
14 ;; notice must be preserved on all copies.
15
16 (setq rmail-message-filter 'rmail-maybe-execute-message
17 rmail-mode-hook '((lambda ()
18 (define-key rmail-mode-map "e" 'rmail-extract-rejected-message)
19 (define-key rmail-mode-map "b" 'rmail-beginning-of-message)
20 (define-key rmail-mode-map "K" 'rmail-execute-messages))))
21
22 ;; a-list with each entry (rmail-field-name . pattern)
23 (defvar rmail-usual-suspects
24 '(("subject" . "Smithsonian Astronomical Observatory")
25 ("subject" . "MGR, Bellcore window manager, Part"))
26 "An alist used to kill rmail messages based on regex matches to different fields.
27 The car of each entry is the name of a mail header, the cdr is a pattern.
28 Case is not significant.)
29
30 See also the documentation for rmail-maybe-execute-message and
31 rmail-execute-messages.")
32
33 (setq kill-emacs-hook 'maybe-book-some-suspects)
34
35 (defun maybe-book-some-suspects ()
36 (save-window-excursion
37 (find-file "~/.emacs")
38 (goto-char (point-min))
39 (re-search-forward "^(defvar rmail-usual-suspects$")
40 (down-list 1)
41 (backward-char 1)
42 (if (not (equal rmail-usual-suspects
43 (save-excursion (read (current-buffer)))))
44 (progn
45 (switch-to-buffer-other-window "SUSPECTS")
46 (erase-buffer)
47 (mapcar '(lambda (x) (print x (current-buffer)))
48 rmail-usual-suspects)
49 (set-buffer-modified-p nil)
50 (if (y-or-n-p "Save the usual suspects? ")
51 (progn
52 (set-buffer ".emacs")
53 (kill-sexp 1)
54 (prin1 rmail-usual-suspects (get-buffer ".emacs"))
55 (save-buffer)))))))
56
57 (defun rmail-maybe-execute-message (&optional suspects dont-move)
58 "Kill the current message if it matches an entry in SUSPECTS.
59 SUSPECTS is alist of the form of rmail-usual-suspects (which see).
60 If the current message contains a mail header that matches pattern,
61 it is deleted.
62
63 This function can be used as a rmail-message-filter (which see)."
64 (if (null suspects)
65 (setq suspects rmail-usual-suspects))
66 (while suspects
67 (if (and (string-match (cdr (car suspects))
68 ;; if not such field, can never match
69 (or (mail-fetch-field (car (car suspects))) "$^"))
70 (not (rmail-message-deleted-p rmail-current-message)))
71 (progn
72 (message "Deleted message %d" rmail-current-message)
73 (if dont-move
74 (rmail-delete-message)
75 (rmail-delete-forward))
76 (setq suspects nil))
77 (setq suspects (cdr suspects)))))
78
79 (defun rmail-execute-messages (round-up-the-usual-suspects)
80 "Kill some rmail messages based on regex matches to a kill-alist.
81 With a prefix arg, use rmail-usual-suspects as the kill-alist, otherwise
82 prompt for a field name."
83 (interactive "P")
84 (let ((scene-of-the-crime rmail-current-message)
85 (alleged-perpetrator)
86 (cuffed-all-suspects nil))
87 (if round-up-the-usual-suspects
88 (setq alleged-perpetrator rmail-usual-suspects)
89 (let* ((weapon (rmail-get-current-header "Kill what field? (default Subject) " "Subject"))
90 (default-description (or (regexp-quote (mail-fetch-field weapon))
91 "some regex"))
92 (most-wanted-notice (format "Kill messages having a \"%s\" field matching? (default %s) "
93 weapon default-description))
94 (suspect-description (read-string-with-default most-wanted-notice default-description)))
95 (setq alleged-perpetrator (list (cons weapon suspect-description)))
96 (if (y-or-n-p "Add it to rmail-usual-suspects? ")
97 (setq rmail-usual-suspects (append alleged-perpetrator rmail-usual-suspects)))))
98
99 (while (not cuffed-all-suspects)
100 (rmail-maybe-execute-message alleged-perpetrator 'dont-move)
101 ;;
102 ;; rmail-next-undeleted-message returns a string when there are no more, but
103 ;; we also want a chance to delete that last message...
104 ;;
105 (if (stringp alleged-perpetrator)
106 (setq cuffed-all-suspects t)
107 (setq cuffed-all-suspects (rmail-next-undeleted-message 1))))
108
109 (rmail-show-message scene-of-the-crime)
110 (if (rmail-message-deleted-p rmail-current-message)
111 (rmail-next-undeleted-message 1))
112 (if (rmail-message-deleted-p rmail-current-message)
113 (rmail-previous-undeleted-message 1))))
114
115 (defun rmail-get-current-header (prompt default)
116 (save-excursion
117 (let* ((end (progn (end-of-line) (point))))
118 (beginning-of-line)
119 (if (re-search-forward "^\\([^ \t]*\\):" end t)
120 (buffer-substring (match-beginning 1) (match-end 1))
121 (read-string-with-default prompt default)))))
122
123 (defun read-string-with-default (prompt default)
124 (let ((s (read-string prompt)))
125 (if (string= s "") default s)))