Commit | Line | Data |
---|---|---|
537ab246 BG |
1 | ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs |
2 | ||
acaf905b | 3 | ;; Copyright (C) 1985-1988, 1993-1998, 2000-2012 |
537ab246 BG |
4 | ;; Free Software Foundation, Inc. |
5 | ||
6 | ;; Maintainer: FSF | |
7 | ;; Keywords: mail | |
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 3 of the License, or | |
14 | ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>. | |
23 | ||
24 | ;;; Commentary: | |
25 | ||
26 | ;;; Code: | |
27 | ||
28 | ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu | |
29 | ;; New features include attribute and keyword support, message | |
30 | ;; selection by dispatch table, summary by attributes and keywords, | |
31 | ;; expunging by dispatch table, sticky options for file commands. | |
32 | ||
33 | ;; Extended by Bob Weiner of Motorola | |
34 | ;; New features include: rmail and rmail-summary buffers remain | |
35 | ;; synchronized and key bindings basically operate the same way in both | |
36 | ;; buffers, summary by topic or by regular expression, rmail-reply-prefix | |
37 | ;; variable, and a bury rmail buffer (wipe) command. | |
38 | ;; | |
39 | ||
40 | (require 'mail-utils) | |
3d9ee611 | 41 | (require 'rfc2047) |
537ab246 | 42 | |
4d6769e1 JB |
43 | (declare-function compilation--message->loc "compile" (cl-x) t) |
44 | (declare-function epa--find-coding-system-for-mime-charset "epa" (mime-charset)) | |
45 | ||
537ab246 BG |
46 | (defconst rmail-attribute-header "X-RMAIL-ATTRIBUTES" |
47 | "The header that stores the Rmail attribute data.") | |
48 | ||
49 | (defconst rmail-keyword-header "X-RMAIL-KEYWORDS" | |
50 | "The header that stores the Rmail keyword data.") | |
51 | ||
52 | ;;; Attribute indexes | |
53 | ||
54 | (defconst rmail-answered-attr-index 0 | |
55 | "The index for the `answered' attribute.") | |
56 | ||
57 | (defconst rmail-deleted-attr-index 1 | |
58 | "The index for the `deleted' attribute.") | |
59 | ||
60 | (defconst rmail-edited-attr-index 2 | |
61 | "The index for the `edited' attribute.") | |
62 | ||
63 | (defconst rmail-filed-attr-index 3 | |
64 | "The index for the `filed' attribute.") | |
65 | ||
66 | (defconst rmail-retried-attr-index 4 | |
67 | "The index for the `retried' attribute.") | |
68 | ||
69 | (defconst rmail-forwarded-attr-index 5 | |
70 | "The index for the `forwarded' attribute.") | |
71 | ||
72 | (defconst rmail-unseen-attr-index 6 | |
73 | "The index for the `unseen' attribute.") | |
74 | ||
2f6e3774 | 75 | (defconst rmail-resent-attr-index 7 |
537ab246 BG |
76 | "The index for the `resent' attribute.") |
77 | ||
78 | (defconst rmail-attr-array | |
79 | '[(?A "answered") | |
80 | (?D "deleted") | |
81 | (?E "edited") | |
82 | (?F "filed") | |
83 | (?R "retried") | |
84 | (?S "forwarded") | |
85 | (?U "unseen") | |
86 | (?r "resent")] | |
87 | "An array that provides a mapping between an attribute index, | |
88 | its character representation and its display representation.") | |
89 | ||
90 | (defvar deleted-head) | |
91 | (defvar font-lock-fontified) | |
92 | (defvar mail-abbrev-syntax-table) | |
93 | (defvar mail-abbrevs) | |
94 | (defvar messages-head) | |
537ab246 BG |
95 | (defvar total-messages) |
96 | (defvar tool-bar-map) | |
fd59d131 | 97 | (defvar mail-encode-mml) |
537ab246 BG |
98 | |
99 | (defvar rmail-header-style 'normal | |
100 | "The current header display style choice, one of | |
101 | 'normal (selected headers) or 'full (all headers).") | |
102 | ||
537ab246 BG |
103 | (defgroup rmail nil |
104 | "Mail reader for Emacs." | |
105 | :group 'mail) | |
106 | ||
107 | (defgroup rmail-retrieve nil | |
108 | "Rmail retrieval options." | |
109 | :prefix "rmail-" | |
110 | :group 'rmail) | |
111 | ||
112 | (defgroup rmail-files nil | |
113 | "Rmail files." | |
114 | :prefix "rmail-" | |
115 | :group 'rmail) | |
116 | ||
117 | (defgroup rmail-headers nil | |
118 | "Rmail header options." | |
119 | :prefix "rmail-" | |
120 | :group 'rmail) | |
121 | ||
122 | (defgroup rmail-reply nil | |
123 | "Rmail reply options." | |
124 | :prefix "rmail-" | |
125 | :group 'rmail) | |
126 | ||
127 | (defgroup rmail-summary nil | |
128 | "Rmail summary options." | |
129 | :prefix "rmail-" | |
130 | :prefix "rmail-summary-" | |
131 | :group 'rmail) | |
132 | ||
133 | (defgroup rmail-output nil | |
134 | "Output message to a file." | |
135 | :prefix "rmail-output-" | |
136 | :prefix "rmail-" | |
137 | :group 'rmail) | |
138 | ||
139 | (defgroup rmail-edit nil | |
140 | "Rmail editing." | |
141 | :prefix "rmail-edit-" | |
142 | :group 'rmail) | |
c8f3b42c GM |
143 | |
144 | ;;;###autoload | |
145 | (defcustom rmail-file-name (purecopy "~/RMAIL") | |
146 | "Name of user's primary mail file." | |
147 | :type 'string | |
148 | :group 'rmail | |
149 | :version "21.1") | |
150 | ||
98e15937 GM |
151 | ;;;###autoload |
152 | (put 'rmail-spool-directory 'standard-value | |
153 | '((cond ((file-exists-p "/var/mail") "/var/mail/") | |
154 | ((file-exists-p "/var/spool/mail") "/var/spool/mail/") | |
155 | ((memq system-type '(hpux usg-unix-v irix)) "/usr/mail/") | |
156 | (t "/usr/spool/mail/")))) | |
157 | ||
c8f3b42c GM |
158 | ;;;###autoload |
159 | (defcustom rmail-spool-directory | |
160 | (purecopy | |
161 | (cond ((file-exists-p "/var/mail") | |
162 | ;; SVR4 and recent BSD are said to use this. | |
163 | ;; Rather than trying to know precisely which systems use it, | |
164 | ;; let's assume this dir is never used for anything else. | |
165 | "/var/mail/") | |
166 | ;; Many GNU/Linux systems use this name. | |
167 | ((file-exists-p "/var/spool/mail") "/var/spool/mail/") | |
168 | ((memq system-type '(hpux usg-unix-v irix)) "/usr/mail/") | |
169 | (t "/usr/spool/mail/"))) | |
170 | "Name of directory used by system mailer for delivering new mail. | |
171 | Its name should end with a slash." | |
172 | :initialize 'custom-initialize-delay | |
173 | :type 'directory | |
174 | :group 'rmail) | |
98e15937 GM |
175 | |
176 | ;;;###autoload(custom-initialize-delay 'rmail-spool-directory nil) | |
537ab246 | 177 | |
537ab246 BG |
178 | (defcustom rmail-movemail-program nil |
179 | "If non-nil, the file name of the `movemail' program." | |
180 | :group 'rmail-retrieve | |
181 | :type '(choice (const nil) string)) | |
182 | ||
d6261cc1 GM |
183 | (define-obsolete-variable-alias 'rmail-pop-password |
184 | 'rmail-remote-password "22.1") | |
537ab246 BG |
185 | |
186 | (defcustom rmail-remote-password nil | |
778ef2ef | 187 | "Password to use when reading mail from a remote server. |
537ab246 BG |
188 | This setting is ignored for mailboxes whose URL already contains a password." |
189 | :type '(choice (string :tag "Password") | |
190 | (const :tag "Not Required" nil)) | |
537ab246 BG |
191 | :group 'rmail-retrieve |
192 | :version "22.1") | |
193 | ||
d6261cc1 GM |
194 | (define-obsolete-variable-alias 'rmail-pop-password-required |
195 | 'rmail-remote-password-required "22.1") | |
196 | ||
537ab246 | 197 | (defcustom rmail-remote-password-required nil |
778ef2ef | 198 | "Non-nil if a password is required when reading mail from a remote server." |
537ab246 | 199 | :type 'boolean |
537ab246 BG |
200 | :group 'rmail-retrieve |
201 | :version "22.1") | |
202 | ||
203 | (defcustom rmail-movemail-flags nil | |
778ef2ef | 204 | "List of flags to pass to movemail. |
537ab246 BG |
205 | Most commonly used to specify `-g' to enable GSS-API authentication |
206 | or `-k' to enable Kerberos authentication." | |
207 | :type '(repeat string) | |
208 | :group 'rmail-retrieve | |
209 | :version "20.3") | |
210 | ||
211 | (defvar rmail-remote-password-error "invalid usercode or password\\| | |
212 | unknown user name or bad password\\|Authentication failed\\|MU_ERR_AUTH_FAILURE" | |
213 | "Regular expression matching incorrect-password POP or IMAP server error | |
214 | messages. | |
215 | If you get an incorrect-password error that this expression does not match, | |
216 | please report it with \\[report-emacs-bug].") | |
217 | ||
218 | (defvar rmail-encoded-remote-password nil) | |
219 | ||
220 | (defcustom rmail-preserve-inbox nil | |
778ef2ef | 221 | "Non-nil means leave incoming mail in the user's inbox--don't delete it." |
537ab246 BG |
222 | :type 'boolean |
223 | :group 'rmail-retrieve) | |
224 | ||
225 | (defcustom rmail-movemail-search-path nil | |
778ef2ef GM |
226 | "List of directories to search for movemail (in addition to `exec-path')." |
227 | :group 'rmail-retrieve | |
228 | :type '(repeat (directory))) | |
537ab246 | 229 | |
38a71655 | 230 | (declare-function mail-dont-reply-to "mail-utils" (destinations)) |
537ab246 | 231 | (declare-function rmail-update-summary "rmailsum" (&rest ignore)) |
53479029 | 232 | (declare-function rmail-mime-toggle-hidden "rmailmm" ()) |
537ab246 BG |
233 | |
234 | (defun rmail-probe (prog) | |
235 | "Determine what flavor of movemail PROG is. | |
236 | We do this by executing it with `--version' and analyzing its output." | |
237 | (with-temp-buffer | |
238 | (let ((tbuf (current-buffer))) | |
239 | (buffer-disable-undo tbuf) | |
240 | (call-process prog nil tbuf nil "--version") | |
241 | (if (not (buffer-modified-p tbuf)) | |
242 | ;; Should not happen... | |
243 | nil | |
244 | (goto-char (point-min)) | |
245 | (cond | |
246 | ((looking-at ".*movemail: invalid option") | |
247 | 'emacs) ;; Possibly... | |
248 | ((looking-at "movemail (GNU Mailutils .*)") | |
249 | 'mailutils) | |
250 | (t | |
251 | ;; FIXME: | |
252 | 'emacs)))))) | |
253 | ||
254 | (defun rmail-autodetect () | |
255 | "Determine the file name of the `movemail' program and return its flavor. | |
256 | If `rmail-movemail-program' is non-nil, use it. | |
257 | Otherwise, look for `movemail' in the directories in | |
258 | `rmail-movemail-search-path', those in `exec-path', and `exec-directory'." | |
259 | (if rmail-movemail-program | |
260 | (rmail-probe rmail-movemail-program) | |
261 | (catch 'scan | |
262 | (dolist (dir (append rmail-movemail-search-path exec-path | |
263 | (list exec-directory))) | |
264 | (when (and dir (file-accessible-directory-p dir)) | |
265 | ;; Previously, this didn't have to work on Windows, because | |
266 | ;; rmail-insert-inbox-text before r1.439 fell back to using | |
267 | ;; (expand-file-name "movemail" exec-directory) and just | |
268 | ;; assuming it would work. | |
269 | ;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2008-02/msg00087.html | |
270 | (let ((progname (expand-file-name | |
271 | (concat "movemail" | |
272 | (if (memq system-type '(ms-dos windows-nt)) | |
273 | ".exe")) dir))) | |
274 | (when (and (not (file-directory-p progname)) | |
275 | (file-executable-p progname)) | |
276 | (let ((x (rmail-probe progname))) | |
277 | (when x | |
278 | (setq rmail-movemail-program progname) | |
279 | (throw 'scan x)))))))))) | |
280 | ||
281 | (defvar rmail-movemail-variant-in-use nil | |
282 | "The movemail variant currently in use. Known variants are: | |
283 | ||
284 | `emacs' Means any implementation, compatible with the native Emacs one. | |
285 | This is the default; | |
286 | `mailutils' Means GNU mailutils implementation, capable of handling full | |
287 | mail URLs as the source mailbox.") | |
288 | ||
289 | ;;;###autoload | |
290 | (defun rmail-movemail-variant-p (&rest variants) | |
291 | "Return t if the current movemail variant is any of VARIANTS. | |
292 | Currently known variants are 'emacs and 'mailutils." | |
293 | (when (not rmail-movemail-variant-in-use) | |
294 | ;; Autodetect | |
295 | (setq rmail-movemail-variant-in-use (rmail-autodetect))) | |
296 | (not (null (member rmail-movemail-variant-in-use variants)))) | |
297 | ||
298 | ;; Call for effect, to set rmail-movemail-program (if not set by the | |
299 | ;; user), and rmail-movemail-variant-in-use. Used by various functions. | |
300 | ;; I'm not sure if M-x rmail is the only entry point to this package. | |
301 | ;; If so, this can be moved there. | |
302 | (rmail-movemail-variant-p) | |
303 | ||
6c82bad2 GM |
304 | ;;;###autoload |
305 | (defcustom rmail-user-mail-address-regexp nil | |
306 | "Regexp matching user mail addresses. | |
307 | If non-nil, this variable is used to identify the correspondent | |
308 | when receiving new mail. If it matches the address of the sender, | |
309 | the recipient is taken as correspondent of a mail. | |
310 | If nil \(default value\), your `user-login-name' and `user-mail-address' | |
311 | are used to exclude yourself as correspondent. | |
312 | ||
313 | Usually you don't have to set this variable, except if you collect mails | |
314 | sent by you under different user names. | |
315 | Then it should be a regexp matching your mail addresses. | |
316 | ||
317 | Setting this variable has an effect only before reading a mail." | |
318 | :type '(choice (const :tag "None" nil) regexp) | |
319 | :group 'rmail-retrieve | |
320 | :version "21.1") | |
321 | ||
537ab246 | 322 | ;;;###autoload |
b474519e GM |
323 | (define-obsolete-variable-alias 'rmail-dont-reply-to-names |
324 | 'mail-dont-reply-to-names "24.1") | |
537ab246 | 325 | |
b474519e | 326 | ;; Prior to 24.1, this used to contain "\\`info-". |
537ab246 | 327 | ;;;###autoload |
38a71655 CY |
328 | (defvar rmail-default-dont-reply-to-names nil |
329 | "Regexp specifying part of the default value of `mail-dont-reply-to-names'. | |
330 | This is used when the user does not set `mail-dont-reply-to-names' | |
331 | explicitly.") | |
332 | ;;;###autoload | |
333 | (make-obsolete-variable 'rmail-default-dont-reply-to-names | |
334 | 'mail-dont-reply-to-names "24.1") | |
537ab246 | 335 | |
236ab005 | 336 | ;;;###autoload |
537ab246 | 337 | (defcustom rmail-ignored-headers |
a7610c52 | 338 | (purecopy |
537ab246 BG |
339 | (concat "^via:\\|^mail-from:\\|^origin:\\|^references:\\|^sender:" |
340 | "\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:" | |
341 | "\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:" | |
342 | "\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:" | |
343 | "\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^face:" | |
344 | "\\|^x-mailer:\\|^delivered-to:\\|^lines:" | |
345 | "\\|^content-transfer-encoding:\\|^x-coding-system:" | |
346 | "\\|^return-path:\\|^errors-to:\\|^return-receipt-to:" | |
347 | "\\|^precedence:\\|^mime-version:" | |
348 | "\\|^list-owner:\\|^list-help:\\|^list-post:\\|^list-subscribe:" | |
349 | "\\|^list-id:\\|^list-unsubscribe:\\|^list-archive:" | |
350 | "\\|^content-length:\\|^nntp-posting-date:\\|^user-agent" | |
351 | "\\|^importance:\\|^envelope-to:\\|^delivery-date\\|^openpgp:" | |
352 | "\\|^mbox-line:\\|^cancel-lock:" | |
353 | "\\|^DomainKey-Signature:\\|^dkim-signature:" | |
354 | "\\|^resent-face:\\|^resent-x.*:\\|^resent-organization:\\|^resent-openpgp:" | |
a7610c52 | 355 | "\\|^x-.*:")) |
778ef2ef | 356 | "Regexp to match header fields that Rmail should normally hide. |
537ab246 BG |
357 | \(See also `rmail-nonignored-headers', which overrides this regexp.) |
358 | This variable is used for reformatting the message header, | |
359 | which normally happens once for each message, | |
360 | when you view the message for the first time in Rmail. | |
361 | To make a change in this variable take effect | |
362 | for a message that you have already viewed, | |
363 | go to that message and type \\[rmail-toggle-header] twice." | |
364 | :type 'regexp | |
365 | :group 'rmail-headers) | |
366 | ||
367 | (defcustom rmail-nonignored-headers "^x-spam-status:" | |
778ef2ef | 368 | "Regexp to match X header fields that Rmail should show. |
537ab246 BG |
369 | This regexp overrides `rmail-ignored-headers'; if both this regexp |
370 | and that one match a certain header field, Rmail shows the field. | |
371 | If this is nil, ignore all header fields in `rmail-ignored-headers'. | |
372 | ||
373 | This variable is used for reformatting the message header, | |
374 | which normally happens once for each message, | |
375 | when you view the message for the first time in Rmail. | |
376 | To make a change in this variable take effect | |
377 | for a message that you have already viewed, | |
378 | go to that message and type \\[rmail-toggle-header] twice." | |
379 | :type '(choice (const nil) (regexp)) | |
380 | :group 'rmail-headers) | |
381 | ||
236ab005 | 382 | ;;;###autoload |
537ab246 | 383 | (defcustom rmail-displayed-headers nil |
778ef2ef | 384 | "Regexp to match Header fields that Rmail should display. |
537ab246 BG |
385 | If nil, display all header fields except those matched by |
386 | `rmail-ignored-headers'." | |
387 | :type '(choice regexp (const :tag "All")) | |
388 | :group 'rmail-headers) | |
389 | ||
236ab005 | 390 | ;;;###autoload |
56c2cc9a | 391 | (defcustom rmail-retry-ignored-headers (purecopy "^x-authentication-warning:\\|^x-detected-operating-system:\\|^x-spam[-a-z]*:\\|content-type:\\|content-transfer-encoding:\\|mime-version:\\|message-id:") |
778ef2ef | 392 | "Headers that should be stripped when retrying a failed message." |
537ab246 | 393 | :type '(choice regexp (const nil :tag "None")) |
33e38fa1 | 394 | :group 'rmail-headers |
4ed32706 | 395 | :version "23.2") ; added x-detected-operating-system, x-spam |
537ab246 | 396 | |
236ab005 | 397 | ;;;###autoload |
a7610c52 | 398 | (defcustom rmail-highlighted-headers (purecopy "^From:\\|^Subject:") |
778ef2ef | 399 | "Regexp to match Header fields that Rmail should normally highlight. |
66be413e | 400 | A value of nil means don't highlight. Uses the face `rmail-highlight'." |
537ab246 BG |
401 | :type 'regexp |
402 | :group 'rmail-headers) | |
403 | ||
404 | (defface rmail-highlight | |
405 | '((t (:inherit highlight))) | |
66be413e GM |
406 | "Face to use for highlighting the most important header fields. |
407 | The variable `rmail-highlighted-headers' specifies which headers." | |
537ab246 BG |
408 | :group 'rmail-headers |
409 | :version "22.1") | |
410 | ||
f454672b GM |
411 | ;; This was removed in Emacs 23.1 with no notification, an unnecessary |
412 | ;; incompatible change. | |
413 | (defcustom rmail-highlight-face 'rmail-highlight | |
414 | "Face used by Rmail for highlighting headers." | |
415 | ;; Note that nil doesn't actually mean use the default face, it | |
416 | ;; means use either bold or highlight. It's not worth fixing this | |
417 | ;; now that this is obsolete. | |
418 | :type '(choice (const :tag "Default" nil) | |
419 | face) | |
420 | :group 'rmail-headers) | |
421 | (make-obsolete-variable 'rmail-highlight-face | |
422 | "customize the face `rmail-highlight' instead." | |
423 | "23.2") | |
424 | ||
537ab246 BG |
425 | (defface rmail-header-name |
426 | '((t (:inherit font-lock-function-name-face))) | |
66be413e GM |
427 | "Face to use for highlighting the header names. |
428 | The variable `rmail-font-lock-keywords' specifies which headers | |
429 | get highlighted." | |
537ab246 BG |
430 | :group 'rmail-headers |
431 | :version "23.1") | |
432 | ||
778ef2ef GM |
433 | (defcustom rmail-delete-after-output nil |
434 | "Non-nil means automatically delete a message that is copied to a file." | |
537ab246 BG |
435 | :type 'boolean |
436 | :group 'rmail-files) | |
437 | ||
438 | ;;;###autoload | |
778ef2ef | 439 | (defcustom rmail-primary-inbox-list nil |
dec5f46d GM |
440 | "List of files that are inboxes for your primary mail file `rmail-file-name'. |
441 | If this is nil, uses the environment variable MAIL. If that is | |
442 | unset, uses a file named by the function `user-login-name' in the | |
443 | directory `rmail-spool-directory' (whose value depends on the | |
444 | operating system). For example, \"/var/mail/USER\"." | |
445 | ;; Don't use backquote here, because we don't want to need it at load time. | |
446 | ;; (That must be an old comment - it's dumped these days.) | |
537ab246 BG |
447 | :type (list 'choice '(const :tag "Default" nil) |
448 | (list 'repeat ':value (list (or (getenv "MAIL") | |
dec5f46d GM |
449 | (concat rmail-spool-directory |
450 | (user-login-name)))) | |
537ab246 BG |
451 | 'file)) |
452 | :group 'rmail-retrieve | |
453 | :group 'rmail-files) | |
454 | ||
537ab246 | 455 | (defcustom rmail-mail-new-frame nil |
778ef2ef | 456 | "Non-nil means Rmail makes a new frame for composing outgoing mail. |
537ab246 BG |
457 | This is handy if you want to preserve the window configuration of |
458 | the frame where you have the RMAIL buffer displayed." | |
459 | :type 'boolean | |
460 | :group 'rmail-reply) | |
461 | ||
462 | ;;;###autoload | |
a7610c52 | 463 | (defcustom rmail-secondary-file-directory (purecopy "~/") |
778ef2ef | 464 | "Directory for additional secondary Rmail files." |
537ab246 BG |
465 | :type 'directory |
466 | :group 'rmail-files) | |
467 | ;;;###autoload | |
a7610c52 | 468 | (defcustom rmail-secondary-file-regexp (purecopy "\\.xmail$") |
778ef2ef | 469 | "Regexp for which files are secondary Rmail files." |
537ab246 BG |
470 | :type 'regexp |
471 | :group 'rmail-files) | |
472 | ||
537ab246 | 473 | (defcustom rmail-confirm-expunge 'y-or-n-p |
4df49ff1 RS |
474 | "Whether and how to ask for confirmation before expunging deleted messages. |
475 | The value, if non-nil is a function to call with a question (string) | |
476 | as argument, to ask the user that question." | |
537ab246 BG |
477 | :type '(choice (const :tag "No confirmation" nil) |
478 | (const :tag "Confirm with y-or-n-p" y-or-n-p) | |
479 | (const :tag "Confirm with yes-or-no-p" yes-or-no-p)) | |
480 | :version "21.1" | |
481 | :group 'rmail-files) | |
4df49ff1 | 482 | (put 'rmail-confirm-expunge 'risky-local-variable t) |
537ab246 BG |
483 | |
484 | ;;;###autoload | |
485 | (defvar rmail-mode-hook nil | |
486 | "List of functions to call when Rmail is invoked.") | |
487 | ||
537ab246 BG |
488 | (defvar rmail-get-new-mail-hook nil |
489 | "List of functions to call when Rmail has retrieved new mail.") | |
490 | ||
491 | ;;;###autoload | |
492 | (defcustom rmail-show-message-hook nil | |
493 | "List of functions to call when Rmail displays a message." | |
494 | :type 'hook | |
495 | :options '(goto-address) | |
496 | :group 'rmail) | |
497 | ||
537ab246 BG |
498 | (defvar rmail-quit-hook nil |
499 | "List of functions to call when quitting out of Rmail.") | |
500 | ||
537ab246 BG |
501 | (defvar rmail-delete-message-hook nil |
502 | "List of functions to call when Rmail deletes a message. | |
503 | When the hooks are called, the message has been marked deleted but is | |
504 | still the current message in the Rmail buffer.") | |
505 | ||
506 | ;; These may be altered by site-init.el to match the format of mmdf files | |
507 | ;; delimiting used on a given host (delim1 and delim2 from the config | |
508 | ;; files). | |
509 | ||
510 | (defvar rmail-mmdf-delim1 "^\001\001\001\001\n" | |
511 | "Regexp marking the start of an mmdf message.") | |
512 | (defvar rmail-mmdf-delim2 "^\001\001\001\001\n" | |
513 | "Regexp marking the end of an mmdf message.") | |
514 | ||
66be413e GM |
515 | ;; FIXME Post-mbox, this is now unused. |
516 | ;; In Emacs-22, this was called: | |
517 | ;; i) the very first time a message was shown. | |
518 | ;; ii) when toggling the headers to the normal state, every time. | |
519 | ;; It's not clear what it should do now, since there is nothing that | |
520 | ;; records when a message is shown for the first time (unseen is not | |
521 | ;; necessarily the same thing). | |
50419064 | 522 | ;; See http://lists.gnu.org/archive/html/emacs-devel/2009-03/msg00013.html |
537ab246 BG |
523 | (defcustom rmail-message-filter nil |
524 | "If non-nil, a filter function for new messages in RMAIL. | |
525 | Called with region narrowed to the message, including headers, | |
526 | before obeying `rmail-ignored-headers'." | |
527 | :group 'rmail-headers | |
528 | :type '(choice (const nil) function)) | |
529 | ||
50419064 GM |
530 | (make-obsolete-variable 'rmail-message-filter |
531 | "it is not used (try `rmail-show-message-hook')." | |
532 | "23.1") | |
533 | ||
537ab246 | 534 | (defcustom rmail-automatic-folder-directives nil |
9aac4de2 GM |
535 | "List of directives specifying how to automatically file messages. |
536 | Whenever Rmail shows a message in the folder that `rmail-file-name' | |
537 | specifies, it calls `rmail-auto-file' to maybe file the message in | |
538 | another folder according to this list. Messages that are already | |
539 | marked as `filed', or are in different folders, are left alone. | |
540 | ||
537ab246 BG |
541 | Each element of the list is of the form: |
542 | ||
543 | (FOLDERNAME FIELD REGEXP [ FIELD REGEXP ] ... ) | |
544 | ||
9aac4de2 GM |
545 | FOLDERNAME is the name of a folder in which to put the message. |
546 | If FOLDERNAME is nil then Rmail deletes the message, and moves on to | |
547 | the next. If FOLDERNAME is \"/dev/null\", Rmail deletes the message, | |
548 | but does not move to the next. | |
537ab246 | 549 | |
9aac4de2 GM |
550 | FIELD is the name of a header field in the message, such as |
551 | \"subject\" or \"from\". A FIELD of \"to\" includes all text | |
552 | from both the \"to\" and \"cc\" headers. | |
537ab246 | 553 | |
9aac4de2 GM |
554 | REGEXP is a regular expression to match (case-sensitively) against |
555 | the preceding specified FIELD. | |
537ab246 | 556 | |
9aac4de2 GM |
557 | There may be any number of FIELD/REGEXP pairs. |
558 | All pairs must match for a directive to apply to a message. | |
559 | For a given message, Rmail applies only the first matching directive. | |
537ab246 | 560 | |
9aac4de2 | 561 | Examples: |
537ab246 | 562 | (\"/dev/null\" \"from\" \"@spam.com\") ; delete all mail from spam.com |
75790248 | 563 | (\"RMS\" \"from\" \"rms@\") ; save all mail from RMS. |
9aac4de2 | 564 | " |
537ab246 BG |
565 | :group 'rmail |
566 | :version "21.1" | |
567 | :type '(repeat (sexp :tag "Directive"))) | |
568 | ||
569 | (defvar rmail-reply-prefix "Re: " | |
570 | "String to prepend to Subject line when replying to a message.") | |
571 | ||
572 | ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:". | |
573 | ;; This pattern should catch all the common variants. | |
574 | ;; rms: I deleted the change to delete tags in square brackets | |
575 | ;; because they mess up RT tags. | |
576 | (defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*" | |
577 | "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.") | |
578 | ||
579 | (defcustom rmail-display-summary nil | |
778ef2ef | 580 | "If non-nil, Rmail always displays the summary buffer." |
537ab246 BG |
581 | :group 'rmail-summary |
582 | :type 'boolean) | |
583 | \f | |
584 | (defvar rmail-inbox-list nil) | |
585 | (put 'rmail-inbox-list 'permanent-local t) | |
586 | ||
587 | (defvar rmail-buffer nil | |
588 | "The RMAIL buffer related to the current buffer. | |
589 | In an RMAIL buffer, this holds the RMAIL buffer itself. | |
590 | In a summary buffer, this holds the RMAIL buffer it is a summary for.") | |
591 | (put 'rmail-buffer 'permanent-local t) | |
592 | ||
9aadce25 RS |
593 | (defvar rmail-was-converted nil |
594 | "Non-nil in an Rmail buffer that was just converted from Babyl format.") | |
595 | (put 'rmail-was-converted 'permanent-local t) | |
596 | ||
597 | (defvar rmail-seriously-modified nil | |
598 | "Non-nil in an Rmail buffer that has been modified in a major way.") | |
599 | (put 'rmail-seriously-modified 'permanent-local t) | |
600 | ||
537ab246 BG |
601 | ;; Message counters and markers. Deleted flags. |
602 | ||
75790248 | 603 | (defvar rmail-current-message nil |
f3fe222a GM |
604 | "Integer specifying the message currently being displayed in this folder. |
605 | Counts messages from 1 to `rmail-total-messages'. A value of 0 | |
606 | means there are no messages in the folder.") | |
537ab246 BG |
607 | (put 'rmail-current-message 'permanent-local t) |
608 | ||
75790248 GM |
609 | (defvar rmail-total-messages nil |
610 | "Integer specifying the total number of messages in this folder. | |
611 | Includes deleted messages.") | |
537ab246 BG |
612 | (put 'rmail-total-messages 'permanent-local t) |
613 | ||
75790248 GM |
614 | (defvar rmail-message-vector nil |
615 | "Vector of markers specifying the start and end of each message. | |
616 | Element N and N+1 specify the start and end of message N.") | |
537ab246 BG |
617 | (put 'rmail-message-vector 'permanent-local t) |
618 | ||
75790248 GM |
619 | (defvar rmail-deleted-vector nil |
620 | "A string of length `rmail-total-messages' plus one. | |
621 | Character N is either a space or \"D\", according to whether | |
622 | message N is deleted or not.") | |
537ab246 BG |
623 | (put 'rmail-deleted-vector 'permanent-local t) |
624 | ||
625 | (defvar rmail-msgref-vector nil | |
626 | "In an Rmail buffer, a vector whose Nth element is a list (N). | |
627 | When expunging renumbers messages, these lists are modified | |
628 | by substituting the new message number into the existing list.") | |
629 | (put 'rmail-msgref-vector 'permanent-local t) | |
630 | ||
631 | (defvar rmail-overlay-list nil) | |
632 | (put 'rmail-overlay-list 'permanent-local t) | |
633 | ||
634 | ;; These are used by autoloaded rmail-summary. | |
635 | ||
636 | (defvar rmail-summary-buffer nil) | |
637 | (put 'rmail-summary-buffer 'permanent-local t) | |
95ca567f GM |
638 | (defvar rmail-summary-vector nil |
639 | "In an Rmail buffer, vector of (newline-terminated) strings. | |
640 | Element N specifies the summary line for message N+1.") | |
537ab246 BG |
641 | (put 'rmail-summary-vector 'permanent-local t) |
642 | ||
643 | ;; Rmail buffer swapping variables. | |
644 | ||
645 | (defvar rmail-buffer-swapped nil | |
646 | "If non-nil, `rmail-buffer' is swapped with `rmail-view-buffer'.") | |
eec86b01 | 647 | (make-variable-buffer-local 'rmail-buffer-swapped) |
51aca0f5 | 648 | (put 'rmail-buffer-swapped 'permanent-local t) |
537ab246 BG |
649 | |
650 | (defvar rmail-view-buffer nil | |
651 | "Buffer which holds RMAIL message for MIME displaying.") | |
2dc00ad0 | 652 | (make-variable-buffer-local 'rmail-view-buffer) |
537ab246 BG |
653 | (put 'rmail-view-buffer 'permanent-local t) |
654 | \f | |
655 | ;; `Sticky' default variables. | |
656 | ||
657 | ;; Last individual label specified to a or k. | |
658 | (defvar rmail-last-label nil) | |
659 | ||
660 | ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l. | |
661 | (defvar rmail-last-multi-labels nil) | |
662 | ||
663 | (defvar rmail-last-regexp nil) | |
664 | (put 'rmail-last-regexp 'permanent-local t) | |
665 | ||
ee218151 | 666 | ;; Note that rmail-output-read-file-name modifies this. |
537ab246 | 667 | (defcustom rmail-default-file "~/xmail" |
778ef2ef | 668 | "Default file name for \\[rmail-output]." |
537ab246 BG |
669 | :type 'file |
670 | :group 'rmail-files) | |
671 | (defcustom rmail-default-body-file "~/mailout" | |
778ef2ef | 672 | "Default file name for \\[rmail-output-body-to-file]." |
537ab246 BG |
673 | :type 'file |
674 | :group 'rmail-files | |
675 | :version "20.3") | |
676 | ||
677 | ;; Mule and MIME related variables. | |
678 | ||
679 | ;;;###autoload | |
680 | (defvar rmail-file-coding-system nil | |
681 | "Coding system used in RMAIL file. | |
682 | ||
683 | This is set to nil by default.") | |
684 | ||
d1be4ec2 | 685 | (defcustom rmail-enable-mime t |
45261b50 GM |
686 | "If non-nil, RMAIL automatically displays decoded MIME messages. |
687 | For this to work, the feature specified by `rmail-mime-feature' must | |
688 | be available." | |
689 | :type 'boolean | |
d1be4ec2 | 690 | :version "23.3" |
537ab246 BG |
691 | :group 'rmail) |
692 | ||
7fb18e9e GM |
693 | (defcustom rmail-enable-mime-composing t |
694 | "If non-nil, use `rmail-insert-mime-forwarded-message-function' to forward." | |
695 | :type 'boolean | |
696 | :version "24.1" ; nil -> t | |
697 | :group 'rmail) | |
537ab246 | 698 | |
537ab246 | 699 | (defvar rmail-show-mime-function nil |
45261b50 | 700 | "Function of no argument called to show a decoded MIME message. |
537ab246 | 701 | This function is called when `rmail-enable-mime' is non-nil. |
45261b50 | 702 | The package providing MIME support should set this.") |
537ab246 BG |
703 | |
704 | ;;;###autoload | |
705 | (defvar rmail-insert-mime-forwarded-message-function nil | |
706 | "Function to insert a message in MIME format so it can be forwarded. | |
7fb18e9e GM |
707 | This function is called if `rmail-enable-mime' and |
708 | `rmail-enable-mime-composing' are non-nil. | |
537ab246 BG |
709 | It is called with one argument FORWARD-BUFFER, which is a |
710 | buffer containing the message to forward. The current buffer | |
711 | is the outgoing mail buffer.") | |
712 | ||
537ab246 BG |
713 | (defvar rmail-insert-mime-resent-message-function nil |
714 | "Function to insert a message in MIME format so it can be resent. | |
778ef2ef | 715 | This function is called by `rmail-resend' if `rmail-enable-mime' is non-nil. |
537ab246 BG |
716 | It is called with one argument FORWARD-BUFFER, which is a |
717 | buffer containing the message to forward. The current buffer | |
718 | is the outgoing mail buffer.") | |
719 | ||
778ef2ef GM |
720 | ;; FIXME one might want to pass a LIMIT, as per |
721 | ;; rmail-search-mime-header-function. | |
537ab246 BG |
722 | (defvar rmail-search-mime-message-function nil |
723 | "Function to check if a regexp matches a MIME message. | |
778ef2ef GM |
724 | This function is called by `rmail-search-message' if |
725 | `rmail-enable-mime' is non-nil. It is called (with point at the | |
726 | start of the message) with two arguments MSG and REGEXP, where | |
537ab246 BG |
727 | MSG is the message number, REGEXP is the regular expression.") |
728 | ||
537ab246 BG |
729 | (defvar rmail-search-mime-header-function nil |
730 | "Function to check if a regexp matches a header of MIME message. | |
778ef2ef GM |
731 | This function is called by `rmail-message-regexp-p-1' if |
732 | `rmail-enable-mime' is non-nil. It is called (with point at the | |
733 | start of the header) with three arguments MSG, REGEXP, and LIMIT, | |
734 | where MSG is the message number, REGEXP is the regular | |
735 | expression, LIMIT is the position specifying the end of header.") | |
537ab246 | 736 | |
d1be4ec2 | 737 | (defvar rmail-mime-feature 'rmailmm |
45261b50 | 738 | "Feature to require for MIME support in Rmail. |
7fb18e9e GM |
739 | When starting Rmail, if `rmail-enable-mime' is non-nil, this |
740 | feature is loaded with `require'. The default value is `rmailmm'. | |
537ab246 | 741 | |
7fb18e9e GM |
742 | The library should set the variable `rmail-show-mime-function' |
743 | to an appropriate value, and optionally also set | |
744 | `rmail-search-mime-message-function', | |
745 | `rmail-search-mime-header-function', | |
746 | `rmail-insert-mime-forwarded-message-function', and | |
747 | `rmail-insert-mime-resent-message-function'.") | |
537ab246 | 748 | |
537ab246 BG |
749 | (defvar rmail-mime-charset-pattern |
750 | (concat "^content-type:[ \t]*text/plain;" | |
751 | "\\(?:[ \t\n]*\\(?:format\\|delsp\\)=\"?[-a-z0-9]+\"?;\\)*" | |
752 | "[ \t\n]*charset=\"?\\([^ \t\n\";]+\\)\"?") | |
753 | "Regexp to match MIME-charset specification in a header of message. | |
754 | The first parenthesized expression should match the MIME-charset name.") | |
755 | ||
756 | \f | |
537ab246 BG |
757 | (defvar rmail-unix-mail-delimiter |
758 | (let ((time-zone-regexp | |
759 | (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?" | |
760 | "\\|[-+]?[0-9][0-9][0-9][0-9]" | |
761 | "\\|" | |
762 | "\\) *"))) | |
763 | (concat | |
764 | "From " | |
765 | ||
766 | ;; Many things can happen to an RFC 822 mailbox before it is put into | |
767 | ;; a `From' line. The leading phrase can be stripped, e.g. | |
768 | ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g. | |
769 | ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF | |
770 | ;; can be removed, e.g. | |
771 | ;; From: joe@y.z (Joe K | |
772 | ;; User) | |
773 | ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and | |
774 | ;; From: Joe User | |
775 | ;; <joe@y.z> | |
776 | ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'. | |
777 | ;; The mailbox can be removed or be replaced by white space, e.g. | |
778 | ;; From: "Joe User"{space}{tab} | |
779 | ;; <joe@y.z> | |
780 | ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996', | |
781 | ;; where {space} and {tab} represent the Ascii space and tab characters. | |
782 | ;; We want to match the results of any of these manglings. | |
783 | ;; The following regexp rejects names whose first characters are | |
784 | ;; obviously bogus, but after that anything goes. | |
785 | "\\([^\0-\b\n-\r\^?].*\\)? " | |
786 | ||
787 | ;; The time the message was sent. | |
788 | "\\([^\0-\r \^?]+\\) +" ; day of the week | |
789 | "\\([^\0-\r \^?]+\\) +" ; month | |
790 | "\\([0-3]?[0-9]\\) +" ; day of month | |
791 | "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day | |
792 | ||
793 | ;; Perhaps a time zone, specified by an abbreviation, or by a | |
794 | ;; numeric offset. | |
795 | time-zone-regexp | |
796 | ||
797 | ;; The year. | |
798 | " \\([0-9][0-9]+\\) *" | |
799 | ||
800 | ;; On some systems the time zone can appear after the year, too. | |
801 | time-zone-regexp | |
802 | ||
803 | ;; Old uucp cruft. | |
804 | "\\(remote from .*\\)?" | |
805 | ||
806 | "\n")) | |
2dc00ad0 SM |
807 | "Regexp matching the delimiter of messages in UNIX mail format |
808 | \(UNIX From lines), minus the initial ^. Note that if you change | |
4de724b0 | 809 | this expression, you must change the code in `rmail-nuke-pinhead-header' |
2dc00ad0 | 810 | that knows the exact ordering of the \\( \\) subexpressions.") |
537ab246 | 811 | |
66be413e GM |
812 | ;; FIXME the rmail-header-name headers ought to be customizable. |
813 | ;; It seems a bit arbitrary, for example, that all of the Date: line | |
814 | ;; gets highlighted. | |
537ab246 BG |
815 | (defvar rmail-font-lock-keywords |
816 | ;; These are all matched case-insensitively. | |
817 | (eval-when-compile | |
818 | (let* ((cite-chars "[>|}]") | |
a7183d7c | 819 | (cite-prefix "[:alpha:]") |
537ab246 BG |
820 | (cite-suffix (concat cite-prefix "0-9_.@-`'\""))) |
821 | (list '("^\\(From\\|Sender\\|Resent-From\\):" | |
822 | . 'rmail-header-name) | |
4ad1654a GM |
823 | '("^\\(Mail-\\)?Reply-To:.*$" . 'rmail-header-name) |
824 | ;; FIXME Mail-Followup-To should probably be here too. | |
537ab246 BG |
825 | '("^Subject:" . 'rmail-header-name) |
826 | '("^X-Spam-Status:" . 'rmail-header-name) | |
827 | '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):" | |
828 | . 'rmail-header-name) | |
829 | ;; Use MATCH-ANCHORED to effectively anchor the regexp left side. | |
830 | `(,cite-chars | |
831 | (,(concat "\\=[ \t]*" | |
832 | "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?" | |
833 | "\\(" cite-chars "[ \t]*\\)\\)+\\)" | |
834 | "\\(.*\\)") | |
835 | (beginning-of-line) (end-of-line) | |
836 | (1 font-lock-comment-delimiter-face nil t) | |
837 | (5 font-lock-comment-face nil t))) | |
838 | '("^\\(X-[a-z0-9-]+\\|In-reply-to\\|Date\\):.*\\(\n[ \t]+.*\\)*$" | |
839 | . 'rmail-header-name)))) | |
840 | "Additional expressions to highlight in Rmail mode.") | |
841 | ||
c920f222 GM |
842 | ;; Rmail does not expect horizontal splitting. (Bug#2282) |
843 | (defun rmail-pop-to-buffer (&rest args) | |
844 | "Like `pop-to-buffer', but with `split-width-threshold' set to nil." | |
845 | (let (split-width-threshold) | |
846 | (apply 'pop-to-buffer args))) | |
847 | ||
537ab246 BG |
848 | ;; Perform BODY in the summary buffer |
849 | ;; in such a way that its cursor is properly updated in its own window. | |
850 | (defmacro rmail-select-summary (&rest body) | |
851 | `(let ((total rmail-total-messages)) | |
852 | (if (rmail-summary-displayed) | |
853 | (let ((window (selected-window))) | |
854 | (save-excursion | |
855 | (unwind-protect | |
856 | (progn | |
c920f222 | 857 | (rmail-pop-to-buffer rmail-summary-buffer) |
537ab246 BG |
858 | ;; rmail-total-messages is a buffer-local var |
859 | ;; in the rmail buffer. | |
860 | ;; This way we make it available for the body | |
861 | ;; even tho the rmail buffer is not current. | |
862 | (let ((rmail-total-messages total)) | |
863 | ,@body)) | |
864 | (select-window window)))) | |
2dc00ad0 | 865 | (with-current-buffer rmail-summary-buffer |
537ab246 BG |
866 | (let ((rmail-total-messages total)) |
867 | ,@body))) | |
868 | (rmail-maybe-display-summary))) | |
869 | \f | |
870 | ;;;; *** Rmail Mode *** | |
871 | ||
537ab246 BG |
872 | (defun rmail-require-mime-maybe () |
873 | "Require `rmail-mime-feature' if that is non-nil. | |
874 | Signal an error and set `rmail-mime-feature' to nil if the feature | |
875 | isn't provided." | |
876 | (when rmail-enable-mime | |
877 | (condition-case err | |
878 | (require rmail-mime-feature) | |
879 | (error | |
880 | (display-warning | |
881 | 'rmail | |
882 | (format "Although MIME support is requested | |
45261b50 | 883 | through `rmail-enable-mime' being non-nil, the required feature |
537ab246 BG |
884 | `%s' (the value of `rmail-mime-feature') |
885 | is not available in the current session. | |
45261b50 | 886 | So, MIME support is turned off for the moment." |
537ab246 BG |
887 | rmail-mime-feature) |
888 | :warning) | |
889 | (setq rmail-enable-mime nil))))) | |
890 | ||
891 | ||
892 | ;;;###autoload | |
893 | (defun rmail (&optional file-name-arg) | |
894 | "Read and edit incoming mail. | |
6e9db169 EZ |
895 | Moves messages into file named by `rmail-file-name' and edits that |
896 | file in RMAIL Mode. | |
537ab246 BG |
897 | Type \\[describe-mode] once editing that file, for a list of RMAIL commands. |
898 | ||
899 | May be called with file name as argument; then performs rmail editing on | |
900 | that file, but does not copy any new mail into the file. | |
901 | Interactively, if you supply a prefix argument, then you | |
902 | have a chance to specify a file name with the minibuffer. | |
903 | ||
904 | If `rmail-display-summary' is non-nil, make a summary for this RMAIL file." | |
905 | (interactive (if current-prefix-arg | |
906 | (list (read-file-name "Run rmail on RMAIL file: ")))) | |
907 | (rmail-require-mime-maybe) | |
908 | (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name))) | |
909 | ;; Use find-buffer-visiting, not get-file-buffer, for those users | |
910 | ;; who have find-file-visit-truename set to t. | |
911 | (existed (find-buffer-visiting file-name)) | |
912 | run-mail-hook mail-buf msg-shown) | |
913 | ;; Determine if an existing mail file has been changed behind the | |
914 | ;; scene... | |
915 | (if (and existed (not (verify-visited-file-modtime existed))) | |
916 | ;; The mail file has been changed. Revisit it and reset the | |
917 | ;; message state variables when in rmail mode. | |
918 | (progn | |
919 | (find-file file-name) | |
920 | (when (and (verify-visited-file-modtime existed) | |
921 | (eq major-mode 'rmail-mode)) | |
40f9db32 | 922 | (rmail-swap-buffers-maybe) |
537ab246 BG |
923 | (rmail-set-message-counters))) |
924 | ;; The mail file is either unchanged or not visited. Visit it. | |
925 | (switch-to-buffer | |
634b455a EZ |
926 | (let ((enable-local-variables nil) |
927 | ;; Force no-conversion by default, since that's what | |
928 | ;; pre-mbox Rmail did with BABYL files (via | |
929 | ;; auto-coding-regexp-alist). | |
930 | (coding-system-for-read | |
931 | (or coding-system-for-read 'no-conversion))) | |
537ab246 | 932 | (find-file-noselect file-name)))) |
2dc00ad0 SM |
933 | ;; Ensure that the collection and view buffers are in sync and |
934 | ;; ensure that a message is not being edited. | |
537ab246 BG |
935 | (if (eq major-mode 'rmail-mode) |
936 | (rmail-swap-buffers-maybe)) | |
937 | (if (eq major-mode 'rmail-edit-mode) | |
938 | (error "Exit Rmail Edit mode before getting new mail")) | |
939 | (or (and existed (> (buffer-size) 0)) | |
940 | (setq run-mail-hook t)) | |
2dc00ad0 | 941 | ;; Ensure that the Rmail file is in mbox format, the buffer is in |
537ab246 BG |
942 | ;; Rmail mode and has been scanned to find all the messages |
943 | ;; (setting the global message variables in the process). | |
944 | (rmail-convert-file-maybe) | |
945 | (unless (eq major-mode 'rmail-mode) | |
946 | (rmail-mode-2)) | |
947 | (goto-char (point-max)) | |
948 | (rmail-maybe-set-message-counters) | |
949 | (setq mail-buf rmail-buffer) | |
950 | ;; Show the first unread message and process summary mode. | |
951 | (unwind-protect | |
952 | ;; Only get new mail when there is not a file name argument. | |
953 | (unless file-name-arg | |
433c1652 | 954 | (setq msg-shown (rmail-get-new-mail))) |
537ab246 BG |
955 | (progn |
956 | (set-buffer mail-buf) | |
433c1652 GM |
957 | (or msg-shown |
958 | (rmail-show-message (rmail-first-unseen-message))) | |
537ab246 BG |
959 | (if rmail-display-summary (rmail-summary)) |
960 | (rmail-construct-io-menu) | |
961 | (if run-mail-hook | |
962 | (run-hooks 'rmail-mode-hook)))))) | |
963 | ||
964 | (defun rmail-convert-file-maybe () | |
965 | "Determine if the file needs to be converted to mbox format." | |
966 | (widen) | |
967 | (goto-char (point-min)) | |
968 | ;; Detect previous Babyl format files. | |
c7eb0ba1 RS |
969 | (let ((case-fold-search nil)) |
970 | (cond ((looking-at "BABYL OPTIONS:") | |
971 | ;; The file is Babyl version 5. Use unrmail to convert | |
972 | ;; it. | |
973 | (rmail-convert-babyl-to-mbox)) | |
974 | ((looking-at "Version: 5\n") | |
975 | ;; Losing babyl file made by old version of Rmail. Fix the | |
976 | ;; babyl file header and use unrmail to convert to mbox | |
977 | ;; format. | |
978 | (let ((buffer-read-only nil)) | |
979 | (insert "BABYL OPTIONS: -*- rmail -*-\n") | |
980 | (rmail-convert-babyl-to-mbox))) | |
981 | ((equal (point-min) (point-max)) | |
982 | (message "Empty Rmail file.")) | |
983 | ((looking-at "From ")) | |
984 | (t (error "Invalid mbox file"))))) | |
537ab246 BG |
985 | |
986 | (defun rmail-error-bad-format (&optional msgnum) | |
987 | "Report that the buffer is not in the mbox file format. | |
988 | MSGNUM, if present, indicates the malformed message." | |
989 | (if msgnum | |
990 | (error "Message %d is not a valid RFC2822 message" msgnum) | |
991 | (error "Message is not a valid RFC2822 message"))) | |
992 | ||
993 | (defun rmail-convert-babyl-to-mbox () | |
994 | "Convert the mail file from Babyl version 5 to mbox. | |
995 | This function also reinitializes local variables used by Rmail." | |
996 | (let ((old-file (make-temp-file "rmail")) | |
997 | (new-file (make-temp-file "rmail"))) | |
998 | (unwind-protect | |
999 | (progn | |
1000 | (kill-all-local-variables) | |
1001 | (write-region (point-min) (point-max) old-file) | |
1002 | (unrmail old-file new-file) | |
1003 | (message "Replacing BABYL format with mbox format...") | |
39514778 | 1004 | (let ((inhibit-read-only t) |
bad4f1fd GM |
1005 | (coding-system-for-read 'raw-text) |
1006 | (buffer-undo-list t)) | |
537ab246 | 1007 | (erase-buffer) |
39514778 | 1008 | (insert-file-contents new-file) |
aa8c6958 EZ |
1009 | ;; Rmail buffers need to be saved with Unix EOLs, or else |
1010 | ;; the format will not be recognized. | |
1011 | (set-buffer-file-coding-system 'raw-text-unix) | |
537ab246 BG |
1012 | (rmail-mode-1) |
1013 | (rmail-perm-variables) | |
1014 | (rmail-variables) | |
9aadce25 | 1015 | (setq rmail-was-converted t) |
4df49ff1 | 1016 | (rmail-dont-modify-format) |
537ab246 BG |
1017 | (goto-char (point-max)) |
1018 | (rmail-set-message-counters)) | |
1019 | (message "Replacing BABYL format with mbox format...done")) | |
1020 | (delete-file old-file) | |
1021 | (delete-file new-file)))) | |
1022 | ||
1023 | (defun rmail-get-coding-system () | |
1024 | "Return a suitable coding system to use for the current mail message. | |
1025 | The buffer is expected to be narrowed to just the header of the message." | |
fe83a300 KH |
1026 | (save-excursion |
1027 | (goto-char (point-min)) | |
6b0c98f5 | 1028 | (if (re-search-forward rmail-mime-charset-pattern nil t) |
fe83a300 | 1029 | (coding-system-from-name (match-string 1)) |
537ab246 BG |
1030 | 'undecided))) |
1031 | \f | |
1032 | ;;; Set up Rmail mode keymaps | |
1033 | ||
2dc00ad0 SM |
1034 | (defvar rmail-mode-map |
1035 | (let ((map (make-keymap))) | |
1036 | (suppress-keymap map) | |
1037 | (define-key map "a" 'rmail-add-label) | |
1038 | (define-key map "b" 'rmail-bury) | |
1039 | (define-key map "c" 'rmail-continue) | |
1040 | (define-key map "d" 'rmail-delete-forward) | |
1041 | (define-key map "\C-d" 'rmail-delete-backward) | |
1042 | (define-key map "e" 'rmail-edit-current-message) | |
017166ce | 1043 | ;; If you change this, change the rmail-resend menu-item's :keys. |
2dc00ad0 SM |
1044 | (define-key map "f" 'rmail-forward) |
1045 | (define-key map "g" 'rmail-get-new-mail) | |
1046 | (define-key map "h" 'rmail-summary) | |
1047 | (define-key map "i" 'rmail-input) | |
809f3af0 | 1048 | (define-key map "j" 'rmail-show-message) |
2dc00ad0 SM |
1049 | (define-key map "k" 'rmail-kill-label) |
1050 | (define-key map "l" 'rmail-summary-by-labels) | |
1051 | (define-key map "\e\C-h" 'rmail-summary) | |
1052 | (define-key map "\e\C-l" 'rmail-summary-by-labels) | |
1053 | (define-key map "\e\C-r" 'rmail-summary-by-recipients) | |
1054 | (define-key map "\e\C-s" 'rmail-summary-by-regexp) | |
b0c4cdcf | 1055 | (define-key map "\e\C-f" 'rmail-summary-by-senders) |
2dc00ad0 SM |
1056 | (define-key map "\e\C-t" 'rmail-summary-by-topic) |
1057 | (define-key map "m" 'rmail-mail) | |
1058 | (define-key map "\em" 'rmail-retry-failure) | |
1059 | (define-key map "n" 'rmail-next-undeleted-message) | |
1060 | (define-key map "\en" 'rmail-next-message) | |
1061 | (define-key map "\e\C-n" 'rmail-next-labeled-message) | |
1062 | (define-key map "o" 'rmail-output) | |
1063 | (define-key map "\C-o" 'rmail-output-as-seen) | |
1064 | (define-key map "p" 'rmail-previous-undeleted-message) | |
1065 | (define-key map "\ep" 'rmail-previous-message) | |
1066 | (define-key map "\e\C-p" 'rmail-previous-labeled-message) | |
1067 | (define-key map "q" 'rmail-quit) | |
1068 | (define-key map "r" 'rmail-reply) | |
1069 | ;; I find I can't live without the default M-r command -- rms. | |
1070 | ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards) | |
1071 | (define-key map "s" 'rmail-expunge-and-save) | |
1072 | (define-key map "\es" 'rmail-search) | |
1073 | (define-key map "t" 'rmail-toggle-header) | |
1074 | (define-key map "u" 'rmail-undelete-previous-message) | |
dec5f46d | 1075 | (define-key map "v" 'rmail-mime) |
2dc00ad0 SM |
1076 | (define-key map "w" 'rmail-output-body-to-file) |
1077 | (define-key map "\C-c\C-w" 'rmail-widen) | |
1078 | (define-key map "x" 'rmail-expunge) | |
1079 | (define-key map "." 'rmail-beginning-of-message) | |
1080 | (define-key map "/" 'rmail-end-of-message) | |
1081 | (define-key map "<" 'rmail-first-message) | |
1082 | (define-key map ">" 'rmail-last-message) | |
ce3cefcc CY |
1083 | (define-key map " " 'scroll-up-command) |
1084 | (define-key map "\177" 'scroll-down-command) | |
2dc00ad0 SM |
1085 | (define-key map "?" 'describe-mode) |
1086 | (define-key map "\C-c\C-s\C-d" 'rmail-sort-by-date) | |
1087 | (define-key map "\C-c\C-s\C-s" 'rmail-sort-by-subject) | |
1088 | (define-key map "\C-c\C-s\C-a" 'rmail-sort-by-author) | |
1089 | (define-key map "\C-c\C-s\C-r" 'rmail-sort-by-recipient) | |
1090 | (define-key map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent) | |
1091 | (define-key map "\C-c\C-s\C-l" 'rmail-sort-by-lines) | |
1092 | (define-key map "\C-c\C-s\C-k" 'rmail-sort-by-labels) | |
1093 | (define-key map "\C-c\C-n" 'rmail-next-same-subject) | |
1094 | (define-key map "\C-c\C-p" 'rmail-previous-same-subject) | |
1095 | ||
537ab246 | 1096 | \f |
2dc00ad0 SM |
1097 | (define-key map [menu-bar] (make-sparse-keymap)) |
1098 | ||
1099 | (define-key map [menu-bar classify] | |
1100 | (cons "Classify" (make-sparse-keymap "Classify"))) | |
537ab246 | 1101 | |
2dc00ad0 SM |
1102 | (define-key map [menu-bar classify input-menu] |
1103 | nil) | |
537ab246 | 1104 | |
2dc00ad0 SM |
1105 | (define-key map [menu-bar classify output-menu] |
1106 | nil) | |
537ab246 | 1107 | |
2dc00ad0 SM |
1108 | (define-key map [menu-bar classify output-body] |
1109 | '("Output body to file..." . rmail-output-body-to-file)) | |
537ab246 | 1110 | |
2dc00ad0 SM |
1111 | (define-key map [menu-bar classify output-inbox] |
1112 | '("Output..." . rmail-output)) | |
537ab246 | 1113 | |
2dc00ad0 SM |
1114 | (define-key map [menu-bar classify output] |
1115 | '("Output as seen..." . rmail-output-as-seen)) | |
537ab246 | 1116 | |
2dc00ad0 SM |
1117 | (define-key map [menu-bar classify kill-label] |
1118 | '("Kill Label..." . rmail-kill-label)) | |
537ab246 | 1119 | |
2dc00ad0 SM |
1120 | (define-key map [menu-bar classify add-label] |
1121 | '("Add Label..." . rmail-add-label)) | |
537ab246 | 1122 | |
2dc00ad0 SM |
1123 | (define-key map [menu-bar summary] |
1124 | (cons "Summary" (make-sparse-keymap "Summary"))) | |
537ab246 | 1125 | |
2dc00ad0 SM |
1126 | (define-key map [menu-bar summary senders] |
1127 | '("By Senders..." . rmail-summary-by-senders)) | |
537ab246 | 1128 | |
2dc00ad0 SM |
1129 | (define-key map [menu-bar summary labels] |
1130 | '("By Labels..." . rmail-summary-by-labels)) | |
537ab246 | 1131 | |
2dc00ad0 SM |
1132 | (define-key map [menu-bar summary recipients] |
1133 | '("By Recipients..." . rmail-summary-by-recipients)) | |
537ab246 | 1134 | |
2dc00ad0 SM |
1135 | (define-key map [menu-bar summary topic] |
1136 | '("By Topic..." . rmail-summary-by-topic)) | |
537ab246 | 1137 | |
2dc00ad0 SM |
1138 | (define-key map [menu-bar summary regexp] |
1139 | '("By Regexp..." . rmail-summary-by-regexp)) | |
537ab246 | 1140 | |
2dc00ad0 SM |
1141 | (define-key map [menu-bar summary all] |
1142 | '("All" . rmail-summary)) | |
537ab246 | 1143 | |
2dc00ad0 SM |
1144 | (define-key map [menu-bar mail] |
1145 | (cons "Mail" (make-sparse-keymap "Mail"))) | |
537ab246 | 1146 | |
2dc00ad0 SM |
1147 | (define-key map [menu-bar mail rmail-get-new-mail] |
1148 | '("Get New Mail" . rmail-get-new-mail)) | |
537ab246 | 1149 | |
2dc00ad0 SM |
1150 | (define-key map [menu-bar mail lambda] |
1151 | '("----")) | |
537ab246 | 1152 | |
2dc00ad0 SM |
1153 | (define-key map [menu-bar mail continue] |
1154 | '("Continue" . rmail-continue)) | |
537ab246 | 1155 | |
2dc00ad0 | 1156 | (define-key map [menu-bar mail resend] |
017166ce | 1157 | '(menu-item "Resend..." rmail-resend :keys "C-u f")) |
537ab246 | 1158 | |
2dc00ad0 SM |
1159 | (define-key map [menu-bar mail forward] |
1160 | '("Forward" . rmail-forward)) | |
537ab246 | 1161 | |
2dc00ad0 SM |
1162 | (define-key map [menu-bar mail retry] |
1163 | '("Retry" . rmail-retry-failure)) | |
537ab246 | 1164 | |
2dc00ad0 SM |
1165 | (define-key map [menu-bar mail reply] |
1166 | '("Reply" . rmail-reply)) | |
537ab246 | 1167 | |
2dc00ad0 SM |
1168 | (define-key map [menu-bar mail mail] |
1169 | '("Mail" . rmail-mail)) | |
537ab246 | 1170 | |
2dc00ad0 SM |
1171 | (define-key map [menu-bar delete] |
1172 | (cons "Delete" (make-sparse-keymap "Delete"))) | |
537ab246 | 1173 | |
2dc00ad0 SM |
1174 | (define-key map [menu-bar delete expunge/save] |
1175 | '("Expunge/Save" . rmail-expunge-and-save)) | |
537ab246 | 1176 | |
2dc00ad0 SM |
1177 | (define-key map [menu-bar delete expunge] |
1178 | '("Expunge" . rmail-expunge)) | |
537ab246 | 1179 | |
2dc00ad0 SM |
1180 | (define-key map [menu-bar delete undelete] |
1181 | '("Undelete" . rmail-undelete-previous-message)) | |
537ab246 | 1182 | |
2dc00ad0 SM |
1183 | (define-key map [menu-bar delete delete] |
1184 | '("Delete" . rmail-delete-forward)) | |
537ab246 | 1185 | |
2dc00ad0 SM |
1186 | (define-key map [menu-bar move] |
1187 | (cons "Move" (make-sparse-keymap "Move"))) | |
537ab246 | 1188 | |
2dc00ad0 SM |
1189 | (define-key map [menu-bar move search-back] |
1190 | '("Search Back..." . rmail-search-backwards)) | |
537ab246 | 1191 | |
2dc00ad0 SM |
1192 | (define-key map [menu-bar move search] |
1193 | '("Search..." . rmail-search)) | |
537ab246 | 1194 | |
2dc00ad0 SM |
1195 | (define-key map [menu-bar move previous] |
1196 | '("Previous Nondeleted" . rmail-previous-undeleted-message)) | |
537ab246 | 1197 | |
2dc00ad0 SM |
1198 | (define-key map [menu-bar move next] |
1199 | '("Next Nondeleted" . rmail-next-undeleted-message)) | |
537ab246 | 1200 | |
2dc00ad0 SM |
1201 | (define-key map [menu-bar move last] |
1202 | '("Last" . rmail-last-message)) | |
537ab246 | 1203 | |
2dc00ad0 SM |
1204 | (define-key map [menu-bar move first] |
1205 | '("First" . rmail-first-message)) | |
537ab246 | 1206 | |
2dc00ad0 SM |
1207 | (define-key map [menu-bar move previous] |
1208 | '("Previous" . rmail-previous-message)) | |
537ab246 | 1209 | |
2dc00ad0 SM |
1210 | (define-key map [menu-bar move next] |
1211 | '("Next" . rmail-next-message)) | |
537ab246 | 1212 | |
95ca567f GM |
1213 | map) |
1214 | "Keymap used in Rmail mode.") | |
537ab246 BG |
1215 | |
1216 | ;; Rmail toolbar | |
1217 | (defvar rmail-tool-bar-map | |
1218 | (let ((map (make-sparse-keymap))) | |
1219 | (tool-bar-local-item-from-menu 'rmail-get-new-mail "mail/inbox" | |
1220 | map rmail-mode-map) | |
1221 | (tool-bar-local-item-from-menu 'rmail-next-undeleted-message "right-arrow" | |
1222 | map rmail-mode-map) | |
1223 | (tool-bar-local-item-from-menu 'rmail-previous-undeleted-message "left-arrow" | |
1224 | map rmail-mode-map) | |
1225 | (tool-bar-local-item-from-menu 'rmail-search "search" | |
1226 | map rmail-mode-map) | |
1227 | (tool-bar-local-item-from-menu 'rmail-input "open" | |
1228 | map rmail-mode-map) | |
1229 | (tool-bar-local-item-from-menu 'rmail-mail "mail/compose" | |
1230 | map rmail-mode-map) | |
1231 | (tool-bar-local-item-from-menu 'rmail-reply "mail/reply-all" | |
1232 | map rmail-mode-map) | |
1233 | (tool-bar-local-item-from-menu 'rmail-forward "mail/forward" | |
1234 | map rmail-mode-map) | |
1235 | (tool-bar-local-item-from-menu 'rmail-delete-forward "close" | |
1236 | map rmail-mode-map) | |
1237 | (tool-bar-local-item-from-menu 'rmail-output "mail/move" | |
1238 | map rmail-mode-map) | |
1239 | (tool-bar-local-item-from-menu 'rmail-output-body-to-file "mail/save" | |
1240 | map rmail-mode-map) | |
1241 | (tool-bar-local-item-from-menu 'rmail-expunge "delete" | |
1242 | map rmail-mode-map) | |
1243 | map)) | |
1244 | ||
1245 | ||
1246 | \f | |
1247 | ;; Rmail mode is suitable only for specially formatted data. | |
1248 | (put 'rmail-mode 'mode-class 'special) | |
1249 | ||
1250 | (defun rmail-mode-kill-summary () | |
1251 | (if rmail-summary-buffer (kill-buffer rmail-summary-buffer))) | |
1252 | ||
4de724b0 GM |
1253 | (defvar rmail-enable-multibyte) ; dynamically bound |
1254 | ||
537ab246 BG |
1255 | ;;;###autoload |
1256 | (defun rmail-mode () | |
1257 | "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files. | |
1258 | All normal editing commands are turned off. | |
1259 | Instead, these commands are available: | |
1260 | ||
1261 | \\[rmail-beginning-of-message] Move point to front of this message. | |
1262 | \\[rmail-end-of-message] Move point to bottom of this message. | |
1263 | \\[scroll-up] Scroll to next screen of this message. | |
1264 | \\[scroll-down] Scroll to previous screen of this message. | |
1265 | \\[rmail-next-undeleted-message] Move to Next non-deleted message. | |
1266 | \\[rmail-previous-undeleted-message] Move to Previous non-deleted message. | |
1267 | \\[rmail-next-message] Move to Next message whether deleted or not. | |
1268 | \\[rmail-previous-message] Move to Previous message whether deleted or not. | |
1269 | \\[rmail-first-message] Move to the first message in Rmail file. | |
1270 | \\[rmail-last-message] Move to the last message in Rmail file. | |
809f3af0 | 1271 | \\[rmail-show-message] Jump to message specified by numeric position in file. |
537ab246 BG |
1272 | \\[rmail-search] Search for string and show message it is found in. |
1273 | \\[rmail-delete-forward] Delete this message, move to next nondeleted. | |
1274 | \\[rmail-delete-backward] Delete this message, move to previous nondeleted. | |
1275 | \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages | |
1276 | till a deleted message is found. | |
1277 | \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail. | |
1278 | \\[rmail-expunge] Expunge deleted messages. | |
1279 | \\[rmail-expunge-and-save] Expunge and save the file. | |
1280 | \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer. | |
1281 | \\[save-buffer] Save without expunging. | |
1282 | \\[rmail-get-new-mail] Move new mail from system spool directory into this file. | |
1283 | \\[rmail-mail] Mail a message (same as \\[mail-other-window]). | |
1284 | \\[rmail-continue] Continue composing outgoing message started before. | |
1285 | \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields. | |
1286 | \\[rmail-retry-failure] Send this message again. Used on a mailer failure message. | |
1287 | \\[rmail-forward] Forward this message to another user. | |
1288 | \\[rmail-output] Output (append) this message to another mail file. | |
1289 | \\[rmail-output-as-seen] Output (append) this message to file as it's displayed. | |
1290 | \\[rmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line. | |
1291 | \\[rmail-input] Input Rmail file. Run Rmail on that file. | |
1292 | \\[rmail-add-label] Add label to message. It will be displayed in the mode line. | |
1293 | \\[rmail-kill-label] Kill label. Remove a label from current message. | |
1294 | \\[rmail-next-labeled-message] Move to Next message with specified label | |
1295 | (label defaults to last one specified). | |
1296 | Standard labels: filed, unseen, answered, forwarded, deleted. | |
1297 | Any other label is present only if you add it with \\[rmail-add-label]. | |
1298 | \\[rmail-previous-labeled-message] Move to Previous message with specified label | |
1299 | \\[rmail-summary] Show headers buffer, with a one line summary of each message. | |
1300 | \\[rmail-summary-by-labels] Summarize only messages with particular label(s). | |
1301 | \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s). | |
1302 | \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s). | |
1303 | \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s). | |
1304 | \\[rmail-toggle-header] Toggle display of complete header." | |
1305 | (interactive) | |
1306 | (let ((finding-rmail-file (not (eq major-mode 'rmail-mode)))) | |
1307 | (rmail-mode-2) | |
1308 | (when (and finding-rmail-file | |
1309 | (null coding-system-for-read) | |
597e2240 | 1310 | (default-value 'enable-multibyte-characters)) |
537ab246 BG |
1311 | (let ((rmail-enable-multibyte t)) |
1312 | (rmail-require-mime-maybe) | |
1313 | (rmail-convert-file-maybe) | |
1314 | (goto-char (point-max)) | |
1315 | (set-buffer-multibyte t))) | |
1316 | (rmail-set-message-counters) | |
809f3af0 | 1317 | (rmail-show-message rmail-total-messages) |
537ab246 BG |
1318 | (when finding-rmail-file |
1319 | (when rmail-display-summary | |
1320 | (rmail-summary)) | |
1321 | (rmail-construct-io-menu)) | |
1322 | (run-mode-hooks 'rmail-mode-hook))) | |
1323 | ||
1324 | (defun rmail-mode-2 () | |
1325 | (kill-all-local-variables) | |
1326 | (rmail-mode-1) | |
1327 | (rmail-perm-variables) | |
1328 | (rmail-variables)) | |
1329 | ||
1330 | (defun rmail-mode-1 () | |
1331 | (setq major-mode 'rmail-mode) | |
1332 | (setq mode-name "RMAIL") | |
1333 | (setq buffer-read-only t) | |
1334 | ;; No need to auto save RMAIL files in normal circumstances | |
1335 | ;; because they contain no info except attribute changes | |
1336 | ;; and deletion of messages. | |
1337 | ;; The one exception is when messages are copied into another mbox buffer. | |
1338 | ;; rmail-output enables auto save when you do that. | |
1339 | (setq buffer-auto-save-file-name nil) | |
1340 | (use-local-map rmail-mode-map) | |
1341 | (set-syntax-table text-mode-syntax-table) | |
1342 | (setq local-abbrev-table text-mode-abbrev-table) | |
1343 | ;; Functions to support buffer swapping: | |
1344 | (add-hook 'write-region-annotate-functions | |
1345 | 'rmail-write-region-annotate nil t) | |
1346 | (add-hook 'kill-buffer-hook 'rmail-mode-kill-buffer-hook nil t) | |
1347 | (add-hook 'change-major-mode-hook 'rmail-change-major-mode-hook nil t)) | |
1348 | ||
1349 | (defun rmail-generate-viewer-buffer () | |
1350 | "Return a reusable buffer suitable for viewing messages. | |
1351 | Create the buffer if necessary." | |
ac3c593c GM |
1352 | ;; We want to reuse any existing view buffer, so as not to create an |
1353 | ;; endless number of them. But we must avoid clashes if we visit | |
1354 | ;; two different rmail files with the same basename (Bug#4593). | |
1355 | (if (and (local-variable-p 'rmail-view-buffer) | |
1356 | (buffer-live-p rmail-view-buffer)) | |
1357 | rmail-view-buffer | |
e08b633b RS |
1358 | (let ((newbuf |
1359 | (generate-new-buffer | |
1360 | (format " *message-viewer %s*" | |
1361 | (file-name-nondirectory | |
1362 | (or buffer-file-name (buffer-name))))))) | |
1363 | (with-current-buffer newbuf | |
1364 | (add-hook 'kill-buffer-hook 'rmail-view-buffer-kill-buffer-hook nil t)) | |
1365 | newbuf))) | |
537ab246 | 1366 | |
6e9db169 EZ |
1367 | (defun rmail-swap-buffers () |
1368 | "Swap text between current buffer and `rmail-view-buffer'. | |
1369 | This function preserves the current buffer's modified flag, and also | |
1370 | sets the current buffer's `buffer-file-coding-system' to that of | |
1371 | `rmail-view-buffer'." | |
5e7a9022 EZ |
1372 | (let ((modp-this (buffer-modified-p)) |
1373 | (modp-that | |
1374 | (with-current-buffer rmail-view-buffer (buffer-modified-p))) | |
1375 | (coding-this buffer-file-coding-system) | |
1376 | (coding-that | |
6e9db169 EZ |
1377 | (with-current-buffer rmail-view-buffer |
1378 | buffer-file-coding-system))) | |
1379 | (buffer-swap-text rmail-view-buffer) | |
5e7a9022 EZ |
1380 | (setq buffer-file-coding-system coding-that) |
1381 | (with-current-buffer rmail-view-buffer | |
1382 | (setq buffer-file-coding-system coding-this) | |
1383 | (restore-buffer-modified-p modp-that)) | |
1384 | (restore-buffer-modified-p modp-this))) | |
537ab246 BG |
1385 | |
1386 | (defun rmail-buffers-swapped-p () | |
1387 | "Return non-nil if the message collection is in `rmail-view-buffer'." | |
1388 | ;; This is analogous to tar-data-swapped-p in tar-mode.el. | |
de3bc99a | 1389 | rmail-buffer-swapped) |
537ab246 | 1390 | |
6e9db169 EZ |
1391 | (defun rmail-change-major-mode-hook () |
1392 | ;; Bring the actual Rmail messages back into the main buffer. | |
1393 | (when (rmail-buffers-swapped-p) | |
1394 | (rmail-swap-buffers) | |
1395 | (setq rmail-buffer-swapped nil))) | |
1396 | ||
537ab246 BG |
1397 | (defun rmail-swap-buffers-maybe () |
1398 | "Determine if the Rmail buffer is showing a message. | |
1399 | If so restore the actual mbox message collection." | |
1400 | (with-current-buffer rmail-buffer | |
1401 | (when (rmail-buffers-swapped-p) | |
6e9db169 | 1402 | (rmail-swap-buffers) |
537ab246 BG |
1403 | (setq rmail-buffer-swapped nil)))) |
1404 | ||
9aadce25 RS |
1405 | (defun rmail-modify-format () |
1406 | "Warn if important modifications would change Rmail file's format." | |
1407 | (with-current-buffer rmail-buffer | |
1408 | (and rmail-was-converted | |
1409 | ;; If it's already modified, don't warn again. | |
1410 | (not rmail-seriously-modified) | |
1411 | (not | |
1412 | (yes-or-no-p | |
1413 | (message "After this, %s would be saved in mbox format. Proceed? " | |
1414 | (buffer-name)))) | |
1415 | (error "Aborted")) | |
1416 | (setq rmail-seriously-modified t))) | |
1417 | ||
1418 | (defun rmail-dont-modify-format () | |
1419 | (when (and rmail-was-converted (not rmail-seriously-modified)) | |
1420 | (set-buffer-modified-p nil) | |
1421 | (message "Marking buffer unmodified to avoid rewriting Babyl file as mbox file"))) | |
1422 | ||
537ab246 | 1423 | (defun rmail-mode-kill-buffer-hook () |
e08b633b RS |
1424 | ;; Turn off the hook on the view buffer, so we can kill it, then kill it. |
1425 | (if (buffer-live-p rmail-view-buffer) | |
1426 | (with-current-buffer rmail-view-buffer | |
1427 | (setq kill-buffer-hook nil) | |
1428 | (kill-buffer rmail-view-buffer)))) | |
1429 | ||
1430 | (defun rmail-view-buffer-kill-buffer-hook () | |
de3bc99a RS |
1431 | (error "Can't kill Rmail view buffer `%s' by itself" |
1432 | (buffer-name (current-buffer)))) | |
537ab246 BG |
1433 | |
1434 | ;; Set up the permanent locals associated with an Rmail file. | |
1435 | (defun rmail-perm-variables () | |
1436 | (make-local-variable 'rmail-last-regexp) | |
1437 | (make-local-variable 'rmail-deleted-vector) | |
1438 | (make-local-variable 'rmail-buffer) | |
9aadce25 RS |
1439 | (make-local-variable 'rmail-was-converted) |
1440 | (setq rmail-was-converted nil) | |
1441 | (make-local-variable 'rmail-seriously-modified) | |
1442 | (setq rmail-seriously-modified nil) | |
537ab246 BG |
1443 | (setq rmail-buffer (current-buffer)) |
1444 | (set-buffer-multibyte nil) | |
2dc00ad0 | 1445 | (with-current-buffer (setq rmail-view-buffer (rmail-generate-viewer-buffer)) |
537ab246 | 1446 | (setq buffer-undo-list t) |
ac3c593c GM |
1447 | ;; Note that this does not erase the buffer. Should it? |
1448 | ;; It depends on how this is called. If somehow called with the | |
1449 | ;; rmail buffers swapped, it would erase the message collection. | |
26eb677a | 1450 | (set (make-local-variable 'rmail-overlay-list) nil) |
aa8c6958 EZ |
1451 | (set-buffer-multibyte t) |
1452 | ;; Force C-x C-s write Unix EOLs. | |
1453 | (set-buffer-file-coding-system 'undecided-unix)) | |
537ab246 BG |
1454 | (make-local-variable 'rmail-summary-buffer) |
1455 | (make-local-variable 'rmail-summary-vector) | |
1456 | (make-local-variable 'rmail-current-message) | |
1457 | (make-local-variable 'rmail-total-messages) | |
1458 | (setq rmail-total-messages 0) | |
537ab246 BG |
1459 | (make-local-variable 'rmail-message-vector) |
1460 | (make-local-variable 'rmail-msgref-vector) | |
1461 | (make-local-variable 'rmail-inbox-list) | |
1462 | ;; Provide default set of inboxes for primary mail file ~/RMAIL. | |
1463 | (and (null rmail-inbox-list) | |
1464 | (or (equal buffer-file-name (expand-file-name rmail-file-name)) | |
1465 | (equal buffer-file-truename | |
1466 | (abbreviate-file-name (file-truename rmail-file-name)))) | |
1467 | (setq rmail-inbox-list | |
1468 | (or rmail-primary-inbox-list | |
1469 | (list (or (getenv "MAIL") | |
dec5f46d | 1470 | ;; FIXME expand-file-name? |
537ab246 BG |
1471 | (concat rmail-spool-directory |
1472 | (user-login-name))))))) | |
1473 | (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map)) | |
1474 | ||
1475 | ;; Set up the non-permanent locals associated with Rmail mode. | |
1476 | (defun rmail-variables () | |
1477 | ;; Turn off undo. We turn it back on in rmail-edit. | |
1478 | (setq buffer-undo-list t) | |
1479 | ;; Don't let a local variables list in a message cause confusion. | |
1480 | (make-local-variable 'local-enable-local-variables) | |
1481 | (setq local-enable-local-variables nil) | |
7a907299 RS |
1482 | ;; Don't turn off auto-saving based on the size of the buffer |
1483 | ;; because that code does not understand buffer-swapping. | |
a4f69701 CY |
1484 | (make-local-variable 'auto-save-include-big-deletions) |
1485 | (setq auto-save-include-big-deletions t) | |
537ab246 BG |
1486 | (make-local-variable 'revert-buffer-function) |
1487 | (setq revert-buffer-function 'rmail-revert) | |
1488 | (make-local-variable 'font-lock-defaults) | |
1489 | (setq font-lock-defaults | |
1490 | '(rmail-font-lock-keywords | |
1491 | t t nil nil | |
1492 | (font-lock-maximum-size . nil) | |
1493 | (font-lock-fontify-buffer-function . rmail-fontify-buffer-function) | |
1494 | (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function) | |
1495 | (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode)))) | |
1496 | (make-local-variable 'require-final-newline) | |
1497 | (setq require-final-newline nil) | |
1498 | (make-local-variable 'version-control) | |
1499 | (setq version-control 'never) | |
1500 | (make-local-variable 'kill-buffer-hook) | |
1501 | (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary) | |
1502 | (make-local-variable 'file-precious-flag) | |
1503 | (setq file-precious-flag t) | |
1504 | (make-local-variable 'desktop-save-buffer) | |
14b4e83d RS |
1505 | (setq desktop-save-buffer t) |
1506 | (setq next-error-move-function 'rmail-next-error-move)) | |
537ab246 BG |
1507 | \f |
1508 | ;; Handle M-x revert-buffer done in an rmail-mode buffer. | |
1509 | (defun rmail-revert (arg noconfirm) | |
1510 | (set-buffer rmail-buffer) | |
1511 | (let* ((revert-buffer-function (default-value 'revert-buffer-function)) | |
1512 | (rmail-enable-multibyte enable-multibyte-characters) | |
1513 | ;; See similar code in `rmail'. | |
8e7a0f83 | 1514 | ;; FIXME needs updating? |
40f9db32 GM |
1515 | (coding-system-for-read (and rmail-enable-multibyte 'raw-text)) |
1516 | (before-revert-hook 'rmail-swap-buffers-maybe)) | |
537ab246 BG |
1517 | ;; Call our caller again, but this time it does the default thing. |
1518 | (when (revert-buffer arg noconfirm) | |
1519 | ;; If the user said "yes", and we changed something, | |
1520 | ;; reparse the messages. | |
1521 | (set-buffer rmail-buffer) | |
1522 | (rmail-mode-2) | |
1523 | ;; Convert all or part to Babyl file if possible. | |
1524 | (rmail-convert-file-maybe) | |
1525 | ;; We have read the file as raw-text, so the buffer is set to | |
1526 | ;; unibyte. Make it multibyte if necessary. | |
1527 | (if (and rmail-enable-multibyte | |
1528 | (not enable-multibyte-characters)) | |
1529 | (set-buffer-multibyte t)) | |
1530 | (goto-char (point-max)) | |
1531 | (rmail-set-message-counters) | |
809f3af0 | 1532 | (rmail-show-message rmail-total-messages) |
537ab246 BG |
1533 | (run-hooks 'rmail-mode-hook)))) |
1534 | ||
1535 | (defun rmail-expunge-and-save () | |
1536 | "Expunge and save RMAIL file." | |
1537 | (interactive) | |
1538 | (set-buffer rmail-buffer) | |
384b53ab | 1539 | (rmail-expunge) |
2dc00ad0 SM |
1540 | ;; No need to swap buffers: rmail-write-region-annotate takes care of it. |
1541 | ;; (rmail-swap-buffers-maybe) | |
537ab246 BG |
1542 | (save-buffer) |
1543 | (if (rmail-summary-exists) | |
384b53ab | 1544 | (rmail-select-summary (set-buffer-modified-p nil)))) |
537ab246 BG |
1545 | |
1546 | (defun rmail-quit () | |
1547 | "Quit out of RMAIL. | |
1548 | Hook `rmail-quit-hook' is run after expunging." | |
1549 | (interactive) | |
1550 | (set-buffer rmail-buffer) | |
1551 | (rmail-expunge t) | |
537ab246 BG |
1552 | (save-buffer) |
1553 | (when (boundp 'rmail-quit-hook) | |
1554 | (run-hooks 'rmail-quit-hook)) | |
1555 | ;; Don't switch to the summary buffer even if it was recently visible. | |
1556 | (when rmail-summary-buffer | |
384b53ab GM |
1557 | (with-current-buffer rmail-summary-buffer |
1558 | (set-buffer-modified-p nil)) | |
537ab246 BG |
1559 | (replace-buffer-in-windows rmail-summary-buffer) |
1560 | (bury-buffer rmail-summary-buffer)) | |
d1be4ec2 KH |
1561 | (let ((obuf (current-buffer))) |
1562 | (quit-window) | |
1563 | (replace-buffer-in-windows obuf))) | |
537ab246 BG |
1564 | |
1565 | (defun rmail-bury () | |
1566 | "Bury current Rmail buffer and its summary buffer." | |
1567 | (interactive) | |
1568 | ;; This let var was called rmail-buffer, but that interfered | |
1569 | ;; with the buffer-local var used in summary buffers. | |
1570 | (let ((buffer-to-bury (current-buffer))) | |
1571 | (if (rmail-summary-exists) | |
1572 | (let (window) | |
1573 | (while (setq window (get-buffer-window rmail-summary-buffer)) | |
1574 | (quit-window nil window)) | |
1575 | (bury-buffer rmail-summary-buffer))) | |
1576 | (quit-window))) | |
1577 | \f | |
1578 | (defun rmail-duplicate-message () | |
1579 | "Create a duplicated copy of the current message. | |
75790248 | 1580 | The duplicate copy goes into the Rmail file just after the original." |
50dcb784 | 1581 | ;; If we are in a summary buffer, switch to the Rmail buffer. |
75790248 | 1582 | ;; FIXME simpler to swap the contents, not the buffers? |
50dcb784 | 1583 | (set-buffer rmail-buffer) |
9aadce25 | 1584 | (rmail-modify-format) |
50dcb784 GM |
1585 | (let ((buff (current-buffer)) |
1586 | (n rmail-current-message) | |
1587 | (beg (rmail-msgbeg rmail-current-message)) | |
1588 | (end (rmail-msgend rmail-current-message))) | |
1589 | (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer)) | |
75790248 GM |
1590 | (widen) |
1591 | (let ((buffer-read-only nil) | |
50dcb784 GM |
1592 | (string (buffer-substring-no-properties beg end))) |
1593 | (goto-char end) | |
1594 | (insert string)) | |
1595 | (set-buffer buff) | |
1596 | (rmail-swap-buffers-maybe) | |
1597 | (goto-char (point-max)) | |
1598 | (rmail-set-message-counters) | |
1599 | (set-buffer-modified-p t) | |
574bc51d | 1600 | (rmail-show-message-1 n)) |
50dcb784 GM |
1601 | (if (rmail-summary-exists) |
1602 | (rmail-select-summary (rmail-update-summary))) | |
1603 | (message "Message duplicated")) | |
537ab246 BG |
1604 | \f |
1605 | ;;;###autoload | |
1606 | (defun rmail-input (filename) | |
1607 | "Run Rmail on file FILENAME." | |
1608 | (interactive "FRun rmail on RMAIL file: ") | |
1609 | (rmail filename)) | |
1610 | ||
1611 | ;; This used to scan subdirectories recursively, but someone pointed out | |
1612 | ;; that if the user wants that, person can put all the files in one dir. | |
1613 | ;; And the recursive scan was slow. So I took it out. | |
1614 | ;; rms, Sep 1996. | |
1615 | (defun rmail-find-all-files (start) | |
1616 | "Return list of file in dir START that match `rmail-secondary-file-regexp'." | |
1617 | (if (file-accessible-directory-p start) | |
1618 | ;; Don't sort here. | |
1619 | (let* ((case-fold-search t) | |
1620 | (files (directory-files start t rmail-secondary-file-regexp))) | |
1621 | ;; Sort here instead of in directory-files | |
1622 | ;; because this list is usually much shorter. | |
1623 | (sort files 'string<)))) | |
1624 | ||
1625 | (defun rmail-list-to-menu (menu-name l action &optional full-name) | |
433c1652 GM |
1626 | (let ((menu (make-sparse-keymap menu-name)) |
1627 | name) | |
537ab246 BG |
1628 | (mapc |
1629 | (lambda (item) | |
1630 | (let (command) | |
1631 | (if (consp item) | |
1632 | (setq command | |
1633 | (rmail-list-to-menu | |
1634 | (car item) (cdr item) action | |
1635 | (if full-name | |
1636 | (concat full-name "/" | |
1637 | (car item)) | |
1638 | (car item))) | |
1639 | name (car item)) | |
1640 | (setq name item) | |
1641 | (setq command | |
1642 | (list 'lambda () '(interactive) | |
1643 | (list action | |
1644 | (expand-file-name | |
1645 | (if full-name | |
1646 | (concat full-name "/" item) | |
1647 | item) | |
1648 | rmail-secondary-file-directory))))) | |
1649 | (define-key menu (vector (intern name)) | |
1650 | (cons name command)))) | |
1651 | (reverse l)) | |
1652 | menu)) | |
1653 | ||
1654 | ;; This command is always "disabled" when it appears in a menu. | |
1655 | (put 'rmail-disable-menu 'menu-enable ''nil) | |
1656 | ||
1657 | (defun rmail-construct-io-menu () | |
1658 | (let ((files (rmail-find-all-files rmail-secondary-file-directory))) | |
1659 | (if files | |
1660 | (progn | |
1661 | (define-key rmail-mode-map [menu-bar classify input-menu] | |
1662 | (cons "Input Rmail File" | |
1663 | (rmail-list-to-menu "Input Rmail File" | |
1664 | files | |
1665 | 'rmail-input))) | |
1666 | (define-key rmail-mode-map [menu-bar classify output-menu] | |
1667 | (cons "Output Rmail File" | |
1668 | (rmail-list-to-menu "Output Rmail File" | |
1669 | files | |
1670 | 'rmail-output)))) | |
1671 | ||
1672 | (define-key rmail-mode-map [menu-bar classify input-menu] | |
1673 | '("Input Rmail File" . rmail-disable-menu)) | |
1674 | (define-key rmail-mode-map [menu-bar classify output-menu] | |
1675 | '("Output Rmail File" . rmail-disable-menu))))) | |
1676 | ||
1677 | \f | |
1678 | ;;;; *** Rmail input *** | |
1679 | ||
537ab246 BG |
1680 | (declare-function rmail-summary-goto-msg "rmailsum" (&optional n nowarn skip-rmail)) |
1681 | (declare-function rmail-summary-mark-undeleted "rmailsum" (n)) | |
1682 | (declare-function rmail-summary-mark-deleted "rmailsum" (&optional n undel)) | |
1683 | (declare-function rfc822-addresses "rfc822" (header-text)) | |
1684 | (declare-function mail-abbrev-make-syntax-table "mailabbrev.el" ()) | |
537ab246 BG |
1685 | |
1686 | ;; RLK feature not added in this version: | |
1687 | ;; argument specifies inbox file or files in various ways. | |
1688 | ||
dec5f46d GM |
1689 | ;; In Babyl, the Mail: header in the preamble overrode rmail-inbox-list. |
1690 | ;; Mbox does not have this feature. | |
537ab246 | 1691 | (defun rmail-get-new-mail (&optional file-name) |
dec5f46d GM |
1692 | "Move any new mail from this Rmail file's inbox files. |
1693 | The buffer-local variable `rmail-inbox-list' specifies the list | |
1694 | of inbox files. By default, this is nil, except for your primary | |
1695 | Rmail file `rmail-file-name'. In this case, when you first visit | |
1696 | the Rmail file it is initialized using either | |
1697 | `rmail-primary-inbox-list', or the \"MAIL\" environment variable, | |
1698 | or the function `user-login-name' and the directory | |
1699 | `rmail-spool-directory' (whose value depends on the operating system). | |
1700 | ||
1701 | The command `set-rmail-inbox-list' sets `rmail-inbox-list' to the | |
1702 | value you specify. | |
1703 | ||
1704 | You can also specify the file to get new mail from just for one | |
1705 | instance of this command. In this case, the file of new mail is | |
1706 | not changed or deleted. Noninteractively, you can pass the inbox | |
1707 | file name as an argument. Interactively, a prefix argument | |
1708 | causes us to read a file name and use that file as the inbox. | |
537ab246 BG |
1709 | |
1710 | If the variable `rmail-preserve-inbox' is non-nil, new mail will | |
1711 | always be left in inbox files rather than deleted. | |
1712 | ||
dec5f46d GM |
1713 | Before doing anything, this runs `rmail-before-get-new-mail-hook'. |
1714 | Just before returning, it runs `rmail-after-get-new-mail-hook', | |
1715 | whether or not there is new mail. | |
1716 | ||
1717 | If there is new mail, it runs `rmail-get-new-mail-hook', saves | |
1718 | the updated file, and shows the first unseen message (which might | |
1719 | not be a new one). It returns non-nil if it got any new messages." | |
537ab246 BG |
1720 | (interactive |
1721 | (list (if current-prefix-arg | |
1722 | (read-file-name "Get new mail from file: ")))) | |
1723 | (run-hooks 'rmail-before-get-new-mail-hook) | |
1724 | ;; If the disk file has been changed from under us, | |
1725 | ;; revert to it before we get new mail. | |
1726 | (or (verify-visited-file-modtime (current-buffer)) | |
1727 | (find-file (buffer-file-name))) | |
1728 | (set-buffer rmail-buffer) | |
9aadce25 | 1729 | (rmail-modify-format) |
537ab246 BG |
1730 | (rmail-swap-buffers-maybe) |
1731 | (rmail-maybe-set-message-counters) | |
1732 | (widen) | |
1733 | ;; Get rid of all undo records for this buffer. | |
1734 | (or (eq buffer-undo-list t) | |
1735 | (setq buffer-undo-list nil)) | |
1736 | (let ((all-files (if file-name (list file-name) rmail-inbox-list)) | |
1737 | (rmail-enable-multibyte (default-value 'enable-multibyte-characters)) | |
1738 | found) | |
1739 | (unwind-protect | |
a4e80191 GM |
1740 | (progn |
1741 | ;; This loops if any members of the inbox list have the same | |
1742 | ;; basename (see "name conflict" below). | |
1743 | (while all-files | |
1744 | (let ((opoint (point)) | |
1745 | ;; If buffer has not changed yet, and has not been | |
1746 | ;; saved yet, don't replace the old backup file now. | |
dec5f46d GM |
1747 | (make-backup-files (and make-backup-files |
1748 | (buffer-modified-p))) | |
a4e80191 GM |
1749 | (buffer-read-only nil) |
1750 | ;; Don't make undo records while getting mail. | |
1751 | (buffer-undo-list t) | |
1752 | delete-files success files file-last-names) | |
1753 | ;; Pull files off all-files onto files as long as there is | |
1754 | ;; no name conflict. A conflict happens when two inbox | |
1755 | ;; file names have the same last component. | |
1756 | ;; The reason this careful handling is necessary seems | |
1757 | ;; to be that rmail-insert-inbox-text uses .newmail-BASENAME. | |
bad4f1fd | 1758 | (while (and all-files |
a4e80191 | 1759 | (not (member (file-name-nondirectory (car all-files)) |
dec5f46d | 1760 | file-last-names))) |
a4e80191 GM |
1761 | (setq files (cons (car all-files) files) |
1762 | file-last-names | |
1763 | (cons (file-name-nondirectory (car all-files)) files)) | |
1764 | (setq all-files (cdr all-files))) | |
1765 | ;; Put them back in their original order. | |
1766 | (setq files (nreverse files)) | |
1767 | (goto-char (point-max)) | |
3c6702ef ML |
1768 | ;; Make sure we end with a blank line unless there are |
1769 | ;; no messages, as required by mbox format (Bug#9974). | |
1770 | (unless (bobp) | |
1771 | (while (not (looking-back "\n\n")) | |
1772 | (insert "\n"))) | |
a4e80191 GM |
1773 | (setq found (or |
1774 | (rmail-get-new-mail-1 file-name files delete-files) | |
bad4f1fd | 1775 | found)))) |
a4e80191 GM |
1776 | ;; Move to the first new message unless we have other unseen |
1777 | ;; messages before it. | |
809f3af0 | 1778 | (if found (rmail-show-message (rmail-first-unseen-message))) |
bad4f1fd GM |
1779 | (run-hooks 'rmail-after-get-new-mail-hook) |
1780 | found) | |
1781 | ;; Don't leave the buffer screwed up if we get a disk-full error. | |
809f3af0 | 1782 | (rmail-show-message)))) |
537ab246 | 1783 | |
25833b8c GM |
1784 | (defvar rmail-use-spam-filter) |
1785 | (declare-function rmail-get-new-mail-filter-spam "rmail-spam-filter" (nnew)) | |
1786 | ||
537ab246 BG |
1787 | (defun rmail-get-new-mail-1 (file-name files delete-files) |
1788 | "Return t if new messages are detected without error, nil otherwise." | |
1789 | (save-excursion | |
1790 | (save-restriction | |
1791 | (let ((new-messages 0) | |
1792 | (spam-filter-p (and (featurep 'rmail-spam-filter) | |
1793 | rmail-use-spam-filter)) | |
1794 | (blurb "") | |
1795 | result success suffix) | |
1796 | (narrow-to-region (point) (point)) | |
1797 | ;; Read in the contents of the inbox files, renaming them as | |
1798 | ;; necessary, and adding to the list of files to delete | |
1799 | ;; eventually. | |
1800 | (if file-name | |
1801 | (rmail-insert-inbox-text files nil) | |
1802 | (setq delete-files (rmail-insert-inbox-text files t))) | |
1803 | ;; Scan the new text and convert each message to | |
1804 | ;; Rmail/mbox format. | |
1805 | (goto-char (point-min)) | |
1806 | (skip-chars-forward " \n") | |
1807 | (narrow-to-region (point) (point-max)) | |
1808 | (unwind-protect | |
1809 | (setq new-messages (rmail-add-mbox-headers) | |
1810 | success t) | |
1811 | ;; Try to delete the garbage just inserted. | |
1812 | (or success (delete-region (point-min) (point-max))) | |
1813 | ;; If we could not convert the file's inboxes, rename the | |
1814 | ;; files we tried to read so we won't over and over again. | |
1815 | (if (and (not file-name) (not success)) | |
1816 | (let ((delfiles delete-files) | |
1817 | (count 0)) | |
1818 | (while delfiles | |
1819 | (while (file-exists-p (format "RMAILOSE.%d" count)) | |
1820 | (setq count (1+ count))) | |
1821 | (rename-file (car delfiles) (format "RMAILOSE.%d" count)) | |
1822 | (setq delfiles (cdr delfiles)))))) | |
1823 | ;; Determine if there are messages. | |
1824 | (unless (zerop new-messages) | |
1825 | ;; There are. Process them. | |
1826 | (goto-char (point-min)) | |
1827 | (rmail-count-new-messages) | |
1828 | (run-hooks 'rmail-get-new-mail-hook) | |
1829 | (save-buffer)) | |
1830 | ;; Delete the old files, now that the Rmail file is saved. | |
1831 | (while delete-files | |
1832 | (condition-case () | |
1833 | ;; First, try deleting. | |
1834 | (condition-case () | |
1835 | (delete-file (car delete-files)) | |
1836 | (file-error | |
1837 | ;; If we can't delete it, truncate it. | |
1838 | (write-region (point) (point) (car delete-files)))) | |
1839 | (file-error nil)) | |
1840 | (setq delete-files (cdr delete-files))) | |
1841 | (if (zerop new-messages) | |
1842 | (when (or file-name rmail-inbox-list) | |
1843 | (message "(No new mail has arrived)")) | |
25833b8c GM |
1844 | (if spam-filter-p |
1845 | (setq blurb (rmail-get-new-mail-filter-spam new-messages)))) | |
537ab246 BG |
1846 | (if (rmail-summary-exists) |
1847 | (rmail-select-summary (rmail-update-summary))) | |
1848 | (setq suffix (if (= 1 new-messages) "" "s")) | |
1849 | (message "%d new message%s read%s" new-messages suffix blurb) | |
bad4f1fd | 1850 | ;; Establish the return value. |
537ab246 | 1851 | (setq result (> new-messages 0)) |
537ab246 BG |
1852 | result)))) |
1853 | ||
537ab246 BG |
1854 | (defun rmail-parse-url (file) |
1855 | "Parse the supplied URL. Return (list MAILBOX-NAME REMOTE PASSWORD GOT-PASSWORD) | |
1856 | WHERE MAILBOX-NAME is the name of the mailbox suitable as argument to the | |
1857 | actual version of `movemail', REMOTE is non-nil if MAILBOX-NAME refers to | |
1858 | a remote mailbox, PASSWORD is the password if it should be | |
1859 | supplied as a separate argument to `movemail' or nil otherwise, GOT-PASSWORD | |
1860 | is non-nil if the user has supplied the password interactively. | |
1861 | " | |
1862 | (cond | |
1863 | ((string-match "^\\([^:]+\\)://\\(\\([^:@]+\\)\\(:\\([^@]+\\)\\)?@\\)?.*" file) | |
1864 | (let (got-password supplied-password | |
1865 | (proto (match-string 1 file)) | |
1866 | (user (match-string 3 file)) | |
1867 | (pass (match-string 5 file)) | |
1868 | (host (substring file (or (match-end 2) | |
1869 | (+ 3 (match-end 1)))))) | |
1870 | ||
1871 | (if (not pass) | |
1872 | (when rmail-remote-password-required | |
1873 | (setq got-password (not (rmail-have-password))) | |
1874 | (setq supplied-password (rmail-get-remote-password | |
1875 | (string-equal proto "imap")))) | |
1876 | ;; The password is embedded. Strip it out since movemail | |
1877 | ;; does not really like it, in spite of the movemail spec. | |
1878 | (setq file (concat proto "://" user "@" host))) | |
1879 | ||
1880 | (if (rmail-movemail-variant-p 'emacs) | |
1881 | (if (string-equal proto "pop") | |
1882 | (list (concat "po:" user ":" host) | |
1883 | t | |
1884 | (or pass supplied-password) | |
1885 | got-password) | |
1886 | (error "Emacs movemail does not support %s protocol" proto)) | |
1887 | (list file | |
1888 | (or (string-equal proto "pop") (string-equal proto "imap")) | |
1889 | (or supplied-password pass) | |
1890 | got-password)))) | |
1891 | ||
1892 | ((string-match "^po:\\([^:]+\\)\\(:\\(.*\\)\\)?" file) | |
1893 | (let (got-password supplied-password | |
1894 | (proto "pop") | |
1895 | (user (match-string 1 file)) | |
1896 | (host (match-string 3 file))) | |
1897 | ||
1898 | (when rmail-remote-password-required | |
1899 | (setq got-password (not (rmail-have-password))) | |
1900 | (setq supplied-password (rmail-get-remote-password nil))) | |
1901 | ||
1902 | (list file "pop" supplied-password got-password))) | |
1903 | ||
1904 | (t | |
1905 | (list file nil nil nil)))) | |
1906 | ||
91034865 EZ |
1907 | (defun rmail-unrmail-new-mail (from-file) |
1908 | "Replace newly read mail in Babyl format with equivalent mbox format. | |
1909 | ||
1910 | FROM-FILE is the Babyl file from which the new mail should be read." | |
1911 | (let ((to-file (make-temp-file "rmail")) | |
1912 | size) | |
1913 | (unrmail from-file to-file) | |
1914 | (let ((inhibit-read-only t) | |
1915 | (coding-system-for-read 'raw-text) | |
1916 | (buffer-undo-list t)) | |
1917 | (delete-region (point) (point-max)) | |
1918 | (setq size (nth 1 (insert-file-contents to-file))) | |
1919 | (delete-file to-file) | |
1920 | size))) | |
1921 | ||
1922 | (defun rmail-unrmail-new-mail-maybe (file size) | |
1923 | "If newly read mail from FILE is in Babyl format, convert it to mbox format. | |
1924 | ||
1925 | SIZE is the original size of the newly read mail. | |
1926 | Value is the size of the newly read mail after conversion." | |
1927 | ;; Detect previous Babyl format files. | |
1928 | (let ((case-fold-search nil) | |
1929 | (old-file file) | |
1930 | new-file) | |
1931 | (cond ((looking-at "BABYL OPTIONS:") | |
1932 | ;; The new mail is in Babyl version 5 format. Use unrmail | |
1933 | ;; to convert it. | |
1934 | (setq size (rmail-unrmail-new-mail old-file))) | |
1935 | ((looking-at "Version: 5\n") | |
1936 | ;; New mail is in Babyl format made by old version of | |
1937 | ;; Rmail. Fix the babyl file header and use unrmail to | |
1938 | ;; convert it. | |
1939 | (let ((buffer-read-only nil) | |
1940 | (write-region-annotate-functions nil) | |
1941 | (write-region-post-annotation-function nil) | |
1942 | (old-file (make-temp-file "rmail"))) | |
1943 | (insert "BABYL OPTIONS: -*- rmail -*-\n") | |
1944 | (forward-line -1) | |
1945 | (write-region (point) (point-max) old-file) | |
1946 | (setq size (rmail-unrmail-new-mail old-file)) | |
1947 | (delete-file old-file)))) | |
1948 | size)) | |
1949 | ||
537ab246 BG |
1950 | (defun rmail-insert-inbox-text (files renamep) |
1951 | ;; Detect a locked file now, so that we avoid moving mail | |
1952 | ;; out of the real inbox file. (That could scare people.) | |
1953 | (or (memq (file-locked-p buffer-file-name) '(nil t)) | |
1954 | (error "RMAIL file %s is locked" | |
1955 | (file-name-nondirectory buffer-file-name))) | |
38291a39 | 1956 | (let (file tofile delete-files movemail popmail got-password password) |
537ab246 BG |
1957 | (while files |
1958 | ;; Handle remote mailbox names specially; don't expand as filenames | |
1959 | ;; in case the userid contains a directory separator. | |
1960 | (setq file (car files)) | |
1961 | (let ((url-data (rmail-parse-url file))) | |
1962 | (setq file (nth 0 url-data)) | |
38291a39 | 1963 | (setq popmail (nth 1 url-data)) |
537ab246 BG |
1964 | (setq password (nth 2 url-data)) |
1965 | (setq got-password (nth 3 url-data))) | |
1966 | ||
38291a39 | 1967 | (if popmail |
537ab246 BG |
1968 | (setq renamep t) |
1969 | (setq file (file-truename | |
1970 | (substitute-in-file-name (expand-file-name file))))) | |
1971 | (setq tofile (expand-file-name | |
1972 | ;; Generate name to move to from inbox name, | |
1973 | ;; in case of multiple inboxes that need moving. | |
1974 | (concat ".newmail-" | |
1975 | (file-name-nondirectory | |
1976 | (if (memq system-type '(windows-nt cygwin ms-dos)) | |
1977 | ;; cannot have colons in file name | |
1978 | (replace-regexp-in-string ":" "-" file) | |
1979 | file))) | |
1980 | ;; Use the directory of this rmail file | |
1981 | ;; because it's a nuisance to use the homedir | |
1982 | ;; if that is on a full disk and this rmail | |
1983 | ;; file isn't. | |
1984 | (file-name-directory | |
1985 | (expand-file-name buffer-file-name)))) | |
1986 | ;; Always use movemail to rename the file, | |
1987 | ;; since there can be mailboxes in various directories. | |
38291a39 | 1988 | (when (not popmail) |
537ab246 BG |
1989 | ;; On some systems, /usr/spool/mail/foo is a directory |
1990 | ;; and the actual inbox is /usr/spool/mail/foo/foo. | |
1991 | (if (file-directory-p file) | |
1992 | (setq file (expand-file-name (user-login-name) | |
1993 | file)))) | |
38291a39 | 1994 | (cond (popmail |
537ab246 BG |
1995 | (message "Getting mail from the remote server ...")) |
1996 | ((and (file-exists-p tofile) | |
1997 | (/= 0 (nth 7 (file-attributes tofile)))) | |
1998 | (message "Getting mail from %s..." tofile)) | |
1999 | ((and (file-exists-p file) | |
2000 | (/= 0 (nth 7 (file-attributes file)))) | |
2001 | (message "Getting mail from %s..." file))) | |
2002 | ;; Set TOFILE if have not already done so, and | |
2003 | ;; rename or copy the file FILE to TOFILE if and as appropriate. | |
2004 | (cond ((not renamep) | |
2005 | (setq tofile file)) | |
38291a39 | 2006 | ((or (file-exists-p tofile) (and (not popmail) |
537ab246 BG |
2007 | (not (file-exists-p file)))) |
2008 | nil) | |
2009 | (t | |
2010 | (with-temp-buffer | |
2011 | (let ((errors (current-buffer))) | |
2012 | (buffer-disable-undo errors) | |
2013 | (let ((args | |
2014 | (append | |
2015 | (list (or rmail-movemail-program "movemail") nil errors nil) | |
2016 | (if rmail-preserve-inbox | |
2017 | (list "-p") | |
2018 | nil) | |
2019 | (if (rmail-movemail-variant-p 'mailutils) | |
2020 | (append (list "--emacs") rmail-movemail-flags) | |
2021 | rmail-movemail-flags) | |
2022 | (list file tofile) | |
2023 | (if password (list password) nil)))) | |
2024 | (apply 'call-process args)) | |
2025 | (if (not (buffer-modified-p errors)) | |
2026 | ;; No output => movemail won | |
2027 | nil | |
2028 | (set-buffer errors) | |
2029 | (subst-char-in-region (point-min) (point-max) | |
8e7a0f83 | 2030 | ?\n ?\s) |
537ab246 BG |
2031 | (goto-char (point-max)) |
2032 | (skip-chars-backward " \t") | |
2033 | (delete-region (point) (point-max)) | |
2034 | (goto-char (point-min)) | |
2035 | (if (looking-at "movemail: ") | |
2036 | (delete-region (point-min) (match-end 0))) | |
2037 | (beep t) | |
2038 | ;; If we just read the password, most likely it is | |
2039 | ;; wrong. Otherwise, see if there is a specific | |
2040 | ;; reason to think that the problem is a wrong passwd. | |
2041 | (if (or got-password | |
2042 | (re-search-forward rmail-remote-password-error | |
2043 | nil t)) | |
2044 | (rmail-set-remote-password nil)) | |
2045 | ||
2046 | ;; If using Mailutils, remove initial error code | |
2047 | ;; abbreviation | |
2048 | (when (rmail-movemail-variant-p 'mailutils) | |
2049 | (goto-char (point-min)) | |
2050 | (when (looking-at "[A-Z][A-Z0-9_]*:") | |
2051 | (delete-region (point-min) (match-end 0)))) | |
2052 | ||
2053 | (message "movemail: %s" | |
2054 | (buffer-substring (point-min) | |
2055 | (point-max))) | |
2056 | ||
2057 | (sit-for 3) | |
2058 | nil))))) | |
2059 | ||
2060 | ;; At this point, TOFILE contains the name to read: | |
2061 | ;; Either the alternate name (if we renamed) | |
2062 | ;; or the actual inbox (if not renaming). | |
2063 | (if (file-exists-p tofile) | |
2064 | (let ((coding-system-for-read 'no-conversion) | |
2065 | size) | |
2066 | (goto-char (point-max)) | |
91034865 EZ |
2067 | (setq size |
2068 | ;; If new mail is in Babyl format, convert it to mbox. | |
2069 | (rmail-unrmail-new-mail-maybe | |
2070 | tofile | |
2071 | (nth 1 (insert-file-contents tofile)))) | |
537ab246 | 2072 | (goto-char (point-max)) |
3c6702ef ML |
2073 | ;; Make sure the read-in mbox data properly ends with a |
2074 | ;; blank line unless it is of size 0. | |
2075 | (unless (zerop size) | |
2076 | (while (not (looking-back "\n\n")) | |
2077 | (insert "\n"))) | |
537ab246 BG |
2078 | (if (not (and rmail-preserve-inbox (string= file tofile))) |
2079 | (setq delete-files (cons tofile delete-files))))) | |
2080 | (message "") | |
2081 | (setq files (cdr files))) | |
2082 | delete-files)) | |
2083 | ||
2084 | ;; Decode the region specified by FROM and TO by CODING. | |
2085 | ;; If CODING is nil or an invalid coding system, decode by `undecided'. | |
2086 | (defun rmail-decode-region (from to coding &optional destination) | |
2087 | (if (or (not coding) (not (coding-system-p coding))) | |
2088 | (setq coding 'undecided)) | |
2089 | ;; Use -dos decoding, to remove ^M characters left from base64 or | |
2090 | ;; rogue qp-encoded text. | |
2091 | (decode-coding-region | |
2092 | from to (coding-system-change-eol-conversion coding 1) destination) | |
2093 | ;; Don't reveal the fact we used -dos decoding, as users generally | |
2094 | ;; will not expect the RMAIL buffer to use DOS EOL format. | |
6e9db169 EZ |
2095 | (cond |
2096 | ((null destination) | |
2097 | (setq buffer-file-coding-system | |
2098 | (setq last-coding-system-used | |
2099 | (coding-system-change-eol-conversion coding 0)))) | |
2100 | ((bufferp destination) | |
2101 | (with-current-buffer destination | |
2102 | (setq buffer-file-coding-system | |
2103 | (setq last-coding-system-used | |
2104 | (coding-system-change-eol-conversion coding 0))))))) | |
537ab246 | 2105 | |
26eb677a GM |
2106 | (defun rmail-ensure-blank-line () |
2107 | "Ensure a message ends in a blank line. | |
2108 | Call with point at the end of the message." | |
2109 | (unless (bolp) | |
2110 | (insert "\n")) | |
2111 | (unless (looking-back "\n\n") | |
2112 | (insert "\n"))) | |
2113 | ||
537ab246 BG |
2114 | (defun rmail-add-mbox-headers () |
2115 | "Validate the RFC2822 format for the new messages. | |
2116 | Point should be at the first new message. | |
8350f087 | 2117 | An error is signaled if the new messages are not RFC2822 |
537ab246 BG |
2118 | compliant. |
2119 | Unless an Rmail attribute header already exists, add it to the | |
2120 | new messages. Return the number of new messages." | |
2121 | (save-excursion | |
2122 | (save-restriction | |
2123 | (let ((count 0) | |
2124 | (start (point)) | |
2125 | (value "------U-") | |
c7eb0ba1 | 2126 | (case-fold-search nil) |
0f25a277 EZ |
2127 | (delim (concat "\n\n" rmail-unix-mail-delimiter)) |
2128 | limit stop) | |
537ab246 BG |
2129 | ;; Detect an empty inbox file. |
2130 | (unless (= start (point-max)) | |
2dc00ad0 | 2131 | ;; Scan the new messages to establish a count and to ensure that |
537ab246 | 2132 | ;; an attribute header is present. |
0f25a277 EZ |
2133 | (if (looking-at rmail-unix-mail-delimiter) |
2134 | (while (not stop) | |
2135 | ;; Determine if a new attribute header needs to be | |
2136 | ;; added to the message. | |
2137 | (if (search-forward "\n\n" nil t) | |
2138 | (progn | |
2139 | (setq count (1+ count)) | |
2140 | (narrow-to-region start (point)) | |
2141 | (unless (mail-fetch-field rmail-attribute-header) | |
2142 | (backward-char 1) | |
2143 | (insert rmail-attribute-header ": " value "\n")) | |
2144 | (widen)) | |
2145 | (rmail-error-bad-format)) | |
2146 | ;; Move to the next message. | |
2147 | (if (not (re-search-forward delim nil 'move)) | |
2148 | (setq stop t) | |
2149 | (goto-char (match-beginning 0)) | |
2150 | (forward-char 2)) | |
0e9aba0f EZ |
2151 | (setq start (point))) |
2152 | (rmail-error-bad-format))) | |
537ab246 BG |
2153 | count)))) |
2154 | \f | |
75790248 GM |
2155 | (defun rmail-get-header-1 (name) |
2156 | "Subroutine of `rmail-get-header'. | |
2157 | Narrow to header, call `mail-fetch-field' to find header NAME." | |
2158 | (if (search-forward "\n\n" nil t) | |
2159 | (progn | |
2160 | (narrow-to-region (point-min) (point)) | |
2161 | (mail-fetch-field name)) | |
2162 | (rmail-error-bad-format))) | |
2163 | ||
537ab246 BG |
2164 | (defun rmail-get-header (name &optional msgnum) |
2165 | "Return the value of message header NAME, nil if it has none. | |
2166 | MSGNUM specifies the message number to get it from. | |
2167 | If MSGNUM is nil, use the current message." | |
75790248 GM |
2168 | (rmail-apply-in-message msgnum 'rmail-get-header-1 name)) |
2169 | ||
2170 | (defun rmail-set-header-1 (name value) | |
2171 | "Subroutine of `rmail-set-header'. | |
95ca567f GM |
2172 | Narrow to header, set header NAME to VALUE, replacing existing if present. |
2173 | VALUE nil means to remove NAME altogether." | |
75790248 GM |
2174 | (if (search-forward "\n\n" nil t) |
2175 | (progn | |
2176 | (forward-char -1) | |
2177 | (narrow-to-region (point-min) (point)) | |
2178 | (goto-char (point-min)) | |
2179 | (if (re-search-forward (concat "^" (regexp-quote name) ":") nil 'move) | |
95ca567f GM |
2180 | (if value |
2181 | (progn | |
2182 | (delete-region (point) (line-end-position)) | |
2183 | (insert " " value)) | |
2184 | (delete-region (line-beginning-position) | |
2185 | (line-beginning-position 2))) | |
2186 | (if value (insert name ": " value "\n")))) | |
75790248 | 2187 | (rmail-error-bad-format))) |
537ab246 BG |
2188 | |
2189 | (defun rmail-set-header (name &optional msgnum value) | |
95ca567f GM |
2190 | "Set message header NAME to VALUE in message number MSGNUM. |
2191 | If MSGNUM is nil, use the current message. NAME and VALUE are strings. | |
2192 | VALUE may also be nil, meaning to remove the header." | |
75790248 | 2193 | (rmail-apply-in-message msgnum 'rmail-set-header-1 name value) |
9aadce25 RS |
2194 | (with-current-buffer rmail-buffer |
2195 | ;; Ensure header changes get saved. | |
2196 | ;; (Note replacing a header with an identical copy modifies.) | |
2197 | (set-buffer-modified-p t) | |
2198 | ;; However: don't save in mbox format over a Babyl file | |
2199 | ;; merely because of this. | |
2200 | (rmail-dont-modify-format))) | |
537ab246 BG |
2201 | \f |
2202 | ;;;; *** Rmail Attributes and Keywords *** | |
2203 | ||
2204 | (defun rmail-get-attr-names (&optional msg) | |
2205 | "Return the message attributes in a comma separated string. | |
2206 | MSG specifies the message number to get it from. | |
2207 | If MSG is nil, use the current message." | |
2208 | (let ((value (rmail-get-header rmail-attribute-header msg)) | |
75790248 | 2209 | (nmax (length rmail-attr-array)) |
537ab246 | 2210 | result temp) |
75790248 | 2211 | (when value |
5c950923 | 2212 | (if (> (length value) nmax) |
f58faced | 2213 | (message "Warning: corrupt attribute header in message") |
5c950923 | 2214 | (dotimes (index (length value)) |
f58faced GM |
2215 | (setq temp (and (not (= ?- (aref value index))) |
2216 | (nth 1 (aref rmail-attr-array index))) | |
2217 | result | |
2218 | (cond | |
2219 | ((and temp result) (format "%s, %s" result temp)) | |
2220 | (temp temp) | |
2221 | (t result))))) | |
75790248 | 2222 | result))) |
537ab246 BG |
2223 | |
2224 | (defun rmail-get-keywords (&optional msg) | |
2225 | "Return the message keywords in a comma separated string. | |
2226 | MSG, if non-nil, identifies the message number to use. | |
2227 | If nil, that means the current message." | |
2228 | (rmail-get-header rmail-keyword-header msg)) | |
2229 | ||
2230 | (defun rmail-get-labels (&optional msg) | |
2231 | "Return a string with the labels (attributes and keywords) of msg MSG. | |
2232 | It is put in comma-separated form. | |
2233 | MSG, if non-nil, identifies the message number to use. | |
2234 | If nil, that means the current message." | |
a8f67b04 | 2235 | (or msg (setq msg rmail-current-message)) |
8e7a0f83 | 2236 | (let (attr-names keywords) |
778ef2ef | 2237 | ;; Combine the message attributes and keywords |
537ab246 | 2238 | ;; into a comma-separated list. |
a8f67b04 CY |
2239 | (setq attr-names (rmail-get-attr-names msg) |
2240 | keywords (rmail-get-keywords msg)) | |
537ab246 BG |
2241 | (if (string= keywords "") |
2242 | (setq keywords nil)) | |
2243 | (cond | |
9940a3ec GM |
2244 | ;; FIXME ? old rmail did not have spaces in the comma-separated lists. |
2245 | ((and attr-names keywords) (concat " " attr-names "; " keywords)) | |
537ab246 BG |
2246 | (attr-names (concat " " attr-names)) |
2247 | (keywords (concat " " keywords)) | |
2248 | (t "")))) | |
2249 | ||
2250 | (defun rmail-display-labels () | |
2251 | "Update the current messages's attributes and keywords in mode line." | |
2252 | (let ((blurb (rmail-get-labels))) | |
2253 | (setq mode-line-process | |
2254 | (format " %d/%d%s" | |
d1be4ec2 | 2255 | rmail-current-message rmail-total-messages blurb)))) |
537ab246 BG |
2256 | |
2257 | (defun rmail-get-attr-value (attr state) | |
2258 | "Return the character value for ATTR. | |
2259 | ATTR is a (numeric) index, an offset into the mbox attribute | |
2260 | header value. STATE is one of nil, t, or a character value." | |
2261 | (cond | |
2262 | ((numberp state) state) | |
2263 | ((not state) ?-) | |
2264 | (t (nth 0 (aref rmail-attr-array attr))))) | |
2265 | ||
75790248 GM |
2266 | (defun rmail-set-attribute-1 (attr state) |
2267 | "Subroutine of `rmail-set-attribute'. | |
2268 | Set Rmail attribute ATTR to STATE in `rmail-attribute-header', | |
2269 | creating the header if necessary. Returns non-nil if a | |
2270 | significant attribute change was made." | |
2271 | (let ((limit (search-forward "\n\n" nil t)) | |
2272 | (value (rmail-get-attr-value attr state)) | |
2273 | (inhibit-read-only t) | |
2274 | altered) | |
2275 | (goto-char (point-min)) | |
2276 | (if (search-forward (concat rmail-attribute-header ": ") limit t) | |
2277 | ;; If this message already records attributes, just change the | |
2278 | ;; value for this one. | |
2279 | (let ((missing (- (+ (point) attr) (line-end-position)))) | |
2280 | ;; Position point at this attribute, adding attributes if necessary. | |
2281 | (if (> missing 0) | |
2282 | (progn | |
2283 | (end-of-line) | |
2284 | (insert-char ?- missing) | |
2285 | (backward-char 1)) | |
2286 | (forward-char attr)) | |
2287 | ;; Change this attribute. | |
2288 | (when (/= value (char-after)) | |
2289 | (setq altered t) | |
2290 | (delete-char 1) | |
2291 | (insert value))) | |
2292 | ;; Otherwise add a header line to record the attributes and set | |
2293 | ;; all but this one to no. | |
2294 | (let ((header-value "--------")) | |
2295 | (aset header-value attr value) | |
2296 | (goto-char (if limit (1- limit) (point-max))) | |
2297 | (setq altered (/= value ?-)) | |
2298 | (insert rmail-attribute-header ": " header-value "\n"))) | |
2299 | altered)) | |
2300 | ||
537ab246 BG |
2301 | (defun rmail-set-attribute (attr state &optional msgnum) |
2302 | "Turn an attribute of a message on or off according to STATE. | |
2303 | STATE is either nil or the character (numeric) value associated | |
2304 | with the state (nil represents off and non-nil represents on). | |
44baa8ce GM |
2305 | ATTR is either the index number of the attribute, or a string, |
2306 | both from `rmail-attr-array'. MSGNUM is message number to | |
537ab246 | 2307 | change; nil means current message." |
44baa8ce GM |
2308 | (let ((n 0) |
2309 | (nmax (length rmail-attr-array))) | |
2310 | (while (and (stringp attr) | |
2311 | (< n nmax)) | |
2312 | (if (string-equal attr (cadr (aref rmail-attr-array n))) | |
2313 | (setq attr n)) | |
2314 | (setq n (1+ n)))) | |
2315 | (if (stringp attr) | |
2316 | (error "Unknown attribute `%s'" attr)) | |
4df49ff1 RS |
2317 | ;; Ask for confirmation before setting any attribute except `unseen' |
2318 | ;; if it would force a format change. | |
2319 | (unless (= attr rmail-unseen-attr-index) | |
2320 | (rmail-modify-format)) | |
537ab246 | 2321 | (with-current-buffer rmail-buffer |
75790248 GM |
2322 | (or msgnum (setq msgnum rmail-current-message)) |
2323 | (when (> msgnum 0) | |
2324 | ;; The "deleted" attribute is also stored in a special vector so | |
2325 | ;; update that too. | |
2326 | (if (= attr rmail-deleted-attr-index) | |
2327 | (rmail-set-message-deleted-p msgnum state)) | |
2328 | (if (prog1 | |
2329 | (rmail-apply-in-message msgnum 'rmail-set-attribute-1 attr state) | |
2330 | (if (= msgnum rmail-current-message) | |
2331 | (rmail-display-labels))) | |
9aadce25 | 2332 | ;; Don't save in mbox format over a Babyl file |
4df49ff1 RS |
2333 | ;; merely because of a change in `unseen' attribute. |
2334 | (if (= attr rmail-unseen-attr-index) | |
2335 | (rmail-dont-modify-format) | |
2336 | ;; Otherwise, if we modified the file text via the view buffer, | |
2337 | ;; mark the main buffer modified too. | |
2338 | (set-buffer-modified-p t)))))) | |
537ab246 BG |
2339 | |
2340 | (defun rmail-message-attr-p (msg attrs) | |
8e7a0f83 | 2341 | "Return non-nil if message number MSG has attributes matching regexp ATTRS." |
75790248 GM |
2342 | (let ((value (rmail-get-header rmail-attribute-header msg))) |
2343 | (and value (string-match attrs value)))) | |
537ab246 BG |
2344 | |
2345 | (defun rmail-message-unseen-p (msgnum) | |
8e7a0f83 | 2346 | "Return non-nil if message number MSGNUM has the unseen attribute." |
537ab246 BG |
2347 | (rmail-message-attr-p msgnum "......U")) |
2348 | ||
26eb677a GM |
2349 | ;; FIXME rmail-get-labels does some formatting (eg leading space, `;' |
2350 | ;; between attributes and labels), so this might not do what you want. | |
2351 | ;; Eg see rmail-sort-by-labels. rmail-get-labels could have an | |
2352 | ;; optional `noformat' argument. | |
537ab246 | 2353 | (defun rmail-message-labels-p (msg labels) |
8e7a0f83 | 2354 | "Return non-nil if message number MSG has labels matching regexp LABELS." |
537ab246 BG |
2355 | (string-match labels (rmail-get-labels msg))) |
2356 | \f | |
2357 | ;;;; *** Rmail Message Selection And Support *** | |
2358 | ||
2359 | (defun rmail-msgend (n) | |
ccb55d27 | 2360 | "Return the end position for message number N." |
537ab246 BG |
2361 | (marker-position (aref rmail-message-vector (1+ n)))) |
2362 | ||
2363 | (defun rmail-msgbeg (n) | |
ccb55d27 | 2364 | "Return the start position for message number N." |
537ab246 BG |
2365 | (marker-position (aref rmail-message-vector n))) |
2366 | ||
2367 | (defun rmail-apply-in-message (msgnum function &rest args) | |
2368 | "Call FUNCTION on ARGS while narrowed to message MSGNUM. | |
2369 | Point is at the start of the message. | |
2370 | This returns what the call to FUNCTION returns. | |
2371 | If MSGNUM is nil, use the current message." | |
2372 | (with-current-buffer rmail-buffer | |
2373 | (or msgnum (setq msgnum rmail-current-message)) | |
2374 | (when (> msgnum 0) | |
2375 | (let (msgbeg msgend) | |
2376 | (setq msgbeg (rmail-msgbeg msgnum)) | |
2377 | (setq msgend (rmail-msgend msgnum)) | |
2378 | ;; All access to the rmail-buffer's local variables is now finished... | |
2379 | (save-excursion | |
2380 | ;; ... so it is ok to go to a different buffer. | |
2381 | (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer)) | |
75790248 GM |
2382 | (save-excursion |
2383 | (save-restriction | |
2384 | (widen) | |
537ab246 | 2385 | (goto-char msgbeg) |
75790248 GM |
2386 | (narrow-to-region msgbeg msgend) |
2387 | (apply function args)))))))) | |
537ab246 | 2388 | |
75790248 | 2389 | ;; Unused (save for commented out code in rmailedit.el). |
537ab246 BG |
2390 | (defun rmail-widen-to-current-msgbeg (function) |
2391 | "Call FUNCTION with point at start of internal data of current message. | |
2392 | Assumes that bounds were previously narrowed to display the message in Rmail. | |
2393 | The bounds are widened enough to move point where desired, then narrowed | |
2394 | again afterward. | |
2395 | ||
2396 | FUNCTION may not change the visible text of the message, but it may | |
2397 | change the invisible header text." | |
2398 | (save-excursion | |
2399 | (unwind-protect | |
2400 | (progn | |
2401 | (narrow-to-region (rmail-msgbeg rmail-current-message) | |
2402 | (point-max)) | |
2403 | (goto-char (point-min)) | |
2404 | (funcall function)) | |
2405 | ;; Note: we don't use save-restriction because that does not work right | |
2406 | ;; if changes are made outside the saved restriction | |
2407 | ;; before that restriction is restored. | |
2408 | (narrow-to-region (rmail-msgbeg rmail-current-message) | |
2409 | (rmail-msgend rmail-current-message))))) | |
2410 | \f | |
2411 | ;; Manage the message vectors and counters. | |
2412 | ||
2413 | (defun rmail-forget-messages () | |
2414 | (unwind-protect | |
2415 | (if (vectorp rmail-message-vector) | |
76e1e40b | 2416 | (let* ((v rmail-message-vector) |
537ab246 | 2417 | (n (length v))) |
76e1e40b RS |
2418 | (dotimes (i n) |
2419 | (if (aref v i) | |
2420 | (move-marker (aref v i) nil))))) | |
537ab246 BG |
2421 | (setq rmail-message-vector nil) |
2422 | (setq rmail-msgref-vector nil) | |
2423 | (setq rmail-deleted-vector nil))) | |
2424 | ||
2425 | (defun rmail-maybe-set-message-counters () | |
2426 | (if (not (and rmail-deleted-vector | |
2427 | rmail-message-vector | |
2428 | rmail-current-message | |
2429 | rmail-total-messages)) | |
2430 | (rmail-set-message-counters))) | |
2431 | ||
2432 | (defun rmail-count-new-messages (&optional nomsg) | |
2433 | "Count the number of new messages. | |
2434 | The buffer should be narrowed to include only the new messages. | |
2435 | Output a helpful message unless NOMSG is non-nil." | |
2436 | (let* ((case-fold-search nil) | |
2437 | (total-messages 0) | |
2438 | (messages-head nil) | |
2439 | (deleted-head nil)) | |
2440 | (or nomsg (message "Counting new messages...")) | |
2441 | (goto-char (point-max)) | |
2442 | ;; Put at the end of messages-head | |
2443 | ;; the entry for message N+1, which marks | |
2444 | ;; the end of message N. (N = number of messages). | |
2445 | (setq messages-head (list (point-marker))) | |
2446 | (rmail-set-message-counters-counter (point-min)) | |
2447 | (setq rmail-current-message (1+ rmail-total-messages)) | |
2448 | (setq rmail-total-messages | |
2449 | (+ rmail-total-messages total-messages)) | |
2450 | (setq rmail-message-vector | |
2451 | (vconcat rmail-message-vector (cdr messages-head))) | |
2452 | (aset rmail-message-vector | |
2453 | rmail-current-message (car messages-head)) | |
2454 | (setq rmail-deleted-vector | |
2455 | (concat rmail-deleted-vector deleted-head)) | |
2456 | (setq rmail-summary-vector | |
2457 | (vconcat rmail-summary-vector (make-vector total-messages nil))) | |
2458 | (setq rmail-msgref-vector | |
2459 | (vconcat rmail-msgref-vector (make-vector total-messages nil))) | |
2460 | ;; Fill in the new elements of rmail-msgref-vector. | |
2461 | (let ((i (1+ (- rmail-total-messages total-messages)))) | |
2462 | (while (<= i rmail-total-messages) | |
2463 | (aset rmail-msgref-vector i (list i)) | |
2464 | (setq i (1+ i)))) | |
2465 | (goto-char (point-min)) | |
2466 | (or nomsg (message "Counting new messages...done (%d)" total-messages)))) | |
2467 | ||
2468 | (defun rmail-set-message-counters () | |
2469 | (rmail-forget-messages) | |
2470 | (save-excursion | |
2471 | (save-restriction | |
2472 | (widen) | |
2473 | (let* ((point-save (point)) | |
2474 | (total-messages 0) | |
2475 | (messages-after-point) | |
2476 | (case-fold-search nil) | |
2477 | (messages-head nil) | |
2478 | (deleted-head nil)) | |
2479 | ;; Determine how many messages follow point. | |
2480 | (message "Counting messages...") | |
2481 | (goto-char (point-max)) | |
2482 | ;; Put at the end of messages-head | |
2483 | ;; the entry for message N+1, which marks | |
2484 | ;; the end of message N. (N = number of messages). | |
2485 | (setq messages-head (list (point-marker))) | |
8350f087 | 2486 | (setq messages-after-point |
76e1e40b RS |
2487 | (or (rmail-set-message-counters-counter (min (point) point-save)) |
2488 | 0)) | |
537ab246 | 2489 | |
537ab246 BG |
2490 | (setq rmail-total-messages total-messages) |
2491 | (setq rmail-current-message | |
2492 | (min total-messages | |
2493 | (max 1 (- total-messages messages-after-point)))) | |
76e1e40b RS |
2494 | |
2495 | ;; Make an element 0 in rmail-message-vector and rmail-deleted-vector | |
2496 | ;; which will never be used. | |
2497 | (push nil messages-head) | |
2498 | (push ?0 deleted-head) | |
2499 | (setq rmail-message-vector (apply 'vector messages-head) | |
2500 | rmail-deleted-vector (concat deleted-head)) | |
2501 | ||
2502 | (setq rmail-summary-vector (make-vector rmail-total-messages nil) | |
537ab246 | 2503 | rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil)) |
76e1e40b | 2504 | |
537ab246 BG |
2505 | (let ((i 0)) |
2506 | (while (<= i rmail-total-messages) | |
2507 | (aset rmail-msgref-vector i (list i)) | |
2508 | (setq i (1+ i)))) | |
2509 | (let ((i 0)) | |
2510 | (while (<= i rmail-total-messages) | |
2511 | (rmail-set-message-deleted-p i (rmail-message-attr-p i ".D")) | |
2512 | (setq i (1+ i)))) | |
2513 | (message "Counting messages...done"))))) | |
2514 | ||
2515 | ||
2516 | (defsubst rmail-collect-deleted (message-end) | |
2517 | "Collect the message deletion flags for each message. | |
2518 | MESSAGE-END is the buffer position corresponding to the end of | |
2519 | the message. Point is at the beginning of the message." | |
2520 | ;; NOTE: This piece of code will be executed on a per-message basis. | |
2521 | ;; In the face of thousands of messages, it has to be as fast as | |
2522 | ;; possible, hence some brute force constant use is employed in | |
2523 | ;; addition to inlining. | |
2524 | (save-excursion | |
2525 | (setq deleted-head | |
2526 | (cons (if (and (search-forward (concat rmail-attribute-header ": ") message-end t) | |
2527 | (looking-at "?D")) | |
2528 | ?D | |
8e7a0f83 | 2529 | ?\s) deleted-head)))) |
537ab246 | 2530 | |
76e1e40b RS |
2531 | (defun rmail-set-message-counters-counter (&optional spot-to-find) |
2532 | "Collect the start positions of messages in list `messages-head'. | |
2533 | Return the number of messages after the one containing SPOT-TO-FIND." | |
2534 | (let ((start (point)) | |
2535 | messages-after-spot) | |
2536 | (while (search-backward "\n\nFrom " nil t) | |
537ab246 | 2537 | (forward-char 2) |
0f25a277 | 2538 | (when (looking-at rmail-unix-mail-delimiter) |
76e1e40b RS |
2539 | (if (and (<= (point) spot-to-find) |
2540 | (null messages-after-spot)) | |
2541 | (setq messages-after-spot total-messages)) | |
0f25a277 EZ |
2542 | (rmail-collect-deleted start) |
2543 | (setq messages-head (cons (point-marker) messages-head) | |
2544 | total-messages (1+ total-messages) | |
2545 | start (point)) | |
2546 | ;; Show progress after every 20 messages or so. | |
2547 | (if (zerop (% total-messages 20)) | |
2548 | (message "Counting messages...%d" total-messages)))) | |
537ab246 | 2549 | ;; Handle the first message, maybe. |
76e1e40b | 2550 | (goto-char (point-min)) |
0f25a277 | 2551 | (unless (not (looking-at rmail-unix-mail-delimiter)) |
76e1e40b RS |
2552 | (if (and (<= (point) spot-to-find) |
2553 | (null messages-after-spot)) | |
2554 | (setq messages-after-spot total-messages)) | |
537ab246 BG |
2555 | (rmail-collect-deleted start) |
2556 | (setq messages-head (cons (point-marker) messages-head) | |
76e1e40b RS |
2557 | total-messages (1+ total-messages))) |
2558 | messages-after-spot)) | |
537ab246 BG |
2559 | \f |
2560 | ;; Display a message. | |
2561 | ||
2562 | ;;;; *** Rmail Message Formatting and Header Manipulation *** | |
2563 | ||
603c5d8d GM |
2564 | ;; This is used outside of rmail. |
2565 | (defun rmail-msg-is-pruned () | |
2566 | "Return nil if the current message is showing full headers." | |
2567 | (with-current-buffer (if (rmail-buffers-swapped-p) rmail-view-buffer | |
2568 | rmail-buffer) | |
2569 | (eq rmail-header-style 'normal))) | |
2570 | ||
537ab246 | 2571 | (defun rmail-toggle-header (&optional arg) |
603c5d8d GM |
2572 | "Toggle between showing full and normal message headers. |
2573 | With optional integer ARG, show the normal message header if ARG | |
2574 | is greater than zero; otherwise, show it in full." | |
537ab246 | 2575 | (interactive "P") |
8372c367 | 2576 | (let ((rmail-header-style |
00c6f62e CY |
2577 | (if (numberp arg) |
2578 | (if (> arg 0) 'normal 'full) | |
603c5d8d | 2579 | (if (rmail-msg-is-pruned) 'full 'normal)))) |
809f3af0 | 2580 | (rmail-show-message))) |
537ab246 BG |
2581 | |
2582 | (defun rmail-beginning-of-message () | |
2583 | "Show current message starting from the beginning." | |
2584 | (interactive) | |
dec5f46d GM |
2585 | (let ((rmail-show-message-hook '((lambda () (goto-char (point-min))))) |
2586 | (rmail-header-style (with-current-buffer (if (rmail-buffers-swapped-p) | |
2587 | rmail-view-buffer | |
2588 | rmail-buffer) | |
2589 | rmail-header-style))) | |
809f3af0 | 2590 | (rmail-show-message rmail-current-message))) |
537ab246 BG |
2591 | |
2592 | (defun rmail-end-of-message () | |
2593 | "Show bottom of current message." | |
2594 | (interactive) | |
dec5f46d GM |
2595 | (let ((rmail-show-message-hook '((lambda () |
2596 | (goto-char (point-max)) | |
2597 | (recenter (1- (window-height)))))) | |
2598 | (rmail-header-style (with-current-buffer (if (rmail-buffers-swapped-p) | |
2599 | rmail-view-buffer | |
2600 | rmail-buffer) | |
2601 | rmail-header-style))) | |
809f3af0 | 2602 | (rmail-show-message rmail-current-message))) |
537ab246 BG |
2603 | |
2604 | (defun rmail-unknown-mail-followup-to () | |
2605 | "Handle a \"Mail-Followup-To\" header field with an unknown mailing list. | |
2606 | Ask the user whether to add that list name to `mail-mailing-lists'." | |
dec5f46d GM |
2607 | ;; FIXME s-r not needed? Use rmail-get-header? |
2608 | ;; We have not narrowed to the headers at ths point? | |
537ab246 BG |
2609 | (save-restriction |
2610 | (let ((mail-followup-to (mail-fetch-field "mail-followup-to" nil t))) | |
2611 | (when mail-followup-to | |
2612 | (let ((addresses | |
2613 | (split-string | |
2614 | (mail-strip-quoted-names mail-followup-to) | |
2615 | ",[[:space:]]+" t))) | |
2616 | (dolist (addr addresses) | |
2617 | (when (and (not (member addr mail-mailing-lists)) | |
2618 | (not | |
2619 | ;; taken from rmailsum.el | |
2620 | (string-match | |
2621 | (or rmail-user-mail-address-regexp | |
2622 | (concat "^\\(" | |
2623 | (regexp-quote (user-login-name)) | |
2624 | "\\($\\|@\\)\\|" | |
2625 | (regexp-quote | |
2626 | (or user-mail-address | |
2627 | (concat (user-login-name) "@" | |
2628 | (or mail-host-address | |
2629 | (system-name))))) | |
2630 | "\\>\\)")) | |
2631 | addr)) | |
2632 | (y-or-n-p | |
2633 | (format "Add `%s' to `mail-mailing-lists'? " | |
2634 | addr))) | |
2635 | (customize-save-variable 'mail-mailing-lists | |
2636 | (cons addr mail-mailing-lists))))))))) | |
2637 | ||
2638 | (defun rmail-widen () | |
2639 | "Display the entire mailbox file." | |
2640 | (interactive) | |
2641 | (rmail-swap-buffers-maybe) | |
2642 | (widen)) | |
2643 | \f | |
9940a3ec GM |
2644 | (defun rmail-no-mail-p () |
2645 | "Return nil if there is mail, else \"No mail.\"." | |
2646 | (if (zerop rmail-total-messages) | |
2647 | (save-excursion | |
d3cfca60 GM |
2648 | ;; Eg we deleted all the messages, so remove the old N/M mark. |
2649 | (with-current-buffer rmail-buffer (setq mode-line-process nil)) | |
9940a3ec GM |
2650 | (with-current-buffer rmail-view-buffer |
2651 | (erase-buffer) | |
2652 | "No mail.")))) | |
2653 | ||
809f3af0 | 2654 | (defun rmail-show-message (&optional n no-summary) |
537ab246 | 2655 | "Show message number N (prefix argument), counting from start of file. |
6e9db169 EZ |
2656 | If summary buffer is currently displayed, update current message there also. |
2657 | N defaults to the current message." | |
537ab246 BG |
2658 | (interactive "p") |
2659 | (or (eq major-mode 'rmail-mode) | |
2660 | (switch-to-buffer rmail-buffer)) | |
2dc00ad0 | 2661 | ;; FIXME: Why do we swap the raw data back in? |
537ab246 BG |
2662 | (rmail-swap-buffers-maybe) |
2663 | (rmail-maybe-set-message-counters) | |
2664 | (widen) | |
574bc51d | 2665 | (let ((blurb (rmail-show-message-1 n))) |
6e9db169 EZ |
2666 | (or (zerop rmail-total-messages) |
2667 | (progn | |
2668 | (when mail-mailing-lists | |
2669 | (rmail-unknown-mail-followup-to)) | |
2670 | (if transient-mark-mode (deactivate-mark)) | |
2671 | ;; If there is a summary buffer, try to move to this message | |
2672 | ;; in that buffer. But don't complain if this message is | |
2673 | ;; not mentioned in the summary. Don't do this at all if we | |
2674 | ;; were called on behalf of cursor motion in the summary | |
2675 | ;; buffer. | |
2676 | (and (rmail-summary-exists) (not no-summary) | |
2677 | (let ((curr-msg rmail-current-message)) | |
2678 | (rmail-select-summary | |
2679 | (rmail-summary-goto-msg curr-msg t t)))) | |
2680 | (with-current-buffer rmail-buffer | |
2681 | (rmail-auto-file)))) | |
537ab246 BG |
2682 | (if blurb |
2683 | (message blurb)))) | |
2684 | ||
2685 | (defun rmail-is-text-p () | |
2686 | "Return t if the region contains a text message, nil otherwise." | |
2687 | (save-excursion | |
2688 | (let ((text-regexp "\\(text\\|message\\)/") | |
2689 | (content-type-header (mail-fetch-field "content-type"))) | |
2690 | ;; The message is text if either there is no content type header | |
2691 | ;; (a default of "text/plain; charset=US-ASCII" is assumed) or | |
2692 | ;; the base content type is either text or message. | |
2693 | (or (not content-type-header) | |
2694 | (string-match text-regexp content-type-header))))) | |
2695 | ||
51584624 RS |
2696 | (defcustom rmail-show-message-verbose-min 200000 |
2697 | "Message size at which to show progress messages for displaying it." | |
2698 | :type 'integer | |
66be413e GM |
2699 | :group 'rmail |
2700 | :version "23.1") | |
51584624 | 2701 | |
574bc51d | 2702 | (defun rmail-show-message-1 (&optional msg) |
6e9db169 | 2703 | "Show message MSG (default: current message) using `rmail-view-buffer'. |
537ab246 BG |
2704 | Return text to display in the minibuffer if MSG is out of |
2705 | range (displaying a reasonable choice as well), nil otherwise. | |
2706 | The current mail message becomes the message displayed." | |
2707 | (let ((mbox-buf rmail-buffer) | |
2708 | (view-buf rmail-view-buffer) | |
00c6f62e CY |
2709 | blurb beg end body-start coding-system character-coding |
2710 | is-text-message header-style) | |
537ab246 BG |
2711 | (if (not msg) |
2712 | (setq msg rmail-current-message)) | |
9940a3ec GM |
2713 | (unless (setq blurb (rmail-no-mail-p)) |
2714 | (cond ((<= msg 0) | |
2715 | (setq msg 1 | |
2716 | rmail-current-message 1 | |
2717 | blurb "No previous message")) | |
2718 | ((> msg rmail-total-messages) | |
2719 | (setq msg rmail-total-messages | |
2720 | rmail-current-message rmail-total-messages | |
2721 | blurb "No following message")) | |
2722 | (t (setq rmail-current-message msg))) | |
2723 | (with-current-buffer rmail-buffer | |
00c6f62e | 2724 | (setq header-style rmail-header-style) |
f444564c RS |
2725 | ;; Mark the message as seen, but preserve buffer modified flag. |
2726 | (let ((modiff (buffer-modified-p))) | |
2727 | (rmail-set-attribute rmail-unseen-attr-index nil) | |
2728 | (unless modiff | |
c206f5b0 | 2729 | (restore-buffer-modified-p modiff))) |
9aadce25 RS |
2730 | ;; bracket the message in the mail |
2731 | ;; buffer and determine the coding system the transfer encoding. | |
9940a3ec GM |
2732 | (rmail-swap-buffers-maybe) |
2733 | (setq beg (rmail-msgbeg msg) | |
2734 | end (rmail-msgend msg)) | |
51584624 RS |
2735 | (when (> (- end beg) rmail-show-message-verbose-min) |
2736 | (message "Showing message %d" msg)) | |
9940a3ec GM |
2737 | (narrow-to-region beg end) |
2738 | (goto-char beg) | |
9940a3ec | 2739 | (with-current-buffer rmail-view-buffer |
d1be4ec2 KH |
2740 | ;; We give the view buffer a buffer-local value of |
2741 | ;; rmail-header-style based on the binding in effect when | |
2742 | ;; this function is called; `rmail-toggle-headers' can | |
2743 | ;; inspect this value to determine how to toggle. | |
186f7f0b KH |
2744 | (set (make-local-variable 'rmail-header-style) header-style)) |
2745 | (if (and rmail-enable-mime | |
45261b50 | 2746 | rmail-show-mime-function |
186f7f0b KH |
2747 | (re-search-forward "mime-version: 1.0" nil t)) |
2748 | (let ((rmail-buffer mbox-buf) | |
2749 | (rmail-view-buffer view-buf)) | |
2750 | (funcall rmail-show-mime-function)) | |
2751 | (setq body-start (search-forward "\n\n" nil t)) | |
2752 | (narrow-to-region beg (point)) | |
2753 | (goto-char beg) | |
2754 | (save-excursion | |
2755 | (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t) | |
2756 | (setq coding-system (intern (match-string 1))) | |
2757 | (setq coding-system (rmail-get-coding-system)))) | |
2758 | (setq character-coding (mail-fetch-field "content-transfer-encoding") | |
2759 | is-text-message (rmail-is-text-p)) | |
2760 | (if character-coding | |
2761 | (setq character-coding (downcase character-coding))) | |
2762 | (narrow-to-region beg end) | |
2763 | ;; Decode the message body into an empty view buffer using a | |
2764 | ;; unibyte temporary buffer where the character decoding takes | |
2765 | ;; place. | |
2766 | (with-current-buffer rmail-view-buffer | |
2767 | (erase-buffer)) | |
2768 | (if (null character-coding) | |
2769 | ;; Do it directly since that is fast. | |
2770 | (rmail-decode-region body-start end coding-system view-buf) | |
2771 | ;; Can this be done directly, skipping the temp buffer? | |
2772 | (with-temp-buffer | |
2773 | (set-buffer-multibyte nil) | |
2774 | (insert-buffer-substring mbox-buf body-start end) | |
2775 | (cond | |
2776 | ((string= character-coding "quoted-printable") | |
2777 | ;; See bug#5441. | |
2778 | (or (mail-unquote-printable-region (point-min) (point-max) | |
2779 | nil t 'unibyte) | |
2780 | (message "Malformed MIME quoted-printable message"))) | |
2781 | ((and (string= character-coding "base64") is-text-message) | |
2782 | (condition-case err | |
2783 | (base64-decode-region (point-min) (point-max)) | |
2784 | (error (message "%s" (cdr err))))) | |
2785 | ((eq character-coding 'uuencode) | |
2786 | (error "uuencoded messages are not supported yet")) | |
2787 | (t)) | |
2788 | (rmail-decode-region (point-min) (point-max) | |
2789 | coding-system view-buf))) | |
2790 | (with-current-buffer rmail-view-buffer | |
2791 | ;; Prepare the separator (blank line) before the body. | |
2792 | (goto-char (point-min)) | |
2793 | (insert "\n") | |
2794 | ;; Unquote quoted From lines | |
2795 | (while (re-search-forward "^>+From " nil t) | |
2796 | (beginning-of-line) | |
2797 | (delete-char 1) | |
2798 | (forward-line)) | |
2799 | (goto-char (point-min))) | |
2800 | ;; Copy the headers to the front of the message view buffer. | |
8d43f3cd EZ |
2801 | (rmail-copy-headers beg end) |
2802 | ;; Decode any RFC2047 encoded message headers. | |
2803 | (if rmail-enable-mime | |
2804 | (with-current-buffer rmail-view-buffer | |
2805 | (rfc2047-decode-region | |
2806 | (point-min) | |
2807 | (progn | |
2808 | (search-forward "\n\n" nil 'move) | |
2809 | (point)))))) | |
9940a3ec GM |
2810 | ;; highlight the message, activate any URL like text and add |
2811 | ;; special highlighting for and quoted material. | |
2812 | (with-current-buffer rmail-view-buffer | |
9940a3ec GM |
2813 | (goto-char (point-min)) |
2814 | (rmail-highlight-headers) | |
2815 | ;(rmail-activate-urls) | |
2816 | ;(rmail-process-quoted-material) | |
2817 | ) | |
2818 | ;; Update the mode-line with message status information and swap | |
2819 | ;; the view buffer/mail buffer contents. | |
2820 | (rmail-display-labels) | |
6e9db169 | 2821 | (rmail-swap-buffers) |
9940a3ec | 2822 | (setq rmail-buffer-swapped t) |
51584624 RS |
2823 | (run-hooks 'rmail-show-message-hook) |
2824 | (when (> (- end beg) rmail-show-message-verbose-min) | |
2825 | (message "Showing message %d...done" msg)))) | |
537ab246 BG |
2826 | blurb)) |
2827 | ||
2828 | (defun rmail-copy-headers (beg end &optional ignored-headers) | |
2829 | "Copy displayed header fields to the message viewer buffer. | |
2830 | BEG and END marks the start and end positions of the message in | |
2831 | the mbox buffer. If the optional argument IGNORED-HEADERS is | |
2832 | non-nil, ignore all header fields whose names match that regexp. | |
2833 | Otherwise, if `rmail-displayed-headers' is non-nil, copy only | |
2834 | those header fields whose names match that regexp. Otherwise, | |
2835 | copy all header fields whose names do not match | |
2836 | `rmail-ignored-headers' (unless they also match | |
4546235f GM |
2837 | `rmail-nonignored-headers'). Moves point in the message viewer |
2838 | buffer to the end of the headers." | |
537ab246 BG |
2839 | (let ((header-start-regexp "\n[^ \t]") |
2840 | lim) | |
2841 | (with-current-buffer rmail-buffer | |
2842 | (when (search-forward "\n\n" nil t) | |
2843 | (forward-char -1) | |
2844 | (save-restriction | |
2845 | ;; Put point right after the From header line. | |
2846 | (narrow-to-region beg (point)) | |
2847 | (goto-char (point-min)) | |
2848 | (unless (re-search-forward header-start-regexp nil t) | |
2849 | (rmail-error-bad-format)) | |
2850 | (forward-char -1) | |
2851 | (cond | |
2852 | ;; Handle the case where all headers should be copied. | |
2853 | ((eq rmail-header-style 'full) | |
3b5ad654 | 2854 | (prepend-to-buffer rmail-view-buffer beg (point-max)) |
574bc51d | 2855 | ;; rmail-show-message-1 expects this function to leave point |
3b5ad654 | 2856 | ;; at the end of the headers. |
5a34da04 RS |
2857 | |
2858 | (let ((len (- (point-max) beg))) | |
2859 | (with-current-buffer rmail-view-buffer | |
2860 | (goto-char (1+ len))))) | |
2861 | ||
3b5ad654 | 2862 | ;; Handle the case where the headers matching the displayed |
537ab246 BG |
2863 | ;; headers regexp should be copied. |
2864 | ((and rmail-displayed-headers (null ignored-headers)) | |
2865 | (while (not (eobp)) | |
2866 | (save-excursion | |
2867 | (setq lim (if (re-search-forward header-start-regexp nil t) | |
2868 | (1+ (match-beginning 0)) | |
2869 | (point-max)))) | |
2870 | (when (looking-at rmail-displayed-headers) | |
2871 | (append-to-buffer rmail-view-buffer (point) lim)) | |
2872 | (goto-char lim))) | |
2873 | ;; Handle the ignored headers. | |
2874 | ((or ignored-headers (setq ignored-headers rmail-ignored-headers)) | |
2875 | (while (and ignored-headers (not (eobp))) | |
2876 | (save-excursion | |
2877 | (setq lim (if (re-search-forward header-start-regexp nil t) | |
2878 | (1+ (match-beginning 0)) | |
2879 | (point-max)))) | |
2880 | (if (and (looking-at ignored-headers) | |
2881 | (not (looking-at rmail-nonignored-headers))) | |
2882 | (goto-char lim) | |
2883 | (append-to-buffer rmail-view-buffer (point) lim) | |
2884 | (goto-char lim)))) | |
2885 | (t (error "No headers selected for display!")))))))) | |
2886 | ||
fc9682ad | 2887 | (defun rmail-redecode-body (coding) |
2e55c1b7 CY |
2888 | "Decode the body of the current message using coding system CODING. |
2889 | This is useful with mail messages that have malformed or missing | |
2890 | charset= headers. | |
2891 | ||
2892 | This function assumes that the current message is already decoded | |
2893 | and displayed in the RMAIL buffer, but the coding system used to | |
fc9682ad EZ |
2894 | decode it was incorrect. It then decodes the message again, |
2895 | using the coding system CODING." | |
2e55c1b7 CY |
2896 | (interactive "zCoding system for re-decoding this message: ") |
2897 | (when (not rmail-enable-mime) | |
2dc00ad0 | 2898 | (with-current-buffer rmail-buffer |
2e55c1b7 CY |
2899 | (rmail-swap-buffers-maybe) |
2900 | (save-restriction | |
2901 | (widen) | |
fc9682ad | 2902 | (let ((msgbeg (rmail-msgbeg rmail-current-message)) |
2e55c1b7 CY |
2903 | (msgend (rmail-msgend rmail-current-message)) |
2904 | (buffer-read-only nil) | |
2905 | body-start x-coding-header old-coding) | |
2906 | (narrow-to-region msgbeg msgend) | |
2907 | (goto-char (point-min)) | |
2908 | (unless (setq body-start (search-forward "\n\n" (point-max) 1)) | |
2909 | (error "No message body")) | |
2910 | ||
2911 | (save-restriction | |
2912 | ;; Narrow to headers | |
2913 | (narrow-to-region (point-min) body-start) | |
fc9682ad EZ |
2914 | (setq x-coding-header (goto-char (point-min))) |
2915 | (if (not (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)) | |
2916 | (setq old-coding (rmail-get-coding-system)) | |
2917 | (setq old-coding (intern (match-string 1))) | |
2918 | (setq x-coding-header (point))) | |
2e55c1b7 CY |
2919 | (check-coding-system old-coding) |
2920 | ;; Make sure the new coding system uses the same EOL | |
2921 | ;; conversion, to prevent ^M characters from popping up | |
2922 | ;; all over the place. | |
0300c339 EZ |
2923 | (let ((eol-type (coding-system-eol-type old-coding))) |
2924 | (if (numberp eol-type) | |
2925 | (setq coding | |
2926 | (coding-system-change-eol-conversion coding eol-type)))) | |
fc9682ad EZ |
2927 | (when (not (coding-system-equal |
2928 | (coding-system-base old-coding) | |
2929 | (coding-system-base coding))) | |
2930 | ;; Rewrite the coding-system header. | |
2931 | (goto-char x-coding-header) | |
2932 | (if (> (point) (point-min)) | |
2933 | (delete-region (line-beginning-position) (point)) | |
2934 | (forward-line) | |
2935 | (insert "\n") | |
2936 | (forward-line -1)) | |
2937 | (insert "X-Coding-System: " | |
2938 | (symbol-name coding)))) | |
809f3af0 | 2939 | (rmail-show-message)))))) |
2e55c1b7 | 2940 | |
537ab246 | 2941 | (defun rmail-highlight-headers () |
66be413e | 2942 | "Highlight the headers specified by `rmail-highlighted-headers'. |
f454672b | 2943 | Uses the face specified by `rmail-highlight-face'." |
66be413e | 2944 | (if rmail-highlighted-headers |
537ab246 BG |
2945 | (save-excursion |
2946 | (search-forward "\n\n" nil 'move) | |
2947 | (save-restriction | |
2948 | (narrow-to-region (point-min) (point)) | |
2949 | (let ((case-fold-search t) | |
2950 | (inhibit-read-only t) | |
f454672b GM |
2951 | ;; When rmail-highlight-face is removed, just |
2952 | ;; use 'rmail-highlight here. | |
2953 | (face (or rmail-highlight-face | |
2954 | (if (face-differs-from-default-p 'bold) | |
2955 | 'bold 'highlight))) | |
537ab246 BG |
2956 | ;; List of overlays to reuse. |
2957 | (overlays rmail-overlay-list)) | |
2958 | (goto-char (point-min)) | |
2959 | (while (re-search-forward rmail-highlighted-headers nil t) | |
2960 | (skip-chars-forward " \t") | |
2961 | (let ((beg (point)) | |
2962 | overlay) | |
2963 | (while (progn (forward-line 1) | |
2964 | (looking-at "[ \t]"))) | |
2965 | ;; Back up over newline, then trailing spaces or tabs | |
2966 | (forward-char -1) | |
2967 | (while (member (preceding-char) '(? ?\t)) | |
2968 | (forward-char -1)) | |
2969 | (if overlays | |
2970 | ;; Reuse an overlay we already have. | |
2971 | (progn | |
2972 | (setq overlay (car overlays) | |
2973 | overlays (cdr overlays)) | |
f454672b | 2974 | (overlay-put overlay 'face face) |
537ab246 BG |
2975 | (move-overlay overlay beg (point))) |
2976 | ;; Make a new overlay and add it to | |
2977 | ;; rmail-overlay-list. | |
2978 | (setq overlay (make-overlay beg (point))) | |
f454672b | 2979 | (overlay-put overlay 'face face) |
537ab246 BG |
2980 | (setq rmail-overlay-list |
2981 | (cons overlay rmail-overlay-list)))))))))) | |
2982 | ||
2983 | (defun rmail-auto-file () | |
9aac4de2 GM |
2984 | "Automatically move a message into another sfolder based on criteria. |
2985 | This moves messages according to `rmail-automatic-folder-directives'. | |
2986 | It only does something in the folder that `rmail-file-name' specifies. | |
2987 | The function `rmail-show-message' calls this whenever it shows a message. | |
2988 | This leaves a message alone if it already has the `filed' attribute." | |
537ab246 BG |
2989 | (if (or (zerop rmail-total-messages) |
2990 | (rmail-message-attr-p rmail-current-message "...F") | |
2991 | (not (string= (buffer-file-name) | |
2992 | (expand-file-name rmail-file-name)))) | |
2993 | ;; Do nothing if the message has already been filed or if there | |
2994 | ;; are no messages. | |
2995 | nil | |
2996 | ;; Find out some basics (common fields) | |
2997 | (let ((from (mail-fetch-field "from")) | |
2998 | (subj (mail-fetch-field "subject")) | |
2999 | (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc"))) | |
3000 | (d rmail-automatic-folder-directives) | |
3001 | (directive-loop nil) | |
3002 | (folder nil)) | |
3003 | (while d | |
3004 | (setq folder (car (car d)) | |
3005 | directive-loop (cdr (car d))) | |
3006 | (while (and (car directive-loop) | |
3007 | (let ((f (cond | |
9aac4de2 GM |
3008 | ((string= (downcase (car directive-loop)) "from") |
3009 | from) | |
3010 | ((string= (downcase (car directive-loop)) "to") | |
3011 | to) | |
3012 | ((string= (downcase (car directive-loop)) | |
3013 | "subject") subj) | |
537ab246 | 3014 | (t (mail-fetch-field (car directive-loop)))))) |
9aac4de2 | 3015 | ;; FIXME - shouldn't this ignore case? |
537ab246 BG |
3016 | (and f (string-match (car (cdr directive-loop)) f)))) |
3017 | (setq directive-loop (cdr (cdr directive-loop)))) | |
3018 | ;; If there are no directives left, then it was a complete match. | |
3019 | (if (null directive-loop) | |
3020 | (if (null folder) | |
3021 | (rmail-delete-forward) | |
3022 | (if (string= "/dev/null" folder) | |
3023 | (rmail-delete-message) | |
75790248 | 3024 | (rmail-output folder 1) |
537ab246 BG |
3025 | (setq d nil)))) |
3026 | (setq d (cdr d)))))) | |
3027 | \f | |
3028 | ;; Simple message motion commands. | |
3029 | ||
3030 | (defun rmail-next-message (n) | |
3031 | "Show following message whether deleted or not. | |
3032 | With prefix arg N, moves forward N messages, or backward if N is negative." | |
3033 | (interactive "p") | |
3034 | (set-buffer rmail-buffer) | |
3035 | (rmail-maybe-set-message-counters) | |
809f3af0 | 3036 | (rmail-show-message (+ rmail-current-message n))) |
537ab246 BG |
3037 | |
3038 | (defun rmail-previous-message (n) | |
3039 | "Show previous message whether deleted or not. | |
3040 | With prefix arg N, moves backward N messages, or forward if N is negative." | |
3041 | (interactive "p") | |
3042 | (rmail-next-message (- n))) | |
3043 | ||
3044 | (defun rmail-next-undeleted-message (n) | |
3045 | "Show following non-deleted message. | |
3046 | With prefix arg N, moves forward N non-deleted messages, | |
3047 | or backward if N is negative. | |
3048 | ||
3049 | Returns t if a new message is being shown, nil otherwise." | |
3050 | (interactive "p") | |
3051 | (set-buffer rmail-buffer) | |
3052 | (rmail-maybe-set-message-counters) | |
3053 | (let ((lastwin rmail-current-message) | |
3054 | (current rmail-current-message)) | |
3055 | (while (and (> n 0) (< current rmail-total-messages)) | |
3056 | (setq current (1+ current)) | |
3057 | (if (not (rmail-message-deleted-p current)) | |
3058 | (setq lastwin current n (1- n)))) | |
3059 | (while (and (< n 0) (> current 1)) | |
3060 | (setq current (1- current)) | |
3061 | (if (not (rmail-message-deleted-p current)) | |
3062 | (setq lastwin current n (1+ n)))) | |
3063 | (if (/= lastwin rmail-current-message) | |
809f3af0 | 3064 | (progn (rmail-show-message lastwin) |
537ab246 BG |
3065 | t) |
3066 | (if (< n 0) | |
3067 | (message "No previous nondeleted message")) | |
3068 | (if (> n 0) | |
3069 | (message "No following nondeleted message")) | |
3070 | nil))) | |
3071 | ||
3072 | (defun rmail-previous-undeleted-message (n) | |
3073 | "Show previous non-deleted message. | |
3074 | With prefix argument N, moves backward N non-deleted messages, | |
3075 | or forward if N is negative." | |
3076 | (interactive "p") | |
3077 | (rmail-next-undeleted-message (- n))) | |
3078 | ||
3079 | (defun rmail-first-message () | |
3080 | "Show first message in file." | |
3081 | (interactive) | |
3082 | (rmail-maybe-set-message-counters) | |
809f3af0 | 3083 | (rmail-show-message 1)) |
537ab246 BG |
3084 | |
3085 | (defun rmail-last-message () | |
3086 | "Show last message in file." | |
3087 | (interactive) | |
3088 | (rmail-maybe-set-message-counters) | |
809f3af0 | 3089 | (rmail-show-message rmail-total-messages)) |
537ab246 | 3090 | |
14b4e83d RS |
3091 | (defun rmail-next-error-move (msg-pos bad-marker) |
3092 | "Move to an error locus (probably grep hit) in an Rmail buffer. | |
3093 | MSG-POS is a marker pointing at the error message in the grep buffer. | |
3094 | BAD-MARKER is a marker that ought to point at where to move to, | |
3095 | but probably is garbage." | |
c2f9aec8 RS |
3096 | |
3097 | (let* ((message-loc (compilation--message->loc | |
3098 | (get-text-property msg-pos 'compilation-message | |
3099 | (marker-buffer msg-pos)))) | |
3100 | (column (car message-loc)) | |
3101 | (linenum (cadr message-loc)) | |
3102 | line-text | |
14b4e83d RS |
3103 | pos |
3104 | msgnum msgbeg msgend | |
3105 | header-field | |
3106 | line-number-within) | |
3107 | ||
3108 | ;; Look at the whole Rmail file. | |
3109 | (rmail-swap-buffers-maybe) | |
3110 | ||
3111 | (save-restriction | |
3112 | (widen) | |
3113 | (save-excursion | |
3114 | ;; Find the line that the error message points at. | |
3115 | (goto-char (point-min)) | |
c2f9aec8 | 3116 | (forward-line (1- linenum)) |
14b4e83d RS |
3117 | (setq pos (point)) |
3118 | ||
c2f9aec8 RS |
3119 | ;; Find the text at the start of the line, |
3120 | ;; before the first = sign. | |
3121 | ;; This text has a good chance of being also in the | |
3122 | ;; decoded message. | |
3123 | (save-excursion | |
3124 | (skip-chars-forward "^=\n") | |
3125 | (setq line-text (buffer-substring pos (point)))) | |
3126 | ||
3127 | ;; Find which message this position is in, | |
14b4e83d RS |
3128 | ;; and the limits of that message. |
3129 | (setq msgnum (rmail-what-message pos)) | |
3130 | (setq msgbeg (rmail-msgbeg msgnum)) | |
3131 | (setq msgend (rmail-msgend msgnum)) | |
3132 | ||
3133 | ;; Find which header this locus is in, | |
3134 | ;; or if it's in the message body, | |
3135 | ;; and the line-based position within that. | |
3136 | (goto-char msgbeg) | |
3137 | (let ((header-end msgend)) | |
3138 | (if (search-forward "\n\n" nil t) | |
3139 | (setq header-end (point))) | |
3140 | (if (>= pos header-end) | |
3141 | (setq line-number-within | |
3142 | (count-lines header-end pos)) | |
3143 | (goto-char pos) | |
3144 | (unless (looking-at "^[^ \t]") | |
3145 | (re-search-backward "^[^ \t]")) | |
3146 | (looking-at "[^:\n]*[:\n]") | |
3147 | (setq header-field (match-string 0) | |
3148 | line-number-within (count-lines (point) pos)))))) | |
3149 | ||
3150 | ;; Display the right message. | |
3151 | (rmail-show-message msgnum) | |
3152 | ||
3153 | ;; Move to the right position within the displayed message. | |
c2f9aec8 RS |
3154 | ;; Or at least try. The decoded message's lines may not |
3155 | ;; correspond to the lines in the inbox file. | |
3156 | (goto-char (point-min)) | |
14b4e83d | 3157 | (if header-field |
8350f087 | 3158 | (progn |
c2f9aec8 RS |
3159 | (re-search-forward (concat "^" (regexp-quote header-field)) nil t) |
3160 | (forward-line line-number-within)) | |
3161 | (search-forward "\n\n" nil t) | |
3162 | (if (re-search-forward (concat "^" (regexp-quote line-text)) nil t) | |
3163 | (goto-char (match-beginning 0)))) | |
3164 | (if (eobp) | |
3165 | ;; If the decoded message doesn't have enough lines, | |
3166 | ;; go to the beginning rather than the end. | |
3167 | (goto-char (point-min)) | |
3168 | ;; Otherwise, go to the right column. | |
3169 | (if column | |
3170 | (forward-char column))))) | |
14b4e83d RS |
3171 | |
3172 | (defun rmail-what-message (&optional pos) | |
3173 | "Return message number POS (or point) is in." | |
6b6a84a1 GM |
3174 | (let* ((high rmail-total-messages) |
3175 | (mid (/ high 2)) | |
3176 | (low 1) | |
14b4e83d RS |
3177 | (where (or pos |
3178 | (with-current-buffer (if (rmail-buffers-swapped-p) | |
3179 | rmail-view-buffer | |
3180 | (current-buffer)) | |
3181 | (point))))) | |
537ab246 BG |
3182 | (while (> (- high low) 1) |
3183 | (if (>= where (rmail-msgbeg mid)) | |
6b6a84a1 GM |
3184 | (setq low mid) |
3185 | (setq high mid)) | |
537ab246 BG |
3186 | (setq mid (+ low (/ (- high low) 2)))) |
3187 | (if (>= where (rmail-msgbeg high)) high low))) | |
3188 | \f | |
3189 | ;; Searching in Rmail file. | |
3190 | ||
3191 | (defun rmail-search-message (msg regexp) | |
3192 | "Return non-nil, if for message number MSG, regexp REGEXP matches." | |
3193 | ;; This is adequate because its only caller, rmail-search, | |
3194 | ;; unswaps the buffers. | |
3195 | (goto-char (rmail-msgbeg msg)) | |
7fb18e9e GM |
3196 | (if (and rmail-enable-mime |
3197 | rmail-search-mime-message-function) | |
3198 | (funcall rmail-search-mime-message-function msg regexp) | |
537ab246 BG |
3199 | (re-search-forward regexp (rmail-msgend msg) t))) |
3200 | ||
3201 | (defvar rmail-search-last-regexp nil) | |
3202 | (defun rmail-search (regexp &optional n) | |
3203 | "Show message containing next match for REGEXP (but not the current msg). | |
3204 | Prefix argument gives repeat count; negative argument means search | |
3205 | backwards (through earlier messages). | |
3206 | Interactively, empty argument means use same regexp used last time." | |
3207 | (interactive | |
3208 | (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0)) | |
3209 | (prompt | |
3210 | (concat (if reversep "Reverse " "") "Rmail search (regexp")) | |
3211 | regexp) | |
3212 | (setq prompt | |
3213 | (concat prompt | |
3214 | (if rmail-search-last-regexp | |
3215 | (concat ", default " | |
3216 | rmail-search-last-regexp "): ") | |
3217 | "): "))) | |
3218 | (setq regexp (read-string prompt)) | |
3219 | (cond ((not (equal regexp "")) | |
3220 | (setq rmail-search-last-regexp regexp)) | |
3221 | ((not rmail-search-last-regexp) | |
3222 | (error "No previous Rmail search string"))) | |
3223 | (list rmail-search-last-regexp | |
3224 | (prefix-numeric-value current-prefix-arg)))) | |
3225 | (or n (setq n 1)) | |
3226 | (message "%sRmail search for %s..." | |
3227 | (if (< n 0) "Reverse " "") | |
3228 | regexp) | |
3229 | (set-buffer rmail-buffer) | |
3230 | (let ((orig-message rmail-current-message) | |
3231 | (msg rmail-current-message) | |
3232 | (reversep (< n 0)) | |
3233 | (opoint (if (rmail-buffers-swapped-p) (point))) | |
3234 | found) | |
3235 | (rmail-swap-buffers-maybe) | |
3236 | (rmail-maybe-set-message-counters) | |
3237 | (widen) | |
3238 | (unwind-protect | |
3239 | (while (/= n 0) | |
3240 | ;; Check messages one by one, advancing message number up or | |
3241 | ;; down but searching forward through each message. | |
3242 | (if reversep | |
3243 | (while (and (null found) (> msg 1)) | |
3244 | (setq msg (1- msg) | |
3245 | found (rmail-search-message msg regexp))) | |
3246 | (while (and (null found) (< msg rmail-total-messages)) | |
3247 | (setq msg (1+ msg) | |
3248 | found (rmail-search-message msg regexp)))) | |
3249 | (setq n (+ n (if reversep 1 -1)))) | |
3250 | (if found | |
3251 | (progn | |
809f3af0 | 3252 | (rmail-show-message msg) |
537ab246 BG |
3253 | ;; Search forward (if this is a normal search) or backward |
3254 | ;; (if this is a reverse search) through this message to | |
3255 | ;; position point. This search may fail because REGEXP | |
3256 | ;; was found in the hidden portion of this message. In | |
3257 | ;; that case, move point to the beginning of visible | |
3258 | ;; portion. | |
3259 | (if reversep | |
3260 | (progn | |
3261 | (goto-char (point-max)) | |
3262 | (re-search-backward regexp nil 'move)) | |
3263 | (goto-char (point-min)) | |
3264 | (re-search-forward regexp nil t)) | |
3265 | (message "%sRmail search for %s...done" | |
3266 | (if reversep "Reverse " "") | |
3267 | regexp)) | |
809f3af0 | 3268 | (rmail-show-message orig-message) |
537ab246 BG |
3269 | (if opoint (goto-char opoint)) |
3270 | (ding) | |
3271 | (message "Search failed: %s" regexp))))) | |
3272 | ||
3273 | (defun rmail-search-backwards (regexp &optional n) | |
3274 | "Show message containing previous match for REGEXP. | |
3275 | Prefix argument gives repeat count; negative argument means search | |
3276 | forward (through later messages). | |
3277 | Interactively, empty argument means use same regexp used last time." | |
3278 | (interactive | |
3279 | (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0)) | |
3280 | (prompt | |
3281 | (concat (if reversep "Reverse " "") "Rmail search (regexp")) | |
3282 | regexp) | |
3283 | (setq prompt | |
3284 | (concat prompt | |
3285 | (if rmail-search-last-regexp | |
3286 | (concat ", default " | |
3287 | rmail-search-last-regexp "): ") | |
3288 | "): "))) | |
3289 | (setq regexp (read-string prompt)) | |
3290 | (cond ((not (equal regexp "")) | |
3291 | (setq rmail-search-last-regexp regexp)) | |
3292 | ((not rmail-search-last-regexp) | |
3293 | (error "No previous Rmail search string"))) | |
3294 | (list rmail-search-last-regexp | |
3295 | (prefix-numeric-value current-prefix-arg)))) | |
3296 | (rmail-search regexp (- (or n 1)))) | |
3297 | \f | |
3298 | ;; Scan for attributes, and compare subjects. | |
3299 | ||
3300 | (defun rmail-first-unseen-message () | |
3301 | "Return message number of first message which has `unseen' attribute." | |
3302 | (rmail-maybe-set-message-counters) | |
3303 | (let ((current 1) | |
3304 | found) | |
3305 | (save-restriction | |
3306 | (widen) | |
3307 | (while (and (not found) (<= current rmail-total-messages)) | |
3308 | (if (rmail-message-attr-p current "......U") | |
3309 | (setq found current)) | |
3310 | (setq current (1+ current)))) | |
3311 | found)) | |
3312 | ||
3313 | (defun rmail-simplified-subject (&optional msgnum) | |
3314 | "Return the simplified subject of message MSGNUM (or current message). | |
3315 | Simplifying the subject means stripping leading and trailing whitespace, | |
3316 | and typical reply prefixes such as Re:." | |
3317 | (let ((subject (or (rmail-get-header "Subject" msgnum) ""))) | |
53479029 | 3318 | (setq subject (rfc2047-decode-string subject)) |
537ab246 BG |
3319 | (if (string-match "\\`[ \t]+" subject) |
3320 | (setq subject (substring subject (match-end 0)))) | |
3321 | (if (string-match rmail-reply-regexp subject) | |
3322 | (setq subject (substring subject (match-end 0)))) | |
3323 | (if (string-match "[ \t]+\\'" subject) | |
3324 | (setq subject (substring subject 0 (match-beginning 0)))) | |
92655f6c EZ |
3325 | ;; If Subject is long, mailers will break it into several lines at |
3326 | ;; arbitrary places, so normalize whitespace by replacing every | |
3327 | ;; run of whitespace characters with a single space. | |
3328 | (setq subject (replace-regexp-in-string "[ \t\n]+" " " subject)) | |
537ab246 BG |
3329 | subject)) |
3330 | ||
3331 | (defun rmail-simplified-subject-regexp () | |
3332 | "Return a regular expression matching the current simplified subject. | |
3333 | The idea is to match it against simplified subjects of other messages." | |
3334 | (let ((subject (rmail-simplified-subject))) | |
3335 | (setq subject (regexp-quote subject)) | |
3336 | ;; Hide commas so it will work ok if parsed as a comma-separated list | |
3337 | ;; of regexps. | |
3338 | (setq subject | |
3339 | (replace-regexp-in-string "," "\054" subject t t)) | |
3340 | (concat "\\`" subject "\\'"))) | |
3341 | ||
3342 | (defun rmail-next-same-subject (n) | |
3343 | "Go to the next mail message having the same subject header. | |
3344 | With prefix argument N, do this N times. | |
3345 | If N is negative, go backwards instead." | |
3346 | (interactive "p") | |
3347 | (let ((subject (rmail-simplified-subject)) | |
3348 | (forward (> n 0)) | |
3349 | (i rmail-current-message) | |
3350 | found) | |
3351 | (while (and (/= n 0) | |
3352 | (if forward | |
3353 | (< i rmail-total-messages) | |
3354 | (> i 1))) | |
3355 | (let (done) | |
3356 | (while (and (not done) | |
3357 | (if forward | |
3358 | (< i rmail-total-messages) | |
3359 | (> i 1))) | |
3360 | (setq i (if forward (1+ i) (1- i))) | |
3361 | (setq done (string-equal subject (rmail-simplified-subject i)))) | |
3362 | (if done (setq found i))) | |
3363 | (setq n (if forward (1- n) (1+ n)))) | |
3364 | (if found | |
809f3af0 | 3365 | (rmail-show-message found) |
537ab246 BG |
3366 | (error "No %s message with same subject" |
3367 | (if forward "following" "previous"))))) | |
3368 | ||
3369 | (defun rmail-previous-same-subject (n) | |
3370 | "Go to the previous mail message having the same subject header. | |
3371 | With prefix argument N, do this N times. | |
3372 | If N is negative, go forwards instead." | |
3373 | (interactive "p") | |
3374 | (rmail-next-same-subject (- n))) | |
3375 | \f | |
3376 | ;;;; *** Rmail Message Deletion Commands *** | |
3377 | ||
3378 | (defun rmail-message-deleted-p (n) | |
8e7a0f83 | 3379 | "Return non-nil if message number N is deleted (in `rmail-deleted-vector')." |
537ab246 BG |
3380 | (= (aref rmail-deleted-vector n) ?D)) |
3381 | ||
3382 | (defun rmail-set-message-deleted-p (n state) | |
8e7a0f83 GM |
3383 | "Set the deleted state of message number N (in `rmail-deleted-vector'). |
3384 | STATE non-nil means mark as deleted." | |
3385 | (aset rmail-deleted-vector n (if state ?D ?\s))) | |
537ab246 BG |
3386 | |
3387 | (defun rmail-delete-message () | |
3388 | "Delete this message and stay on it." | |
3389 | (interactive) | |
3390 | (rmail-set-attribute rmail-deleted-attr-index t) | |
3391 | (run-hooks 'rmail-delete-message-hook)) | |
3392 | ||
3393 | (defun rmail-undelete-previous-message () | |
3394 | "Back up to deleted message, select it, and undelete it." | |
3395 | (interactive) | |
3396 | (set-buffer rmail-buffer) | |
3397 | (let ((msg rmail-current-message)) | |
3398 | (while (and (> msg 0) | |
3399 | (not (rmail-message-deleted-p msg))) | |
3400 | (setq msg (1- msg))) | |
3401 | (if (= msg 0) | |
3402 | (error "No previous deleted message") | |
3403 | (if (/= msg rmail-current-message) | |
809f3af0 | 3404 | (rmail-show-message msg)) |
537ab246 BG |
3405 | (rmail-set-attribute rmail-deleted-attr-index nil) |
3406 | (if (rmail-summary-exists) | |
2dc00ad0 | 3407 | (with-current-buffer rmail-summary-buffer |
537ab246 BG |
3408 | (rmail-summary-mark-undeleted msg))) |
3409 | (rmail-maybe-display-summary)))) | |
3410 | ||
3411 | (defun rmail-delete-forward (&optional backward) | |
3412 | "Delete this message and move to next nondeleted one. | |
3413 | Deleted messages stay in the file until the \\[rmail-expunge] command is given. | |
3414 | With prefix argument, delete and move backward. | |
3415 | ||
3416 | Returns t if a new message is displayed after the delete, or nil otherwise." | |
3417 | (interactive "P") | |
3418 | (rmail-set-attribute rmail-deleted-attr-index t) | |
3419 | (run-hooks 'rmail-delete-message-hook) | |
3420 | (let ((del-msg rmail-current-message)) | |
3421 | (if (rmail-summary-exists) | |
3422 | (rmail-select-summary | |
3423 | (rmail-summary-mark-deleted del-msg))) | |
3424 | (prog1 (rmail-next-undeleted-message (if backward -1 1)) | |
3425 | (rmail-maybe-display-summary)))) | |
3426 | ||
3427 | (defun rmail-delete-backward () | |
3428 | "Delete this message and move to previous nondeleted one. | |
3429 | Deleted messages stay in the file until the \\[rmail-expunge] command is given." | |
3430 | (interactive) | |
3431 | (rmail-delete-forward t)) | |
3432 | \f | |
3433 | ;; Expunging. | |
3434 | ||
3435 | ;; Compute the message number a given message would have after expunging. | |
3436 | ;; The present number of the message is OLDNUM. | |
3437 | ;; DELETEDVEC should be rmail-deleted-vector. | |
3438 | ;; The value is nil for a message that would be deleted. | |
3439 | (defun rmail-msg-number-after-expunge (deletedvec oldnum) | |
3440 | (if (or (null oldnum) (= (aref deletedvec oldnum) ?D)) | |
3441 | nil | |
3442 | (let ((i 0) | |
3443 | (newnum 0)) | |
3444 | (while (< i oldnum) | |
3445 | (if (/= (aref deletedvec i) ?D) | |
3446 | (setq newnum (1+ newnum))) | |
3447 | (setq i (1+ i))) | |
3448 | newnum))) | |
3449 | ||
3450 | (defun rmail-expunge-confirmed () | |
4df49ff1 RS |
3451 | "Return t if expunge is needed and desirable. |
3452 | If `rmail-confirm-expunge' is non-nil, ask user to confirm." | |
537ab246 | 3453 | (set-buffer rmail-buffer) |
4df49ff1 RS |
3454 | (and (stringp rmail-deleted-vector) |
3455 | (string-match "D" rmail-deleted-vector) | |
3456 | (if rmail-confirm-expunge | |
3457 | (funcall rmail-confirm-expunge | |
3458 | "Erase deleted messages from Rmail file? ") | |
3459 | t))) | |
537ab246 BG |
3460 | |
3461 | (defun rmail-only-expunge (&optional dont-show) | |
3462 | "Actually erase all deleted messages in the file." | |
3463 | (interactive) | |
3464 | (rmail-swap-buffers-maybe) | |
3465 | (set-buffer rmail-buffer) | |
3466 | (message "Expunging deleted messages...") | |
3467 | ;; Discard all undo records for this buffer. | |
3468 | (or (eq buffer-undo-list t) | |
3469 | (setq buffer-undo-list nil)) | |
3470 | (rmail-maybe-set-message-counters) | |
3471 | (let* ((omax (- (buffer-size) (point-max))) | |
3472 | (omin (- (buffer-size) (point-min))) | |
3473 | (opoint (if (and (> rmail-current-message 0) | |
3474 | (rmail-message-deleted-p rmail-current-message)) | |
3475 | 0 | |
3476 | (if rmail-enable-mime | |
3477 | (with-current-buffer rmail-view-buffer | |
3478 | (- (point)(point-min))) | |
3479 | (- (point) (point-min))))) | |
3480 | (messages-head (cons (aref rmail-message-vector 0) nil)) | |
3481 | (messages-tail messages-head) | |
3482 | ;; Don't make any undo records for the expunging. | |
3483 | (buffer-undo-list t) | |
3484 | (win)) | |
3485 | (unwind-protect | |
3486 | (save-excursion | |
3487 | (widen) | |
3488 | (goto-char (point-min)) | |
3489 | (let ((counter 0) | |
3490 | (number 1) | |
3491 | new-summary | |
3492 | (new-msgref (list (list 0))) | |
3493 | (buffer-read-only nil) | |
3494 | (total rmail-total-messages) | |
3495 | (new-message-number rmail-current-message) | |
3496 | (messages rmail-message-vector) | |
3497 | (deleted rmail-deleted-vector) | |
3498 | (summary rmail-summary-vector)) | |
3499 | (setq rmail-total-messages nil | |
3500 | rmail-current-message nil | |
3501 | rmail-message-vector nil | |
3502 | rmail-deleted-vector nil | |
3503 | rmail-summary-vector nil) | |
3504 | ||
3505 | (while (<= number total) | |
3506 | (if (= (aref deleted number) ?D) | |
3507 | (progn | |
3508 | (delete-region (aref messages number) | |
3509 | (aref messages (1+ number))) | |
3510 | (move-marker (aref messages number) nil) | |
3511 | (if (> new-message-number counter) | |
3512 | (setq new-message-number (1- new-message-number)))) | |
3513 | (setq counter (1+ counter)) | |
3514 | (setq messages-tail | |
3515 | (setcdr messages-tail | |
3516 | (cons (aref messages number) nil))) | |
3517 | (setq new-summary | |
3518 | (cons (if (= counter number) (aref summary (1- number))) | |
3519 | new-summary)) | |
3520 | (setq new-msgref | |
3521 | (cons (aref rmail-msgref-vector number) | |
3522 | new-msgref)) | |
3523 | (setcar (car new-msgref) counter)) | |
3524 | (if (zerop (% (setq number (1+ number)) 20)) | |
3525 | (message "Expunging deleted messages...%d" number))) | |
3526 | (setq messages-tail | |
3527 | (setcdr messages-tail | |
3528 | (cons (aref messages number) nil))) | |
3529 | (setq rmail-current-message new-message-number | |
3530 | rmail-total-messages counter | |
3531 | rmail-message-vector (apply 'vector messages-head) | |
8e7a0f83 | 3532 | rmail-deleted-vector (make-string (1+ counter) ?\s) |
537ab246 BG |
3533 | rmail-summary-vector (vconcat (nreverse new-summary)) |
3534 | rmail-msgref-vector (apply 'vector (nreverse new-msgref)) | |
3535 | win t))) | |
3536 | (message "Expunging deleted messages...done") | |
3537 | (if (not win) | |
3538 | (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax))) | |
3539 | (if (not dont-show) | |
809f3af0 | 3540 | (rmail-show-message (min rmail-current-message rmail-total-messages))) |
537ab246 BG |
3541 | (if rmail-enable-mime |
3542 | (goto-char (+ (point-min) opoint)) | |
3543 | (goto-char (+ (point) opoint)))))) | |
3544 | ||
384b53ab | 3545 | ;; The DONT-SHOW argument is new in 23. Does not seem very important. |
537ab246 | 3546 | (defun rmail-expunge (&optional dont-show) |
384b53ab GM |
3547 | "Erase deleted messages from Rmail file and summary buffer. |
3548 | This always shows a message (so as not to leave the Rmail buffer | |
3549 | unswapped), and always updates any summary (so that it remains | |
3550 | consistent with the Rmail buffer). If DONT-SHOW is non-nil, it | |
3551 | does not pop any summary buffer." | |
537ab246 BG |
3552 | (interactive) |
3553 | (when (rmail-expunge-confirmed) | |
4df49ff1 | 3554 | (rmail-modify-format) |
0b9b4ab9 | 3555 | (let ((was-deleted (rmail-message-deleted-p rmail-current-message)) |
8882002a | 3556 | (was-swapped (rmail-buffers-swapped-p))) |
0b9b4ab9 | 3557 | (rmail-only-expunge t) |
384b53ab GM |
3558 | ;; We always update the summary buffer, so that the contents |
3559 | ;; remain consistent with the rmail buffer. | |
3560 | ;; The only difference is, in the dont-show case, we use a | |
3561 | ;; cut-down version of rmail-select-summary that does not pop | |
3562 | ;; the summary buffer. It's only used by rmail-quit, which is | |
3563 | ;; just going to bury any summary immediately after. If we made | |
3564 | ;; rmail-quit bury the summary first, dont-show could be removed. | |
3565 | ;; But the expunge might not be confirmed... | |
3566 | (if (rmail-summary-exists) | |
3567 | (if dont-show | |
3568 | (let ((total rmail-total-messages)) | |
3569 | (with-current-buffer rmail-summary-buffer | |
3570 | (let ((rmail-total-messages total)) | |
3571 | (rmail-update-summary)))) | |
3572 | (rmail-select-summary (rmail-update-summary)))) | |
3573 | ;; We always show a message, because (rmail-only-expunge t) | |
3574 | ;; leaves the rmail buffer unswapped. | |
3575 | ;; If we expunged the current message, a new one is current now, | |
3576 | ;; so show it. If we weren't showing a message, show it. | |
3577 | (if (or was-deleted (not was-swapped)) | |
3578 | (rmail-show-message-1 rmail-current-message) | |
3579 | ;; We can just show the same message that was being shown before. | |
3580 | (rmail-display-labels) | |
3581 | (rmail-swap-buffers) | |
3582 | (setq rmail-buffer-swapped t))))) | |
537ab246 BG |
3583 | \f |
3584 | ;;;; *** Rmail Mailing Commands *** | |
3585 | ||
5f6530ea RS |
3586 | (defun rmail-yank-current-message (buffer) |
3587 | "Yank into the current buffer the current message of Rmail buffer BUFFER. | |
3588 | If BUFFER is swapped with its message viewer buffer, yank out of BUFFER. | |
3589 | If BUFFER is not swapped, yank out of its message viewer buffer." | |
3590 | (with-current-buffer buffer | |
3591 | (unless (rmail-buffers-swapped-p) | |
3592 | (setq buffer rmail-view-buffer))) | |
9e1b8ec4 | 3593 | (insert-buffer-substring buffer) |
91af76bf EZ |
3594 | ;; If they yank the text of BUFFER, the encoding of BUFFER is a |
3595 | ;; better default for the reply message than the default value of | |
3596 | ;; buffer-file-coding-system. | |
3597 | (and (coding-system-equal (default-value 'buffer-file-coding-system) | |
3598 | buffer-file-coding-system) | |
3599 | (setq buffer-file-coding-system | |
3600 | (coding-system-change-text-conversion | |
3601 | buffer-file-coding-system (coding-system-base | |
3602 | (with-current-buffer buffer | |
3603 | buffer-file-coding-system)))))) | |
5f6530ea | 3604 | |
537ab246 | 3605 | (defun rmail-start-mail (&optional noerase to subject in-reply-to cc |
25ca2e61 CY |
3606 | replybuffer sendactions same-window |
3607 | other-headers) | |
3608 | (let ((switch-function | |
3609 | (cond (same-window nil) | |
3610 | (rmail-mail-new-frame 'switch-to-buffer-other-frame) | |
3611 | (t 'switch-to-buffer-other-window))) | |
3612 | yank-action) | |
537ab246 | 3613 | (if replybuffer |
433c1652 GM |
3614 | ;; The function used here must behave like insert-buffer wrt |
3615 | ;; point and mark (see doc of sc-cite-original). | |
5f6530ea RS |
3616 | (setq yank-action |
3617 | `(rmail-yank-current-message ,replybuffer))) | |
25ca2e61 CY |
3618 | (push (cons "cc" cc) other-headers) |
3619 | (push (cons "in-reply-to" in-reply-to) other-headers) | |
cce7d530 | 3620 | (setq other-headers |
1f459fa4 KH |
3621 | (mapcar #'(lambda (elt) |
3622 | (cons (car elt) (if (stringp (cdr elt)) | |
3623 | (rfc2047-decode-string (cdr elt))))) | |
cce7d530 | 3624 | other-headers)) |
1f459fa4 KH |
3625 | (if (stringp to) (setq to (rfc2047-decode-string to))) |
3626 | (if (stringp in-reply-to) | |
3627 | (setq in-reply-to (rfc2047-decode-string in-reply-to))) | |
3628 | (if (stringp cc) (setq cc (rfc2047-decode-string cc))) | |
3629 | (if (stringp subject) (setq subject (rfc2047-decode-string subject))) | |
25ca2e61 CY |
3630 | (prog1 |
3631 | (compose-mail to subject other-headers noerase | |
d7a9e63b | 3632 | switch-function yank-action sendactions |
5f6530ea | 3633 | (if replybuffer `(rmail-mail-return ,replybuffer))) |
25ca2e61 CY |
3634 | (if (eq switch-function 'switch-to-buffer-other-frame) |
3635 | ;; This is not a standard frame parameter; nothing except | |
3636 | ;; sendmail.el looks at it. | |
d7a9e63b | 3637 | (modify-frame-parameters (selected-frame) |
25ca2e61 CY |
3638 | '((mail-dedicated-frame . t))))))) |
3639 | ||
8050daa1 | 3640 | (defun rmail-mail-return (&optional newbuf) |
d7a9e63b CY |
3641 | "Try to return to Rmail from the mail window. |
3642 | If optional argument NEWBUF is specified, it is the Rmail buffer | |
3643 | to switch to." | |
25ca2e61 CY |
3644 | (cond |
3645 | ;; If there is only one visible frame with no special handling, | |
3646 | ;; consider deleting the mail window to return to Rmail. | |
3647 | ((or (null (delq (selected-frame) (visible-frame-list))) | |
3648 | (not (or (window-dedicated-p (frame-selected-window)) | |
3649 | (and pop-up-frames (one-window-p)) | |
3650 | (cdr (assq 'mail-dedicated-frame | |
3651 | (frame-parameters)))))) | |
3652 | (let (rmail-flag summary-buffer) | |
d7a9e63b CY |
3653 | (unless (one-window-p) |
3654 | (with-current-buffer | |
3655 | (window-buffer (next-window (selected-window) 'not)) | |
3656 | (setq rmail-flag (eq major-mode 'rmail-mode)) | |
3657 | (setq summary-buffer | |
3658 | (and (boundp 'mail-bury-selects-summary) | |
3659 | mail-bury-selects-summary | |
3660 | (boundp 'rmail-summary-buffer) | |
3661 | rmail-summary-buffer | |
3662 | (buffer-name rmail-summary-buffer) | |
3663 | (not (get-buffer-window rmail-summary-buffer)) | |
3664 | rmail-summary-buffer)))) | |
3665 | (cond ((null rmail-flag) | |
3666 | ;; If the Rmail buffer is not in the next window, switch | |
3667 | ;; directly to the Rmail buffer specified by NEWBUF. | |
0f2bad27 | 3668 | (if (buffer-live-p newbuf) |
d7a9e63b CY |
3669 | (switch-to-buffer newbuf))) |
3670 | ;; If the Rmail buffer is in the next window, switch to | |
3671 | ;; the summary buffer if `mail-bury-selects-summary' is | |
3672 | ;; non-nil. Otherwise just delete this window. | |
3673 | (summary-buffer | |
3674 | (switch-to-buffer summary-buffer)) | |
3675 | (t | |
3676 | (delete-window))))) | |
25ca2e61 CY |
3677 | ;; If the frame was probably made for this buffer, the user |
3678 | ;; probably wants to delete it now. | |
3679 | ((display-multi-frame-p) | |
3680 | (delete-frame (selected-frame))) | |
3681 | ;; The previous frame is where normally they have the Rmail buffer | |
3682 | ;; displayed. | |
3683 | (t (other-frame -1)))) | |
537ab246 BG |
3684 | |
3685 | (defun rmail-mail () | |
3686 | "Send mail in another window. | |
3687 | While composing the message, use \\[mail-yank-original] to yank the | |
3688 | original message into it." | |
3689 | (interactive) | |
5f6530ea | 3690 | (rmail-start-mail nil nil nil nil nil rmail-buffer)) |
537ab246 | 3691 | |
017166ce | 3692 | ;; FIXME should complain if there is nothing to continue. |
537ab246 BG |
3693 | (defun rmail-continue () |
3694 | "Continue composing outgoing message previously being composed." | |
3695 | (interactive) | |
3696 | (rmail-start-mail t)) | |
3697 | ||
3698 | (defun rmail-reply (just-sender) | |
3699 | "Reply to the current message. | |
3700 | Normally include CC: to all other recipients of original message; | |
3701 | prefix argument means ignore them. While composing the reply, | |
3702 | use \\[mail-yank-original] to yank the original message into it." | |
3703 | (interactive "P") | |
df75fdb7 GM |
3704 | (if (zerop rmail-current-message) |
3705 | (error "There is no message to reply to")) | |
537ab246 BG |
3706 | (let (from reply-to cc subject date to message-id references |
3707 | resent-to resent-cc resent-reply-to | |
3708 | (msgnum rmail-current-message)) | |
5f3b7063 GM |
3709 | (rmail-apply-in-message |
3710 | rmail-current-message | |
3711 | (lambda () | |
3712 | (search-forward "\n\n" nil 'move) | |
3713 | (narrow-to-region (point-min) (point)) | |
3714 | (setq from (mail-fetch-field "from") | |
3715 | reply-to (or (mail-fetch-field "mail-reply-to" nil t) | |
3716 | (mail-fetch-field "reply-to" nil t) | |
3717 | from) | |
3718 | subject (mail-fetch-field "subject") | |
3719 | date (mail-fetch-field "date") | |
3720 | message-id (mail-fetch-field "message-id") | |
3721 | references (mail-fetch-field "references" nil nil t) | |
3722 | resent-reply-to (mail-fetch-field "resent-reply-to" nil t) | |
3723 | ;; Bug#512. It's inappropriate to reply to these addresses. | |
3724 | ;;; resent-cc (and (not just-sender) | |
3725 | ;;; (mail-fetch-field "resent-cc" nil t)) | |
3726 | ;;; resent-to (or (mail-fetch-field "resent-to" nil t) "") | |
3727 | ;;; resent-subject (mail-fetch-field "resent-subject") | |
3728 | ;;; resent-date (mail-fetch-field "resent-date") | |
3729 | ;;; resent-message-id (mail-fetch-field "resent-message-id") | |
3730 | ) | |
3731 | (unless just-sender | |
3732 | (if (mail-fetch-field "mail-followup-to" nil t) | |
3733 | ;; If this header field is present, use it instead of the | |
3734 | ;; To and CC fields. | |
3735 | (setq to (mail-fetch-field "mail-followup-to" nil t)) | |
3736 | (setq cc (or (mail-fetch-field "cc" nil t) "") | |
3737 | to (or (mail-fetch-field "to" nil t) "")))))) | |
537ab246 | 3738 | ;; Merge the resent-to and resent-cc into the to and cc. |
ac120e6d GM |
3739 | ;; Bug#512. It's inappropriate to reply to these addresses. |
3740 | ;;; (if (and resent-to (not (equal resent-to ""))) | |
3741 | ;;; (if (not (equal to "")) | |
3742 | ;;; (setq to (concat to ", " resent-to)) | |
3743 | ;;; (setq to resent-to))) | |
3744 | ;;; (if (and resent-cc (not (equal resent-cc ""))) | |
3745 | ;;; (if (not (equal cc "")) | |
3746 | ;;; (setq cc (concat cc ", " resent-cc)) | |
3747 | ;;; (setq cc resent-cc))) | |
537ab246 BG |
3748 | ;; Add `Re: ' to subject if not there already. |
3749 | (and (stringp subject) | |
3750 | (setq subject | |
3751 | (concat rmail-reply-prefix | |
3752 | (if (let ((case-fold-search t)) | |
3753 | (string-match rmail-reply-regexp subject)) | |
3754 | (substring subject (match-end 0)) | |
3755 | subject)))) | |
3756 | (rmail-start-mail | |
3757 | nil | |
3758 | ;; Using mail-strip-quoted-names is undesirable with newer mailers | |
3759 | ;; since they can handle the names unstripped. | |
3760 | ;; I don't know whether there are other mailers that still | |
3761 | ;; need the names to be stripped. | |
3762 | ;;; (mail-strip-quoted-names reply-to) | |
3763 | ;; Remove unwanted names from reply-to, since Mail-Followup-To | |
3764 | ;; header causes all the names in it to wind up in reply-to, not | |
3765 | ;; in cc. But if what's left is an empty list, use the original. | |
38a71655 | 3766 | (let* ((reply-to-list (mail-dont-reply-to reply-to))) |
537ab246 BG |
3767 | (if (string= reply-to-list "") reply-to reply-to-list)) |
3768 | subject | |
3769 | (rmail-make-in-reply-to-field from date message-id) | |
3770 | (if just-sender | |
3771 | nil | |
38a71655 CY |
3772 | ;; `mail-dont-reply-to' doesn't need `mail-strip-quoted-names'. |
3773 | (let* ((cc-list (mail-dont-reply-to | |
537ab246 BG |
3774 | (mail-strip-quoted-names |
3775 | (if (null cc) to (concat to ", " cc)))))) | |
3776 | (if (string= cc-list "") nil cc-list))) | |
5f6530ea | 3777 | rmail-buffer |
537ab246 BG |
3778 | (list (list 'rmail-mark-message |
3779 | rmail-buffer | |
3780 | (with-current-buffer rmail-buffer | |
3781 | (aref rmail-msgref-vector msgnum)) | |
3782 | rmail-answered-attr-index)) | |
3783 | nil | |
4f02f0c9 GM |
3784 | (if (or references message-id) |
3785 | (list (cons "References" (if references | |
3786 | (concat | |
3787 | (mapconcat 'identity references " ") | |
3788 | " " message-id) | |
3789 | message-id))))))) | |
537ab246 BG |
3790 | \f |
3791 | (defun rmail-mark-message (buffer msgnum-list attribute) | |
3792 | "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE. | |
3793 | This is use in the send-actions for message buffers. | |
3794 | MSGNUM-LIST is a list of the form (MSGNUM) | |
3795 | which is an element of rmail-msgref-vector." | |
2dc00ad0 | 3796 | (with-current-buffer buffer |
537ab246 BG |
3797 | (if (car msgnum-list) |
3798 | (rmail-set-attribute attribute t (car msgnum-list))))) | |
3799 | ||
3800 | (defun rmail-make-in-reply-to-field (from date message-id) | |
3801 | (cond ((not from) | |
3802 | (if message-id | |
3803 | message-id | |
3804 | nil)) | |
3805 | (mail-use-rfc822 | |
3806 | (require 'rfc822) | |
3807 | (let ((tem (car (rfc822-addresses from)))) | |
3808 | (if message-id | |
3809 | (if (or (not tem) | |
3810 | (string-match | |
3811 | (regexp-quote (if (string-match "@[^@]*\\'" tem) | |
3812 | (substring tem 0 | |
3813 | (match-beginning 0)) | |
3814 | tem)) | |
3815 | message-id)) | |
3816 | ;; missing From, or Message-ID is sufficiently informative | |
3817 | message-id | |
3818 | (concat message-id " (" tem ")")) | |
3819 | ;; Copy TEM, discarding text properties. | |
3820 | (setq tem (copy-sequence tem)) | |
3821 | (set-text-properties 0 (length tem) nil tem) | |
3822 | (setq tem (copy-sequence tem)) | |
3823 | ;; Use prin1 to fake RFC822 quoting | |
3824 | (let ((field (prin1-to-string tem))) | |
3825 | (if date | |
3826 | (concat field "'s message of " date) | |
3827 | field))))) | |
3828 | ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+") | |
3829 | (bar "[^][\000-\037()<>@,;:\\\"]+")) | |
3830 | ;; These strings both match all non-ASCII characters. | |
3831 | (or (string-match (concat "\\`[ \t]*\\(" bar | |
3832 | "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'") | |
3833 | ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser" | |
3834 | from) | |
3835 | (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\(" | |
3836 | bar "\\))[ \t]*\\'") | |
3837 | ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix" | |
3838 | from))) | |
3839 | (let ((start (match-beginning 1)) | |
3840 | (end (match-end 1))) | |
3841 | ;; Trim whitespace which above regexp match allows | |
3842 | (while (and (< start end) | |
8e7a0f83 | 3843 | (memq (aref from start) '(?\t ?\s))) |
537ab246 BG |
3844 | (setq start (1+ start))) |
3845 | (while (and (< start end) | |
8e7a0f83 | 3846 | (memq (aref from (1- end)) '(?\t ?\s))) |
537ab246 BG |
3847 | (setq end (1- end))) |
3848 | (let ((field (substring from start end))) | |
3849 | (if date (setq field (concat "message from " field " on " date))) | |
3850 | (if message-id | |
3851 | ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)" | |
3852 | (concat message-id " (" field ")") | |
3853 | field)))) | |
3854 | (t | |
3855 | ;; If we can't kludge it simply, do it correctly | |
3856 | (let ((mail-use-rfc822 t)) | |
3857 | (rmail-make-in-reply-to-field from date message-id))))) | |
3858 | \f | |
3859 | (defun rmail-forward (resend) | |
3860 | "Forward the current message to another user. | |
3861 | With prefix argument, \"resend\" the message instead of forwarding it; | |
3862 | see the documentation of `rmail-resend'." | |
3863 | (interactive "P") | |
8038d2d2 GM |
3864 | (if (zerop rmail-current-message) |
3865 | (error "No message to forward")) | |
537ab246 BG |
3866 | (if resend |
3867 | (call-interactively 'rmail-resend) | |
3868 | (let ((forward-buffer rmail-buffer) | |
3869 | (msgnum rmail-current-message) | |
3870 | (subject (concat "[" | |
3871 | (let ((from (or (mail-fetch-field "From") | |
3872 | (mail-fetch-field ">From")))) | |
3873 | (if from | |
3874 | (concat (mail-strip-quoted-names from) ": ") | |
3875 | "")) | |
3876 | (or (mail-fetch-field "Subject") "") | |
3877 | "]"))) | |
3878 | (if (rmail-start-mail | |
5f6530ea | 3879 | nil nil subject nil nil rmail-buffer |
537ab246 BG |
3880 | (list (list 'rmail-mark-message |
3881 | forward-buffer | |
3882 | (with-current-buffer rmail-buffer | |
3883 | (aref rmail-msgref-vector msgnum)) | |
3884 | rmail-forwarded-attr-index)) | |
3885 | ;; If only one window, use it for the mail buffer. | |
3886 | ;; Otherwise, use another window for the mail buffer | |
3887 | ;; so that the Rmail buffer remains visible | |
3888 | ;; and sending the mail will get back to it. | |
3889 | (and (not rmail-mail-new-frame) (one-window-p t))) | |
3890 | ;; The mail buffer is now current. | |
3891 | (save-excursion | |
3892 | ;; Insert after header separator--before signature if any. | |
fb89a654 GM |
3893 | (rfc822-goto-eoh) |
3894 | (forward-line 1) | |
7fb18e9e GM |
3895 | (if (and rmail-enable-mime rmail-enable-mime-composing |
3896 | rmail-insert-mime-forwarded-message-function) | |
fd59d131 EZ |
3897 | (prog1 |
3898 | (funcall rmail-insert-mime-forwarded-message-function | |
3899 | forward-buffer) | |
3900 | ;; rmail-insert-mime-forwarded-message-function | |
3901 | ;; works by inserting MML tags into forward-buffer. | |
3902 | ;; The MUA will need to convert it to MIME before | |
3903 | ;; sending. mail-encode-mml tells them to do that. | |
3904 | ;; message.el does that automagically. | |
3905 | (or (eq mail-user-agent 'message-user-agent) | |
3906 | (setq mail-encode-mml t))) | |
537ab246 BG |
3907 | (insert "------- Start of forwarded message -------\n") |
3908 | ;; Quote lines with `- ' if they start with `-'. | |
3909 | (let ((beg (point)) end) | |
3910 | (setq end (point-marker)) | |
3911 | (set-marker-insertion-type end t) | |
3912 | (insert-buffer-substring forward-buffer) | |
3913 | (goto-char beg) | |
3914 | (while (re-search-forward "^-" end t) | |
3915 | (beginning-of-line) | |
3916 | (insert "- ") | |
3917 | (forward-line 1)) | |
3918 | (goto-char end) | |
3919 | (skip-chars-backward "\n") | |
3920 | (if (< (point) end) | |
3921 | (forward-char 1)) | |
3922 | (delete-region (point) end) | |
3923 | (set-marker end nil)) | |
3924 | (insert "------- End of forwarded message -------\n")) | |
3925 | (push-mark)))))) | |
3926 | \f | |
3927 | (defun rmail-resend (address &optional from comment mail-alias-file) | |
3928 | "Resend current message to ADDRESSES. | |
3929 | ADDRESSES should be a single address, a string consisting of several | |
3930 | addresses separated by commas, or a list of addresses. | |
3931 | ||
3932 | Optional FROM is the address to resend the message from, and | |
3933 | defaults from the value of `user-mail-address'. | |
3934 | Optional COMMENT is a string to insert as a comment in the resent message. | |
3935 | Optional ALIAS-FILE is alternate aliases file to be used by sendmail, | |
3936 | typically for purposes of moderating a list." | |
3937 | (interactive "sResend to: ") | |
3938 | (require 'sendmail) | |
3939 | (require 'mailalias) | |
3940 | (unless (or (eq rmail-view-buffer (current-buffer)) | |
3941 | (eq rmail-buffer (current-buffer))) | |
3942 | (error "Not an Rmail buffer")) | |
3943 | (if (not from) (setq from user-mail-address)) | |
3944 | (let ((tembuf (generate-new-buffer " sendmail temp")) | |
3945 | (case-fold-search nil) | |
3946 | (mail-personal-alias-file | |
3947 | (or mail-alias-file mail-personal-alias-file)) | |
3948 | (mailbuf rmail-buffer)) | |
3949 | (unwind-protect | |
3950 | (with-current-buffer tembuf | |
3951 | ;;>> Copy message into temp buffer | |
7fb18e9e GM |
3952 | (if (and rmail-enable-mime |
3953 | rmail-insert-mime-resent-message-function) | |
778ef2ef | 3954 | (funcall rmail-insert-mime-resent-message-function mailbuf) |
537ab246 BG |
3955 | (insert-buffer-substring mailbuf)) |
3956 | (goto-char (point-min)) | |
3957 | ;; Delete any Sender field, since that's not specifiable. | |
3958 | ; Only delete Sender fields in the actual header. | |
3959 | (re-search-forward "^$" nil 'move) | |
3960 | ; Using "while" here rather than "if" because some buggy mail | |
3961 | ; software may have inserted multiple Sender fields. | |
3962 | (while (re-search-backward "^Sender:" nil t) | |
3963 | (let (beg) | |
3964 | (setq beg (point)) | |
3965 | (forward-line 1) | |
3966 | (while (looking-at "[ \t]") | |
3967 | (forward-line 1)) | |
3968 | (delete-region beg (point)))) | |
3969 | ; Go back to the beginning of the buffer so the Resent- fields | |
3970 | ; are inserted there. | |
3971 | (goto-char (point-min)) | |
3972 | ;;>> Insert resent-from: | |
3973 | (insert "Resent-From: " from "\n") | |
3974 | (insert "Resent-Date: " (mail-rfc822-date) "\n") | |
3975 | ;;>> Insert resent-to: and bcc if need be. | |
3976 | (let ((before (point))) | |
3977 | (if mail-self-blind | |
3978 | (insert "Resent-Bcc: " (user-login-name) "\n")) | |
3979 | (insert "Resent-To: " (if (stringp address) | |
3980 | address | |
3981 | (mapconcat 'identity address ",\n\t")) | |
3982 | "\n") | |
3983 | ;; Expand abbrevs in the recipients. | |
3984 | (save-excursion | |
3985 | (if (featurep 'mailabbrev) | |
3986 | (let ((end (point-marker)) | |
3987 | (local-abbrev-table mail-abbrevs) | |
3988 | (old-syntax-table (syntax-table))) | |
3989 | (if (and (not (vectorp mail-abbrevs)) | |
3990 | (file-exists-p mail-personal-alias-file)) | |
3991 | (build-mail-abbrevs)) | |
3992 | (unless mail-abbrev-syntax-table | |
3993 | (mail-abbrev-make-syntax-table)) | |
3994 | (set-syntax-table mail-abbrev-syntax-table) | |
3995 | (goto-char before) | |
3996 | (while (and (< (point) end) | |
3997 | (progn (forward-word 1) | |
3998 | (<= (point) end))) | |
3999 | (expand-abbrev)) | |
4000 | (set-syntax-table old-syntax-table)) | |
4001 | (expand-mail-aliases before (point))))) | |
4002 | ;;>> Set up comment, if any. | |
4003 | (if (and (sequencep comment) (not (zerop (length comment)))) | |
4004 | (let ((before (point)) | |
4005 | after) | |
4006 | (insert comment) | |
4007 | (or (eolp) (insert "\n")) | |
4008 | (setq after (point)) | |
4009 | (goto-char before) | |
4010 | (while (< (point) after) | |
4011 | (insert "Resent-Comment: ") | |
4012 | (forward-line 1)))) | |
4013 | ;; Don't expand aliases in the destination fields | |
4014 | ;; of the original message. | |
4015 | (let (mail-aliases) | |
4016 | (funcall send-mail-function))) | |
4017 | (kill-buffer tembuf)) | |
4018 | (with-current-buffer rmail-buffer | |
4019 | (rmail-set-attribute rmail-resent-attr-index t rmail-current-message)))) | |
4020 | \f | |
4021 | (defvar mail-unsent-separator | |
4022 | (concat "^ *---+ +Unsent message follows +---+ *$\\|" | |
4023 | "^ *---+ +Returned message +---+ *$\\|" | |
4024 | "^ *---+ *Returned mail follows *---+ *$\\|" | |
4025 | "^Start of returned message$\\|" | |
4026 | "^---+ Below this line is a copy of the message.$\\|" | |
4027 | "^ *---+ +Original message +---+ *$\\|" | |
4028 | "^ *--+ +begin message +--+ *$\\|" | |
4029 | "^ *---+ +Original message follows +---+ *$\\|" | |
4030 | "^ *---+ +Your message follows +---+ *$\\|" | |
4031 | "^|? *---+ +Message text follows: +---+ *|?$\\|" | |
4032 | "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$") | |
4033 | "A regexp that matches the separator before the text of a failed message.") | |
4034 | ||
4035 | (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$" | |
4036 | "A regexp that matches the header of a MIME body part with a failed message.") | |
4037 | ||
37aca7a8 GM |
4038 | ;; This is a cut-down version of rmail-clear-headers from Emacs 22. |
4039 | ;; It doesn't have the same functionality, hence the name change. | |
4040 | (defun rmail-delete-headers (regexp) | |
4041 | "Delete any mail headers matching REGEXP. | |
4042 | The message should be narrowed to just the headers." | |
4043 | (when regexp | |
4044 | (goto-char (point-min)) | |
4045 | (while (re-search-forward regexp nil t) | |
4046 | (beginning-of-line) | |
4047 | ;; This code from Emacs 22 doesn't seem right, since r-n-h is | |
4048 | ;; just for display. | |
4049 | ;;; (if (looking-at rmail-nonignored-headers) | |
4050 | ;;; (forward-line 1) | |
4051 | (delete-region (point) | |
4052 | (save-excursion | |
4053 | (if (re-search-forward "\n[^ \t]" nil t) | |
4054 | (1- (point)) | |
4055 | (point-max))))))) | |
4056 | ||
58d1ac6d | 4057 | (autoload 'mail-position-on-field "sendmail") |
fb89a654 | 4058 | |
537ab246 BG |
4059 | (defun rmail-retry-failure () |
4060 | "Edit a mail message which is based on the contents of the current message. | |
4061 | For a message rejected by the mail system, extract the interesting headers and | |
4062 | the body of the original message. | |
4063 | If the failed message is a MIME multipart message, it is searched for a | |
4064 | body part with a header which matches the variable `mail-mime-unsent-header'. | |
4065 | Otherwise, the variable `mail-unsent-separator' should match the string that | |
4066 | delimits the returned original message. | |
4067 | The variable `rmail-retry-ignored-headers' is a regular expression | |
4068 | specifying headers which should not be copied into the new message." | |
4069 | (interactive) | |
4070 | (require 'mail-utils) | |
4071 | (let ((rmail-this-buffer (current-buffer)) | |
4072 | (msgnum rmail-current-message) | |
4073 | bounce-start bounce-end bounce-indent resending | |
925d6582 | 4074 | (content-type (rmail-get-header "Content-Type"))) |
537ab246 BG |
4075 | (save-excursion |
4076 | (goto-char (point-min)) | |
4077 | (let ((case-fold-search t)) | |
4078 | (if (and content-type | |
4079 | (string-match | |
4080 | ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?" | |
4081 | content-type)) | |
4082 | ;; Handle a MIME multipart bounce message. | |
4083 | (let ((codestring | |
4084 | (concat "\n--" | |
4085 | (substring content-type (match-beginning 1) | |
4086 | (match-end 1))))) | |
4087 | (unless (re-search-forward mail-mime-unsent-header nil t) | |
4088 | (error "Cannot find beginning of header in failed message")) | |
4089 | (unless (search-forward "\n\n" nil t) | |
4090 | (error "Cannot find start of Mime data in failed message")) | |
4091 | (setq bounce-start (point)) | |
4092 | (if (search-forward codestring nil t) | |
4093 | (setq bounce-end (match-beginning 0)) | |
4094 | (setq bounce-end (point-max)))) | |
4095 | ;; Non-MIME bounce. | |
4096 | (or (re-search-forward mail-unsent-separator nil t) | |
4097 | (error "Cannot parse this as a failure message")) | |
4098 | (skip-chars-forward "\n") | |
4099 | ;; Support a style of failure message in which the original | |
4100 | ;; message is indented, and included within lines saying | |
4101 | ;; `Start of returned message' and `End of returned message'. | |
4102 | (if (looking-at " +Received:") | |
4103 | (progn | |
4104 | (setq bounce-start (point)) | |
4105 | (skip-chars-forward " ") | |
4106 | (setq bounce-indent (- (current-column))) | |
4107 | (goto-char (point-max)) | |
4108 | (re-search-backward "^End of returned message$" nil t) | |
4109 | (setq bounce-end (point))) | |
4110 | ;; One message contained a few random lines before | |
4111 | ;; the old message header. The first line of the | |
4112 | ;; message started with two hyphens. A blank line | |
4113 | ;; followed these random lines. The same line | |
4114 | ;; beginning with two hyphens was possibly marking | |
4115 | ;; the end of the message. | |
4116 | (if (looking-at "^--") | |
4117 | (let ((boundary (buffer-substring-no-properties | |
4118 | (point) | |
4119 | (progn (end-of-line) (point))))) | |
4120 | (search-forward "\n\n") | |
4121 | (skip-chars-forward "\n") | |
4122 | (setq bounce-start (point)) | |
4123 | (goto-char (point-max)) | |
4124 | (search-backward (concat "\n\n" boundary) bounce-start t) | |
4125 | (setq bounce-end (point))) | |
4126 | (setq bounce-start (point) | |
4127 | bounce-end (point-max))) | |
4128 | (unless (search-forward "\n\n" nil t) | |
4129 | (error "Cannot find end of header in failed message")))))) | |
4130 | ;; We have found the message that bounced, within the current message. | |
4131 | ;; Now start sending new message; default header fields from original. | |
4132 | ;; Turn off the usual actions for initializing the message body | |
4133 | ;; because we want to get only the text from the failure message. | |
4134 | (let (mail-signature mail-setup-hook) | |
4135 | (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer | |
4136 | (list (list 'rmail-mark-message | |
4137 | rmail-this-buffer | |
4138 | (aref rmail-msgref-vector msgnum) | |
4139 | rmail-retried-attr-index))) | |
4140 | ;; Insert original text as initial text of new draft message. | |
4141 | ;; Bind inhibit-read-only since the header delimiter | |
4142 | ;; of the previous message was probably read-only. | |
58d1ac6d GM |
4143 | (let ((inhibit-read-only t) |
4144 | eoh) | |
537ab246 BG |
4145 | (erase-buffer) |
4146 | (insert-buffer-substring rmail-this-buffer | |
4147 | bounce-start bounce-end) | |
4148 | (goto-char (point-min)) | |
4149 | (if bounce-indent | |
4150 | (indent-rigidly (point-min) (point-max) bounce-indent)) | |
58d1ac6d GM |
4151 | (rfc822-goto-eoh) |
4152 | (setq eoh (point)) | |
4153 | (insert mail-header-separator) | |
537ab246 | 4154 | (save-restriction |
58d1ac6d | 4155 | (narrow-to-region (point-min) eoh) |
37aca7a8 GM |
4156 | (rmail-delete-headers rmail-retry-ignored-headers) |
4157 | (rmail-delete-headers "^\\(sender\\|return-path\\|received\\):") | |
537ab246 BG |
4158 | (setq resending (mail-fetch-field "resent-to")) |
4159 | (if mail-self-blind | |
4160 | (if resending | |
4161 | (insert "Resent-Bcc: " (user-login-name) "\n") | |
4162 | (insert "BCC: " (user-login-name) "\n")))) | |
4163 | (goto-char (point-min)) | |
4164 | (mail-position-on-field (if resending "Resent-To" "To") t)))))) | |
4165 | \f | |
4166 | (defun rmail-summary-exists () | |
4167 | "Non-nil if in an RMAIL buffer and an associated summary buffer exists. | |
4168 | In fact, the non-nil value returned is the summary buffer itself." | |
4169 | (and rmail-summary-buffer (buffer-name rmail-summary-buffer) | |
4170 | rmail-summary-buffer)) | |
4171 | ||
4172 | (defun rmail-summary-displayed () | |
4173 | "t if in RMAIL buffer and an associated summary buffer is displayed." | |
4174 | (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer))) | |
4175 | ||
4176 | (defcustom rmail-redisplay-summary nil | |
778ef2ef | 4177 | "Non-nil means Rmail should show the summary when it changes. |
537ab246 BG |
4178 | This has an effect only if a summary buffer exists." |
4179 | :type 'boolean | |
4180 | :group 'rmail-summary) | |
4181 | ||
4182 | (defcustom rmail-summary-window-size nil | |
778ef2ef | 4183 | "Non-nil means specify the height for an Rmail summary window." |
537ab246 BG |
4184 | :type '(choice (const :tag "Disabled" nil) integer) |
4185 | :group 'rmail-summary) | |
4186 | ||
4187 | ;; Put the summary buffer back on the screen, if user wants that. | |
4188 | (defun rmail-maybe-display-summary () | |
4189 | (let ((selected (selected-window)) | |
4190 | window) | |
4191 | ;; If requested, make sure the summary is displayed. | |
4192 | (and rmail-summary-buffer (buffer-name rmail-summary-buffer) | |
4193 | rmail-redisplay-summary | |
4194 | (if (get-buffer-window rmail-summary-buffer 0) | |
4195 | ;; It's already in some frame; show that one. | |
4196 | (let ((frame (window-frame | |
4197 | (get-buffer-window rmail-summary-buffer 0)))) | |
4198 | (make-frame-visible frame) | |
4199 | (raise-frame frame)) | |
4200 | (display-buffer rmail-summary-buffer))) | |
4201 | ;; If requested, set the height of the summary window. | |
4202 | (and rmail-summary-buffer (buffer-name rmail-summary-buffer) | |
4203 | rmail-summary-window-size | |
4204 | (setq window (get-buffer-window rmail-summary-buffer)) | |
4205 | ;; Don't try to change the size if just one window in frame. | |
4206 | (not (eq window (frame-root-window (window-frame window)))) | |
4207 | (unwind-protect | |
4208 | (progn | |
4209 | (select-window window) | |
4210 | (enlarge-window (- rmail-summary-window-size (window-height)))) | |
4211 | (select-window selected))))) | |
4212 | \f | |
4213 | ;;;; *** Rmail Local Fontification *** | |
4214 | ||
4215 | (defun rmail-fontify-buffer-function () | |
4216 | ;; This function's symbol is bound to font-lock-fontify-buffer-function. | |
4217 | (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t) | |
4218 | ;; If we're already showing a message, fontify it now. | |
4219 | (if rmail-current-message (rmail-fontify-message)) | |
4220 | ;; Prevent Font Lock mode from kicking in. | |
4221 | (setq font-lock-fontified t)) | |
4222 | ||
4223 | (defun rmail-unfontify-buffer-function () | |
4224 | ;; This function's symbol is bound to font-lock-fontify-unbuffer-function. | |
4225 | (let ((modified (buffer-modified-p)) | |
4226 | (buffer-undo-list t) (inhibit-read-only t) | |
4227 | before-change-functions after-change-functions | |
4228 | buffer-file-name buffer-file-truename) | |
4229 | (save-restriction | |
4230 | (widen) | |
4231 | (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t) | |
4232 | (remove-text-properties (point-min) (point-max) '(rmail-fontified nil)) | |
4233 | (font-lock-default-unfontify-buffer) | |
2dc00ad0 SM |
4234 | (and (not modified) (buffer-modified-p) |
4235 | (restore-buffer-modified-p nil))))) | |
537ab246 BG |
4236 | |
4237 | (defun rmail-fontify-message () | |
4238 | ;; Fontify the current message if it is not already fontified. | |
4239 | (if (text-property-any (point-min) (point-max) 'rmail-fontified nil) | |
4240 | (let ((modified (buffer-modified-p)) | |
4241 | (buffer-undo-list t) (inhibit-read-only t) | |
4242 | before-change-functions after-change-functions | |
4243 | buffer-file-name buffer-file-truename) | |
4244 | (save-excursion | |
4245 | (save-match-data | |
4246 | (add-text-properties (point-min) (point-max) '(rmail-fontified t)) | |
4247 | (font-lock-fontify-region (point-min) (point-max)) | |
2dc00ad0 SM |
4248 | (and (not modified) (buffer-modified-p) |
4249 | (restore-buffer-modified-p nil))))))) | |
537ab246 BG |
4250 | \f |
4251 | ;;; Speedbar support for RMAIL files. | |
4252 | (eval-when-compile (require 'speedbar)) | |
4253 | ||
78f3273a CY |
4254 | (defcustom rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$" |
4255 | "Regexp matching Rmail folder names to be displayed in Speedbar. | |
4256 | Enabling this permits Speedbar to display your folders for easy | |
4257 | browsing, and moving of messages." | |
4258 | :type 'regexp | |
4259 | :group 'rmail | |
4260 | :group 'speedbar) | |
537ab246 BG |
4261 | |
4262 | (defvar rmail-speedbar-last-user nil | |
4263 | "The last user to be displayed in the speedbar.") | |
4264 | ||
4265 | (defvar rmail-speedbar-key-map nil | |
4266 | "Keymap used when in rmail display mode.") | |
4267 | ||
4268 | (defun rmail-install-speedbar-variables () | |
4269 | "Install those variables used by speedbar to enhance rmail." | |
4270 | (if rmail-speedbar-key-map | |
4271 | nil | |
4272 | (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap)) | |
4273 | ||
4274 | (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line) | |
4275 | (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line) | |
4276 | (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line) | |
4277 | (define-key rmail-speedbar-key-map "M" | |
4278 | 'rmail-speedbar-move-message-to-folder-on-line))) | |
4279 | ||
bd26454c | 4280 | ;; Mouse-3. |
537ab246 BG |
4281 | (defvar rmail-speedbar-menu-items |
4282 | '(["Read Folder" speedbar-edit-line t] | |
4283 | ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line | |
4284 | (save-excursion (beginning-of-line) | |
4285 | (looking-at "<M> "))]) | |
4286 | "Additional menu-items to add to speedbar frame.") | |
4287 | ||
4288 | ;; Make sure our special speedbar major mode is loaded | |
4289 | (if (featurep 'speedbar) | |
4290 | (rmail-install-speedbar-variables) | |
4291 | (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables)) | |
4292 | ||
4293 | (defun rmail-speedbar-buttons (buffer) | |
4294 | "Create buttons for BUFFER containing rmail messages. | |
4295 | Click on the address under Reply to: to reply to this person. | |
4296 | Under Folders: Click a name to read it, or on the <M> to move the | |
4297 | current message into that RMAIL folder." | |
4298 | (let ((from nil)) | |
2dc00ad0 | 4299 | (with-current-buffer buffer |
537ab246 BG |
4300 | (goto-char (point-min)) |
4301 | (if (not (re-search-forward "^Reply-To: " nil t)) | |
4302 | (if (not (re-search-forward "^From:? " nil t)) | |
4303 | (setq from t))) | |
4304 | (if from | |
4305 | nil | |
4ddca2c5 | 4306 | (setq from (buffer-substring (point) (line-end-position))))) |
537ab246 BG |
4307 | (goto-char (point-min)) |
4308 | (if (and (looking-at "Reply to:") | |
4309 | (equal from rmail-speedbar-last-user)) | |
4310 | nil | |
4311 | (setq rmail-speedbar-last-user from) | |
4312 | (erase-buffer) | |
4313 | (insert "Reply To:\n") | |
4314 | (if (stringp from) | |
4315 | (speedbar-insert-button from 'speedbar-directory-face 'highlight | |
4316 | 'rmail-speedbar-button 'rmail-reply)) | |
4317 | (insert "Folders:\n") | |
4318 | (let* ((case-fold-search nil) | |
2dc00ad0 SM |
4319 | (df (directory-files (with-current-buffer buffer |
4320 | default-directory) | |
537ab246 | 4321 | nil rmail-speedbar-match-folder-regexp))) |
788c1fc9 GM |
4322 | (dolist (file df) |
4323 | (when (file-regular-p file) | |
4324 | (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight | |
4325 | 'rmail-speedbar-move-message file) | |
4326 | (speedbar-insert-button file 'speedbar-file-face 'highlight | |
4327 | 'rmail-speedbar-find-file nil t))))))) | |
537ab246 BG |
4328 | |
4329 | (defun rmail-speedbar-button (text token indent) | |
4330 | "Execute an rmail command specified by TEXT. | |
4331 | The command used is TOKEN. INDENT is not used." | |
4332 | (speedbar-with-attached-buffer | |
4333 | (funcall token t))) | |
4334 | ||
4335 | (defun rmail-speedbar-find-file (text token indent) | |
4336 | "Load in the rmail file TEXT. | |
4337 | TOKEN and INDENT are not used." | |
4338 | (speedbar-with-attached-buffer | |
4339 | (message "Loading in RMAIL file %s..." text) | |
788c1fc9 | 4340 | (rmail text))) |
537ab246 BG |
4341 | |
4342 | (defun rmail-speedbar-move-message-to-folder-on-line () | |
4343 | "If the current line is a folder, move current message to it." | |
4344 | (interactive) | |
4345 | (save-excursion | |
4346 | (beginning-of-line) | |
4ddca2c5 | 4347 | (if (re-search-forward "<M> " (line-end-position) t) |
537ab246 BG |
4348 | (progn |
4349 | (forward-char -2) | |
4350 | (speedbar-do-function-pointer))))) | |
4351 | ||
4352 | (defun rmail-speedbar-move-message (text token indent) | |
4353 | "From button TEXT, copy current message to the rmail file specified by TOKEN. | |
4354 | TEXT and INDENT are not used." | |
4355 | (speedbar-with-attached-buffer | |
4356 | (message "Moving message to %s" token) | |
592465ca GM |
4357 | ;; expand-file-name is needed due to the unhelpful way in which |
4358 | ;; rmail-output expands non-absolute filenames against rmail-default-file. | |
4359 | ;; What is the point of that, anyway? | |
4360 | (rmail-output (expand-file-name token)))) | |
177549d0 | 4361 | \f |
788c1fc9 GM |
4362 | ;; Functions for setting, getting and encoding the POP password. |
4363 | ;; The password is encoded to prevent it from being easily accessible | |
4364 | ;; to "prying eyes." Obviously, this encoding isn't "real security," | |
4365 | ;; nor is it meant to be. | |
537ab246 BG |
4366 | |
4367 | ;;;###autoload | |
4368 | (defun rmail-set-remote-password (password) | |
4369 | "Set PASSWORD to be used for retrieving mail from a POP or IMAP server." | |
4370 | (interactive "sPassword: ") | |
4371 | (if password | |
4372 | (setq rmail-encoded-remote-password | |
4373 | (rmail-encode-string password (emacs-pid))) | |
4374 | (setq rmail-remote-password nil) | |
4375 | (setq rmail-encoded-remote-password nil))) | |
4376 | ||
4377 | (defun rmail-get-remote-password (imap) | |
4378 | "Get the password for retrieving mail from a POP or IMAP server. If none | |
4379 | has been set, then prompt the user for one." | |
4380 | (when (not rmail-encoded-remote-password) | |
4381 | (if (not rmail-remote-password) | |
4382 | (setq rmail-remote-password | |
4383 | (read-passwd (if imap | |
4384 | "IMAP password: " | |
4385 | "POP password: ")))) | |
4386 | (rmail-set-remote-password rmail-remote-password) | |
4387 | (setq rmail-remote-password nil)) | |
4388 | (rmail-encode-string rmail-encoded-remote-password (emacs-pid))) | |
4389 | ||
4390 | (defun rmail-have-password () | |
4391 | (or rmail-remote-password rmail-encoded-remote-password)) | |
4392 | ||
4393 | (defun rmail-encode-string (string mask) | |
4394 | "Encode STRING with integer MASK, by taking the exclusive OR of the | |
4395 | lowest byte in the mask with the first character of string, the | |
4396 | second-lowest-byte with the second character of the string, etc., | |
4397 | restarting at the lowest byte of the mask whenever it runs out. | |
4398 | Returns the encoded string. Calling the function again with an | |
4399 | encoded string (and the same mask) will decode the string." | |
4400 | (setq mask (abs mask)) ; doesn't work if negative | |
4401 | (let* ((string-vector (string-to-vector string)) (i 0) | |
4402 | (len (length string-vector)) (curmask mask) charmask) | |
4403 | (while (< i len) | |
4404 | (if (= curmask 0) | |
4405 | (setq curmask mask)) | |
4406 | (setq charmask (% curmask 256)) | |
4407 | (setq curmask (lsh curmask -8)) | |
4408 | (aset string-vector i (logxor charmask (aref string-vector i))) | |
4409 | (setq i (1+ i))) | |
4410 | (concat string-vector))) | |
4411 | ||
a7686350 GM |
4412 | ;; Should this have a key-binding, or be in a menu? |
4413 | ;; There doesn't really seem to be an appropriate menu. | |
4414 | ;; Eg the edit command is not in a menu either. | |
177549d0 RS |
4415 | (defun rmail-epa-decrypt () |
4416 | "Decrypt OpenPGP armors in current message." | |
4417 | (interactive) | |
4418 | ||
4419 | ;; Save the current buffer here for cleanliness, in case we | |
4420 | ;; change it in one of the calls to `epa-decrypt-region'. | |
4421 | ||
4422 | (save-excursion | |
d65e4c15 | 4423 | (let (decrypts) |
177549d0 RS |
4424 | (goto-char (point-min)) |
4425 | ||
4426 | ;; In case the encrypted data is inside a mime attachment, | |
4427 | ;; show it. This is a kludge; to be clean, it should not | |
4428 | ;; modify the buffer, but I don't see how to do that. | |
4429 | (when (search-forward "octet-stream" nil t) | |
4430 | (beginning-of-line) | |
4431 | (forward-button 1) | |
4432 | (if (looking-at "Show") | |
4433 | (rmail-mime-toggle-hidden))) | |
4434 | ||
4435 | ;; Now find all armored messages in the buffer | |
4436 | ;; and decrypt them one by one. | |
4437 | (goto-char (point-min)) | |
4438 | (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t) | |
d65e4c15 RS |
4439 | (let ((coding-system-for-read coding-system-for-read) |
4440 | armor-start armor-end after-end) | |
177549d0 RS |
4441 | (setq armor-start (match-beginning 0) |
4442 | armor-end (re-search-forward "^-----END PGP MESSAGE-----$" | |
4443 | nil t)) | |
4444 | (unless armor-end | |
4445 | (error "Encryption armor beginning has no matching end")) | |
4446 | (goto-char armor-start) | |
4447 | ||
4448 | ;; Because epa--find-coding-system-for-mime-charset not autoloaded. | |
4449 | (require 'epa) | |
4450 | ||
4451 | ;; Use the charset specified in the armor. | |
4452 | (unless coding-system-for-read | |
4453 | (if (re-search-forward "^Charset: \\(.*\\)" armor-end t) | |
4454 | (setq coding-system-for-read | |
4455 | (epa--find-coding-system-for-mime-charset | |
4456 | (intern (downcase (match-string 1))))))) | |
4457 | ||
4458 | ;; Advance over this armor. | |
4459 | (goto-char armor-end) | |
d65e4c15 | 4460 | (setq after-end (- (point-max) armor-end)) |
177549d0 RS |
4461 | |
4462 | ;; Decrypt it, maybe in place, maybe making new buffer. | |
4463 | (epa-decrypt-region | |
4464 | armor-start armor-end | |
4465 | ;; Call back this function to prepare the output. | |
4466 | (lambda () | |
d65e4c15 RS |
4467 | (let ((inhibit-read-only t)) |
4468 | (delete-region armor-start armor-end) | |
4469 | (goto-char armor-start) | |
4470 | (current-buffer)))) | |
4471 | ||
4472 | (push (list armor-start (- (point-max) after-end)) | |
4473 | decrypts))) | |
4474 | ||
4475 | (when (and decrypts (rmail-buffers-swapped-p)) | |
4476 | (when (y-or-n-p "Replace the original message? ") | |
4477 | (setq decrypts (nreverse decrypts)) | |
4478 | (let ((beg (rmail-msgbeg rmail-current-message)) | |
4479 | (end (rmail-msgend rmail-current-message)) | |
4480 | (from-buffer (current-buffer))) | |
4481 | (with-current-buffer rmail-view-buffer | |
4482 | (narrow-to-region beg end) | |
4483 | (goto-char (point-min)) | |
4484 | (dolist (d decrypts) | |
4485 | (if (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t) | |
4486 | (let (armor-start armor-end) | |
4487 | (setq armor-start (match-beginning 0) | |
4488 | armor-end (re-search-forward "^-----END PGP MESSAGE-----$" | |
4489 | nil t)) | |
4490 | (when armor-end | |
4491 | (delete-region armor-start armor-end) | |
4492 | (insert-buffer-substring from-buffer (nth 0 d) (nth 1 d))))))))))))) | |
177549d0 | 4493 | \f |
537ab246 BG |
4494 | ;;;; Desktop support |
4495 | ||
4496 | (defun rmail-restore-desktop-buffer (desktop-buffer-file-name | |
8e7a0f83 GM |
4497 | desktop-buffer-name |
4498 | desktop-buffer-misc) | |
537ab246 BG |
4499 | "Restore an rmail buffer specified in a desktop file." |
4500 | (condition-case error | |
4501 | (progn | |
8e7a0f83 GM |
4502 | (rmail-input desktop-buffer-file-name) |
4503 | (if (eq major-mode 'rmail-mode) | |
4504 | (current-buffer) | |
4505 | rmail-buffer)) | |
537ab246 BG |
4506 | (file-locked |
4507 | (kill-buffer (current-buffer)) | |
4508 | nil))) | |
4509 | ||
4510 | (add-to-list 'desktop-buffer-mode-handlers | |
4511 | '(rmail-mode . rmail-restore-desktop-buffer)) | |
4512 | ||
5e7a9022 EZ |
4513 | ;; We use this to record the encoding of the current message before |
4514 | ;; saving the message collection. | |
4515 | (defvar rmail-message-encoding nil) | |
4516 | ||
537ab246 BG |
4517 | ;; Used in `write-region-annotate-functions' to write rmail files. |
4518 | (defun rmail-write-region-annotate (start end) | |
de3bc99a RS |
4519 | (when (and (null start) rmail-buffer-swapped) |
4520 | (unless (buffer-live-p rmail-view-buffer) | |
4521 | (error "Buffer `%s' with real text of `%s' has disappeared" | |
4522 | (buffer-name rmail-view-buffer) | |
4523 | (buffer-name (current-buffer)))) | |
5e7a9022 | 4524 | (setq rmail-message-encoding buffer-file-coding-system) |
537ab246 BG |
4525 | (set-buffer rmail-view-buffer) |
4526 | (widen) | |
4527 | nil)) | |
4528 | ||
5e7a9022 EZ |
4529 | ;; Used to restore the encoding of the buffer where we show the |
4530 | ;; current message, after we save the message collection. This is | |
4531 | ;; needed because rmail-write-region-annotate switches buffers behind | |
4532 | ;; save-file's back, with the side effect that last-coding-system-used | |
4533 | ;; is assigned to buffer-file-coding-system of the wrong buffer. | |
4534 | (defun rmail-after-save-hook () | |
4535 | (if (or (eq rmail-view-buffer (current-buffer)) | |
4536 | (eq rmail-buffer (current-buffer))) | |
4537 | (with-current-buffer | |
4538 | (if (rmail-buffers-swapped-p) rmail-buffer rmail-view-buffer) | |
4539 | (setq buffer-file-coding-system rmail-message-encoding)))) | |
4540 | (add-hook 'after-save-hook 'rmail-after-save-hook) | |
4541 | ||
6c82bad2 GM |
4542 | \f |
4543 | ;;; Start of automatically extracted autoloads. | |
4544 | \f | |
4545 | ;;;### (autoloads (rmail-edit-current-message) "rmailedit" "rmailedit.el" | |
cf3304e1 | 4546 | ;;;;;; "7d558f958574f6003fa474ce2f3c80a8") |
6c82bad2 GM |
4547 | ;;; Generated autoloads from rmailedit.el |
4548 | ||
4549 | (autoload 'rmail-edit-current-message "rmailedit" "\ | |
4550 | Edit the contents of this message. | |
4551 | ||
4552 | \(fn)" t nil) | |
4553 | ||
4554 | ;;;*** | |
4555 | \f | |
4556 | ;;;### (autoloads (rmail-next-labeled-message rmail-previous-labeled-message | |
4557 | ;;;;;; rmail-read-label rmail-kill-label rmail-add-label) "rmailkwd" | |
cf3304e1 | 4558 | ;;;;;; "rmailkwd.el" "4ae5660d86d49e524f4a6bcbc6d9a984") |
6c82bad2 GM |
4559 | ;;; Generated autoloads from rmailkwd.el |
4560 | ||
4561 | (autoload 'rmail-add-label "rmailkwd" "\ | |
4562 | Add LABEL to labels associated with current RMAIL message. | |
4563 | Completes (see `rmail-read-label') over known labels when reading. | |
4564 | LABEL may be a symbol or string. Only one label is allowed. | |
4565 | ||
4566 | \(fn LABEL)" t nil) | |
4567 | ||
4568 | (autoload 'rmail-kill-label "rmailkwd" "\ | |
4569 | Remove LABEL from labels associated with current RMAIL message. | |
4570 | Completes (see `rmail-read-label') over known labels when reading. | |
4571 | LABEL may be a symbol or string. Only one label is allowed. | |
4572 | ||
4573 | \(fn LABEL)" t nil) | |
4574 | ||
4575 | (autoload 'rmail-read-label "rmailkwd" "\ | |
4576 | Read a label with completion, prompting with PROMPT. | |
4577 | Completions are chosen from `rmail-label-obarray'. The default | |
4578 | is `rmail-last-label', if that is non-nil. Updates `rmail-last-label' | |
4579 | according to the choice made, and returns a symbol. | |
4580 | ||
4581 | \(fn PROMPT)" nil nil) | |
4582 | ||
4583 | (autoload 'rmail-previous-labeled-message "rmailkwd" "\ | |
4584 | Show previous message with one of the labels LABELS. | |
4585 | LABELS should be a comma-separated list of label names. | |
4586 | If LABELS is empty, the last set of labels specified is used. | |
4587 | With prefix argument N moves backward N messages with these labels. | |
4588 | ||
4589 | \(fn N LABELS)" t nil) | |
4590 | ||
4591 | (autoload 'rmail-next-labeled-message "rmailkwd" "\ | |
4592 | Show next message with one of the labels LABELS. | |
4593 | LABELS should be a comma-separated list of label names. | |
4594 | If LABELS is empty, the last set of labels specified is used. | |
4595 | With prefix argument N moves forward N messages with these labels. | |
4596 | ||
4597 | \(fn N LABELS)" t nil) | |
4598 | ||
4599 | ;;;*** | |
4600 | \f | |
c7349e19 | 4601 | ;;;### (autoloads (rmail-mime) "rmailmm" "rmailmm.el" "cd7656f82944d0b92b0d093a5f3a4c36") |
6c82bad2 GM |
4602 | ;;; Generated autoloads from rmailmm.el |
4603 | ||
4604 | (autoload 'rmail-mime "rmailmm" "\ | |
ce7ddba0 | 4605 | Toggle the display of a MIME message. |
186f7f0b | 4606 | |
dcecfb4c | 4607 | The actual behavior depends on the value of `rmail-enable-mime'. |
186f7f0b | 4608 | |
ce7ddba0 CY |
4609 | If `rmail-enable-mime' is non-nil (the default), this command toggles |
4610 | the display of a MIME message between decoded presentation form and | |
4611 | raw data. With optional prefix argument ARG, it toggles the display only | |
4612 | of the MIME entity at point, if there is one. The optional argument | |
4613 | STATE forces a particular display state, rather than toggling. | |
4614 | `raw' forces raw mode, any other non-nil value forces decoded mode. | |
4615 | ||
4616 | If `rmail-enable-mime' is nil, this creates a temporary \"*RMAIL*\" | |
4617 | buffer holding a decoded copy of the message. Inline content-types are | |
4618 | handled according to `rmail-mime-media-type-handlers-alist'. | |
4619 | By default, this displays text and multipart messages, and offers to | |
4620 | download attachments as specified by `rmail-mime-attachment-dirs-alist'. | |
4621 | The arguments ARG and STATE have no effect in this case. | |
4622 | ||
4623 | \(fn &optional ARG STATE)" t nil) | |
6c82bad2 GM |
4624 | |
4625 | ;;;*** | |
4626 | \f | |
4627 | ;;;### (autoloads (set-rmail-inbox-list) "rmailmsc" "rmailmsc.el" | |
cf3304e1 | 4628 | ;;;;;; "e2212ea15561d60365ffa1f7a5902939") |
6c82bad2 GM |
4629 | ;;; Generated autoloads from rmailmsc.el |
4630 | ||
4631 | (autoload 'set-rmail-inbox-list "rmailmsc" "\ | |
4632 | Set the inbox list of the current RMAIL file to FILE-NAME. | |
4633 | You can specify one file name, or several names separated by commas. | |
4634 | If FILE-NAME is empty, remove any existing inbox list. | |
4635 | ||
4636 | This applies only to the current session. | |
4637 | ||
4638 | \(fn FILE-NAME)" t nil) | |
4639 | ||
4640 | ;;;*** | |
4641 | \f | |
4642 | ;;;### (autoloads (rmail-sort-by-labels rmail-sort-by-lines rmail-sort-by-correspondent | |
4643 | ;;;;;; rmail-sort-by-recipient rmail-sort-by-author rmail-sort-by-subject | |
cf3304e1 | 4644 | ;;;;;; rmail-sort-by-date) "rmailsort" "rmailsort.el" "38da5f17d4ed0dcd2b09c158642cef63") |
6c82bad2 GM |
4645 | ;;; Generated autoloads from rmailsort.el |
4646 | ||
4647 | (autoload 'rmail-sort-by-date "rmailsort" "\ | |
4648 | Sort messages of current Rmail buffer by \"Date\" header. | |
4649 | If prefix argument REVERSE is non-nil, sorts in reverse order. | |
4650 | ||
4651 | \(fn REVERSE)" t nil) | |
4652 | ||
4653 | (autoload 'rmail-sort-by-subject "rmailsort" "\ | |
4654 | Sort messages of current Rmail buffer by \"Subject\" header. | |
4655 | Ignores any \"Re: \" prefix. If prefix argument REVERSE is | |
4656 | non-nil, sorts in reverse order. | |
4657 | ||
4658 | \(fn REVERSE)" t nil) | |
4659 | ||
4660 | (autoload 'rmail-sort-by-author "rmailsort" "\ | |
4661 | Sort messages of current Rmail buffer by author. | |
4662 | This uses either the \"From\" or \"Sender\" header, downcased. | |
4663 | If prefix argument REVERSE is non-nil, sorts in reverse order. | |
4664 | ||
4665 | \(fn REVERSE)" t nil) | |
4666 | ||
4667 | (autoload 'rmail-sort-by-recipient "rmailsort" "\ | |
4668 | Sort messages of current Rmail buffer by recipient. | |
4669 | This uses either the \"To\" or \"Apparently-To\" header, downcased. | |
4670 | If prefix argument REVERSE is non-nil, sorts in reverse order. | |
4671 | ||
4672 | \(fn REVERSE)" t nil) | |
4673 | ||
4674 | (autoload 'rmail-sort-by-correspondent "rmailsort" "\ | |
4675 | Sort messages of current Rmail buffer by other correspondent. | |
4676 | This uses either the \"From\", \"Sender\", \"To\", or | |
4677 | \"Apparently-To\" header, downcased. Uses the first header not | |
38a71655 | 4678 | excluded by `mail-dont-reply-to-names'. If prefix argument |
6c82bad2 GM |
4679 | REVERSE is non-nil, sorts in reverse order. |
4680 | ||
4681 | \(fn REVERSE)" t nil) | |
4682 | ||
4683 | (autoload 'rmail-sort-by-lines "rmailsort" "\ | |
4684 | Sort messages of current Rmail buffer by the number of lines. | |
4685 | If prefix argument REVERSE is non-nil, sorts in reverse order. | |
4686 | ||
4687 | \(fn REVERSE)" t nil) | |
4688 | ||
4689 | (autoload 'rmail-sort-by-labels "rmailsort" "\ | |
4690 | Sort messages of current Rmail buffer by labels. | |
4691 | LABELS is a comma-separated list of labels. The order of these | |
4692 | labels specifies the order of messages: messages with the first | |
4693 | label come first, messages with the second label come second, and | |
4694 | so on. Messages that have none of these labels come last. | |
4695 | If prefix argument REVERSE is non-nil, sorts in reverse order. | |
4696 | ||
4697 | \(fn REVERSE LABELS)" t nil) | |
4698 | ||
4699 | ;;;*** | |
4700 | \f | |
4701 | ;;;### (autoloads (rmail-summary-by-senders rmail-summary-by-topic | |
4702 | ;;;;;; rmail-summary-by-regexp rmail-summary-by-recipients rmail-summary-by-labels | |
cf3304e1 | 4703 | ;;;;;; rmail-summary) "rmailsum" "rmailsum.el" "bef21a376bd5bd59792a20dd86e6ec34") |
6c82bad2 GM |
4704 | ;;; Generated autoloads from rmailsum.el |
4705 | ||
4706 | (autoload 'rmail-summary "rmailsum" "\ | |
4707 | Display a summary of all messages, one line per message. | |
4708 | ||
4709 | \(fn)" t nil) | |
4710 | ||
4711 | (autoload 'rmail-summary-by-labels "rmailsum" "\ | |
4712 | Display a summary of all messages with one or more LABELS. | |
4713 | LABELS should be a string containing the desired labels, separated by commas. | |
4714 | ||
4715 | \(fn LABELS)" t nil) | |
4716 | ||
4717 | (autoload 'rmail-summary-by-recipients "rmailsum" "\ | |
4718 | Display a summary of all messages with the given RECIPIENTS. | |
4719 | Normally checks the To, From and Cc fields of headers; | |
4720 | but if PRIMARY-ONLY is non-nil (prefix arg given), | |
4721 | only look in the To and From fields. | |
4722 | RECIPIENTS is a string of regexps separated by commas. | |
4723 | ||
4724 | \(fn RECIPIENTS &optional PRIMARY-ONLY)" t nil) | |
4725 | ||
4726 | (autoload 'rmail-summary-by-regexp "rmailsum" "\ | |
4727 | Display a summary of all messages according to regexp REGEXP. | |
4728 | If the regular expression is found in the header of the message | |
4729 | \(including in the date and other lines, as well as the subject line), | |
4730 | Emacs will list the message in the summary. | |
4731 | ||
4732 | \(fn REGEXP)" t nil) | |
4733 | ||
4734 | (autoload 'rmail-summary-by-topic "rmailsum" "\ | |
4735 | Display a summary of all messages with the given SUBJECT. | |
4736 | Normally checks just the Subject field of headers; but with prefix | |
4737 | argument WHOLE-MESSAGE is non-nil, looks in the whole message. | |
4738 | SUBJECT is a string of regexps separated by commas. | |
4739 | ||
4740 | \(fn SUBJECT &optional WHOLE-MESSAGE)" t nil) | |
4741 | ||
4742 | (autoload 'rmail-summary-by-senders "rmailsum" "\ | |
4743 | Display a summary of all messages whose \"From\" field matches SENDERS. | |
4744 | SENDERS is a string of regexps separated by commas. | |
4745 | ||
4746 | \(fn SENDERS)" t nil) | |
4747 | ||
4748 | ;;;*** | |
4749 | \f | |
4750 | ;;;### (autoloads (unforward-rmail-message undigestify-rmail-message) | |
fb7ada5f | 4751 | ;;;;;; "undigest" "undigest.el" "9f270a2571bbbbfabc27498a8d4089c7") |
6c82bad2 GM |
4752 | ;;; Generated autoloads from undigest.el |
4753 | ||
4754 | (autoload 'undigestify-rmail-message "undigest" "\ | |
4755 | Break up a digest message into its constituent messages. | |
4756 | Leaves original message, deleted, before the undigestified messages. | |
4757 | ||
4758 | \(fn)" t nil) | |
4759 | ||
4760 | (autoload 'unforward-rmail-message "undigest" "\ | |
4761 | Extract a forwarded message from the containing message. | |
20af2394 CY |
4762 | This puts the forwarded message into a separate rmail message following |
4763 | the containing message. This command is only useful when messages are | |
4764 | forwarded with `rmail-enable-mime-composing' set to nil. | |
6c82bad2 GM |
4765 | |
4766 | \(fn)" t nil) | |
4767 | ||
4768 | ;;;*** | |
4769 | \f | |
4770 | ;;; End of automatically extracted autoloads. | |
4771 | ||
4772 | ||
537ab246 BG |
4773 | (provide 'rmail) |
4774 | ||
537ab246 | 4775 | ;;; rmail.el ends here |