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