]> code.delx.au - gnu-emacs/blob - lisp/mail/rmail.el
Update copyright year to 2014 by running admin/update-copyright.
[gnu-emacs] / lisp / mail / rmail.el
1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs
2
3 ;; Copyright (C) 1985-1988, 1993-1998, 2000-2014 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 [?\S-\ ] 'scroll-down-command)
1089 (define-key map "\177" 'scroll-down-command)
1090 (define-key map "?" 'describe-mode)
1091 (define-key map "\C-c\C-s\C-d" 'rmail-sort-by-date)
1092 (define-key map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
1093 (define-key map "\C-c\C-s\C-a" 'rmail-sort-by-author)
1094 (define-key map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
1095 (define-key map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
1096 (define-key map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
1097 (define-key map "\C-c\C-s\C-k" 'rmail-sort-by-labels)
1098 (define-key map "\C-c\C-n" 'rmail-next-same-subject)
1099 (define-key map "\C-c\C-p" 'rmail-previous-same-subject)
1100
1101 \f
1102 (define-key map [menu-bar] (make-sparse-keymap))
1103
1104 (define-key map [menu-bar classify]
1105 (cons "Classify" (make-sparse-keymap "Classify")))
1106
1107 (define-key map [menu-bar classify input-menu]
1108 nil)
1109
1110 (define-key map [menu-bar classify output-menu]
1111 nil)
1112
1113 (define-key map [menu-bar classify output-body]
1114 '("Output body to file..." . rmail-output-body-to-file))
1115
1116 (define-key map [menu-bar classify output-inbox]
1117 '("Output..." . rmail-output))
1118
1119 (define-key map [menu-bar classify output]
1120 '("Output as seen..." . rmail-output-as-seen))
1121
1122 (define-key map [menu-bar classify kill-label]
1123 '("Kill Label..." . rmail-kill-label))
1124
1125 (define-key map [menu-bar classify add-label]
1126 '("Add Label..." . rmail-add-label))
1127
1128 (define-key map [menu-bar summary]
1129 (cons "Summary" (make-sparse-keymap "Summary")))
1130
1131 (define-key map [menu-bar summary senders]
1132 '("By Senders..." . rmail-summary-by-senders))
1133
1134 (define-key map [menu-bar summary labels]
1135 '("By Labels..." . rmail-summary-by-labels))
1136
1137 (define-key map [menu-bar summary recipients]
1138 '("By Recipients..." . rmail-summary-by-recipients))
1139
1140 (define-key map [menu-bar summary topic]
1141 '("By Topic..." . rmail-summary-by-topic))
1142
1143 (define-key map [menu-bar summary regexp]
1144 '("By Regexp..." . rmail-summary-by-regexp))
1145
1146 (define-key map [menu-bar summary all]
1147 '("All" . rmail-summary))
1148
1149 (define-key map [menu-bar mail]
1150 (cons "Mail" (make-sparse-keymap "Mail")))
1151
1152 (define-key map [menu-bar mail rmail-get-new-mail]
1153 '("Get New Mail" . rmail-get-new-mail))
1154
1155 (define-key map [menu-bar mail lambda]
1156 '("----"))
1157
1158 (define-key map [menu-bar mail continue]
1159 '("Continue" . rmail-continue))
1160
1161 (define-key map [menu-bar mail resend]
1162 '(menu-item "Resend..." rmail-resend :keys "C-u f"))
1163
1164 (define-key map [menu-bar mail forward]
1165 '("Forward" . rmail-forward))
1166
1167 (define-key map [menu-bar mail retry]
1168 '("Retry" . rmail-retry-failure))
1169
1170 (define-key map [menu-bar mail reply]
1171 '("Reply" . rmail-reply))
1172
1173 (define-key map [menu-bar mail mail]
1174 '("Mail" . rmail-mail))
1175
1176 (define-key map [menu-bar delete]
1177 (cons "Delete" (make-sparse-keymap "Delete")))
1178
1179 (define-key map [menu-bar delete expunge/save]
1180 '("Expunge/Save" . rmail-expunge-and-save))
1181
1182 (define-key map [menu-bar delete expunge]
1183 '("Expunge" . rmail-expunge))
1184
1185 (define-key map [menu-bar delete undelete]
1186 '("Undelete" . rmail-undelete-previous-message))
1187
1188 (define-key map [menu-bar delete delete]
1189 '("Delete" . rmail-delete-forward))
1190
1191 (define-key map [menu-bar move]
1192 (cons "Move" (make-sparse-keymap "Move")))
1193
1194 (define-key map [menu-bar move search-back]
1195 '("Search Back..." . rmail-search-backwards))
1196
1197 (define-key map [menu-bar move search]
1198 '("Search..." . rmail-search))
1199
1200 (define-key map [menu-bar move previous]
1201 '("Previous Nondeleted" . rmail-previous-undeleted-message))
1202
1203 (define-key map [menu-bar move next]
1204 '("Next Nondeleted" . rmail-next-undeleted-message))
1205
1206 (define-key map [menu-bar move last]
1207 '("Last" . rmail-last-message))
1208
1209 (define-key map [menu-bar move first]
1210 '("First" . rmail-first-message))
1211
1212 (define-key map [menu-bar move previous]
1213 '("Previous" . rmail-previous-message))
1214
1215 (define-key map [menu-bar move next]
1216 '("Next" . rmail-next-message))
1217
1218 map)
1219 "Keymap used in Rmail mode.")
1220
1221 ;; Rmail toolbar
1222 (defvar rmail-tool-bar-map
1223 (let ((map (make-sparse-keymap)))
1224 (tool-bar-local-item-from-menu 'rmail-get-new-mail "mail/inbox"
1225 map rmail-mode-map)
1226 (tool-bar-local-item-from-menu 'rmail-next-undeleted-message "right-arrow"
1227 map rmail-mode-map)
1228 (tool-bar-local-item-from-menu 'rmail-previous-undeleted-message "left-arrow"
1229 map rmail-mode-map)
1230 (tool-bar-local-item-from-menu 'rmail-search "search"
1231 map rmail-mode-map)
1232 (tool-bar-local-item-from-menu 'rmail-input "open"
1233 map rmail-mode-map)
1234 (tool-bar-local-item-from-menu 'rmail-mail "mail/compose"
1235 map rmail-mode-map)
1236 (tool-bar-local-item-from-menu 'rmail-reply "mail/reply-all"
1237 map rmail-mode-map)
1238 (tool-bar-local-item-from-menu 'rmail-forward "mail/forward"
1239 map rmail-mode-map)
1240 (tool-bar-local-item-from-menu 'rmail-delete-forward "close"
1241 map rmail-mode-map)
1242 (tool-bar-local-item-from-menu 'rmail-output "mail/move"
1243 map rmail-mode-map)
1244 (tool-bar-local-item-from-menu 'rmail-output-body-to-file "mail/save"
1245 map rmail-mode-map)
1246 (tool-bar-local-item-from-menu 'rmail-expunge "delete"
1247 map rmail-mode-map)
1248 map))
1249
1250
1251 \f
1252 ;; Rmail mode is suitable only for specially formatted data.
1253 (put 'rmail-mode 'mode-class 'special)
1254
1255 (defun rmail-mode-kill-summary ()
1256 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
1257
1258 (defvar rmail-enable-multibyte) ; dynamically bound
1259
1260 ;;;###autoload
1261 (defun rmail-mode ()
1262 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
1263 All normal editing commands are turned off.
1264 Instead, these commands are available:
1265
1266 \\[rmail-beginning-of-message] Move point to front of this message.
1267 \\[rmail-end-of-message] Move point to bottom of this message.
1268 \\[scroll-up] Scroll to next screen of this message.
1269 \\[scroll-down] Scroll to previous screen of this message.
1270 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
1271 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
1272 \\[rmail-next-message] Move to Next message whether deleted or not.
1273 \\[rmail-previous-message] Move to Previous message whether deleted or not.
1274 \\[rmail-first-message] Move to the first message in Rmail file.
1275 \\[rmail-last-message] Move to the last message in Rmail file.
1276 \\[rmail-show-message] Jump to message specified by numeric position in file.
1277 \\[rmail-search] Search for string and show message it is found in.
1278 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
1279 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
1280 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
1281 till a deleted message is found.
1282 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
1283 \\[rmail-expunge] Expunge deleted messages.
1284 \\[rmail-expunge-and-save] Expunge and save the file.
1285 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
1286 \\[save-buffer] Save without expunging.
1287 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
1288 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
1289 \\[rmail-continue] Continue composing outgoing message started before.
1290 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
1291 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
1292 \\[rmail-forward] Forward this message to another user.
1293 \\[rmail-output] Output (append) this message to another mail file.
1294 \\[rmail-output-as-seen] Output (append) this message to file as it's displayed.
1295 \\[rmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
1296 \\[rmail-input] Input Rmail file. Run Rmail on that file.
1297 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
1298 \\[rmail-kill-label] Kill label. Remove a label from current message.
1299 \\[rmail-next-labeled-message] Move to Next message with specified label
1300 (label defaults to last one specified).
1301 Standard labels: filed, unseen, answered, forwarded, deleted.
1302 Any other label is present only if you add it with \\[rmail-add-label].
1303 \\[rmail-previous-labeled-message] Move to Previous message with specified label
1304 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
1305 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
1306 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
1307 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
1308 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
1309 \\[rmail-toggle-header] Toggle display of complete header."
1310 (interactive)
1311 (let ((finding-rmail-file (not (eq major-mode 'rmail-mode))))
1312 (rmail-mode-2)
1313 (when (and finding-rmail-file
1314 (null coding-system-for-read)
1315 (default-value 'enable-multibyte-characters))
1316 (let ((rmail-enable-multibyte t))
1317 (rmail-require-mime-maybe)
1318 (rmail-convert-file-maybe)
1319 (goto-char (point-max))
1320 (set-buffer-multibyte t)))
1321 (rmail-set-message-counters)
1322 (rmail-show-message rmail-total-messages)
1323 (when finding-rmail-file
1324 (when rmail-display-summary
1325 (rmail-summary))
1326 (rmail-construct-io-menu))
1327 (run-mode-hooks 'rmail-mode-hook)))
1328
1329 (defun rmail-mode-2 ()
1330 (kill-all-local-variables)
1331 (rmail-mode-1)
1332 (rmail-perm-variables)
1333 (rmail-variables))
1334
1335 (defun rmail-mode-1 ()
1336 (setq major-mode 'rmail-mode)
1337 (setq mode-name "RMAIL")
1338 (setq buffer-read-only t)
1339 ;; No need to auto save RMAIL files in normal circumstances
1340 ;; because they contain no info except attribute changes
1341 ;; and deletion of messages.
1342 ;; The one exception is when messages are copied into another mbox buffer.
1343 ;; rmail-output enables auto save when you do that.
1344 (setq buffer-auto-save-file-name nil)
1345 (use-local-map rmail-mode-map)
1346 (set-syntax-table text-mode-syntax-table)
1347 (setq local-abbrev-table text-mode-abbrev-table)
1348 ;; Functions to support buffer swapping:
1349 (add-hook 'write-region-annotate-functions
1350 'rmail-write-region-annotate nil t)
1351 (add-hook 'kill-buffer-hook 'rmail-mode-kill-buffer-hook nil t)
1352 (add-hook 'change-major-mode-hook 'rmail-change-major-mode-hook nil t))
1353
1354 (defun rmail-generate-viewer-buffer ()
1355 "Return a reusable buffer suitable for viewing messages.
1356 Create the buffer if necessary."
1357 ;; We want to reuse any existing view buffer, so as not to create an
1358 ;; endless number of them. But we must avoid clashes if we visit
1359 ;; two different rmail files with the same basename (Bug#4593).
1360 (if (and (local-variable-p 'rmail-view-buffer)
1361 (buffer-live-p rmail-view-buffer))
1362 rmail-view-buffer
1363 (let ((newbuf
1364 (generate-new-buffer
1365 (format " *message-viewer %s*"
1366 (file-name-nondirectory
1367 (or buffer-file-name (buffer-name)))))))
1368 (with-current-buffer newbuf
1369 (add-hook 'kill-buffer-hook 'rmail-view-buffer-kill-buffer-hook nil t))
1370 newbuf)))
1371
1372 (defun rmail-swap-buffers ()
1373 "Swap text between current buffer and `rmail-view-buffer'.
1374 This function preserves the current buffer's modified flag, and also
1375 sets the current buffer's `buffer-file-coding-system' to that of
1376 `rmail-view-buffer'."
1377 (let ((modp-this (buffer-modified-p))
1378 (modp-that
1379 (with-current-buffer rmail-view-buffer (buffer-modified-p)))
1380 (coding-this buffer-file-coding-system)
1381 (coding-that
1382 (with-current-buffer rmail-view-buffer
1383 buffer-file-coding-system)))
1384 (buffer-swap-text rmail-view-buffer)
1385 (setq buffer-file-coding-system coding-that)
1386 (with-current-buffer rmail-view-buffer
1387 (setq buffer-file-coding-system coding-this)
1388 (restore-buffer-modified-p modp-that))
1389 (restore-buffer-modified-p modp-this)))
1390
1391 (defun rmail-buffers-swapped-p ()
1392 "Return non-nil if the message collection is in `rmail-view-buffer'."
1393 ;; This is analogous to tar-data-swapped-p in tar-mode.el.
1394 rmail-buffer-swapped)
1395
1396 (defun rmail-change-major-mode-hook ()
1397 ;; Bring the actual Rmail messages back into the main buffer.
1398 (when (rmail-buffers-swapped-p)
1399 (rmail-swap-buffers)
1400 (setq rmail-buffer-swapped nil)))
1401
1402 (defun rmail-swap-buffers-maybe ()
1403 "Determine if the Rmail buffer is showing a message.
1404 If so restore the actual mbox message collection."
1405 (with-current-buffer rmail-buffer
1406 (when (rmail-buffers-swapped-p)
1407 (rmail-swap-buffers)
1408 (setq rmail-buffer-swapped nil))))
1409
1410 (defun rmail-modify-format ()
1411 "Warn if important modifications would change Rmail file's format."
1412 (with-current-buffer rmail-buffer
1413 (and rmail-was-converted
1414 ;; If it's already modified, don't warn again.
1415 (not rmail-seriously-modified)
1416 (not
1417 (yes-or-no-p
1418 (message "After this, %s would be saved in mbox format. Proceed? "
1419 (buffer-name))))
1420 (error "Aborted"))
1421 (setq rmail-seriously-modified t)))
1422
1423 (defun rmail-dont-modify-format ()
1424 (when (and rmail-was-converted (not rmail-seriously-modified))
1425 (set-buffer-modified-p nil)
1426 (message "Marking buffer unmodified to avoid rewriting Babyl file as mbox file")))
1427
1428 (defun rmail-mode-kill-buffer-hook ()
1429 ;; Turn off the hook on the view buffer, so we can kill it, then kill it.
1430 (if (buffer-live-p rmail-view-buffer)
1431 (with-current-buffer rmail-view-buffer
1432 (setq kill-buffer-hook nil)
1433 (kill-buffer rmail-view-buffer))))
1434
1435 (defun rmail-view-buffer-kill-buffer-hook ()
1436 (error "Can't kill Rmail view buffer `%s' by itself"
1437 (buffer-name (current-buffer))))
1438
1439 ;; Set up the permanent locals associated with an Rmail file.
1440 (defun rmail-perm-variables ()
1441 (make-local-variable 'rmail-last-regexp)
1442 (make-local-variable 'rmail-deleted-vector)
1443 (make-local-variable 'rmail-buffer)
1444 (make-local-variable 'rmail-was-converted)
1445 (setq rmail-was-converted nil)
1446 (make-local-variable 'rmail-seriously-modified)
1447 (setq rmail-seriously-modified nil)
1448 (setq rmail-buffer (current-buffer))
1449 (set-buffer-multibyte nil)
1450 (with-current-buffer (setq rmail-view-buffer (rmail-generate-viewer-buffer))
1451 (setq buffer-undo-list t)
1452 ;; Note that this does not erase the buffer. Should it?
1453 ;; It depends on how this is called. If somehow called with the
1454 ;; rmail buffers swapped, it would erase the message collection.
1455 (set (make-local-variable 'rmail-overlay-list) nil)
1456 (set-buffer-multibyte t)
1457 ;; Force C-x C-s write Unix EOLs.
1458 (set-buffer-file-coding-system 'undecided-unix))
1459 (make-local-variable 'rmail-summary-buffer)
1460 (make-local-variable 'rmail-summary-vector)
1461 (make-local-variable 'rmail-current-message)
1462 (make-local-variable 'rmail-total-messages)
1463 (setq rmail-total-messages 0)
1464 (make-local-variable 'rmail-message-vector)
1465 (make-local-variable 'rmail-msgref-vector)
1466 (make-local-variable 'rmail-inbox-list)
1467 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
1468 (and (null rmail-inbox-list)
1469 (or (equal buffer-file-name (expand-file-name rmail-file-name))
1470 (equal buffer-file-truename
1471 (abbreviate-file-name (file-truename rmail-file-name))))
1472 (setq rmail-inbox-list
1473 (or rmail-primary-inbox-list
1474 (list (or (getenv "MAIL")
1475 ;; FIXME expand-file-name?
1476 (concat rmail-spool-directory
1477 (user-login-name)))))))
1478 (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map))
1479
1480 ;; Set up the non-permanent locals associated with Rmail mode.
1481 (defun rmail-variables ()
1482 ;; Turn off undo. We turn it back on in rmail-edit.
1483 (setq buffer-undo-list t)
1484 ;; Don't let a local variables list in a message cause confusion.
1485 (make-local-variable 'local-enable-local-variables)
1486 (setq local-enable-local-variables nil)
1487 ;; Don't turn off auto-saving based on the size of the buffer
1488 ;; because that code does not understand buffer-swapping.
1489 (make-local-variable 'auto-save-include-big-deletions)
1490 (setq auto-save-include-big-deletions t)
1491 (make-local-variable 'revert-buffer-function)
1492 (setq revert-buffer-function 'rmail-revert)
1493 (make-local-variable 'font-lock-defaults)
1494 (setq font-lock-defaults
1495 '(rmail-font-lock-keywords
1496 t t nil nil
1497 (font-lock-maximum-size . nil)
1498 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
1499 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
1500 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
1501 (make-local-variable 'require-final-newline)
1502 (setq require-final-newline nil)
1503 (make-local-variable 'version-control)
1504 (setq version-control 'never)
1505 (make-local-variable 'kill-buffer-hook)
1506 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
1507 (make-local-variable 'file-precious-flag)
1508 (setq file-precious-flag t)
1509 (make-local-variable 'desktop-save-buffer)
1510 (setq desktop-save-buffer t)
1511 (make-local-variable 'save-buffer-coding-system)
1512 (setq save-buffer-coding-system 'no-conversion)
1513 (setq next-error-move-function 'rmail-next-error-move))
1514 \f
1515 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
1516 (defun rmail-revert (arg noconfirm)
1517 (set-buffer rmail-buffer)
1518 (let* ((revert-buffer-function (default-value 'revert-buffer-function))
1519 (rmail-enable-multibyte enable-multibyte-characters)
1520 ;; See similar code in `rmail'.
1521 ;; FIXME needs updating?
1522 (coding-system-for-read (and rmail-enable-multibyte 'raw-text))
1523 (before-revert-hook 'rmail-swap-buffers-maybe))
1524 ;; Call our caller again, but this time it does the default thing.
1525 (when (revert-buffer arg noconfirm)
1526 ;; If the user said "yes", and we changed something,
1527 ;; reparse the messages.
1528 (set-buffer rmail-buffer)
1529 (rmail-mode-2)
1530 ;; Convert all or part to Babyl file if possible.
1531 (rmail-convert-file-maybe)
1532 ;; We have read the file as raw-text, so the buffer is set to
1533 ;; unibyte. Make it multibyte if necessary.
1534 (if (and rmail-enable-multibyte
1535 (not enable-multibyte-characters))
1536 (set-buffer-multibyte t))
1537 (goto-char (point-max))
1538 (rmail-set-message-counters)
1539 (rmail-show-message rmail-total-messages)
1540 (run-hooks 'rmail-mode-hook))))
1541
1542 (defun rmail-expunge-and-save ()
1543 "Expunge and save RMAIL file."
1544 (interactive)
1545 (set-buffer rmail-buffer)
1546 (rmail-expunge)
1547 ;; No need to swap buffers: rmail-write-region-annotate takes care of it.
1548 ;; (rmail-swap-buffers-maybe)
1549 (save-buffer)
1550 (if (rmail-summary-exists)
1551 (rmail-select-summary (set-buffer-modified-p nil))))
1552
1553 (defun rmail-quit ()
1554 "Quit out of RMAIL.
1555 Hook `rmail-quit-hook' is run after expunging."
1556 (interactive)
1557 (set-buffer rmail-buffer)
1558 (rmail-expunge t)
1559 (save-buffer)
1560 (when (boundp 'rmail-quit-hook)
1561 (run-hooks 'rmail-quit-hook))
1562 ;; Don't switch to the summary buffer even if it was recently visible.
1563 (when rmail-summary-buffer
1564 (with-current-buffer rmail-summary-buffer
1565 (set-buffer-modified-p nil))
1566 (replace-buffer-in-windows rmail-summary-buffer)
1567 (bury-buffer rmail-summary-buffer))
1568 (let ((obuf (current-buffer)))
1569 (quit-window)
1570 (replace-buffer-in-windows obuf)))
1571
1572 (defun rmail-bury ()
1573 "Bury current Rmail buffer and its summary buffer."
1574 (interactive)
1575 ;; This let var was called rmail-buffer, but that interfered
1576 ;; with the buffer-local var used in summary buffers.
1577 (let ((buffer-to-bury (current-buffer)))
1578 (if (rmail-summary-exists)
1579 (let (window)
1580 (while (setq window (get-buffer-window rmail-summary-buffer))
1581 (quit-window nil window))
1582 (bury-buffer rmail-summary-buffer)))
1583 (quit-window)))
1584 \f
1585 (defun rmail-duplicate-message ()
1586 "Create a duplicated copy of the current message.
1587 The duplicate copy goes into the Rmail file just after the original."
1588 ;; If we are in a summary buffer, switch to the Rmail buffer.
1589 ;; FIXME simpler to swap the contents, not the buffers?
1590 (set-buffer rmail-buffer)
1591 (rmail-modify-format)
1592 (let ((buff (current-buffer))
1593 (n rmail-current-message)
1594 (beg (rmail-msgbeg rmail-current-message))
1595 (end (rmail-msgend rmail-current-message)))
1596 (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
1597 (widen)
1598 (let ((buffer-read-only nil)
1599 (string (buffer-substring-no-properties beg end)))
1600 (goto-char end)
1601 (insert string))
1602 (set-buffer buff)
1603 (rmail-swap-buffers-maybe)
1604 (goto-char (point-max))
1605 (rmail-set-message-counters)
1606 (set-buffer-modified-p t)
1607 (rmail-show-message-1 n))
1608 (if (rmail-summary-exists)
1609 (rmail-select-summary (rmail-update-summary)))
1610 (message "Message duplicated"))
1611 \f
1612 ;;;###autoload
1613 (defun rmail-input (filename)
1614 "Run Rmail on file FILENAME."
1615 (interactive "FRun rmail on RMAIL file: ")
1616 (rmail filename))
1617
1618 ;; This used to scan subdirectories recursively, but someone pointed out
1619 ;; that if the user wants that, person can put all the files in one dir.
1620 ;; And the recursive scan was slow. So I took it out.
1621 ;; rms, Sep 1996.
1622 (defun rmail-find-all-files (start)
1623 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
1624 (if (file-accessible-directory-p start)
1625 ;; Don't sort here.
1626 (let* ((case-fold-search t)
1627 (files (directory-files start t rmail-secondary-file-regexp)))
1628 ;; Sort here instead of in directory-files
1629 ;; because this list is usually much shorter.
1630 (sort files 'string<))))
1631
1632 (defun rmail-list-to-menu (menu-name l action &optional full-name)
1633 (let ((menu (make-sparse-keymap menu-name))
1634 name)
1635 (mapc
1636 (lambda (item)
1637 (let (command)
1638 (if (consp item)
1639 (setq command
1640 (rmail-list-to-menu
1641 (car item) (cdr item) action
1642 (if full-name
1643 (concat full-name "/"
1644 (car item))
1645 (car item)))
1646 name (car item))
1647 (setq name item)
1648 (setq command
1649 (list 'lambda () '(interactive)
1650 (list action
1651 (expand-file-name
1652 (if full-name
1653 (concat full-name "/" item)
1654 item)
1655 rmail-secondary-file-directory)))))
1656 (define-key menu (vector (intern name))
1657 (cons name command))))
1658 (reverse l))
1659 menu))
1660
1661 ;; This command is always "disabled" when it appears in a menu.
1662 (put 'rmail-disable-menu 'menu-enable ''nil)
1663
1664 (defun rmail-construct-io-menu ()
1665 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1666 (if files
1667 (progn
1668 (define-key rmail-mode-map [menu-bar classify input-menu]
1669 (cons "Input Rmail File"
1670 (rmail-list-to-menu "Input Rmail File"
1671 files
1672 'rmail-input)))
1673 (define-key rmail-mode-map [menu-bar classify output-menu]
1674 (cons "Output Rmail File"
1675 (rmail-list-to-menu "Output Rmail File"
1676 files
1677 'rmail-output))))
1678
1679 (define-key rmail-mode-map [menu-bar classify input-menu]
1680 '("Input Rmail File" . rmail-disable-menu))
1681 (define-key rmail-mode-map [menu-bar classify output-menu]
1682 '("Output Rmail File" . rmail-disable-menu)))))
1683
1684 \f
1685 ;;;; *** Rmail input ***
1686
1687 (declare-function rmail-summary-goto-msg "rmailsum" (&optional n nowarn skip-rmail))
1688 (declare-function rmail-summary-mark-undeleted "rmailsum" (n))
1689 (declare-function rmail-summary-mark-deleted "rmailsum" (&optional n undel))
1690 (declare-function rfc822-addresses "rfc822" (header-text))
1691 (declare-function mail-abbrev-make-syntax-table "mailabbrev.el" ())
1692
1693 ;; RLK feature not added in this version:
1694 ;; argument specifies inbox file or files in various ways.
1695
1696 ;; In Babyl, the Mail: header in the preamble overrode rmail-inbox-list.
1697 ;; Mbox does not have this feature.
1698 (defun rmail-get-new-mail (&optional file-name)
1699 "Move any new mail from this Rmail file's inbox files.
1700 The buffer-local variable `rmail-inbox-list' specifies the list
1701 of inbox files. By default, this is nil, except for your primary
1702 Rmail file `rmail-file-name'. In this case, when you first visit
1703 the Rmail file it is initialized using either
1704 `rmail-primary-inbox-list', or the \"MAIL\" environment variable,
1705 or the function `user-login-name' and the directory
1706 `rmail-spool-directory' (whose value depends on the operating system).
1707
1708 The command `set-rmail-inbox-list' sets `rmail-inbox-list' to the
1709 value you specify.
1710
1711 You can also specify the file to get new mail from just for one
1712 instance of this command. In this case, the file of new mail is
1713 not changed or deleted. Noninteractively, you can pass the inbox
1714 file name as an argument. Interactively, a prefix argument
1715 causes us to read a file name and use that file as the inbox.
1716
1717 If the variable `rmail-preserve-inbox' is non-nil, new mail will
1718 always be left in inbox files rather than deleted.
1719
1720 Before doing anything, this runs `rmail-before-get-new-mail-hook'.
1721 Just before returning, it runs `rmail-after-get-new-mail-hook',
1722 whether or not there is new mail.
1723
1724 If there is new mail, it runs `rmail-get-new-mail-hook', saves
1725 the updated file, and shows the first unseen message (which might
1726 not be a new one). It returns non-nil if it got any new messages."
1727 (interactive
1728 (list (if current-prefix-arg
1729 (read-file-name "Get new mail from file: "))))
1730 (run-hooks 'rmail-before-get-new-mail-hook)
1731 ;; If the disk file has been changed from under us,
1732 ;; revert to it before we get new mail.
1733 (or (verify-visited-file-modtime (current-buffer))
1734 (find-file (buffer-file-name)))
1735 (set-buffer rmail-buffer)
1736 (rmail-modify-format)
1737 (rmail-swap-buffers-maybe)
1738 (rmail-maybe-set-message-counters)
1739 (widen)
1740 ;; Get rid of all undo records for this buffer.
1741 (or (eq buffer-undo-list t)
1742 (setq buffer-undo-list nil))
1743 (let ((all-files (if file-name (list file-name) rmail-inbox-list))
1744 (rmail-enable-multibyte (default-value 'enable-multibyte-characters))
1745 found)
1746 (unwind-protect
1747 (progn
1748 ;; This loops if any members of the inbox list have the same
1749 ;; basename (see "name conflict" below).
1750 (while all-files
1751 (let ((opoint (point))
1752 ;; If buffer has not changed yet, and has not been
1753 ;; saved yet, don't replace the old backup file now.
1754 (make-backup-files (and make-backup-files
1755 (buffer-modified-p)))
1756 (buffer-read-only nil)
1757 ;; Don't make undo records while getting mail.
1758 (buffer-undo-list t)
1759 delete-files success files file-last-names)
1760 ;; Pull files off all-files onto files as long as there is
1761 ;; no name conflict. A conflict happens when two inbox
1762 ;; file names have the same last component.
1763 ;; The reason this careful handling is necessary seems
1764 ;; to be that rmail-insert-inbox-text uses .newmail-BASENAME.
1765 (while (and all-files
1766 (not (member (file-name-nondirectory (car all-files))
1767 file-last-names)))
1768 (setq files (cons (car all-files) files)
1769 file-last-names
1770 (cons (file-name-nondirectory (car all-files)) files))
1771 (setq all-files (cdr all-files)))
1772 ;; Put them back in their original order.
1773 (setq files (nreverse files))
1774 (goto-char (point-max))
1775 ;; Make sure we end with a blank line unless there are
1776 ;; no messages, as required by mbox format (Bug#9974).
1777 (unless (bobp)
1778 (while (not (looking-back "\n\n"))
1779 (insert "\n")))
1780 (setq found (or
1781 (rmail-get-new-mail-1 file-name files delete-files)
1782 found))))
1783 ;; Move to the first new message unless we have other unseen
1784 ;; messages before it.
1785 (if found (rmail-show-message (rmail-first-unseen-message)))
1786 (run-hooks 'rmail-after-get-new-mail-hook)
1787 found)
1788 ;; Don't leave the buffer screwed up if we get a disk-full error.
1789 (rmail-show-message))))
1790
1791 (defvar rmail-use-spam-filter)
1792 (declare-function rmail-get-new-mail-filter-spam "rmail-spam-filter" (nnew))
1793
1794 (defun rmail-get-new-mail-1 (file-name files delete-files)
1795 "Return t if new messages are detected without error, nil otherwise."
1796 (save-excursion
1797 (save-restriction
1798 (let ((new-messages 0)
1799 (spam-filter-p (and (featurep 'rmail-spam-filter)
1800 rmail-use-spam-filter))
1801 (blurb "")
1802 result success suffix)
1803 (narrow-to-region (point) (point))
1804 ;; Read in the contents of the inbox files, renaming them as
1805 ;; necessary, and adding to the list of files to delete
1806 ;; eventually.
1807 (if file-name
1808 (rmail-insert-inbox-text files nil)
1809 (setq delete-files (rmail-insert-inbox-text files t)))
1810 ;; Scan the new text and convert each message to
1811 ;; Rmail/mbox format.
1812 (goto-char (point-min))
1813 (skip-chars-forward " \n")
1814 (narrow-to-region (point) (point-max))
1815 (unwind-protect
1816 (setq new-messages (rmail-add-mbox-headers)
1817 success t)
1818 ;; Try to delete the garbage just inserted.
1819 (or success (delete-region (point-min) (point-max)))
1820 ;; If we could not convert the file's inboxes, rename the
1821 ;; files we tried to read so we won't over and over again.
1822 (if (and (not file-name) (not success))
1823 (let ((delfiles delete-files)
1824 (count 0))
1825 (while delfiles
1826 (while (file-exists-p (format "RMAILOSE.%d" count))
1827 (setq count (1+ count)))
1828 (rename-file (car delfiles) (format "RMAILOSE.%d" count))
1829 (setq delfiles (cdr delfiles))))))
1830 ;; Determine if there are messages.
1831 (unless (zerop new-messages)
1832 ;; There are. Process them.
1833 (goto-char (point-min))
1834 (rmail-count-new-messages)
1835 (run-hooks 'rmail-get-new-mail-hook)
1836 (save-buffer))
1837 ;; Delete the old files, now that the Rmail file is saved.
1838 (while delete-files
1839 (condition-case ()
1840 ;; First, try deleting.
1841 (condition-case ()
1842 (delete-file (car delete-files))
1843 (file-error
1844 ;; If we can't delete it, truncate it.
1845 (write-region (point) (point) (car delete-files))))
1846 (file-error nil))
1847 (setq delete-files (cdr delete-files)))
1848 (if (zerop new-messages)
1849 (when (or file-name rmail-inbox-list)
1850 (message "(No new mail has arrived)"))
1851 (if spam-filter-p
1852 (setq blurb (rmail-get-new-mail-filter-spam new-messages))))
1853 (if (rmail-summary-exists)
1854 (rmail-select-summary (rmail-update-summary)))
1855 (setq suffix (if (= 1 new-messages) "" "s"))
1856 (message "%d new message%s read%s" new-messages suffix blurb)
1857 ;; Establish the return value.
1858 (setq result (> new-messages 0))
1859 result))))
1860
1861 (defun rmail-parse-url (file)
1862 "Parse the supplied URL. Return (list MAILBOX-NAME REMOTE PASSWORD GOT-PASSWORD)
1863 WHERE MAILBOX-NAME is the name of the mailbox suitable as argument to the
1864 actual version of `movemail', REMOTE is non-nil if MAILBOX-NAME refers to
1865 a remote mailbox, PASSWORD is the password if it should be
1866 supplied as a separate argument to `movemail' or nil otherwise, GOT-PASSWORD
1867 is non-nil if the user has supplied the password interactively.
1868 "
1869 (cond
1870 ((string-match "^\\([^:]+\\)://\\(\\([^:@]+\\)\\(:\\([^@]+\\)\\)?@\\)?.*" file)
1871 (let (got-password supplied-password
1872 (proto (match-string 1 file))
1873 (user (match-string 3 file))
1874 (pass (match-string 5 file))
1875 (host (substring file (or (match-end 2)
1876 (+ 3 (match-end 1))))))
1877
1878 (if (not pass)
1879 (when rmail-remote-password-required
1880 (setq got-password (not (rmail-have-password)))
1881 (setq supplied-password (rmail-get-remote-password
1882 (string-equal proto "imap"))))
1883 ;; The password is embedded. Strip it out since movemail
1884 ;; does not really like it, in spite of the movemail spec.
1885 (setq file (concat proto "://" user "@" host)))
1886
1887 (if (rmail-movemail-variant-p 'emacs)
1888 (if (string-equal proto "pop")
1889 (list (concat "po:" user ":" host)
1890 t
1891 (or pass supplied-password)
1892 got-password)
1893 (error "Emacs movemail does not support %s protocol" proto))
1894 (list file
1895 (or (string-equal proto "pop") (string-equal proto "imap"))
1896 (or supplied-password pass)
1897 got-password))))
1898
1899 ((string-match "^po:\\([^:]+\\)\\(:\\(.*\\)\\)?" file)
1900 (let (got-password supplied-password
1901 (proto "pop")
1902 (user (match-string 1 file))
1903 (host (match-string 3 file)))
1904
1905 (when rmail-remote-password-required
1906 (setq got-password (not (rmail-have-password)))
1907 (setq supplied-password (rmail-get-remote-password nil)))
1908
1909 (list file "pop" supplied-password got-password)))
1910
1911 (t
1912 (list file nil nil nil))))
1913
1914 (defun rmail-unrmail-new-mail (from-file)
1915 "Replace newly read mail in Babyl format with equivalent mbox format.
1916
1917 FROM-FILE is the Babyl file from which the new mail should be read."
1918 (let ((to-file (make-temp-file "rmail"))
1919 size)
1920 (unrmail from-file to-file)
1921 (let ((inhibit-read-only t)
1922 (coding-system-for-read 'raw-text)
1923 (buffer-undo-list t))
1924 (delete-region (point) (point-max))
1925 (setq size (nth 1 (insert-file-contents to-file)))
1926 (delete-file to-file)
1927 size)))
1928
1929 (defun rmail-unrmail-new-mail-maybe (file size)
1930 "If newly read mail from FILE is in Babyl format, convert it to mbox format.
1931
1932 SIZE is the original size of the newly read mail.
1933 Value is the size of the newly read mail after conversion."
1934 ;; Detect previous Babyl format files.
1935 (let ((case-fold-search nil)
1936 (old-file file)
1937 new-file)
1938 (cond ((looking-at "BABYL OPTIONS:")
1939 ;; The new mail is in Babyl version 5 format. Use unrmail
1940 ;; to convert it.
1941 (setq size (rmail-unrmail-new-mail old-file)))
1942 ((looking-at "Version: 5\n")
1943 ;; New mail is in Babyl format made by old version of
1944 ;; Rmail. Fix the babyl file header and use unrmail to
1945 ;; convert it.
1946 (let ((buffer-read-only nil)
1947 (write-region-annotate-functions nil)
1948 (write-region-post-annotation-function nil)
1949 (old-file (make-temp-file "rmail")))
1950 (insert "BABYL OPTIONS: -*- rmail -*-\n")
1951 (forward-line -1)
1952 (write-region (point) (point-max) old-file)
1953 (setq size (rmail-unrmail-new-mail old-file))
1954 (delete-file old-file))))
1955 size))
1956
1957 (defun rmail-insert-inbox-text (files renamep)
1958 ;; Detect a locked file now, so that we avoid moving mail
1959 ;; out of the real inbox file. (That could scare people.)
1960 (or (memq (file-locked-p buffer-file-name) '(nil t))
1961 (error "RMAIL file %s is locked"
1962 (file-name-nondirectory buffer-file-name)))
1963 (let (file tofile delete-files movemail popmail got-password password)
1964 (while files
1965 ;; Handle remote mailbox names specially; don't expand as filenames
1966 ;; in case the userid contains a directory separator.
1967 (setq file (car files))
1968 (let ((url-data (rmail-parse-url file)))
1969 (setq file (nth 0 url-data))
1970 (setq popmail (nth 1 url-data))
1971 (setq password (nth 2 url-data))
1972 (setq got-password (nth 3 url-data)))
1973
1974 (if popmail
1975 (setq renamep t)
1976 (setq file (file-truename
1977 (substitute-in-file-name (expand-file-name file)))))
1978 (setq tofile (expand-file-name
1979 ;; Generate name to move to from inbox name,
1980 ;; in case of multiple inboxes that need moving.
1981 (concat ".newmail-"
1982 (file-name-nondirectory
1983 (if (memq system-type '(windows-nt cygwin ms-dos))
1984 ;; cannot have colons in file name
1985 (replace-regexp-in-string ":" "-" file)
1986 file)))
1987 ;; Use the directory of this rmail file
1988 ;; because it's a nuisance to use the homedir
1989 ;; if that is on a full disk and this rmail
1990 ;; file isn't.
1991 (file-name-directory
1992 (expand-file-name buffer-file-name))))
1993 ;; Always use movemail to rename the file,
1994 ;; since there can be mailboxes in various directories.
1995 (when (not popmail)
1996 ;; On some systems, /usr/spool/mail/foo is a directory
1997 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1998 (if (file-directory-p file)
1999 (setq file (expand-file-name (user-login-name)
2000 file))))
2001 (cond (popmail
2002 (message "Getting mail from the remote server ..."))
2003 ((and (file-exists-p tofile)
2004 (/= 0 (nth 7 (file-attributes tofile))))
2005 (message "Getting mail from %s..." tofile))
2006 ((and (file-exists-p file)
2007 (/= 0 (nth 7 (file-attributes file))))
2008 (message "Getting mail from %s..." file)))
2009 ;; Set TOFILE if have not already done so, and
2010 ;; rename or copy the file FILE to TOFILE if and as appropriate.
2011 (cond ((not renamep)
2012 (setq tofile file))
2013 ((or (file-exists-p tofile) (and (not popmail)
2014 (not (file-exists-p file))))
2015 nil)
2016 (t
2017 (with-temp-buffer
2018 (let ((errors (current-buffer)))
2019 (buffer-disable-undo errors)
2020 (let ((args
2021 (append
2022 (list (or rmail-movemail-program "movemail") nil errors nil)
2023 (if rmail-preserve-inbox
2024 (list "-p")
2025 nil)
2026 (if (rmail-movemail-variant-p 'mailutils)
2027 (append (list "--emacs") rmail-movemail-flags)
2028 rmail-movemail-flags)
2029 (list file tofile)
2030 (if password (list password) nil))))
2031 (apply 'call-process args))
2032 (if (not (buffer-modified-p errors))
2033 ;; No output => movemail won
2034 nil
2035 (set-buffer errors)
2036 (subst-char-in-region (point-min) (point-max)
2037 ?\n ?\s)
2038 (goto-char (point-max))
2039 (skip-chars-backward " \t")
2040 (delete-region (point) (point-max))
2041 (goto-char (point-min))
2042 (if (looking-at "movemail: ")
2043 (delete-region (point-min) (match-end 0)))
2044 (beep t)
2045 ;; If we just read the password, most likely it is
2046 ;; wrong. Otherwise, see if there is a specific
2047 ;; reason to think that the problem is a wrong passwd.
2048 (if (or got-password
2049 (re-search-forward rmail-remote-password-error
2050 nil t))
2051 (rmail-set-remote-password nil))
2052
2053 ;; If using Mailutils, remove initial error code
2054 ;; abbreviation
2055 (when (rmail-movemail-variant-p 'mailutils)
2056 (goto-char (point-min))
2057 (when (looking-at "[A-Z][A-Z0-9_]*:")
2058 (delete-region (point-min) (match-end 0))))
2059
2060 (message "movemail: %s"
2061 (buffer-substring (point-min)
2062 (point-max)))
2063
2064 (sit-for 3)
2065 nil)))))
2066
2067 ;; At this point, TOFILE contains the name to read:
2068 ;; Either the alternate name (if we renamed)
2069 ;; or the actual inbox (if not renaming).
2070 (if (file-exists-p tofile)
2071 (let ((coding-system-for-read 'no-conversion)
2072 size)
2073 (goto-char (point-max))
2074 (setq size
2075 ;; If new mail is in Babyl format, convert it to mbox.
2076 (rmail-unrmail-new-mail-maybe
2077 tofile
2078 (nth 1 (insert-file-contents tofile))))
2079 (goto-char (point-max))
2080 ;; Make sure the read-in mbox data properly ends with a
2081 ;; blank line unless it is of size 0.
2082 (unless (zerop size)
2083 (while (not (looking-back "\n\n"))
2084 (insert "\n")))
2085 (if (not (and rmail-preserve-inbox (string= file tofile)))
2086 (setq delete-files (cons tofile delete-files)))))
2087 (message "")
2088 (setq files (cdr files)))
2089 delete-files))
2090
2091 ;; Decode the region specified by FROM and TO by CODING.
2092 ;; If CODING is nil or an invalid coding system, decode by `undecided'.
2093 (defun rmail-decode-region (from to coding &optional destination)
2094 (if (or (not coding) (not (coding-system-p coding)))
2095 (setq coding 'undecided))
2096 ;; Use -dos decoding, to remove ^M characters left from base64 or
2097 ;; rogue qp-encoded text.
2098 (decode-coding-region
2099 from to (coding-system-change-eol-conversion coding 1) destination)
2100 ;; Don't reveal the fact we used -dos decoding, as users generally
2101 ;; will not expect the RMAIL buffer to use DOS EOL format.
2102 (cond
2103 ((null destination)
2104 (setq buffer-file-coding-system
2105 (setq last-coding-system-used
2106 (coding-system-change-eol-conversion coding 0))))
2107 ((bufferp destination)
2108 (with-current-buffer destination
2109 (setq buffer-file-coding-system
2110 (setq last-coding-system-used
2111 (coding-system-change-eol-conversion coding 0)))))))
2112
2113 (defun rmail-ensure-blank-line ()
2114 "Ensure a message ends in a blank line.
2115 Call with point at the end of the message."
2116 (unless (bolp)
2117 (insert "\n"))
2118 (unless (looking-back "\n\n")
2119 (insert "\n")))
2120
2121 (defun rmail-add-mbox-headers ()
2122 "Validate the RFC2822 format for the new messages.
2123 Point should be at the first new message.
2124 An error is signaled if the new messages are not RFC2822
2125 compliant.
2126 Unless an Rmail attribute header already exists, add it to the
2127 new messages. Return the number of new messages."
2128 (save-excursion
2129 (save-restriction
2130 (let ((count 0)
2131 (start (point))
2132 (value "------U-")
2133 (case-fold-search nil)
2134 (delim (concat "\n\n" rmail-unix-mail-delimiter))
2135 limit stop)
2136 ;; Detect an empty inbox file.
2137 (unless (= start (point-max))
2138 ;; Scan the new messages to establish a count and to ensure that
2139 ;; an attribute header is present.
2140 (if (looking-at rmail-unix-mail-delimiter)
2141 (while (not stop)
2142 ;; Determine if a new attribute header needs to be
2143 ;; added to the message.
2144 (if (search-forward "\n\n" nil t)
2145 (progn
2146 (setq count (1+ count))
2147 (narrow-to-region start (point))
2148 (unless (mail-fetch-field rmail-attribute-header)
2149 (backward-char 1)
2150 (insert rmail-attribute-header ": " value "\n"))
2151 (widen))
2152 (rmail-error-bad-format))
2153 ;; Move to the next message.
2154 (if (not (re-search-forward delim nil 'move))
2155 (setq stop t)
2156 (goto-char (match-beginning 0))
2157 (forward-char 2))
2158 (setq start (point)))
2159 (rmail-error-bad-format)))
2160 count))))
2161 \f
2162 (defun rmail-get-header-1 (name)
2163 "Subroutine of `rmail-get-header'.
2164 Narrow to header, call `mail-fetch-field' to find header NAME."
2165 (if (search-forward "\n\n" nil t)
2166 (progn
2167 (narrow-to-region (point-min) (point))
2168 (mail-fetch-field name))
2169 (rmail-error-bad-format)))
2170
2171 (defun rmail-get-header (name &optional msgnum)
2172 "Return the value of message header NAME, nil if it has none.
2173 MSGNUM specifies the message number to get it from.
2174 If MSGNUM is nil, use the current message."
2175 (rmail-apply-in-message msgnum 'rmail-get-header-1 name))
2176
2177 (defun rmail-set-header-1 (name value)
2178 "Subroutine of `rmail-set-header'.
2179 Narrow to headers, set header NAME to VALUE, replacing existing if present.
2180 VALUE nil means to remove NAME altogether.
2181
2182 Only changes the first instance of NAME. If VALUE is multi-line,
2183 continuation lines should already be indented. VALUE should not
2184 end in a newline."
2185 (if (search-forward "\n\n" nil t)
2186 (progn
2187 (forward-char -1)
2188 (narrow-to-region (point-min) (point))
2189 ;; cf mail-fetch-field.
2190 (goto-char (point-min))
2191 (if (let ((case-fold-search t))
2192 (re-search-forward (concat "^" (regexp-quote name) "[ \t]*:")
2193 nil 'move))
2194 (let ((start (point))
2195 end)
2196 (while (and (zerop (forward-line 1))
2197 (looking-at "[ \t]")))
2198 ;; Back up over newline.
2199 (forward-char -1)
2200 (setq end (point))
2201 (goto-char start)
2202 (if value
2203 (progn
2204 (delete-region start end)
2205 (insert " " value))
2206 (delete-region (line-beginning-position) (1+ end))))
2207 ;; Not already present: insert at end of headers.
2208 (if value (insert name ": " value "\n"))))
2209 (rmail-error-bad-format)))
2210
2211 (defun rmail-set-header (name &optional msgnum value)
2212 "Set message header NAME to VALUE in message number MSGNUM.
2213 If MSGNUM is nil, use the current message. NAME and VALUE are strings.
2214 VALUE may also be nil, meaning to remove the header."
2215 (rmail-apply-in-message msgnum 'rmail-set-header-1 name value)
2216 (with-current-buffer rmail-buffer
2217 ;; Ensure header changes get saved.
2218 ;; (Note replacing a header with an identical copy modifies.)
2219 (set-buffer-modified-p t)
2220 ;; However: don't save in mbox format over a Babyl file
2221 ;; merely because of this.
2222 (rmail-dont-modify-format)))
2223 \f
2224 ;;;; *** Rmail Attributes and Keywords ***
2225
2226 (defun rmail-get-attr-names (&optional msg)
2227 "Return the message attributes in a comma separated string.
2228 MSG specifies the message number to get it from.
2229 If MSG is nil, use the current message."
2230 (let ((value (rmail-get-header rmail-attribute-header msg))
2231 (nmax (length rmail-attr-array))
2232 result temp)
2233 (when value
2234 (if (> (length value) nmax)
2235 (message "Warning: corrupt attribute header in message")
2236 (dotimes (index (length value))
2237 (setq temp (and (not (= ?- (aref value index)))
2238 (nth 1 (aref rmail-attr-array index)))
2239 result
2240 (cond
2241 ((and temp result) (format "%s, %s" result temp))
2242 (temp temp)
2243 (t result)))))
2244 result)))
2245
2246 (defun rmail-get-keywords (&optional msg)
2247 "Return the message keywords in a comma separated string.
2248 MSG, if non-nil, identifies the message number to use.
2249 If nil, that means the current message."
2250 (rmail-get-header rmail-keyword-header msg))
2251
2252 (defun rmail-get-labels (&optional msg)
2253 "Return a string with the labels (attributes and keywords) of msg MSG.
2254 It is put in comma-separated form.
2255 MSG, if non-nil, identifies the message number to use.
2256 If nil, that means the current message."
2257 (or msg (setq msg rmail-current-message))
2258 (let (attr-names keywords)
2259 ;; Combine the message attributes and keywords
2260 ;; into a comma-separated list.
2261 (setq attr-names (rmail-get-attr-names msg)
2262 keywords (rmail-get-keywords msg))
2263 (if (string= keywords "")
2264 (setq keywords nil))
2265 (cond
2266 ;; FIXME ? old rmail did not have spaces in the comma-separated lists.
2267 ((and attr-names keywords) (concat " " attr-names "; " keywords))
2268 (attr-names (concat " " attr-names))
2269 (keywords (concat " " keywords))
2270 (t ""))))
2271
2272 (defun rmail-display-labels ()
2273 "Update the current messages's attributes and keywords in mode line."
2274 (let ((blurb (rmail-get-labels)))
2275 (setq mode-line-process
2276 (format " %d/%d%s"
2277 rmail-current-message rmail-total-messages blurb))))
2278
2279 (defun rmail-get-attr-value (attr state)
2280 "Return the character value for ATTR.
2281 ATTR is a (numeric) index, an offset into the mbox attribute
2282 header value. STATE is one of nil, t, or a character value."
2283 (cond
2284 ((numberp state) state)
2285 ((not state) ?-)
2286 (t (nth 0 (aref rmail-attr-array attr)))))
2287
2288 (defun rmail-set-attribute-1 (attr state)
2289 "Subroutine of `rmail-set-attribute'.
2290 Set Rmail attribute ATTR to STATE in `rmail-attribute-header',
2291 creating the header if necessary. Returns non-nil if a
2292 significant attribute change was made."
2293 (let ((limit (search-forward "\n\n" nil t))
2294 (value (rmail-get-attr-value attr state))
2295 (inhibit-read-only t)
2296 altered)
2297 (goto-char (point-min))
2298 (if (search-forward (concat rmail-attribute-header ": ") limit t)
2299 ;; If this message already records attributes, just change the
2300 ;; value for this one.
2301 (let ((missing (- (+ (point) attr) (line-end-position))))
2302 ;; Position point at this attribute, adding attributes if necessary.
2303 (if (> missing 0)
2304 (progn
2305 (end-of-line)
2306 (insert-char ?- missing)
2307 (backward-char 1))
2308 (forward-char attr))
2309 ;; Change this attribute.
2310 (when (/= value (char-after))
2311 (setq altered t)
2312 (delete-char 1)
2313 (insert value)))
2314 ;; Otherwise add a header line to record the attributes and set
2315 ;; all but this one to no.
2316 (let ((header-value "--------"))
2317 (aset header-value attr value)
2318 (goto-char (if limit (1- limit) (point-max)))
2319 (setq altered (/= value ?-))
2320 (insert rmail-attribute-header ": " header-value "\n")))
2321 altered))
2322
2323 (defun rmail-set-attribute (attr state &optional msgnum)
2324 "Turn an attribute of a message on or off according to STATE.
2325 STATE is either nil or the character (numeric) value associated
2326 with the state (nil represents off and non-nil represents on).
2327 ATTR is either the index number of the attribute, or a string,
2328 both from `rmail-attr-array'. MSGNUM is message number to
2329 change; nil means current message."
2330 (let ((n 0)
2331 (nmax (length rmail-attr-array)))
2332 (while (and (stringp attr)
2333 (< n nmax))
2334 (if (string-equal attr (cadr (aref rmail-attr-array n)))
2335 (setq attr n))
2336 (setq n (1+ n))))
2337 (if (stringp attr)
2338 (error "Unknown attribute `%s'" attr))
2339 ;; Ask for confirmation before setting any attribute except `unseen'
2340 ;; if it would force a format change.
2341 (unless (= attr rmail-unseen-attr-index)
2342 (rmail-modify-format))
2343 (with-current-buffer rmail-buffer
2344 (or msgnum (setq msgnum rmail-current-message))
2345 (when (> msgnum 0)
2346 ;; The "deleted" attribute is also stored in a special vector so
2347 ;; update that too.
2348 (if (= attr rmail-deleted-attr-index)
2349 (rmail-set-message-deleted-p msgnum state))
2350 (if (prog1
2351 (rmail-apply-in-message msgnum 'rmail-set-attribute-1 attr state)
2352 (if (= msgnum rmail-current-message)
2353 (rmail-display-labels)))
2354 ;; Don't save in mbox format over a Babyl file
2355 ;; merely because of a change in `unseen' attribute.
2356 (if (= attr rmail-unseen-attr-index)
2357 (rmail-dont-modify-format)
2358 ;; Otherwise, if we modified the file text via the view buffer,
2359 ;; mark the main buffer modified too.
2360 (set-buffer-modified-p t))))))
2361
2362 (defun rmail-message-attr-p (msg attrs)
2363 "Return non-nil if message number MSG has attributes matching regexp ATTRS."
2364 (let ((value (rmail-get-header rmail-attribute-header msg)))
2365 (and value (string-match attrs value))))
2366
2367 (defun rmail-message-unseen-p (msgnum)
2368 "Return non-nil if message number MSGNUM has the unseen attribute."
2369 (rmail-message-attr-p msgnum "......U"))
2370
2371 ;; FIXME rmail-get-labels does some formatting (eg leading space, `;'
2372 ;; between attributes and labels), so this might not do what you want.
2373 ;; Eg see rmail-sort-by-labels. rmail-get-labels could have an
2374 ;; optional `noformat' argument.
2375 (defun rmail-message-labels-p (msg labels)
2376 "Return non-nil if message number MSG has labels matching regexp LABELS."
2377 (string-match labels (rmail-get-labels msg)))
2378 \f
2379 ;;;; *** Rmail Message Selection And Support ***
2380
2381 (defun rmail-msgend (n)
2382 "Return the end position for message number N."
2383 (marker-position (aref rmail-message-vector (1+ n))))
2384
2385 (defun rmail-msgbeg (n)
2386 "Return the start position for message number N."
2387 (marker-position (aref rmail-message-vector n)))
2388
2389 (defun rmail-apply-in-message (msgnum function &rest args)
2390 "Call FUNCTION on ARGS while narrowed to message MSGNUM.
2391 Point is at the start of the message.
2392 This returns what the call to FUNCTION returns.
2393 If MSGNUM is nil, use the current message."
2394 (with-current-buffer rmail-buffer
2395 (or msgnum (setq msgnum rmail-current-message))
2396 (when (> msgnum 0)
2397 (let (msgbeg msgend)
2398 (setq msgbeg (rmail-msgbeg msgnum))
2399 (setq msgend (rmail-msgend msgnum))
2400 ;; All access to the rmail-buffer's local variables is now finished...
2401 (save-excursion
2402 ;; ... so it is ok to go to a different buffer.
2403 (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
2404 (save-excursion
2405 (save-restriction
2406 (widen)
2407 (goto-char msgbeg)
2408 (narrow-to-region msgbeg msgend)
2409 (apply function args))))))))
2410
2411 ;; Unused (save for commented out code in rmailedit.el).
2412 (defun rmail-widen-to-current-msgbeg (function)
2413 "Call FUNCTION with point at start of internal data of current message.
2414 Assumes that bounds were previously narrowed to display the message in Rmail.
2415 The bounds are widened enough to move point where desired, then narrowed
2416 again afterward.
2417
2418 FUNCTION may not change the visible text of the message, but it may
2419 change the invisible header text."
2420 (save-excursion
2421 (unwind-protect
2422 (progn
2423 (narrow-to-region (rmail-msgbeg rmail-current-message)
2424 (point-max))
2425 (goto-char (point-min))
2426 (funcall function))
2427 ;; Note: we don't use save-restriction because that does not work right
2428 ;; if changes are made outside the saved restriction
2429 ;; before that restriction is restored.
2430 (narrow-to-region (rmail-msgbeg rmail-current-message)
2431 (rmail-msgend rmail-current-message)))))
2432 \f
2433 ;; Manage the message vectors and counters.
2434
2435 (defun rmail-forget-messages ()
2436 (unwind-protect
2437 (if (vectorp rmail-message-vector)
2438 (let* ((v rmail-message-vector)
2439 (n (length v)))
2440 (dotimes (i n)
2441 (if (aref v i)
2442 (move-marker (aref v i) nil)))))
2443 (setq rmail-message-vector nil)
2444 (setq rmail-msgref-vector nil)
2445 (setq rmail-deleted-vector nil)))
2446
2447 (defun rmail-maybe-set-message-counters ()
2448 (if (not (and rmail-deleted-vector
2449 rmail-message-vector
2450 rmail-current-message
2451 rmail-total-messages))
2452 (rmail-set-message-counters)))
2453
2454 (defun rmail-count-new-messages (&optional nomsg)
2455 "Count the number of new messages.
2456 The buffer should be narrowed to include only the new messages.
2457 Output a helpful message unless NOMSG is non-nil."
2458 (let* ((case-fold-search nil)
2459 (total-messages 0)
2460 (messages-head nil)
2461 (deleted-head nil))
2462 (or nomsg (message "Counting new messages..."))
2463 (goto-char (point-max))
2464 ;; Put at the end of messages-head
2465 ;; the entry for message N+1, which marks
2466 ;; the end of message N. (N = number of messages).
2467 (setq messages-head (list (point-marker)))
2468 (rmail-set-message-counters-counter (point-min))
2469 (setq rmail-current-message (1+ rmail-total-messages))
2470 (setq rmail-total-messages
2471 (+ rmail-total-messages total-messages))
2472 (setq rmail-message-vector
2473 (vconcat rmail-message-vector (cdr messages-head)))
2474 (aset rmail-message-vector
2475 rmail-current-message (car messages-head))
2476 (setq rmail-deleted-vector
2477 (concat rmail-deleted-vector deleted-head))
2478 (setq rmail-summary-vector
2479 (vconcat rmail-summary-vector (make-vector total-messages nil)))
2480 (setq rmail-msgref-vector
2481 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
2482 ;; Fill in the new elements of rmail-msgref-vector.
2483 (let ((i (1+ (- rmail-total-messages total-messages))))
2484 (while (<= i rmail-total-messages)
2485 (aset rmail-msgref-vector i (list i))
2486 (setq i (1+ i))))
2487 (goto-char (point-min))
2488 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
2489
2490 (defun rmail-set-message-counters ()
2491 (rmail-forget-messages)
2492 (save-excursion
2493 (save-restriction
2494 (widen)
2495 (let* ((point-save (point))
2496 (total-messages 0)
2497 (messages-after-point)
2498 (case-fold-search nil)
2499 (messages-head nil)
2500 (deleted-head nil))
2501 ;; Determine how many messages follow point.
2502 (message "Counting messages...")
2503 (goto-char (point-max))
2504 ;; Put at the end of messages-head
2505 ;; the entry for message N+1, which marks
2506 ;; the end of message N. (N = number of messages).
2507 (setq messages-head (list (point-marker)))
2508 (setq messages-after-point
2509 (or (rmail-set-message-counters-counter (min (point) point-save))
2510 0))
2511
2512 (setq rmail-total-messages total-messages)
2513 (setq rmail-current-message
2514 (min total-messages
2515 (max 1 (- total-messages messages-after-point))))
2516
2517 ;; Make an element 0 in rmail-message-vector and rmail-deleted-vector
2518 ;; which will never be used.
2519 (push nil messages-head)
2520 (push ?0 deleted-head)
2521 (setq rmail-message-vector (apply 'vector messages-head)
2522 rmail-deleted-vector (concat deleted-head))
2523
2524 (setq rmail-summary-vector (make-vector rmail-total-messages nil)
2525 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
2526
2527 (let ((i 0))
2528 (while (<= i rmail-total-messages)
2529 (aset rmail-msgref-vector i (list i))
2530 (setq i (1+ i))))
2531 (let ((i 0))
2532 (while (<= i rmail-total-messages)
2533 (rmail-set-message-deleted-p i (rmail-message-attr-p i ".D"))
2534 (setq i (1+ i))))
2535 (message "Counting messages...done")))))
2536
2537
2538 (defsubst rmail-collect-deleted (message-end)
2539 "Collect the message deletion flags for each message.
2540 MESSAGE-END is the buffer position corresponding to the end of
2541 the message. Point is at the beginning of the message."
2542 ;; NOTE: This piece of code will be executed on a per-message basis.
2543 ;; In the face of thousands of messages, it has to be as fast as
2544 ;; possible, hence some brute force constant use is employed in
2545 ;; addition to inlining.
2546 (save-excursion
2547 (setq deleted-head
2548 (cons (if (and (search-forward (concat rmail-attribute-header ": ") message-end t)
2549 (looking-at "?D"))
2550 ?D
2551 ?\s) deleted-head))))
2552
2553 (defun rmail-set-message-counters-counter (&optional spot-to-find)
2554 "Collect the start positions of messages in list `messages-head'.
2555 Return the number of messages after the one containing SPOT-TO-FIND."
2556 (let ((start (point))
2557 messages-after-spot)
2558 (while (search-backward "\n\nFrom " nil t)
2559 (forward-char 2)
2560 (when (looking-at rmail-unix-mail-delimiter)
2561 (if (and (<= (point) spot-to-find)
2562 (null messages-after-spot))
2563 (setq messages-after-spot total-messages))
2564 (rmail-collect-deleted start)
2565 (setq messages-head (cons (point-marker) messages-head)
2566 total-messages (1+ total-messages)
2567 start (point))
2568 ;; Show progress after every 20 messages or so.
2569 (if (zerop (% total-messages 20))
2570 (message "Counting messages...%d" total-messages))))
2571 ;; Handle the first message, maybe.
2572 (goto-char (point-min))
2573 (unless (not (looking-at rmail-unix-mail-delimiter))
2574 (if (and (<= (point) spot-to-find)
2575 (null messages-after-spot))
2576 (setq messages-after-spot total-messages))
2577 (rmail-collect-deleted start)
2578 (setq messages-head (cons (point-marker) messages-head)
2579 total-messages (1+ total-messages)))
2580 messages-after-spot))
2581 \f
2582 ;; Display a message.
2583
2584 ;;;; *** Rmail Message Formatting and Header Manipulation ***
2585
2586 ;; This is used outside of rmail.
2587 (defun rmail-msg-is-pruned ()
2588 "Return nil if the current message is showing full headers."
2589 (with-current-buffer (if (rmail-buffers-swapped-p) rmail-view-buffer
2590 rmail-buffer)
2591 (eq rmail-header-style 'normal)))
2592
2593 (defun rmail-toggle-header (&optional arg)
2594 "Toggle between showing full and normal message headers.
2595 With optional integer ARG, show the normal message header if ARG
2596 is greater than zero; otherwise, show it in full."
2597 (interactive "P")
2598 (let ((rmail-header-style
2599 (if (numberp arg)
2600 (if (> arg 0) 'normal 'full)
2601 (if (rmail-msg-is-pruned) 'full 'normal))))
2602 (rmail-show-message)))
2603
2604 (defun rmail-beginning-of-message ()
2605 "Show current message starting from the beginning."
2606 (interactive)
2607 (let ((rmail-show-message-hook '((lambda () (goto-char (point-min)))))
2608 (rmail-header-style (with-current-buffer (if (rmail-buffers-swapped-p)
2609 rmail-view-buffer
2610 rmail-buffer)
2611 rmail-header-style)))
2612 (rmail-show-message rmail-current-message)))
2613
2614 (defun rmail-end-of-message ()
2615 "Show bottom of current message."
2616 (interactive)
2617 (let ((rmail-show-message-hook '((lambda ()
2618 (goto-char (point-max))
2619 (recenter (1- (window-height))))))
2620 (rmail-header-style (with-current-buffer (if (rmail-buffers-swapped-p)
2621 rmail-view-buffer
2622 rmail-buffer)
2623 rmail-header-style)))
2624 (rmail-show-message rmail-current-message)))
2625
2626 (defun rmail-unknown-mail-followup-to ()
2627 "Handle a \"Mail-Followup-To\" header field with an unknown mailing list.
2628 Ask the user whether to add that list name to `mail-mailing-lists'."
2629 ;; FIXME s-r not needed? Use rmail-get-header?
2630 ;; We have not narrowed to the headers at ths point?
2631 (save-restriction
2632 (let ((mail-followup-to (mail-fetch-field "mail-followup-to" nil t)))
2633 (when mail-followup-to
2634 (let ((addresses
2635 (split-string
2636 (mail-strip-quoted-names mail-followup-to)
2637 ",[[:space:]]+" t)))
2638 (dolist (addr addresses)
2639 (when (and (not (member addr mail-mailing-lists))
2640 (not
2641 ;; taken from rmailsum.el
2642 (string-match
2643 (or rmail-user-mail-address-regexp
2644 (concat "^\\("
2645 (regexp-quote (user-login-name))
2646 "\\($\\|@\\)\\|"
2647 (regexp-quote
2648 (or user-mail-address
2649 (concat (user-login-name) "@"
2650 (or mail-host-address
2651 (system-name)))))
2652 "\\>\\)"))
2653 addr))
2654 (y-or-n-p
2655 (format "Add `%s' to `mail-mailing-lists'? "
2656 addr)))
2657 (customize-save-variable 'mail-mailing-lists
2658 (cons addr mail-mailing-lists)))))))))
2659
2660 (defun rmail-widen ()
2661 "Display the entire mailbox file."
2662 (interactive)
2663 (rmail-swap-buffers-maybe)
2664 (widen))
2665 \f
2666 (defun rmail-no-mail-p ()
2667 "Return nil if there is mail, else \"No mail.\"."
2668 (if (zerop rmail-total-messages)
2669 (save-excursion
2670 ;; Eg we deleted all the messages, so remove the old N/M mark.
2671 (with-current-buffer rmail-buffer (setq mode-line-process nil))
2672 (with-current-buffer rmail-view-buffer
2673 (erase-buffer)
2674 "No mail."))))
2675
2676 (defun rmail-show-message (&optional n no-summary)
2677 "Show message number N (prefix argument), counting from start of file.
2678 If summary buffer is currently displayed, update current message there also.
2679 N defaults to the current message."
2680 (interactive "p")
2681 (or (eq major-mode 'rmail-mode)
2682 (switch-to-buffer rmail-buffer))
2683 ;; FIXME: Why do we swap the raw data back in?
2684 (rmail-swap-buffers-maybe)
2685 (rmail-maybe-set-message-counters)
2686 (widen)
2687 (let ((blurb (rmail-show-message-1 n)))
2688 (or (zerop rmail-total-messages)
2689 (progn
2690 (when mail-mailing-lists
2691 (rmail-unknown-mail-followup-to))
2692 (if transient-mark-mode (deactivate-mark))
2693 ;; If there is a summary buffer, try to move to this message
2694 ;; in that buffer. But don't complain if this message is
2695 ;; not mentioned in the summary. Don't do this at all if we
2696 ;; were called on behalf of cursor motion in the summary
2697 ;; buffer.
2698 (and (rmail-summary-exists) (not no-summary)
2699 (let ((curr-msg rmail-current-message))
2700 (rmail-select-summary
2701 (rmail-summary-goto-msg curr-msg t t))))
2702 (with-current-buffer rmail-buffer
2703 (rmail-auto-file))))
2704 (if blurb
2705 (message blurb))))
2706
2707 (defun rmail-is-text-p ()
2708 "Return t if the region contains a text message, nil otherwise."
2709 (save-excursion
2710 (let ((text-regexp "\\(text\\|message\\)/")
2711 (content-type-header (mail-fetch-field "content-type")))
2712 ;; The message is text if either there is no content type header
2713 ;; (a default of "text/plain; charset=US-ASCII" is assumed) or
2714 ;; the base content type is either text or message.
2715 (or (not content-type-header)
2716 (string-match text-regexp content-type-header)))))
2717
2718 (defcustom rmail-show-message-verbose-min 200000
2719 "Message size at which to show progress messages for displaying it."
2720 :type 'integer
2721 :group 'rmail
2722 :version "23.1")
2723
2724 ;; FIXME?
2725 ;; rmail-show-mime-function does not unquote >From lines. Should it?
2726 (defcustom rmail-mbox-format 'mboxrd
2727 "The mbox format that your system uses.
2728 There is no way to determine this, so you should set the appropriate value.
2729 The formats quote lines containing \"From \" differently.
2730 The choices are:
2731 `mboxo' : lines that start with \"From \" quoted as \">From \"
2732 `mboxrd': lines that start with \">*From \" quoted with another \">\"
2733 The `mboxo' format is ambiguous, in that one cannot know whether
2734 a line starting with \">From \" originally had a \">\" or not.
2735
2736 It is not critical to set this to the correct value; it only affects
2737 how Rmail displays lines starting with \">*From \" in non-MIME messages.
2738
2739 See also `unrmail-mbox-format'."
2740 :type '(choice (const mboxrd)
2741 (const mboxro))
2742 :version "24.4"
2743 :group 'rmail-files)
2744
2745 (defun rmail-show-message-1 (&optional msg)
2746 "Show message MSG (default: current message) using `rmail-view-buffer'.
2747 Return text to display in the minibuffer if MSG is out of
2748 range (displaying a reasonable choice as well), nil otherwise.
2749 The current mail message becomes the message displayed."
2750 (let ((mbox-buf rmail-buffer)
2751 (view-buf rmail-view-buffer)
2752 blurb beg end body-start coding-system character-coding
2753 is-text-message header-style)
2754 (if (not msg)
2755 (setq msg rmail-current-message))
2756 (unless (setq blurb (rmail-no-mail-p))
2757 (cond ((<= msg 0)
2758 (setq msg 1
2759 rmail-current-message 1
2760 blurb "No previous message"))
2761 ((> msg rmail-total-messages)
2762 (setq msg rmail-total-messages
2763 rmail-current-message rmail-total-messages
2764 blurb "No following message"))
2765 (t (setq rmail-current-message msg)))
2766 (with-current-buffer rmail-buffer
2767 (setq header-style rmail-header-style)
2768 ;; Mark the message as seen, but preserve buffer modified flag.
2769 (let ((modiff (buffer-modified-p)))
2770 (rmail-set-attribute rmail-unseen-attr-index nil)
2771 (unless modiff
2772 (restore-buffer-modified-p modiff)))
2773 ;; bracket the message in the mail
2774 ;; buffer and determine the coding system the transfer encoding.
2775 (rmail-swap-buffers-maybe)
2776 (setq beg (rmail-msgbeg msg)
2777 end (rmail-msgend msg))
2778 (when (> (- end beg) rmail-show-message-verbose-min)
2779 (message "Showing message %d" msg))
2780 (narrow-to-region beg end)
2781 (goto-char beg)
2782 (with-current-buffer rmail-view-buffer
2783 ;; We give the view buffer a buffer-local value of
2784 ;; rmail-header-style based on the binding in effect when
2785 ;; this function is called; `rmail-toggle-headers' can
2786 ;; inspect this value to determine how to toggle.
2787 (set (make-local-variable 'rmail-header-style) header-style))
2788 (if (and rmail-enable-mime
2789 rmail-show-mime-function
2790 (re-search-forward "mime-version: 1.0" nil t))
2791 (let ((rmail-buffer mbox-buf)
2792 (rmail-view-buffer view-buf))
2793 (set (make-local-variable 'rmail-mime-decoded) t)
2794 (funcall rmail-show-mime-function))
2795 (setq body-start (search-forward "\n\n" nil t))
2796 (narrow-to-region beg (point))
2797 (goto-char beg)
2798 (save-excursion
2799 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2800 (setq coding-system (intern (match-string 1)))
2801 (setq coding-system (rmail-get-coding-system))))
2802 (setq character-coding (mail-fetch-field "content-transfer-encoding")
2803 is-text-message (rmail-is-text-p))
2804 (if character-coding
2805 (setq character-coding (downcase character-coding)))
2806 (narrow-to-region beg end)
2807 ;; Decode the message body into an empty view buffer using a
2808 ;; unibyte temporary buffer where the character decoding takes
2809 ;; place.
2810 (with-current-buffer rmail-view-buffer
2811 (erase-buffer))
2812 (if (null character-coding)
2813 ;; Do it directly since that is fast.
2814 (rmail-decode-region body-start end coding-system view-buf)
2815 ;; Can this be done directly, skipping the temp buffer?
2816 (with-temp-buffer
2817 (set-buffer-multibyte nil)
2818 (insert-buffer-substring mbox-buf body-start end)
2819 (cond
2820 ((string= character-coding "quoted-printable")
2821 ;; See bug#5441.
2822 (or (mail-unquote-printable-region (point-min) (point-max)
2823 nil t 'unibyte)
2824 (message "Malformed MIME quoted-printable message")))
2825 ((and (string= character-coding "base64") is-text-message)
2826 (condition-case err
2827 (base64-decode-region (point-min) (point-max))
2828 (error (message "%s" (cdr err)))))
2829 ((eq character-coding 'uuencode)
2830 (error "uuencoded messages are not supported yet"))
2831 (t))
2832 (rmail-decode-region (point-min) (point-max)
2833 coding-system view-buf)))
2834 (with-current-buffer rmail-view-buffer
2835 ;; Prepare the separator (blank line) before the body.
2836 (goto-char (point-min))
2837 (insert "\n")
2838 ;; Unquote quoted From lines.
2839 (let ((fromline (if (eq 'mboxrd rmail-mbox-format)
2840 "^>+From "
2841 "^>From "))
2842 case-fold-search)
2843 (while (re-search-forward fromline nil t)
2844 (beginning-of-line)
2845 (delete-char 1)
2846 (forward-line)))
2847 (goto-char (point-min)))
2848 ;; Copy the headers to the front of the message view buffer.
2849 (rmail-copy-headers beg end)
2850 ;; Decode any RFC2047 encoded message headers.
2851 (if rmail-enable-mime
2852 (with-current-buffer rmail-view-buffer
2853 (rfc2047-decode-region
2854 (point-min)
2855 (progn
2856 (search-forward "\n\n" nil 'move)
2857 (point))))))
2858 ;; highlight the message, activate any URL like text and add
2859 ;; special highlighting for and quoted material.
2860 (with-current-buffer rmail-view-buffer
2861 (goto-char (point-min))
2862 (rmail-highlight-headers)
2863 ;(rmail-activate-urls)
2864 ;(rmail-process-quoted-material)
2865 )
2866 ;; Update the mode-line with message status information and swap
2867 ;; the view buffer/mail buffer contents.
2868 (rmail-display-labels)
2869 (rmail-swap-buffers)
2870 (setq rmail-buffer-swapped t)
2871 (run-hooks 'rmail-show-message-hook)
2872 (when (> (- end beg) rmail-show-message-verbose-min)
2873 (message "Showing message %d...done" msg))))
2874 blurb))
2875
2876 (defun rmail-copy-headers (beg end &optional ignored-headers)
2877 "Copy displayed header fields to the message viewer buffer.
2878 BEG and END marks the start and end positions of the message in
2879 the mbox buffer. If the optional argument IGNORED-HEADERS is
2880 non-nil, ignore all header fields whose names match that regexp.
2881 Otherwise, if `rmail-displayed-headers' is non-nil, copy only
2882 those header fields whose names match that regexp. Otherwise,
2883 copy all header fields whose names do not match
2884 `rmail-ignored-headers' (unless they also match
2885 `rmail-nonignored-headers'). Moves point in the message viewer
2886 buffer to the end of the headers."
2887 (let ((header-start-regexp "\n[^ \t]")
2888 lim)
2889 (with-current-buffer rmail-buffer
2890 (when (search-forward "\n\n" nil t)
2891 (forward-char -1)
2892 (save-restriction
2893 ;; Put point right after the From header line.
2894 (narrow-to-region beg (point))
2895 (goto-char (point-min))
2896 (unless (re-search-forward header-start-regexp nil t)
2897 (rmail-error-bad-format))
2898 (forward-char -1)
2899 (cond
2900 ;; Handle the case where all headers should be copied.
2901 ((eq rmail-header-style 'full)
2902 (prepend-to-buffer rmail-view-buffer beg (point-max))
2903 ;; rmail-show-message-1 expects this function to leave point
2904 ;; at the end of the headers.
2905
2906 (let ((len (- (point-max) beg)))
2907 (with-current-buffer rmail-view-buffer
2908 (goto-char (1+ len)))))
2909
2910 ;; Handle the case where the headers matching the displayed
2911 ;; headers regexp should be copied.
2912 ((and rmail-displayed-headers (null ignored-headers))
2913 (while (not (eobp))
2914 (save-excursion
2915 (setq lim (if (re-search-forward header-start-regexp nil t)
2916 (1+ (match-beginning 0))
2917 (point-max))))
2918 (when (looking-at rmail-displayed-headers)
2919 (append-to-buffer rmail-view-buffer (point) lim))
2920 (goto-char lim)))
2921 ;; Handle the ignored headers.
2922 ((or ignored-headers (setq ignored-headers rmail-ignored-headers))
2923 (while (and ignored-headers (not (eobp)))
2924 (save-excursion
2925 (setq lim (if (re-search-forward header-start-regexp nil t)
2926 (1+ (match-beginning 0))
2927 (point-max))))
2928 (if (and (looking-at ignored-headers)
2929 (not (looking-at rmail-nonignored-headers)))
2930 (goto-char lim)
2931 (append-to-buffer rmail-view-buffer (point) lim)
2932 (goto-char lim))))
2933 (t (error "No headers selected for display!"))))))))
2934
2935 (defun rmail-redecode-body (coding)
2936 "Decode the body of the current message using coding system CODING.
2937 This is useful with mail messages that have malformed or missing
2938 charset= headers.
2939
2940 This function assumes that the current message is already decoded
2941 and displayed in the RMAIL buffer, but the coding system used to
2942 decode it was incorrect. It then decodes the message again,
2943 using the coding system CODING."
2944 (interactive "zCoding system for re-decoding this message: ")
2945 (when (not rmail-enable-mime)
2946 (with-current-buffer rmail-buffer
2947 (rmail-swap-buffers-maybe)
2948 (save-restriction
2949 (widen)
2950 (let ((msgbeg (rmail-msgbeg rmail-current-message))
2951 (msgend (rmail-msgend rmail-current-message))
2952 (buffer-read-only nil)
2953 body-start x-coding-header old-coding)
2954 (narrow-to-region msgbeg msgend)
2955 (goto-char (point-min))
2956 (unless (setq body-start (search-forward "\n\n" (point-max) 1))
2957 (error "No message body"))
2958
2959 (save-restriction
2960 ;; Narrow to headers
2961 (narrow-to-region (point-min) body-start)
2962 (setq x-coding-header (goto-char (point-min)))
2963 (if (not (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t))
2964 (setq old-coding (rmail-get-coding-system))
2965 (setq old-coding (intern (match-string 1)))
2966 (setq x-coding-header (point)))
2967 (check-coding-system old-coding)
2968 ;; Make sure the new coding system uses the same EOL
2969 ;; conversion, to prevent ^M characters from popping up
2970 ;; all over the place.
2971 (let ((eol-type (coding-system-eol-type old-coding)))
2972 (if (numberp eol-type)
2973 (setq coding
2974 (coding-system-change-eol-conversion coding eol-type))))
2975 (when (not (coding-system-equal
2976 (coding-system-base old-coding)
2977 (coding-system-base coding)))
2978 ;; Rewrite the coding-system header.
2979 (goto-char x-coding-header)
2980 (if (> (point) (point-min))
2981 (delete-region (line-beginning-position) (point))
2982 (forward-line)
2983 (insert "\n")
2984 (forward-line -1))
2985 (insert "X-Coding-System: "
2986 (symbol-name coding))))
2987 (rmail-show-message))))))
2988
2989 (defun rmail-highlight-headers ()
2990 "Highlight the headers specified by `rmail-highlighted-headers'.
2991 Uses the face specified by `rmail-highlight-face'."
2992 (if rmail-highlighted-headers
2993 (save-excursion
2994 (search-forward "\n\n" nil 'move)
2995 (save-restriction
2996 (narrow-to-region (point-min) (point))
2997 (let ((case-fold-search t)
2998 (inhibit-read-only t)
2999 ;; When rmail-highlight-face is removed, just
3000 ;; use 'rmail-highlight here.
3001 (face (or rmail-highlight-face
3002 (if (face-differs-from-default-p 'bold)
3003 'bold 'highlight)))
3004 ;; List of overlays to reuse.
3005 (overlays rmail-overlay-list))
3006 (goto-char (point-min))
3007 (while (re-search-forward rmail-highlighted-headers nil t)
3008 (skip-chars-forward " \t")
3009 (let ((beg (point))
3010 overlay)
3011 (while (progn (forward-line 1)
3012 (looking-at "[ \t]")))
3013 ;; Back up over newline, then trailing spaces or tabs
3014 (forward-char -1)
3015 (while (member (preceding-char) '(? ?\t))
3016 (forward-char -1))
3017 (if overlays
3018 ;; Reuse an overlay we already have.
3019 (progn
3020 (setq overlay (car overlays)
3021 overlays (cdr overlays))
3022 (overlay-put overlay 'face face)
3023 (move-overlay overlay beg (point)))
3024 ;; Make a new overlay and add it to
3025 ;; rmail-overlay-list.
3026 (setq overlay (make-overlay beg (point)))
3027 (overlay-put overlay 'face face)
3028 (setq rmail-overlay-list
3029 (cons overlay rmail-overlay-list))))))))))
3030
3031 (defun rmail-auto-file ()
3032 "Automatically move a message into another sfolder based on criteria.
3033 This moves messages according to `rmail-automatic-folder-directives'.
3034 It only does something in the folder that `rmail-file-name' specifies.
3035 The function `rmail-show-message' calls this whenever it shows a message.
3036 This leaves a message alone if it already has the `filed' attribute."
3037 (if (or (zerop rmail-total-messages)
3038 (rmail-message-attr-p rmail-current-message "...F")
3039 (not (string= (buffer-file-name)
3040 (expand-file-name rmail-file-name))))
3041 ;; Do nothing if the message has already been filed or if there
3042 ;; are no messages.
3043 nil
3044 ;; Find out some basics (common fields)
3045 (let ((from (mail-fetch-field "from"))
3046 (subj (mail-fetch-field "subject"))
3047 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
3048 (d rmail-automatic-folder-directives)
3049 (directive-loop nil)
3050 (folder nil))
3051 (while d
3052 (setq folder (car (car d))
3053 directive-loop (cdr (car d)))
3054 (while (and (car directive-loop)
3055 (let ((f (cond
3056 ((string= (downcase (car directive-loop)) "from")
3057 from)
3058 ((string= (downcase (car directive-loop)) "to")
3059 to)
3060 ((string= (downcase (car directive-loop))
3061 "subject") subj)
3062 (t (mail-fetch-field (car directive-loop))))))
3063 ;; FIXME - shouldn't this ignore case?
3064 (and f (string-match (car (cdr directive-loop)) f))))
3065 (setq directive-loop (cdr (cdr directive-loop))))
3066 ;; If there are no directives left, then it was a complete match.
3067 (if (null directive-loop)
3068 (if (null folder)
3069 (rmail-delete-forward)
3070 (if (string= "/dev/null" folder)
3071 (rmail-delete-message)
3072 (rmail-output folder 1)
3073 (setq d nil))))
3074 (setq d (cdr d))))))
3075 \f
3076 ;; Simple message motion commands.
3077
3078 (defun rmail-next-message (n)
3079 "Show following message whether deleted or not.
3080 With prefix arg N, moves forward N messages, or backward if N is negative."
3081 (interactive "p")
3082 (set-buffer rmail-buffer)
3083 (rmail-maybe-set-message-counters)
3084 (rmail-show-message (+ rmail-current-message n)))
3085
3086 (defun rmail-previous-message (n)
3087 "Show previous message whether deleted or not.
3088 With prefix arg N, moves backward N messages, or forward if N is negative."
3089 (interactive "p")
3090 (rmail-next-message (- n)))
3091
3092 (defun rmail-next-undeleted-message (n)
3093 "Show following non-deleted message.
3094 With prefix arg N, moves forward N non-deleted messages,
3095 or backward if N is negative.
3096
3097 Returns t if a new message is being shown, nil otherwise."
3098 (interactive "p")
3099 (set-buffer rmail-buffer)
3100 (rmail-maybe-set-message-counters)
3101 (let ((lastwin rmail-current-message)
3102 (current rmail-current-message))
3103 (while (and (> n 0) (< current rmail-total-messages))
3104 (setq current (1+ current))
3105 (if (not (rmail-message-deleted-p current))
3106 (setq lastwin current n (1- n))))
3107 (while (and (< n 0) (> current 1))
3108 (setq current (1- current))
3109 (if (not (rmail-message-deleted-p current))
3110 (setq lastwin current n (1+ n))))
3111 (if (/= lastwin rmail-current-message)
3112 (progn (rmail-show-message lastwin)
3113 t)
3114 (if (< n 0)
3115 (message "No previous nondeleted message"))
3116 (if (> n 0)
3117 (message "No following nondeleted message"))
3118 nil)))
3119
3120 (defun rmail-previous-undeleted-message (n)
3121 "Show previous non-deleted message.
3122 With prefix argument N, moves backward N non-deleted messages,
3123 or forward if N is negative."
3124 (interactive "p")
3125 (rmail-next-undeleted-message (- n)))
3126
3127 (defun rmail-first-message ()
3128 "Show first message in file."
3129 (interactive)
3130 (rmail-maybe-set-message-counters)
3131 (rmail-show-message 1))
3132
3133 (defun rmail-last-message ()
3134 "Show last message in file."
3135 (interactive)
3136 (rmail-maybe-set-message-counters)
3137 (rmail-show-message rmail-total-messages))
3138
3139 (defun rmail-next-error-move (msg-pos bad-marker)
3140 "Move to an error locus (probably grep hit) in an Rmail buffer.
3141 MSG-POS is a marker pointing at the error message in the grep buffer.
3142 BAD-MARKER is a marker that ought to point at where to move to,
3143 but probably is garbage."
3144
3145 (let* ((message-loc (compilation--message->loc
3146 (get-text-property msg-pos 'compilation-message
3147 (marker-buffer msg-pos))))
3148 (column (car message-loc))
3149 (linenum (cadr message-loc))
3150 line-text
3151 pos
3152 msgnum msgbeg msgend
3153 header-field
3154 line-number-within)
3155
3156 ;; Look at the whole Rmail file.
3157 (rmail-swap-buffers-maybe)
3158
3159 (save-restriction
3160 (widen)
3161 (save-excursion
3162 ;; Find the line that the error message points at.
3163 (goto-char (point-min))
3164 (forward-line (1- linenum))
3165 (setq pos (point))
3166
3167 ;; Find the text at the start of the line,
3168 ;; before the first = sign.
3169 ;; This text has a good chance of being also in the
3170 ;; decoded message.
3171 (save-excursion
3172 (skip-chars-forward "^=\n")
3173 (setq line-text (buffer-substring pos (point))))
3174
3175 ;; Find which message this position is in,
3176 ;; and the limits of that message.
3177 (setq msgnum (rmail-what-message pos))
3178 (setq msgbeg (rmail-msgbeg msgnum))
3179 (setq msgend (rmail-msgend msgnum))
3180
3181 ;; Find which header this locus is in,
3182 ;; or if it's in the message body,
3183 ;; and the line-based position within that.
3184 (goto-char msgbeg)
3185 (let ((header-end msgend))
3186 (if (search-forward "\n\n" nil t)
3187 (setq header-end (point)))
3188 (if (>= pos header-end)
3189 (setq line-number-within
3190 (count-lines header-end pos))
3191 (goto-char pos)
3192 (unless (looking-at "^[^ \t]")
3193 (re-search-backward "^[^ \t]"))
3194 (looking-at "[^:\n]*[:\n]")
3195 (setq header-field (match-string 0)
3196 line-number-within (count-lines (point) pos))))))
3197
3198 ;; Display the right message.
3199 (rmail-show-message msgnum)
3200
3201 ;; Move to the right position within the displayed message.
3202 ;; Or at least try. The decoded message's lines may not
3203 ;; correspond to the lines in the inbox file.
3204 (goto-char (point-min))
3205 (if header-field
3206 (progn
3207 (re-search-forward (concat "^" (regexp-quote header-field)) nil t)
3208 (forward-line line-number-within))
3209 (search-forward "\n\n" nil t)
3210 (if (re-search-forward (concat "^" (regexp-quote line-text)) nil t)
3211 (goto-char (match-beginning 0))))
3212 (if (eobp)
3213 ;; If the decoded message doesn't have enough lines,
3214 ;; go to the beginning rather than the end.
3215 (goto-char (point-min))
3216 ;; Otherwise, go to the right column.
3217 (if column
3218 (forward-char column)))))
3219
3220 (defun rmail-what-message (&optional pos)
3221 "Return message number POS (or point) is in."
3222 (let* ((high rmail-total-messages)
3223 (mid (/ high 2))
3224 (low 1)
3225 (where (or pos
3226 (with-current-buffer (if (rmail-buffers-swapped-p)
3227 rmail-view-buffer
3228 (current-buffer))
3229 (point)))))
3230 (while (> (- high low) 1)
3231 (if (>= where (rmail-msgbeg mid))
3232 (setq low mid)
3233 (setq high mid))
3234 (setq mid (+ low (/ (- high low) 2))))
3235 (if (>= where (rmail-msgbeg high)) high low)))
3236 \f
3237 ;; Searching in Rmail file.
3238
3239 (defun rmail-search-message (msg regexp)
3240 "Return non-nil, if for message number MSG, regexp REGEXP matches."
3241 ;; This is adequate because its only caller, rmail-search,
3242 ;; unswaps the buffers.
3243 (goto-char (rmail-msgbeg msg))
3244 (if (and rmail-enable-mime
3245 rmail-search-mime-message-function)
3246 (funcall rmail-search-mime-message-function msg regexp)
3247 (re-search-forward regexp (rmail-msgend msg) t)))
3248
3249 (defvar rmail-search-last-regexp nil)
3250 (defun rmail-search (regexp &optional n)
3251 "Show message containing next match for REGEXP (but not the current msg).
3252 Prefix argument gives repeat count; negative argument means search
3253 backwards (through earlier messages).
3254 Interactively, empty argument means use same regexp used last time."
3255 (interactive
3256 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
3257 (prompt
3258 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
3259 regexp)
3260 (setq prompt
3261 (concat prompt
3262 (if rmail-search-last-regexp
3263 (concat ", default "
3264 rmail-search-last-regexp "): ")
3265 "): ")))
3266 (setq regexp (read-string prompt))
3267 (cond ((not (equal regexp ""))
3268 (setq rmail-search-last-regexp regexp))
3269 ((not rmail-search-last-regexp)
3270 (error "No previous Rmail search string")))
3271 (list rmail-search-last-regexp
3272 (prefix-numeric-value current-prefix-arg))))
3273 (or n (setq n 1))
3274 (message "%sRmail search for %s..."
3275 (if (< n 0) "Reverse " "")
3276 regexp)
3277 (set-buffer rmail-buffer)
3278 (let ((orig-message rmail-current-message)
3279 (msg rmail-current-message)
3280 (reversep (< n 0))
3281 (opoint (if (rmail-buffers-swapped-p) (point)))
3282 found)
3283 (rmail-swap-buffers-maybe)
3284 (rmail-maybe-set-message-counters)
3285 (widen)
3286 (unwind-protect
3287 (while (/= n 0)
3288 ;; Check messages one by one, advancing message number up or
3289 ;; down but searching forward through each message.
3290 (if reversep
3291 (while (and (null found) (> msg 1))
3292 (setq msg (1- msg)
3293 found (rmail-search-message msg regexp)))
3294 (while (and (null found) (< msg rmail-total-messages))
3295 (setq msg (1+ msg)
3296 found (rmail-search-message msg regexp))))
3297 (setq n (+ n (if reversep 1 -1))))
3298 (if found
3299 (progn
3300 (rmail-show-message msg)
3301 ;; Search forward (if this is a normal search) or backward
3302 ;; (if this is a reverse search) through this message to
3303 ;; position point. This search may fail because REGEXP
3304 ;; was found in the hidden portion of this message. In
3305 ;; that case, move point to the beginning of visible
3306 ;; portion.
3307 (if reversep
3308 (progn
3309 (goto-char (point-max))
3310 (re-search-backward regexp nil 'move))
3311 (goto-char (point-min))
3312 (re-search-forward regexp nil t))
3313 (message "%sRmail search for %s...done"
3314 (if reversep "Reverse " "")
3315 regexp))
3316 (rmail-show-message orig-message)
3317 (if opoint (goto-char opoint))
3318 (ding)
3319 (message "Search failed: %s" regexp)))))
3320
3321 (defun rmail-search-backwards (regexp &optional n)
3322 "Show message containing previous match for REGEXP.
3323 Prefix argument gives repeat count; negative argument means search
3324 forward (through later messages).
3325 Interactively, empty argument means use same regexp used last time."
3326 (interactive
3327 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
3328 (prompt
3329 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
3330 regexp)
3331 (setq prompt
3332 (concat prompt
3333 (if rmail-search-last-regexp
3334 (concat ", default "
3335 rmail-search-last-regexp "): ")
3336 "): ")))
3337 (setq regexp (read-string prompt))
3338 (cond ((not (equal regexp ""))
3339 (setq rmail-search-last-regexp regexp))
3340 ((not rmail-search-last-regexp)
3341 (error "No previous Rmail search string")))
3342 (list rmail-search-last-regexp
3343 (prefix-numeric-value current-prefix-arg))))
3344 (rmail-search regexp (- (or n 1))))
3345 \f
3346 ;; Scan for attributes, and compare subjects.
3347
3348 (defun rmail-first-unseen-message ()
3349 "Return message number of first message which has `unseen' attribute."
3350 (rmail-maybe-set-message-counters)
3351 (let ((current 1)
3352 found)
3353 (save-restriction
3354 (widen)
3355 (while (and (not found) (<= current rmail-total-messages))
3356 (if (rmail-message-attr-p current "......U")
3357 (setq found current))
3358 (setq current (1+ current))))
3359 found))
3360
3361 (defun rmail-simplified-subject (&optional msgnum)
3362 "Return the simplified subject of message MSGNUM (or current message).
3363 Simplifying the subject means stripping leading and trailing whitespace,
3364 and typical reply prefixes such as Re:."
3365 (let ((subject (or (rmail-get-header "Subject" msgnum) "")))
3366 (setq subject (rfc2047-decode-string subject))
3367 (if (string-match "\\`[ \t]+" subject)
3368 (setq subject (substring subject (match-end 0))))
3369 (if (string-match rmail-reply-regexp subject)
3370 (setq subject (substring subject (match-end 0))))
3371 (if (string-match "[ \t]+\\'" subject)
3372 (setq subject (substring subject 0 (match-beginning 0))))
3373 ;; If Subject is long, mailers will break it into several lines at
3374 ;; arbitrary places, so normalize whitespace by replacing every
3375 ;; run of whitespace characters with a single space.
3376 (setq subject (replace-regexp-in-string "[ \t\n]+" " " subject))
3377 subject))
3378
3379 (defun rmail-simplified-subject-regexp ()
3380 "Return a regular expression matching the current simplified subject.
3381 The idea is to match it against simplified subjects of other messages."
3382 (let ((subject (rmail-simplified-subject)))
3383 (setq subject (regexp-quote subject))
3384 ;; Hide commas so it will work ok if parsed as a comma-separated list
3385 ;; of regexps.
3386 (setq subject
3387 (replace-regexp-in-string "," "\054" subject t t))
3388 (concat "\\`" subject "\\'")))
3389
3390 (defun rmail-next-same-subject (n)
3391 "Go to the next mail message having the same subject header.
3392 With prefix argument N, do this N times.
3393 If N is negative, go backwards instead."
3394 (interactive "p")
3395 (let ((subject (rmail-simplified-subject))
3396 (forward (> n 0))
3397 (i rmail-current-message)
3398 found)
3399 (while (and (/= n 0)
3400 (if forward
3401 (< i rmail-total-messages)
3402 (> i 1)))
3403 (let (done)
3404 (while (and (not done)
3405 (if forward
3406 (< i rmail-total-messages)
3407 (> i 1)))
3408 (setq i (if forward (1+ i) (1- i)))
3409 (setq done (string-equal subject (rmail-simplified-subject i))))
3410 (if done (setq found i)))
3411 (setq n (if forward (1- n) (1+ n))))
3412 (if found
3413 (rmail-show-message found)
3414 (error "No %s message with same subject"
3415 (if forward "following" "previous")))))
3416
3417 (defun rmail-previous-same-subject (n)
3418 "Go to the previous mail message having the same subject header.
3419 With prefix argument N, do this N times.
3420 If N is negative, go forwards instead."
3421 (interactive "p")
3422 (rmail-next-same-subject (- n)))
3423 \f
3424 ;;;; *** Rmail Message Deletion Commands ***
3425
3426 (defun rmail-message-deleted-p (n)
3427 "Return non-nil if message number N is deleted (in `rmail-deleted-vector')."
3428 (= (aref rmail-deleted-vector n) ?D))
3429
3430 (defun rmail-set-message-deleted-p (n state)
3431 "Set the deleted state of message number N (in `rmail-deleted-vector').
3432 STATE non-nil means mark as deleted."
3433 (aset rmail-deleted-vector n (if state ?D ?\s)))
3434
3435 (defun rmail-delete-message ()
3436 "Delete this message and stay on it."
3437 (interactive)
3438 (rmail-set-attribute rmail-deleted-attr-index t)
3439 (run-hooks 'rmail-delete-message-hook))
3440
3441 (defun rmail-undelete-previous-message ()
3442 "Back up to deleted message, select it, and undelete it."
3443 (interactive)
3444 (set-buffer rmail-buffer)
3445 (let ((msg rmail-current-message))
3446 (while (and (> msg 0)
3447 (not (rmail-message-deleted-p msg)))
3448 (setq msg (1- msg)))
3449 (if (= msg 0)
3450 (error "No previous deleted message")
3451 (if (/= msg rmail-current-message)
3452 (rmail-show-message msg))
3453 (rmail-set-attribute rmail-deleted-attr-index nil)
3454 (if (rmail-summary-exists)
3455 (with-current-buffer rmail-summary-buffer
3456 (rmail-summary-mark-undeleted msg)))
3457 (rmail-maybe-display-summary))))
3458
3459 (defun rmail-delete-forward (&optional backward)
3460 "Delete this message and move to next nondeleted one.
3461 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
3462 With prefix argument, delete and move backward.
3463
3464 Returns t if a new message is displayed after the delete, or nil otherwise."
3465 (interactive "P")
3466 (rmail-set-attribute rmail-deleted-attr-index t)
3467 (run-hooks 'rmail-delete-message-hook)
3468 (let ((del-msg rmail-current-message))
3469 (if (rmail-summary-exists)
3470 (rmail-select-summary
3471 (rmail-summary-mark-deleted del-msg)))
3472 (prog1 (rmail-next-undeleted-message (if backward -1 1))
3473 (rmail-maybe-display-summary))))
3474
3475 (defun rmail-delete-backward ()
3476 "Delete this message and move to previous nondeleted one.
3477 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
3478 (interactive)
3479 (rmail-delete-forward t))
3480 \f
3481 ;; Expunging.
3482
3483 ;; Compute the message number a given message would have after expunging.
3484 ;; The present number of the message is OLDNUM.
3485 ;; DELETEDVEC should be rmail-deleted-vector.
3486 ;; The value is nil for a message that would be deleted.
3487 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
3488 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
3489 nil
3490 (let ((i 0)
3491 (newnum 0))
3492 (while (< i oldnum)
3493 (if (/= (aref deletedvec i) ?D)
3494 (setq newnum (1+ newnum)))
3495 (setq i (1+ i)))
3496 newnum)))
3497
3498 (defun rmail-expunge-confirmed ()
3499 "Return t if expunge is needed and desirable.
3500 If `rmail-confirm-expunge' is non-nil, ask user to confirm."
3501 (set-buffer rmail-buffer)
3502 (and (stringp rmail-deleted-vector)
3503 (string-match "D" rmail-deleted-vector)
3504 (if rmail-confirm-expunge
3505 (funcall rmail-confirm-expunge
3506 "Erase deleted messages from Rmail file? ")
3507 t)))
3508
3509 (defun rmail-only-expunge (&optional dont-show)
3510 "Actually erase all deleted messages in the file."
3511 (interactive)
3512 (rmail-swap-buffers-maybe)
3513 (set-buffer rmail-buffer)
3514 (message "Expunging deleted messages...")
3515 ;; Discard all undo records for this buffer.
3516 (or (eq buffer-undo-list t)
3517 (setq buffer-undo-list nil))
3518 (rmail-maybe-set-message-counters)
3519 (let* ((omax (- (buffer-size) (point-max)))
3520 (omin (- (buffer-size) (point-min)))
3521 (opoint (if (and (> rmail-current-message 0)
3522 (rmail-message-deleted-p rmail-current-message))
3523 0
3524 (if rmail-enable-mime
3525 (with-current-buffer rmail-view-buffer
3526 (- (point)(point-min)))
3527 (- (point) (point-min)))))
3528 (messages-head (cons (aref rmail-message-vector 0) nil))
3529 (messages-tail messages-head)
3530 ;; Don't make any undo records for the expunging.
3531 (buffer-undo-list t)
3532 (win))
3533 (unwind-protect
3534 (save-excursion
3535 (widen)
3536 (goto-char (point-min))
3537 (let ((counter 0)
3538 (number 1)
3539 new-summary
3540 (new-msgref (list (list 0)))
3541 (buffer-read-only nil)
3542 (total rmail-total-messages)
3543 (new-message-number rmail-current-message)
3544 (messages rmail-message-vector)
3545 (deleted rmail-deleted-vector)
3546 (summary rmail-summary-vector))
3547 (setq rmail-total-messages nil
3548 rmail-current-message nil
3549 rmail-message-vector nil
3550 rmail-deleted-vector nil
3551 rmail-summary-vector nil)
3552
3553 (while (<= number total)
3554 (if (= (aref deleted number) ?D)
3555 (progn
3556 (delete-region (aref messages number)
3557 (aref messages (1+ number)))
3558 (move-marker (aref messages number) nil)
3559 (if (> new-message-number counter)
3560 (setq new-message-number (1- new-message-number))))
3561 (setq counter (1+ counter))
3562 (setq messages-tail
3563 (setcdr messages-tail
3564 (cons (aref messages number) nil)))
3565 (setq new-summary
3566 (cons (if (= counter number) (aref summary (1- number)))
3567 new-summary))
3568 (setq new-msgref
3569 (cons (aref rmail-msgref-vector number)
3570 new-msgref))
3571 (setcar (car new-msgref) counter))
3572 (if (zerop (% (setq number (1+ number)) 20))
3573 (message "Expunging deleted messages...%d" number)))
3574 (setq messages-tail
3575 (setcdr messages-tail
3576 (cons (aref messages number) nil)))
3577 (setq rmail-current-message new-message-number
3578 rmail-total-messages counter
3579 rmail-message-vector (apply 'vector messages-head)
3580 rmail-deleted-vector (make-string (1+ counter) ?\s)
3581 rmail-summary-vector (vconcat (nreverse new-summary))
3582 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
3583 win t)))
3584 (message "Expunging deleted messages...done")
3585 (if (not win)
3586 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
3587 (if (not dont-show)
3588 (rmail-show-message (min rmail-current-message rmail-total-messages)))
3589 (if rmail-enable-mime
3590 (goto-char (+ (point-min) opoint))
3591 (goto-char (+ (point) opoint))))))
3592
3593 ;; The DONT-SHOW argument is new in 23. Does not seem very important.
3594 (defun rmail-expunge (&optional dont-show)
3595 "Erase deleted messages from Rmail file and summary buffer.
3596 This always shows a message (so as not to leave the Rmail buffer
3597 unswapped), and always updates any summary (so that it remains
3598 consistent with the Rmail buffer). If DONT-SHOW is non-nil, it
3599 does not pop any summary buffer."
3600 (interactive)
3601 (when (rmail-expunge-confirmed)
3602 (rmail-modify-format)
3603 (let ((was-deleted (rmail-message-deleted-p rmail-current-message))
3604 (was-swapped (rmail-buffers-swapped-p)))
3605 (rmail-only-expunge t)
3606 ;; We always update the summary buffer, so that the contents
3607 ;; remain consistent with the rmail buffer.
3608 ;; The only difference is, in the dont-show case, we use a
3609 ;; cut-down version of rmail-select-summary that does not pop
3610 ;; the summary buffer. It's only used by rmail-quit, which is
3611 ;; just going to bury any summary immediately after. If we made
3612 ;; rmail-quit bury the summary first, dont-show could be removed.
3613 ;; But the expunge might not be confirmed...
3614 (if (rmail-summary-exists)
3615 (if dont-show
3616 (let ((total rmail-total-messages))
3617 (with-current-buffer rmail-summary-buffer
3618 (let ((rmail-total-messages total))
3619 (rmail-update-summary))))
3620 (rmail-select-summary (rmail-update-summary))))
3621 ;; We always show a message, because (rmail-only-expunge t)
3622 ;; leaves the rmail buffer unswapped.
3623 ;; If we expunged the current message, a new one is current now,
3624 ;; so show it. If we weren't showing a message, show it.
3625 (if (or was-deleted (not was-swapped))
3626 (rmail-show-message-1 rmail-current-message)
3627 ;; We can just show the same message that was being shown before.
3628 (rmail-display-labels)
3629 (rmail-swap-buffers)
3630 (setq rmail-buffer-swapped t)))))
3631 \f
3632 ;;;; *** Rmail Mailing Commands ***
3633
3634 (defun rmail-yank-current-message (buffer)
3635 "Yank into the current buffer the current message of Rmail buffer BUFFER.
3636 If BUFFER is swapped with its message viewer buffer, yank out of BUFFER.
3637 If BUFFER is not swapped, yank out of its message viewer buffer."
3638 (with-current-buffer buffer
3639 (unless (rmail-buffers-swapped-p)
3640 (setq buffer rmail-view-buffer)))
3641 (insert-buffer-substring buffer)
3642 ;; If they yank the text of BUFFER, the encoding of BUFFER is a
3643 ;; better default for the reply message than the default value of
3644 ;; buffer-file-coding-system.
3645 (and (coding-system-equal (default-value 'buffer-file-coding-system)
3646 buffer-file-coding-system)
3647 (setq buffer-file-coding-system
3648 (coding-system-change-text-conversion
3649 buffer-file-coding-system (coding-system-base
3650 (with-current-buffer buffer
3651 buffer-file-coding-system))))))
3652
3653 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3654 replybuffer sendactions same-window
3655 other-headers)
3656 (let ((switch-function
3657 (cond (same-window nil)
3658 (rmail-mail-new-frame 'switch-to-buffer-other-frame)
3659 (t 'switch-to-buffer-other-window)))
3660 yank-action)
3661 (if replybuffer
3662 ;; The function used here must behave like insert-buffer wrt
3663 ;; point and mark (see doc of sc-cite-original).
3664 (setq yank-action
3665 `(rmail-yank-current-message ,replybuffer)))
3666 (push (cons "cc" cc) other-headers)
3667 (push (cons "in-reply-to" in-reply-to) other-headers)
3668 (setq other-headers
3669 (mapcar #'(lambda (elt)
3670 (cons (car elt) (if (stringp (cdr elt))
3671 (rfc2047-decode-string (cdr elt)))))
3672 other-headers))
3673 (if (stringp to) (setq to (rfc2047-decode-string to)))
3674 (if (stringp in-reply-to)
3675 (setq in-reply-to (rfc2047-decode-string in-reply-to)))
3676 (if (stringp cc) (setq cc (rfc2047-decode-string cc)))
3677 (if (stringp subject) (setq subject (rfc2047-decode-string subject)))
3678 (prog1
3679 (compose-mail to subject other-headers noerase
3680 switch-function yank-action sendactions
3681 (if replybuffer `(rmail-mail-return ,replybuffer)))
3682 (if (eq switch-function 'switch-to-buffer-other-frame)
3683 ;; This is not a standard frame parameter; nothing except
3684 ;; sendmail.el looks at it.
3685 (modify-frame-parameters (selected-frame)
3686 '((mail-dedicated-frame . t)))))))
3687
3688 (defun rmail-mail-return (&optional newbuf)
3689 "Try to return to Rmail from the mail window.
3690 If optional argument NEWBUF is specified, it is the Rmail buffer
3691 to switch to."
3692 (cond
3693 ;; If there is only one visible frame with no special handling,
3694 ;; consider deleting the mail window to return to Rmail.
3695 ((or (null (delq (selected-frame) (visible-frame-list)))
3696 (not (or (window-dedicated-p (frame-selected-window))
3697 (and pop-up-frames (one-window-p))
3698 (cdr (assq 'mail-dedicated-frame
3699 (frame-parameters))))))
3700 (let (rmail-flag summary-buffer)
3701 (unless (one-window-p)
3702 (with-current-buffer
3703 (window-buffer (next-window (selected-window) 'not))
3704 (setq rmail-flag (eq major-mode 'rmail-mode))
3705 (setq summary-buffer
3706 (and (boundp 'mail-bury-selects-summary)
3707 mail-bury-selects-summary
3708 (boundp 'rmail-summary-buffer)
3709 rmail-summary-buffer
3710 (buffer-name rmail-summary-buffer)
3711 (not (get-buffer-window rmail-summary-buffer))
3712 rmail-summary-buffer))))
3713 (cond ((null rmail-flag)
3714 ;; If the Rmail buffer is not in the next window, switch
3715 ;; directly to the Rmail buffer specified by NEWBUF.
3716 (if (buffer-live-p newbuf)
3717 (switch-to-buffer newbuf)))
3718 ;; If the Rmail buffer is in the next window, switch to
3719 ;; the summary buffer if `mail-bury-selects-summary' is
3720 ;; non-nil. Otherwise just delete this window.
3721 (summary-buffer
3722 (switch-to-buffer summary-buffer))
3723 (t
3724 (delete-window)))))
3725 ;; If the frame was probably made for this buffer, the user
3726 ;; probably wants to delete it now.
3727 ((display-multi-frame-p)
3728 (delete-frame))
3729 ;; The previous frame is where normally they have the Rmail buffer
3730 ;; displayed.
3731 (t (other-frame -1))))
3732
3733 (defun rmail-mail ()
3734 "Send mail in another window.
3735 While composing the message, use \\[mail-yank-original] to yank the
3736 original message into it."
3737 (interactive)
3738 (rmail-start-mail nil nil nil nil nil rmail-buffer))
3739
3740 ;; FIXME should complain if there is nothing to continue.
3741 (defun rmail-continue ()
3742 "Continue composing outgoing message previously being composed."
3743 (interactive)
3744 (rmail-start-mail t))
3745
3746 (defun rmail-reply (just-sender)
3747 "Reply to the current message.
3748 Normally include CC: to all other recipients of original message;
3749 prefix argument means ignore them. While composing the reply,
3750 use \\[mail-yank-original] to yank the original message into it."
3751 (interactive "P")
3752 (if (zerop rmail-current-message)
3753 (error "There is no message to reply to"))
3754 (let (from reply-to cc subject date to message-id references
3755 resent-to resent-cc resent-reply-to
3756 (msgnum rmail-current-message))
3757 (rmail-apply-in-message
3758 rmail-current-message
3759 (lambda ()
3760 (search-forward "\n\n" nil 'move)
3761 (narrow-to-region (point-min) (point))
3762 (setq from (mail-fetch-field "from")
3763 reply-to (or (mail-fetch-field "mail-reply-to" nil t)
3764 (mail-fetch-field "reply-to" nil t)
3765 from)
3766 subject (mail-fetch-field "subject")
3767 date (mail-fetch-field "date")
3768 message-id (mail-fetch-field "message-id")
3769 references (mail-fetch-field "references" nil nil t)
3770 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
3771 ;; Bug#512. It's inappropriate to reply to these addresses.
3772 ;;; resent-cc (and (not just-sender)
3773 ;;; (mail-fetch-field "resent-cc" nil t))
3774 ;;; resent-to (or (mail-fetch-field "resent-to" nil t) "")
3775 ;;; resent-subject (mail-fetch-field "resent-subject")
3776 ;;; resent-date (mail-fetch-field "resent-date")
3777 ;;; resent-message-id (mail-fetch-field "resent-message-id")
3778 )
3779 (unless just-sender
3780 (if (mail-fetch-field "mail-followup-to" nil t)
3781 ;; If this header field is present, use it instead of the
3782 ;; To and CC fields.
3783 (setq to (mail-fetch-field "mail-followup-to" nil t))
3784 (setq cc (or (mail-fetch-field "cc" nil t) "")
3785 to (or (mail-fetch-field "to" nil t) ""))))))
3786 ;; Merge the resent-to and resent-cc into the to and cc.
3787 ;; Bug#512. It's inappropriate to reply to these addresses.
3788 ;;; (if (and resent-to (not (equal resent-to "")))
3789 ;;; (if (not (equal to ""))
3790 ;;; (setq to (concat to ", " resent-to))
3791 ;;; (setq to resent-to)))
3792 ;;; (if (and resent-cc (not (equal resent-cc "")))
3793 ;;; (if (not (equal cc ""))
3794 ;;; (setq cc (concat cc ", " resent-cc))
3795 ;;; (setq cc resent-cc)))
3796 ;; Add `Re: ' to subject if not there already.
3797 (and (stringp subject)
3798 (setq subject
3799 (concat rmail-reply-prefix
3800 (if (let ((case-fold-search t))
3801 (string-match rmail-reply-regexp subject))
3802 (substring subject (match-end 0))
3803 subject))))
3804 (rmail-start-mail
3805 nil
3806 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3807 ;; since they can handle the names unstripped.
3808 ;; I don't know whether there are other mailers that still
3809 ;; need the names to be stripped.
3810 ;;; (mail-strip-quoted-names reply-to)
3811 ;; Remove unwanted names from reply-to, since Mail-Followup-To
3812 ;; header causes all the names in it to wind up in reply-to, not
3813 ;; in cc. But if what's left is an empty list, use the original.
3814 (let* ((reply-to-list (mail-dont-reply-to reply-to)))
3815 (if (string= reply-to-list "") reply-to reply-to-list))
3816 subject
3817 (rmail-make-in-reply-to-field from date message-id)
3818 (if just-sender
3819 nil
3820 ;; `mail-dont-reply-to' doesn't need `mail-strip-quoted-names'.
3821 (let* ((cc-list (mail-dont-reply-to
3822 (mail-strip-quoted-names
3823 (if (null cc) to (concat to ", " cc))))))
3824 (if (string= cc-list "") nil cc-list)))
3825 rmail-buffer
3826 (list (list 'rmail-mark-message
3827 rmail-buffer
3828 (with-current-buffer rmail-buffer
3829 (aref rmail-msgref-vector msgnum))
3830 rmail-answered-attr-index))
3831 nil
3832 (if (or references message-id)
3833 (list (cons "References" (if references
3834 (concat
3835 (mapconcat 'identity references " ")
3836 " " message-id)
3837 message-id)))))))
3838 \f
3839 (defun rmail-mark-message (buffer msgnum-list attribute)
3840 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3841 This is use in the send-actions for message buffers.
3842 MSGNUM-LIST is a list of the form (MSGNUM)
3843 which is an element of rmail-msgref-vector."
3844 (with-current-buffer buffer
3845 (if (car msgnum-list)
3846 (rmail-set-attribute attribute t (car msgnum-list)))))
3847
3848 (defun rmail-make-in-reply-to-field (from date message-id)
3849 (cond ((not from)
3850 (if message-id
3851 message-id
3852 nil))
3853 (mail-use-rfc822
3854 (require 'rfc822)
3855 (let ((tem (car (rfc822-addresses from))))
3856 (if message-id
3857 (if (or (not tem)
3858 (string-match
3859 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3860 (substring tem 0
3861 (match-beginning 0))
3862 tem))
3863 message-id))
3864 ;; missing From, or Message-ID is sufficiently informative
3865 message-id
3866 (concat message-id " (" tem ")"))
3867 ;; Copy TEM, discarding text properties.
3868 (setq tem (copy-sequence tem))
3869 (set-text-properties 0 (length tem) nil tem)
3870 (setq tem (copy-sequence tem))
3871 ;; Use prin1 to fake RFC822 quoting
3872 (let ((field (prin1-to-string tem)))
3873 (if date
3874 (concat field "'s message of " date)
3875 field)))))
3876 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3877 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3878 ;; These strings both match all non-ASCII characters.
3879 (or (string-match (concat "\\`[ \t]*\\(" bar
3880 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3881 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3882 from)
3883 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3884 bar "\\))[ \t]*\\'")
3885 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3886 from)))
3887 (let ((start (match-beginning 1))
3888 (end (match-end 1)))
3889 ;; Trim whitespace which above regexp match allows
3890 (while (and (< start end)
3891 (memq (aref from start) '(?\t ?\s)))
3892 (setq start (1+ start)))
3893 (while (and (< start end)
3894 (memq (aref from (1- end)) '(?\t ?\s)))
3895 (setq end (1- end)))
3896 (let ((field (substring from start end)))
3897 (if date (setq field (concat "message from " field " on " date)))
3898 (if message-id
3899 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3900 (concat message-id " (" field ")")
3901 field))))
3902 (t
3903 ;; If we can't kludge it simply, do it correctly
3904 (let ((mail-use-rfc822 t))
3905 (rmail-make-in-reply-to-field from date message-id)))))
3906 \f
3907 (defun rmail-forward (resend)
3908 "Forward the current message to another user.
3909 With prefix argument, \"resend\" the message instead of forwarding it;
3910 see the documentation of `rmail-resend'."
3911 (interactive "P")
3912 (if (zerop rmail-current-message)
3913 (error "No message to forward"))
3914 (if resend
3915 (call-interactively 'rmail-resend)
3916 (let ((forward-buffer rmail-buffer)
3917 (msgnum rmail-current-message)
3918 (subject (concat "["
3919 (let ((from (or (mail-fetch-field "From")
3920 ;; FIXME - huh?
3921 (mail-fetch-field ">From"))))
3922 (if from
3923 (concat (mail-strip-quoted-names from) ": ")
3924 ""))
3925 (or (mail-fetch-field "Subject") "")
3926 "]")))
3927 (if (rmail-start-mail
3928 nil nil subject nil nil rmail-buffer
3929 (list (list 'rmail-mark-message
3930 forward-buffer
3931 (with-current-buffer rmail-buffer
3932 (aref rmail-msgref-vector msgnum))
3933 rmail-forwarded-attr-index))
3934 ;; If only one window, use it for the mail buffer.
3935 ;; Otherwise, use another window for the mail buffer
3936 ;; so that the Rmail buffer remains visible
3937 ;; and sending the mail will get back to it.
3938 (and (not rmail-mail-new-frame) (one-window-p t)))
3939 ;; The mail buffer is now current.
3940 (save-excursion
3941 ;; Insert after header separator--before signature if any.
3942 (rfc822-goto-eoh)
3943 (forward-line 1)
3944 (if (and rmail-enable-mime rmail-enable-mime-composing
3945 rmail-insert-mime-forwarded-message-function)
3946 (prog1
3947 (funcall rmail-insert-mime-forwarded-message-function
3948 forward-buffer)
3949 ;; rmail-insert-mime-forwarded-message-function
3950 ;; works by inserting MML tags into forward-buffer.
3951 ;; The MUA will need to convert it to MIME before
3952 ;; sending. mail-encode-mml tells them to do that.
3953 ;; message.el does that automagically.
3954 (or (eq mail-user-agent 'message-user-agent)
3955 (setq mail-encode-mml t)))
3956 (insert "------- Start of forwarded message -------\n")
3957 ;; Quote lines with `- ' if they start with `-'.
3958 (let ((beg (point)) end)
3959 (setq end (point-marker))
3960 (set-marker-insertion-type end t)
3961 (insert-buffer-substring forward-buffer)
3962 (goto-char beg)
3963 (while (re-search-forward "^-" end t)
3964 (beginning-of-line)
3965 (insert "- ")
3966 (forward-line 1))
3967 (goto-char end)
3968 (skip-chars-backward "\n")
3969 (if (< (point) end)
3970 (forward-char 1))
3971 (delete-region (point) end)
3972 (set-marker end nil))
3973 (insert "------- End of forwarded message -------\n"))
3974 (push-mark))))))
3975 \f
3976 (defun rmail-resend (address &optional from comment mail-alias-file)
3977 "Resend current message to ADDRESSES.
3978 ADDRESSES should be a single address, a string consisting of several
3979 addresses separated by commas, or a list of addresses.
3980
3981 Optional FROM is the address to resend the message from, and
3982 defaults from the value of `user-mail-address'.
3983 Optional COMMENT is a string to insert as a comment in the resent message.
3984 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3985 typically for purposes of moderating a list."
3986 (interactive "sResend to: ")
3987 (require 'sendmail)
3988 (require 'mailalias)
3989 (unless (or (eq rmail-view-buffer (current-buffer))
3990 (eq rmail-buffer (current-buffer)))
3991 (error "Not an Rmail buffer"))
3992 (if (not from) (setq from user-mail-address))
3993 (let ((tembuf (generate-new-buffer " sendmail temp"))
3994 (case-fold-search nil)
3995 (mail-personal-alias-file
3996 (or mail-alias-file mail-personal-alias-file))
3997 (mailbuf rmail-buffer))
3998 (unwind-protect
3999 (with-current-buffer tembuf
4000 ;;>> Copy message into temp buffer
4001 (if (and rmail-enable-mime
4002 rmail-insert-mime-resent-message-function)
4003 (funcall rmail-insert-mime-resent-message-function mailbuf)
4004 (insert-buffer-substring mailbuf))
4005 (goto-char (point-min))
4006 ;; Delete any Sender field, since that's not specifiable.
4007 ; Only delete Sender fields in the actual header.
4008 (re-search-forward "^$" nil 'move)
4009 ; Using "while" here rather than "if" because some buggy mail
4010 ; software may have inserted multiple Sender fields.
4011 (while (re-search-backward "^Sender:" nil t)
4012 (let (beg)
4013 (setq beg (point))
4014 (forward-line 1)
4015 (while (looking-at "[ \t]")
4016 (forward-line 1))
4017 (delete-region beg (point))))
4018 ; Go back to the beginning of the buffer so the Resent- fields
4019 ; are inserted there.
4020 (goto-char (point-min))
4021 ;;>> Insert resent-from:
4022 (insert "Resent-From: " from "\n")
4023 (insert "Resent-Date: " (mail-rfc822-date) "\n")
4024 ;;>> Insert resent-to: and bcc if need be.
4025 (let ((before (point)))
4026 (if mail-self-blind
4027 (insert "Resent-Bcc: " (user-login-name) "\n"))
4028 (insert "Resent-To: " (if (stringp address)
4029 address
4030 (mapconcat 'identity address ",\n\t"))
4031 "\n")
4032 ;; Expand abbrevs in the recipients.
4033 (save-excursion
4034 (if (featurep 'mailabbrev)
4035 (let ((end (point-marker))
4036 (local-abbrev-table mail-abbrevs)
4037 (old-syntax-table (syntax-table)))
4038 (if (and (not (vectorp mail-abbrevs))
4039 (file-exists-p mail-personal-alias-file))
4040 (build-mail-abbrevs))
4041 (unless mail-abbrev-syntax-table
4042 (mail-abbrev-make-syntax-table))
4043 (set-syntax-table mail-abbrev-syntax-table)
4044 (goto-char before)
4045 (while (and (< (point) end)
4046 (progn (forward-word 1)
4047 (<= (point) end)))
4048 (expand-abbrev))
4049 (set-syntax-table old-syntax-table))
4050 (expand-mail-aliases before (point)))))
4051 ;;>> Set up comment, if any.
4052 (if (and (sequencep comment) (not (zerop (length comment))))
4053 (let ((before (point))
4054 after)
4055 (insert comment)
4056 (or (eolp) (insert "\n"))
4057 (setq after (point))
4058 (goto-char before)
4059 (while (< (point) after)
4060 (insert "Resent-Comment: ")
4061 (forward-line 1))))
4062 ;; Don't expand aliases in the destination fields
4063 ;; of the original message.
4064 (let (mail-aliases)
4065 (funcall send-mail-function)))
4066 (kill-buffer tembuf))
4067 (with-current-buffer rmail-buffer
4068 (rmail-set-attribute rmail-resent-attr-index t rmail-current-message))))
4069 \f
4070 (defvar mail-unsent-separator
4071 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
4072 "^ *---+ +Returned message +---+ *$\\|"
4073 "^ *---+ *Returned mail follows *---+ *$\\|"
4074 "^Start of returned message$\\|"
4075 "^---+ Below this line is a copy of the message.$\\|"
4076 "^ *---+ +Original message +---+ *$\\|"
4077 "^ *--+ +begin message +--+ *$\\|"
4078 "^ *---+ +Original message follows +---+ *$\\|"
4079 "^ *---+ +Your message follows +---+ *$\\|"
4080 "^|? *---+ +Message text follows: +---+ *|?$\\|"
4081 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
4082 "A regexp that matches the separator before the text of a failed message.")
4083
4084 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
4085 "A regexp that matches the header of a MIME body part with a failed message.")
4086
4087 ;; This is a cut-down version of rmail-clear-headers from Emacs 22.
4088 ;; It doesn't have the same functionality, hence the name change.
4089 (defun rmail-delete-headers (regexp)
4090 "Delete any mail headers matching REGEXP.
4091 The message should be narrowed to just the headers."
4092 (when regexp
4093 (goto-char (point-min))
4094 (while (re-search-forward regexp nil t)
4095 (beginning-of-line)
4096 ;; This code from Emacs 22 doesn't seem right, since r-n-h is
4097 ;; just for display.
4098 ;;; (if (looking-at rmail-nonignored-headers)
4099 ;;; (forward-line 1)
4100 (delete-region (point)
4101 (save-excursion
4102 (if (re-search-forward "\n[^ \t]" nil t)
4103 (1- (point))
4104 (point-max)))))))
4105
4106 (autoload 'mail-position-on-field "sendmail")
4107
4108 (declare-function rmail-mime-message-p "rmailmm" ())
4109 (declare-function rmail-mime-toggle-raw "rmailmm" (&optional state))
4110
4111 (defun rmail-retry-failure ()
4112 "Edit a mail message which is based on the contents of the current message.
4113 For a message rejected by the mail system, extract the interesting headers and
4114 the body of the original message.
4115 If the failed message is a MIME multipart message, it is searched for a
4116 body part with a header which matches the variable `mail-mime-unsent-header'.
4117 Otherwise, the variable `mail-unsent-separator' should match the string that
4118 delimits the returned original message.
4119 The variable `rmail-retry-ignored-headers' is a regular expression
4120 specifying headers which should not be copied into the new message."
4121 (interactive)
4122 (require 'mail-utils)
4123 ;; FIXME This does not handle rmail-mime-feature != 'rmailmm.
4124 ;; There is no API defined for rmail-mime-feature to provide
4125 ;; rmail-mime-message-p, rmail-mime-toggle-raw equivalents.
4126 ;; But does anyone actually use rmail-mime-feature != 'rmailmm?
4127 (if (and rmail-enable-mime
4128 (eq rmail-mime-feature 'rmailmm)
4129 (featurep rmail-mime-feature))
4130 (with-current-buffer rmail-buffer
4131 (if (rmail-mime-message-p)
4132 (let ((rmail-mime-mbox-buffer rmail-view-buffer)
4133 (rmail-mime-view-buffer rmail-buffer))
4134 (rmail-mime-toggle-raw 'raw)))))
4135
4136 (let ((rmail-this-buffer (current-buffer))
4137 (msgnum rmail-current-message)
4138 bounce-start bounce-end bounce-indent resending
4139 (content-type (rmail-get-header "Content-Type")))
4140 (save-excursion
4141 (goto-char (point-min))
4142 (let ((case-fold-search t))
4143 (if (and content-type
4144 (string-match
4145 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
4146 content-type))
4147 ;; Handle a MIME multipart bounce message.
4148 (let ((codestring
4149 (concat "\n--"
4150 (substring content-type (match-beginning 1)
4151 (match-end 1)))))
4152 (unless (re-search-forward mail-mime-unsent-header nil t)
4153 (error "Cannot find beginning of header in failed message"))
4154 (unless (search-forward "\n\n" nil t)
4155 (error "Cannot find start of Mime data in failed message"))
4156 (setq bounce-start (point))
4157 (if (search-forward codestring nil t)
4158 (setq bounce-end (match-beginning 0))
4159 (setq bounce-end (point-max))))
4160 ;; Non-MIME bounce.
4161 (or (re-search-forward mail-unsent-separator nil t)
4162 (error "Cannot parse this as a failure message"))
4163 (skip-chars-forward "\n")
4164 ;; Support a style of failure message in which the original
4165 ;; message is indented, and included within lines saying
4166 ;; `Start of returned message' and `End of returned message'.
4167 (if (looking-at " +Received:")
4168 (progn
4169 (setq bounce-start (point))
4170 (skip-chars-forward " ")
4171 (setq bounce-indent (- (current-column)))
4172 (goto-char (point-max))
4173 (re-search-backward "^End of returned message$" nil t)
4174 (setq bounce-end (point)))
4175 ;; One message contained a few random lines before
4176 ;; the old message header. The first line of the
4177 ;; message started with two hyphens. A blank line
4178 ;; followed these random lines. The same line
4179 ;; beginning with two hyphens was possibly marking
4180 ;; the end of the message.
4181 (if (looking-at "^--")
4182 (let ((boundary (buffer-substring-no-properties
4183 (point)
4184 (progn (end-of-line) (point)))))
4185 (search-forward "\n\n")
4186 (skip-chars-forward "\n")
4187 (setq bounce-start (point))
4188 (goto-char (point-max))
4189 (search-backward (concat "\n\n" boundary) bounce-start t)
4190 (setq bounce-end (point)))
4191 (setq bounce-start (point)
4192 bounce-end (point-max)))
4193 (unless (search-forward "\n\n" nil t)
4194 (error "Cannot find end of header in failed message"))))))
4195 ;; We have found the message that bounced, within the current message.
4196 ;; Now start sending new message; default header fields from original.
4197 ;; Turn off the usual actions for initializing the message body
4198 ;; because we want to get only the text from the failure message.
4199 (let (mail-signature mail-setup-hook)
4200 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
4201 (list (list 'rmail-mark-message
4202 rmail-this-buffer
4203 (aref rmail-msgref-vector msgnum)
4204 rmail-retried-attr-index)))
4205 ;; Insert original text as initial text of new draft message.
4206 ;; Bind inhibit-read-only since the header delimiter
4207 ;; of the previous message was probably read-only.
4208 (let ((inhibit-read-only t)
4209 eoh)
4210 (erase-buffer)
4211 (insert-buffer-substring rmail-this-buffer
4212 bounce-start bounce-end)
4213 (goto-char (point-min))
4214 (if bounce-indent
4215 (indent-rigidly (point-min) (point-max) bounce-indent))
4216 (rfc822-goto-eoh)
4217 (setq eoh (point))
4218 (insert mail-header-separator)
4219 (save-restriction
4220 (narrow-to-region (point-min) eoh)
4221 (rmail-delete-headers rmail-retry-ignored-headers)
4222 (rmail-delete-headers "^\\(sender\\|return-path\\|received\\):")
4223 (setq resending (mail-fetch-field "resent-to"))
4224 (if mail-self-blind
4225 (if resending
4226 (insert "Resent-Bcc: " (user-login-name) "\n")
4227 (insert "BCC: " (user-login-name) "\n"))))
4228 (goto-char (point-min))
4229 (mail-position-on-field (if resending "Resent-To" "To") t))))))
4230 \f
4231 (defun rmail-summary-exists ()
4232 "Non-nil if in an RMAIL buffer and an associated summary buffer exists.
4233 In fact, the non-nil value returned is the summary buffer itself."
4234 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4235 rmail-summary-buffer))
4236
4237 (defun rmail-summary-displayed ()
4238 "t if in RMAIL buffer and an associated summary buffer is displayed."
4239 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
4240
4241 (defcustom rmail-redisplay-summary nil
4242 "Non-nil means Rmail should show the summary when it changes.
4243 This has an effect only if a summary buffer exists."
4244 :type 'boolean
4245 :group 'rmail-summary)
4246
4247 (defcustom rmail-summary-window-size nil
4248 "Non-nil means specify the height for an Rmail summary window."
4249 :type '(choice (const :tag "Disabled" nil) integer)
4250 :group 'rmail-summary)
4251
4252 ;; Put the summary buffer back on the screen, if user wants that.
4253 (defun rmail-maybe-display-summary ()
4254 (cond
4255 ((or (not rmail-summary-buffer)
4256 (not (buffer-name rmail-summary-buffer))))
4257 (rmail-redisplay-summary
4258 ;; If `rmail-redisplay-summary' is non-nil, make sure the summary
4259 ;; buffer is displayed.
4260 (display-buffer
4261 rmail-summary-buffer
4262 `(nil
4263 (reusable-frames . 0)
4264 ,(when rmail-summary-window-size
4265 `(window-height . ,rmail-summary-window-size)))))
4266 (rmail-summary-window-size
4267 ;; If `rmail-summary-window-size' is non-nil and the summary buffer
4268 ;; is displayed, make sure it gets resized.
4269 (let ((window (get-buffer-window rmail-summary-buffer 0)))
4270 (when window
4271 (window-resize-no-error
4272 window (- rmail-summary-window-size (window-height window))))))))
4273 \f
4274 ;;;; *** Rmail Local Fontification ***
4275
4276 (defun rmail-fontify-buffer-function ()
4277 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
4278 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
4279 ;; If we're already showing a message, fontify it now.
4280 (if rmail-current-message (rmail-fontify-message))
4281 ;; Prevent Font Lock mode from kicking in.
4282 (setq font-lock-fontified t))
4283
4284 (defun rmail-unfontify-buffer-function ()
4285 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
4286 (let ((modified (buffer-modified-p))
4287 (buffer-undo-list t) (inhibit-read-only t)
4288 before-change-functions after-change-functions
4289 buffer-file-name buffer-file-truename)
4290 (save-restriction
4291 (widen)
4292 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
4293 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
4294 (font-lock-default-unfontify-buffer)
4295 (and (not modified) (buffer-modified-p)
4296 (restore-buffer-modified-p nil)))))
4297
4298 (defun rmail-fontify-message ()
4299 ;; Fontify the current message if it is not already fontified.
4300 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
4301 (let ((modified (buffer-modified-p))
4302 (buffer-undo-list t) (inhibit-read-only t)
4303 before-change-functions after-change-functions
4304 buffer-file-name buffer-file-truename)
4305 (save-excursion
4306 (save-match-data
4307 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
4308 (font-lock-fontify-region (point-min) (point-max))
4309 (and (not modified) (buffer-modified-p)
4310 (restore-buffer-modified-p nil)))))))
4311 \f
4312 ;;; Speedbar support for RMAIL files.
4313 (defcustom rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
4314 "Regexp matching Rmail folder names to be displayed in Speedbar.
4315 Enabling this permits Speedbar to display your folders for easy
4316 browsing, and moving of messages."
4317 :type 'regexp
4318 :group 'rmail
4319 :group 'speedbar)
4320
4321 (defvar rmail-speedbar-last-user nil
4322 "The last user to be displayed in the speedbar.")
4323
4324 (defvar rmail-speedbar-key-map nil
4325 "Keymap used when in rmail display mode.")
4326
4327 (declare-function speedbar-make-specialized-keymap "speedbar" ())
4328
4329 (defun rmail-install-speedbar-variables ()
4330 "Install those variables used by speedbar to enhance rmail."
4331 (unless rmail-speedbar-key-map
4332 (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap))
4333 (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line)
4334 (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line)
4335 (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
4336 (define-key rmail-speedbar-key-map "M"
4337 'rmail-speedbar-move-message-to-folder-on-line)))
4338
4339 ;; Mouse-3.
4340 (defvar rmail-speedbar-menu-items
4341 '(["Read Folder" speedbar-edit-line t]
4342 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
4343 (save-excursion (beginning-of-line)
4344 (looking-at "<M> "))])
4345 "Additional menu-items to add to speedbar frame.")
4346
4347 (declare-function speedbar-insert-button "speedbar"
4348 (text face mouse function &optional token prevline))
4349
4350 ;; Make sure our special speedbar major mode is loaded
4351 (if (featurep 'speedbar)
4352 (rmail-install-speedbar-variables)
4353 (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables))
4354
4355 (defun rmail-speedbar-buttons (buffer)
4356 "Create buttons for BUFFER containing rmail messages.
4357 Click on the address under Reply to: to reply to this person.
4358 Under Folders: Click a name to read it, or on the <M> to move the
4359 current message into that RMAIL folder."
4360 (let ((from nil))
4361 (with-current-buffer buffer
4362 (goto-char (point-min))
4363 (if (not (re-search-forward "^Reply-To: " nil t))
4364 (if (not (re-search-forward "^From:? " nil t))
4365 (setq from t)))
4366 (if from
4367 nil
4368 (setq from (buffer-substring (point) (line-end-position)))))
4369 (goto-char (point-min))
4370 (if (and (looking-at "Reply to:")
4371 (equal from rmail-speedbar-last-user))
4372 nil
4373 (setq rmail-speedbar-last-user from)
4374 (erase-buffer)
4375 (insert "Reply To:\n")
4376 (if (stringp from)
4377 (speedbar-insert-button from 'speedbar-directory-face 'highlight
4378 'rmail-speedbar-button 'rmail-reply))
4379 (insert "Folders:\n")
4380 (let* ((case-fold-search nil)
4381 (df (directory-files (with-current-buffer buffer
4382 default-directory)
4383 nil rmail-speedbar-match-folder-regexp)))
4384 (dolist (file df)
4385 (when (file-regular-p file)
4386 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
4387 'rmail-speedbar-move-message file)
4388 (speedbar-insert-button file 'speedbar-file-face 'highlight
4389 'rmail-speedbar-find-file nil t)))))))
4390
4391 (eval-when-compile (require 'dframe))
4392 ;; Part of the macro expansion of dframe-with-attached-buffer.
4393 ;; At runtime, will be pulled in as a require of speedbar.
4394 (declare-function dframe-select-attached-frame "dframe" (&optional frame))
4395 (declare-function dframe-maybee-jump-to-attached-frame "dframe" ())
4396
4397 (defun rmail-speedbar-button (text token indent)
4398 "Execute an rmail command specified by TEXT.
4399 The command used is TOKEN. INDENT is not used."
4400 (dframe-with-attached-buffer
4401 (funcall token t)))
4402
4403 (defun rmail-speedbar-find-file (text token indent)
4404 "Load in the rmail file TEXT.
4405 TOKEN and INDENT are not used."
4406 (dframe-with-attached-buffer
4407 (message "Loading in RMAIL file %s..." text)
4408 (rmail text)))
4409
4410 (declare-function speedbar-do-function-pointer "speedbar" ())
4411
4412 (defun rmail-speedbar-move-message-to-folder-on-line ()
4413 "If the current line is a folder, move current message to it."
4414 (interactive)
4415 (save-excursion
4416 (beginning-of-line)
4417 (if (re-search-forward "<M> " (line-end-position) t)
4418 (progn
4419 (forward-char -2)
4420 (speedbar-do-function-pointer)))))
4421
4422 (defun rmail-speedbar-move-message (text token indent)
4423 "From button TEXT, copy current message to the rmail file specified by TOKEN.
4424 TEXT and INDENT are not used."
4425 (dframe-with-attached-buffer
4426 (message "Moving message to %s" token)
4427 ;; expand-file-name is needed due to the unhelpful way in which
4428 ;; rmail-output expands non-absolute filenames against rmail-default-file.
4429 ;; What is the point of that, anyway?
4430 (rmail-output (expand-file-name token))))
4431 \f
4432 ;; Functions for setting, getting and encoding the POP password.
4433 ;; The password is encoded to prevent it from being easily accessible
4434 ;; to "prying eyes." Obviously, this encoding isn't "real security,"
4435 ;; nor is it meant to be.
4436
4437 ;;;###autoload
4438 (defun rmail-set-remote-password (password)
4439 "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
4440 (interactive "sPassword: ")
4441 (if password
4442 (setq rmail-encoded-remote-password
4443 (rmail-encode-string password (emacs-pid)))
4444 (setq rmail-remote-password nil)
4445 (setq rmail-encoded-remote-password nil)))
4446
4447 (defun rmail-get-remote-password (imap)
4448 "Get the password for retrieving mail from a POP or IMAP server. If none
4449 has been set, then prompt the user for one."
4450 (when (not rmail-encoded-remote-password)
4451 (if (not rmail-remote-password)
4452 (setq rmail-remote-password
4453 (read-passwd (if imap
4454 "IMAP password: "
4455 "POP password: "))))
4456 (rmail-set-remote-password rmail-remote-password)
4457 (setq rmail-remote-password nil))
4458 (rmail-encode-string rmail-encoded-remote-password (emacs-pid)))
4459
4460 (defun rmail-have-password ()
4461 (or rmail-remote-password rmail-encoded-remote-password))
4462
4463 (defun rmail-encode-string (string mask)
4464 "Encode STRING with integer MASK, by taking the exclusive OR of the
4465 lowest byte in the mask with the first character of string, the
4466 second-lowest-byte with the second character of the string, etc.,
4467 restarting at the lowest byte of the mask whenever it runs out.
4468 Returns the encoded string. Calling the function again with an
4469 encoded string (and the same mask) will decode the string."
4470 (setq mask (abs mask)) ; doesn't work if negative
4471 (let* ((string-vector (string-to-vector string)) (i 0)
4472 (len (length string-vector)) (curmask mask) charmask)
4473 (while (< i len)
4474 (if (= curmask 0)
4475 (setq curmask mask))
4476 (setq charmask (% curmask 256))
4477 (setq curmask (lsh curmask -8))
4478 (aset string-vector i (logxor charmask (aref string-vector i)))
4479 (setq i (1+ i)))
4480 (concat string-vector)))
4481
4482 ;; Should this have a key-binding, or be in a menu?
4483 ;; There doesn't really seem to be an appropriate menu.
4484 ;; Eg the edit command is not in a menu either.
4485 (defun rmail-epa-decrypt ()
4486 "Decrypt OpenPGP armors in current message."
4487 (interactive)
4488
4489 ;; Save the current buffer here for cleanliness, in case we
4490 ;; change it in one of the calls to `epa-decrypt-region'.
4491
4492 (save-excursion
4493 (let (decrypts)
4494 (goto-char (point-min))
4495
4496 ;; In case the encrypted data is inside a mime attachment,
4497 ;; show it. This is a kludge; to be clean, it should not
4498 ;; modify the buffer, but I don't see how to do that.
4499 (when (search-forward "octet-stream" nil t)
4500 (beginning-of-line)
4501 (forward-button 1)
4502 (if (looking-at "Show")
4503 (rmail-mime-toggle-hidden)))
4504
4505 ;; Now find all armored messages in the buffer
4506 ;; and decrypt them one by one.
4507 (goto-char (point-min))
4508 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
4509 (let ((coding-system-for-read coding-system-for-read)
4510 armor-start armor-end after-end)
4511 (setq armor-start (match-beginning 0)
4512 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
4513 nil t))
4514 (unless armor-end
4515 (error "Encryption armor beginning has no matching end"))
4516 (goto-char armor-start)
4517
4518 ;; Because epa--find-coding-system-for-mime-charset not autoloaded.
4519 (require 'epa)
4520
4521 ;; Use the charset specified in the armor.
4522 (unless coding-system-for-read
4523 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
4524 (setq coding-system-for-read
4525 (epa--find-coding-system-for-mime-charset
4526 (intern (downcase (match-string 1)))))))
4527
4528 ;; Advance over this armor.
4529 (goto-char armor-end)
4530 (setq after-end (- (point-max) armor-end))
4531
4532 ;; Decrypt it, maybe in place, maybe making new buffer.
4533 (epa-decrypt-region
4534 armor-start armor-end
4535 ;; Call back this function to prepare the output.
4536 (lambda ()
4537 (let ((inhibit-read-only t))
4538 (delete-region armor-start armor-end)
4539 (goto-char armor-start)
4540 (current-buffer))))
4541
4542 (push (list armor-start (- (point-max) after-end))
4543 decrypts)))
4544
4545 (when (and decrypts (rmail-buffers-swapped-p))
4546 (when (y-or-n-p "Replace the original message? ")
4547 (setq decrypts (nreverse decrypts))
4548 (let ((beg (rmail-msgbeg rmail-current-message))
4549 (end (rmail-msgend rmail-current-message))
4550 (from-buffer (current-buffer)))
4551 (with-current-buffer rmail-view-buffer
4552 (narrow-to-region beg end)
4553 (goto-char (point-min))
4554 (dolist (d decrypts)
4555 (if (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
4556 (let (armor-start armor-end)
4557 (setq armor-start (match-beginning 0)
4558 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
4559 nil t))
4560 (when armor-end
4561 (delete-region armor-start armor-end)
4562 (insert-buffer-substring from-buffer (nth 0 d) (nth 1 d)))))))))))))
4563 \f
4564 ;;;; Desktop support
4565
4566 (defun rmail-restore-desktop-buffer (desktop-buffer-file-name
4567 desktop-buffer-name
4568 desktop-buffer-misc)
4569 "Restore an rmail buffer specified in a desktop file."
4570 (condition-case error
4571 (progn
4572 (rmail-input desktop-buffer-file-name)
4573 (if (eq major-mode 'rmail-mode)
4574 (current-buffer)
4575 rmail-buffer))
4576 (file-locked
4577 (kill-buffer (current-buffer))
4578 nil)))
4579
4580 (add-to-list 'desktop-buffer-mode-handlers
4581 '(rmail-mode . rmail-restore-desktop-buffer))
4582
4583 ;; We use this to record the encoding of the current message before
4584 ;; saving the message collection.
4585 (defvar rmail-message-encoding nil)
4586
4587 ;; Used in `write-region-annotate-functions' to write rmail files.
4588 (defun rmail-write-region-annotate (start end)
4589 (when (and (null start) rmail-buffer-swapped)
4590 (unless (buffer-live-p rmail-view-buffer)
4591 (error "Buffer `%s' with real text of `%s' has disappeared"
4592 (buffer-name rmail-view-buffer)
4593 (buffer-name (current-buffer))))
4594 (setq rmail-message-encoding buffer-file-coding-system)
4595 (set-buffer rmail-view-buffer)
4596 (widen)
4597 nil))
4598
4599 ;; Used to restore the encoding of the buffer where we show the
4600 ;; current message, after we save the message collection. This is
4601 ;; needed because rmail-write-region-annotate switches buffers behind
4602 ;; save-file's back, with the side effect that last-coding-system-used
4603 ;; is assigned to buffer-file-coding-system of the wrong buffer.
4604 (defun rmail-after-save-hook ()
4605 (if (or (eq rmail-view-buffer (current-buffer))
4606 (eq rmail-buffer (current-buffer)))
4607 (with-current-buffer
4608 (if (rmail-buffers-swapped-p) rmail-buffer rmail-view-buffer)
4609 (setq buffer-file-coding-system rmail-message-encoding))))
4610 (add-hook 'after-save-hook 'rmail-after-save-hook)
4611
4612 \f
4613 ;;; Start of automatically extracted autoloads.
4614 \f
4615 ;;;### (autoloads nil "rmailedit" "rmailedit.el" "dd2c1220747ca044a4d9b7ca6dc1843f")
4616 ;;; Generated autoloads from rmailedit.el
4617
4618 (autoload 'rmail-edit-current-message "rmailedit" "\
4619 Edit the contents of this message.
4620
4621 \(fn)" t nil)
4622
4623 ;;;***
4624 \f
4625 ;;;### (autoloads nil "rmailkwd" "rmailkwd.el" "b704fe915e8fe42e5d655096ed2fa21f")
4626 ;;; Generated autoloads from rmailkwd.el
4627
4628 (autoload 'rmail-add-label "rmailkwd" "\
4629 Add LABEL to labels associated with current RMAIL message.
4630 Completes (see `rmail-read-label') over known labels when reading.
4631 LABEL may be a symbol or string. Only one label is allowed.
4632
4633 \(fn LABEL)" t nil)
4634
4635 (autoload 'rmail-kill-label "rmailkwd" "\
4636 Remove LABEL from labels associated with current RMAIL message.
4637 Completes (see `rmail-read-label') over known labels when reading.
4638 LABEL may be a symbol or string. Only one label is allowed.
4639
4640 \(fn LABEL)" t nil)
4641
4642 (autoload 'rmail-read-label "rmailkwd" "\
4643 Read a label with completion, prompting with PROMPT.
4644 Completions are chosen from `rmail-label-obarray'. The default
4645 is `rmail-last-label', if that is non-nil. Updates `rmail-last-label'
4646 according to the choice made, and returns a symbol.
4647
4648 \(fn PROMPT)" nil nil)
4649
4650 (autoload 'rmail-previous-labeled-message "rmailkwd" "\
4651 Show previous message with one of the labels LABELS.
4652 LABELS should be a comma-separated list of label names.
4653 If LABELS is empty, the last set of labels specified is used.
4654 With prefix argument N moves backward N messages with these labels.
4655
4656 \(fn N LABELS)" t nil)
4657
4658 (autoload 'rmail-next-labeled-message "rmailkwd" "\
4659 Show next message with one of the labels LABELS.
4660 LABELS should be a comma-separated list of label names.
4661 If LABELS is empty, the last set of labels specified is used.
4662 With prefix argument N moves forward N messages with these labels.
4663
4664 \(fn N LABELS)" t nil)
4665
4666 ;;;***
4667 \f
4668 ;;;### (autoloads nil "rmailmm" "rmailmm.el" "47599e3257b0fc7c84f237db0324d718")
4669 ;;; Generated autoloads from rmailmm.el
4670
4671 (autoload 'rmail-mime "rmailmm" "\
4672 Toggle the display of a MIME message.
4673
4674 The actual behavior depends on the value of `rmail-enable-mime'.
4675
4676 If `rmail-enable-mime' is non-nil (the default), this command toggles
4677 the display of a MIME message between decoded presentation form and
4678 raw data. With optional prefix argument ARG, it toggles the display only
4679 of the MIME entity at point, if there is one. The optional argument
4680 STATE forces a particular display state, rather than toggling.
4681 `raw' forces raw mode, any other non-nil value forces decoded mode.
4682
4683 If `rmail-enable-mime' is nil, this creates a temporary \"*RMAIL*\"
4684 buffer holding a decoded copy of the message. Inline content-types are
4685 handled according to `rmail-mime-media-type-handlers-alist'.
4686 By default, this displays text and multipart messages, and offers to
4687 download attachments as specified by `rmail-mime-attachment-dirs-alist'.
4688 The arguments ARG and STATE have no effect in this case.
4689
4690 \(fn &optional ARG STATE)" t nil)
4691
4692 ;;;***
4693 \f
4694 ;;;### (autoloads nil "rmailmsc" "rmailmsc.el" "342dc295a4574ea68fc29a7303b01876")
4695 ;;; Generated autoloads from rmailmsc.el
4696
4697 (autoload 'set-rmail-inbox-list "rmailmsc" "\
4698 Set the inbox list of the current RMAIL file to FILE-NAME.
4699 You can specify one file name, or several names separated by commas.
4700 If FILE-NAME is empty, remove any existing inbox list.
4701
4702 This applies only to the current session.
4703
4704 \(fn FILE-NAME)" t nil)
4705
4706 ;;;***
4707 \f
4708 ;;;### (autoloads nil "rmailsort" "rmailsort.el" "e9f48c399319c56542c88446b2ccf1dc")
4709 ;;; Generated autoloads from rmailsort.el
4710
4711 (autoload 'rmail-sort-by-date "rmailsort" "\
4712 Sort messages of current Rmail buffer by \"Date\" header.
4713 If prefix argument REVERSE is non-nil, sorts in reverse order.
4714
4715 \(fn REVERSE)" t nil)
4716
4717 (autoload 'rmail-sort-by-subject "rmailsort" "\
4718 Sort messages of current Rmail buffer by \"Subject\" header.
4719 Ignores any \"Re: \" prefix. If prefix argument REVERSE is
4720 non-nil, sorts in reverse order.
4721
4722 \(fn REVERSE)" t nil)
4723
4724 (autoload 'rmail-sort-by-author "rmailsort" "\
4725 Sort messages of current Rmail buffer by author.
4726 This uses either the \"From\" or \"Sender\" header, downcased.
4727 If prefix argument REVERSE is non-nil, sorts in reverse order.
4728
4729 \(fn REVERSE)" t nil)
4730
4731 (autoload 'rmail-sort-by-recipient "rmailsort" "\
4732 Sort messages of current Rmail buffer by recipient.
4733 This uses either the \"To\" or \"Apparently-To\" header, downcased.
4734 If prefix argument REVERSE is non-nil, sorts in reverse order.
4735
4736 \(fn REVERSE)" t nil)
4737
4738 (autoload 'rmail-sort-by-correspondent "rmailsort" "\
4739 Sort messages of current Rmail buffer by other correspondent.
4740 This uses either the \"From\", \"Sender\", \"To\", or
4741 \"Apparently-To\" header, downcased. Uses the first header not
4742 excluded by `mail-dont-reply-to-names'. If prefix argument
4743 REVERSE is non-nil, sorts in reverse order.
4744
4745 \(fn REVERSE)" t nil)
4746
4747 (autoload 'rmail-sort-by-lines "rmailsort" "\
4748 Sort messages of current Rmail buffer by the number of lines.
4749 If prefix argument REVERSE is non-nil, sorts in reverse order.
4750
4751 \(fn REVERSE)" t nil)
4752
4753 (autoload 'rmail-sort-by-labels "rmailsort" "\
4754 Sort messages of current Rmail buffer by labels.
4755 LABELS is a comma-separated list of labels. The order of these
4756 labels specifies the order of messages: messages with the first
4757 label come first, messages with the second label come second, and
4758 so on. Messages that have none of these labels come last.
4759 If prefix argument REVERSE is non-nil, sorts in reverse order.
4760
4761 \(fn REVERSE LABELS)" t nil)
4762
4763 ;;;***
4764 \f
4765 ;;;### (autoloads nil "rmailsum" "rmailsum.el" "56fe07cc46b6146b51506b3e4c27e49f")
4766 ;;; Generated autoloads from rmailsum.el
4767
4768 (autoload 'rmail-summary "rmailsum" "\
4769 Display a summary of all messages, one line per message.
4770
4771 \(fn)" t nil)
4772
4773 (autoload 'rmail-summary-by-labels "rmailsum" "\
4774 Display a summary of all messages with one or more LABELS.
4775 LABELS should be a string containing the desired labels, separated by commas.
4776
4777 \(fn LABELS)" t nil)
4778
4779 (autoload 'rmail-summary-by-recipients "rmailsum" "\
4780 Display a summary of all messages with the given RECIPIENTS.
4781 Normally checks the To, From and Cc fields of headers;
4782 but if PRIMARY-ONLY is non-nil (prefix arg given),
4783 only look in the To and From fields.
4784 RECIPIENTS is a string of regexps separated by commas.
4785
4786 \(fn RECIPIENTS &optional PRIMARY-ONLY)" t nil)
4787
4788 (autoload 'rmail-summary-by-regexp "rmailsum" "\
4789 Display a summary of all messages according to regexp REGEXP.
4790 If the regular expression is found in the header of the message
4791 \(including in the date and other lines, as well as the subject line),
4792 Emacs will list the message in the summary.
4793
4794 \(fn REGEXP)" t nil)
4795
4796 (autoload 'rmail-summary-by-topic "rmailsum" "\
4797 Display a summary of all messages with the given SUBJECT.
4798 Normally checks just the Subject field of headers; but with prefix
4799 argument WHOLE-MESSAGE is non-nil, looks in the whole message.
4800 SUBJECT is a string of regexps separated by commas.
4801
4802 \(fn SUBJECT &optional WHOLE-MESSAGE)" t nil)
4803
4804 (autoload 'rmail-summary-by-senders "rmailsum" "\
4805 Display a summary of all messages whose \"From\" field matches SENDERS.
4806 SENDERS is a string of regexps separated by commas.
4807
4808 \(fn SENDERS)" t nil)
4809
4810 ;;;***
4811 \f
4812 ;;;### (autoloads nil "undigest" "undigest.el" "763e782c6c8cfa744acfcce990689162")
4813 ;;; Generated autoloads from undigest.el
4814
4815 (autoload 'undigestify-rmail-message "undigest" "\
4816 Break up a digest message into its constituent messages.
4817 Leaves original message, deleted, before the undigestified messages.
4818
4819 \(fn)" t nil)
4820
4821 (autoload 'unforward-rmail-message "undigest" "\
4822 Extract a forwarded message from the containing message.
4823 This puts the forwarded message into a separate rmail message following
4824 the containing message. This command is only useful when messages are
4825 forwarded with `rmail-enable-mime-composing' set to nil.
4826
4827 \(fn)" t nil)
4828
4829 ;;;***
4830 \f
4831 ;;; End of automatically extracted autoloads.
4832
4833
4834 (provide 'rmail)
4835
4836 ;;; rmail.el ends here