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