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